You are here

public function FormTest::testLabelOnMultiValueFields in Drupal 9

Same name and namespace in other branches
  1. 8 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 658

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
    ->assertSession()
    ->pageTextContains('A field with multiple values');

  // Test if labels were XSS filtered.
  $this
    ->assertSession()
    ->assertEscaped("<script>alert('a configurable field');</script>");
}