You are here

public function EntityViewControllerTest::testFieldItemAttributes in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityViewControllerTest.php \Drupal\system\Tests\Entity\EntityViewControllerTest::testFieldItemAttributes()

Tests field item attributes.

File

core/modules/system/src/Tests/Entity/EntityViewControllerTest.php, line 79
Contains \Drupal\system\Tests\Entity\EntityViewControllerTest.

Class

EntityViewControllerTest
Tests EntityViewController functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testFieldItemAttributes() {

  // Make sure the test field will be rendered.
  entity_get_display('entity_test', 'entity_test', 'default')
    ->setComponent('field_test_text', array(
    'type' => 'text_default',
  ))
    ->save();

  // Create an entity and save test value in field_test_text.
  $test_value = $this
    ->randomMachineName();
  $entity = entity_create('entity_test');
  $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());
  $xpath = $this
    ->xpath('//div[@data-field-item-attr="foobar"]/p[text()=:value]', array(
    ':value' => $test_value,
  ));
  $this
    ->assertTrue($xpath, 'The field item attribute has been found in the rendered output of the field.');

  // Enable the RDF module to ensure that two modules can add attributes to
  // the same field item.
  \Drupal::service('module_installer')
    ->install(array(
    'rdf',
  ));
  $this
    ->resetAll();

  // Set an RDF mapping for the field_test_text field. This RDF mapping will
  // be turned into RDFa attributes in the field item output.
  $mapping = rdf_get_mapping('entity_test', 'entity_test');
  $mapping
    ->setFieldMapping('field_test_text', array(
    'properties' => array(
      'schema:text',
    ),
  ))
    ->save();

  // Browse to the entity and verify that the attributes from both modules
  // are rendered in the field item HTML markup.
  $this
    ->drupalGet('entity_test/' . $entity
    ->id());
  $xpath = $this
    ->xpath('//div[@data-field-item-attr="foobar" and @property="schema:text"]/p[text()=:value]', array(
    ':value' => $test_value,
  ));
  $this
    ->assertTrue($xpath, 'The field item attributes from both modules have been found in the rendered output of the field.');
}