function FormTest::testLabelOnMultiValueFields

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testLabelOnMultiValueFields()
  2. 10 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testLabelOnMultiValueFields()
  3. 11.x core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testLabelOnMultiValueFields()

Tests the form display of the label for multi-value fields.

File

core/modules/field/tests/src/Functional/FormTest.php, line 646

Class

FormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

public function testLabelOnMultiValueFields() {
    $user = $this->drupalCreateUser([
        'administer entity_test content',
    ]);
    $this->drupalLogin($user);
    FieldStorageConfig::create([
        'entity_type' => 'entity_test_base_field_display',
        'field_name' => 'foo',
        'type' => 'text',
        'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
    ])->save();
    FieldConfig::create([
        'entity_type' => 'entity_test_base_field_display',
        'bundle' => 'bar',
        'field_name' => 'foo',
        // Set a dangerous label to test XSS filtering.
'label' => "<script>alert('a configurable field');</script>",
    ])->save();
    EntityFormDisplay::create([
        'targetEntityType' => 'entity_test_base_field_display',
        'bundle' => 'bar',
        'mode' => 'default',
    ])->setComponent('foo', [
        'type' => 'text_textfield',
    ])
        ->enable()
        ->save();
    $entity = EntityTestBaseFieldDisplay::create([
        'type' => 'bar',
    ]);
    $entity->save();
    $this->drupalGet('entity_test_base_field_display/manage/' . $entity->id());
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertText('A field with multiple values');
    // Test if labels were XSS filtered.
    $this->assertEscaped("<script>alert('a configurable field');</script>");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.