function EntityViewControllerTest::testFieldItemAttributes

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityViewControllerTest.php \Drupal\Tests\system\Functional\Entity\EntityViewControllerTest::testFieldItemAttributes()
  2. 10 core/modules/system/tests/src/Functional/Entity/EntityViewControllerTest.php \Drupal\Tests\system\Functional\Entity\EntityViewControllerTest::testFieldItemAttributes()
  3. 11.x core/modules/system/tests/src/Functional/Entity/EntityViewControllerTest.php \Drupal\Tests\system\Functional\Entity\EntityViewControllerTest::testFieldItemAttributes()

Tests field item attributes.

File

core/modules/system/tests/src/Functional/Entity/EntityViewControllerTest.php, line 94

Class

EntityViewControllerTest
Tests EntityViewController functionality.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testFieldItemAttributes() {
    // Make sure the test field will be rendered.
    \Drupal::service('entity_display.repository')->getViewDisplay('entity_test', 'entity_test')
        ->setComponent('field_test_text', [
        'type' => 'text_default',
    ])
        ->save();
    // Create an entity and save test value in field_test_text.
    $test_value = $this->randomMachineName();
    $entity = EntityTest::create();
    $entity->field_test_text = $test_value;
    $entity->save();
    // Browse to the entity and verify that the attribute is rendered in the
    // field item HTML markup.
    $this->drupalGet('entity_test/' . $entity->id());
    $this->assertSession()
        ->elementTextEquals('xpath', '//div[@data-field-item-attr="foobar" and @property="schema:text"]/p', $test_value);
}

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