function MultipleWidgetFormTest::testLabelOnMultiValueFields

Same name in other branches
  1. 10 core/modules/field/tests/src/Functional/MultipleWidgetFormTest.php \Drupal\Tests\field\Functional\MultipleWidgetFormTest::testLabelOnMultiValueFields()

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

File

core/modules/field/tests/src/Functional/MultipleWidgetFormTest.php, line 145

Class

MultipleWidgetFormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

public function testLabelOnMultiValueFields() : void {
    $user = $this->drupalCreateUser([
        'administer entity_test content',
    ]);
    $this->drupalLogin($user);
    // Ensure that the 'bar' bundle exists, to avoid config validation errors.
    entity_test_create_bundle('bar', entity_type: 'entity_test_base_field_display');
    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>");
}

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