3 namespace Drupal\Tests\system\Functional\Entity;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests EntityViewController functionality.
13 class EntityViewControllerTest extends BrowserTestBase {
20 public static $modules = ['entity_test'];
23 * Array of test entities.
27 protected $entities = [];
29 protected function setUp() {
31 // Create some dummy entity_test entities.
32 for ($i = 0; $i < 2; $i++) {
33 $entity_test = $this->createTestEntity('entity_test');
35 $this->entities[] = $entity_test;
38 $this->drupalLogin($this->drupalCreateUser(['view test entity']));
42 * Tests EntityViewController.
44 public function testEntityViewController() {
45 $get_label_markup = function ($label) {
46 return '<h1 class="page-title">
47 <div class="field field--name-name field--type-string field--label-hidden field__item">' . $label . '</div>
51 foreach ($this->entities as $entity) {
52 $this->drupalGet('entity_test/' . $entity->id());
53 $this->assertRaw($entity->label());
54 $this->assertRaw($get_label_markup($entity->label()));
55 $this->assertRaw('full');
57 $this->drupalGet('entity_test_converter/' . $entity->id());
58 $this->assertRaw($entity->label());
59 $this->assertRaw('full');
61 $this->drupalGet('entity_test_no_view_mode/' . $entity->id());
62 $this->assertRaw($entity->label());
63 $this->assertRaw('full');
66 // Test viewing a revisionable entity.
67 $entity_test_rev = $this->createTestEntity('entity_test_rev');
68 $entity_test_rev->save();
69 $entity_test_rev->name->value = 'rev 2';
70 $entity_test_rev->setNewRevision(TRUE);
71 $entity_test_rev->isDefaultRevision(TRUE);
72 $entity_test_rev->save();
73 $this->drupalGet('entity_test_rev/' . $entity_test_rev->id() . '/revision/' . $entity_test_rev->revision_id->value . '/view');
74 $this->assertRaw($entity_test_rev->label());
75 $this->assertRaw($get_label_markup($entity_test_rev->label()));
77 // As entity_test IDs must be integers, make sure requests for non-integer
78 // IDs return a page not found error.
79 $this->drupalGet('entity_test/invalid');
80 $this->assertResponse(404);
84 * Tests field item attributes.
86 public function testFieldItemAttributes() {
87 // Make sure the test field will be rendered.
88 entity_get_display('entity_test', 'entity_test', 'default')
89 ->setComponent('field_test_text', ['type' => 'text_default'])
92 // Create an entity and save test value in field_test_text.
93 $test_value = $this->randomMachineName();
94 $entity = EntityTest::create();
95 $entity->field_test_text = $test_value;
98 // Browse to the entity and verify that the attribute is rendered in the
99 // field item HTML markup.
100 $this->drupalGet('entity_test/' . $entity->id());
101 $xpath = $this->xpath('//div[@data-field-item-attr="foobar"]/p[text()=:value]', [':value' => $test_value]);
102 $this->assertTrue($xpath, 'The field item attribute has been found in the rendered output of the field.');
104 // Enable the RDF module to ensure that two modules can add attributes to
105 // the same field item.
106 \Drupal::service('module_installer')->install(['rdf']);
109 // Set an RDF mapping for the field_test_text field. This RDF mapping will
110 // be turned into RDFa attributes in the field item output.
111 $mapping = rdf_get_mapping('entity_test', 'entity_test');
112 $mapping->setFieldMapping('field_test_text', [
113 'properties' => ['schema:text'],
115 // Browse to the entity and verify that the attributes from both modules
116 // are rendered in the field item HTML markup.
117 $this->drupalGet('entity_test/' . $entity->id());
118 $xpath = $this->xpath('//div[@data-field-item-attr="foobar" and @property="schema:text"]/p[text()=:value]', [':value' => $test_value]);
119 $this->assertTrue($xpath, 'The field item attributes from both modules have been found in the rendered output of the field.');
123 * Tests that a view builder can successfully override the view builder.
125 public function testEntityViewControllerViewBuilder() {
126 $entity_test = $this->createTestEntity('entity_test_view_builder');
127 $entity_test->save();
128 $this->drupalGet('entity_test_view_builder/' . $entity_test->id());
129 $this->assertText($entity_test->label());
133 * Creates an entity for testing.
135 * @param string $entity_type
138 * @return \Drupal\Core\Entity\EntityInterface
139 * The created entity.
141 protected function createTestEntity($entity_type) {
143 'bundle' => $entity_type,
144 'name' => $this->randomMachineName(),
146 return $this->container->get('entity.manager')->getStorage($entity_type)->create($data);