363736975328b813cc28c3d1ce8b7009121be31c
[yaffs-website] / web / core / modules / system / tests / src / Functional / Entity / EntityViewControllerTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Entity;
4
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests EntityViewController functionality.
10  *
11  * @group Entity
12  */
13 class EntityViewControllerTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['entity_test'];
21
22   /**
23    * Array of test entities.
24    *
25    * @var array
26    */
27   protected $entities = [];
28
29   protected function setUp() {
30     parent::setUp();
31     // Create some dummy entity_test entities.
32     for ($i = 0; $i < 2; $i++) {
33       $entity_test = $this->createTestEntity('entity_test');
34       $entity_test->save();
35       $this->entities[] = $entity_test;
36     }
37
38     $this->drupalLogin($this->drupalCreateUser(['view test entity']));
39   }
40
41   /**
42    * Tests EntityViewController.
43    */
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>
48       </h1>';
49     };
50
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');
56
57       $this->drupalGet('entity_test_converter/' . $entity->id());
58       $this->assertRaw($entity->label());
59       $this->assertRaw('full');
60
61       $this->drupalGet('entity_test_no_view_mode/' . $entity->id());
62       $this->assertRaw($entity->label());
63       $this->assertRaw('full');
64     }
65
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()));
76
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);
81   }
82
83   /**
84    * Tests field item attributes.
85    */
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'])
90       ->save();
91
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;
96     $entity->save();
97
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.');
103
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']);
107     $this->resetAll();
108
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'],
114     ])->save();
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.');
120   }
121
122   /**
123    * Tests that a view builder can successfully override the view builder.
124    */
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());
130   }
131
132   /**
133    * Creates an entity for testing.
134    *
135    * @param string $entity_type
136    *   The entity type.
137    *
138    * @return \Drupal\Core\Entity\EntityInterface
139    *   The created entity.
140    */
141   protected function createTestEntity($entity_type) {
142     $data = [
143       'bundle' => $entity_type,
144       'name' => $this->randomMachineName(),
145     ];
146     return $this->container->get('entity.manager')->getStorage($entity_type)->create($data);
147   }
148
149 }