Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Handler / AreaEntityTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Handler;
4
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\simpletest\BlockCreationTrait;
8 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
9 use Drupal\views\Entity\View;
10 use Drupal\views\Views;
11
12 /**
13  * Tests the generic entity area handler.
14  *
15  * @group views
16  * @see \Drupal\views\Plugin\views\area\Entity
17  */
18 class AreaEntityTest extends ViewsKernelTestBase {
19
20   use BlockCreationTrait;
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['entity_test', 'user', 'block'];
28
29   /**
30    * Views used by this test.
31    *
32    * @var array
33    */
34   public static $testViews = ['test_entity_area'];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp($import_test_views = TRUE) {
40     parent::setUp();
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function setUpFixtures() {
47     // Install the themes used for this test.
48     $this->container->get('theme_installer')->install(['bartik']);
49     $this->container->get('config.factory')->getEditable('system.theme')->set('default', 'bartik')->save();
50
51     $this->installEntitySchema('user');
52     $this->installEntitySchema('entity_test');
53     $this->installConfig(['entity_test']);
54
55     $this->placeBlock('system_main_block', ['id' => 'test_block']);
56
57     parent::setUpFixtures();
58   }
59
60   /**
61    * Tests views data for entity area handlers.
62    */
63   public function testEntityAreaData() {
64     $data = $this->container->get('views.views_data')->get('views');
65     $entity_types = $this->container->get('entity.manager')->getDefinitions();
66
67     $expected_entities = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
68       return $entity_type->hasViewBuilderClass();
69     });
70
71     // Test that all expected entity types have data.
72     foreach (array_keys($expected_entities) as $entity) {
73       $this->assertTrue(!empty($data['entity_' . $entity]), format_string('Views entity area data found for @entity', ['@entity' => $entity]));
74       // Test that entity_type is set correctly in the area data.
75       $this->assertEqual($entity, $data['entity_' . $entity]['area']['entity_type'], format_string('Correct entity_type set for @entity', ['@entity' => $entity]));
76     }
77
78     $expected_entities = array_filter($entity_types, function (EntityTypeInterface $type) {
79       return !$type->hasViewBuilderClass();
80     });
81
82     // Test that no configuration entity types have data.
83     foreach (array_keys($expected_entities) as $entity) {
84       $this->assertTrue(empty($data['entity_' . $entity]), format_string('Views config entity area data not found for @entity', ['@entity' => $entity]));
85     }
86   }
87
88   /**
89    * Tests the area handler.
90    */
91   public function testEntityArea() {
92     /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
93     $entities = [];
94     for ($i = 0; $i < 3; $i++) {
95       $random_label = $this->randomMachineName();
96       $data = ['bundle' => 'entity_test', 'name' => $random_label];
97       $entity_test = $this->container->get('entity.manager')
98         ->getStorage('entity_test')
99         ->create($data);
100
101       $uuid_map[0] = 'aa0c61cb-b7bb-4795-972a-493dabcf529c';
102       $uuid_map[1] = '62cef0ff-6f30-4f7a-b9d6-a8ed5a3a6bf3';
103       $uuid_map[2] = '3161d6e9-3326-4719-b513-8fa68a731ba2';
104       $entity_test->uuid->value = $uuid_map[$i];
105
106       $entity_test->save();
107       $entities[] = $entity_test;
108       \Drupal::state()
109         ->set('entity_test_entity_access.view.' . $entity_test->id(), $i != 2);
110     }
111
112     $this->doTestCalculateDependencies();
113     $this->doTestRender($entities);
114   }
115
116   /**
117    * Tests rendering the entity area handler.
118    *
119    * @param \Drupal\Core\Entity\EntityInterface[] $entities
120    *   The entities.
121    */
122   public function doTestRender($entities) {
123     /** @var \Drupal\Core\Render\RendererInterface $renderer */
124     $renderer = $this->container->get('renderer');
125     $view = Views::getView('test_entity_area');
126     $preview = $view->preview('default', [$entities[1]->id()]);
127     $this->setRawContent(\Drupal::service('renderer')->renderRoot($preview));
128     $view_class = 'js-view-dom-id-' . $view->dom_id;
129     $header_xpath = '//div[@class = "' . $view_class . '"]/header[1]';
130     $footer_xpath = '//div[@class = "' . $view_class . '"]/footer[1]';
131
132     $result = $this->xpath($header_xpath);
133     $this->assertTrue(strpos(trim((string) $result[0]), $entities[0]->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
134     $this->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
135
136     $result = $this->xpath($footer_xpath);
137     $this->assertTrue(strpos(trim((string) $result[0]), $entities[1]->label()) !== FALSE, 'The rendered entity appears in the footer of the view.');
138     $this->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
139
140     $preview = $view->preview('default', [$entities[1]->id()]);
141     $this->setRawContent($renderer->renderRoot($preview));
142
143     $result = $this->xpath($header_xpath);
144     $this->assertTrue(strpos(trim((string) $result[0]), $entities[0]->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
145     $this->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
146
147     $result = $this->xpath($footer_xpath);
148     $this->assertTrue(strpos(trim((string) $result[0]), $entities[1]->label()) !== FALSE, 'The rendered entity appears in the footer of the view.');
149     $this->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
150
151     // Mark entity_test test view_mode as customizable.
152     $entity_view_mode = \Drupal::entityManager()->getStorage('entity_view_mode')->load('entity_test.test');
153     $entity_view_mode->enable();
154     $entity_view_mode->save();
155
156     // Change the view mode of the area handler.
157     $view = Views::getView('test_entity_area');
158     $item = $view->getHandler('default', 'header', 'entity_entity_test');
159     $item['view_mode'] = 'test';
160     $view->setHandler('default', 'header', 'entity_entity_test', $item);
161
162     $preview = $view->preview('default', [$entities[1]->id()]);
163     $this->setRawContent($renderer->renderRoot($preview));
164     $view_class = 'js-view-dom-id-' . $view->dom_id;
165     $result = $this->xpath('//div[@class = "' . $view_class . '"]/header[1]');
166     $this->assertTrue(strpos(trim((string) $result[0]), $entities[0]->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
167     $this->assertTrue(strpos(trim((string) $result[0]), 'test') !== FALSE, 'The rendered entity appeared in the right view mode.');
168
169     // Test entity access.
170     $view = Views::getView('test_entity_area');
171     $preview = $view->preview('default', [$entities[2]->id()]);
172     $this->setRawContent($renderer->renderRoot($preview));
173     $view_class = 'js-view-dom-id-' . $view->dom_id;
174     $result = $this->xpath('//div[@class = "' . $view_class . '"]/footer[1]');
175     $this->assertTrue(strpos($result[0], $entities[2]->label()) === FALSE, 'The rendered entity does not appear in the footer of the view.');
176
177     // Test the available view mode options.
178     $form = [];
179     $form_state = (new FormState())
180       ->set('type', 'header');
181     $view->display_handler->getHandler('header', 'entity_entity_test')->buildOptionsForm($form, $form_state);
182     $this->assertTrue(isset($form['view_mode']['#options']['test']), 'Ensure that the test view mode is available.');
183     $this->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available.');
184   }
185
186   /**
187    * Tests the calculation of the rendered dependencies.
188    */
189   public function doTestCalculateDependencies() {
190     $view = View::load('test_entity_area');
191
192     $dependencies = $view->calculateDependencies()->getDependencies();
193     // Ensure that both config and content entity dependencies are calculated.
194     $this->assertEqual([
195       'config' => ['block.block.test_block'],
196       'content' => ['entity_test:entity_test:aa0c61cb-b7bb-4795-972a-493dabcf529c'],
197       'module' => ['views_test_data'],
198     ], $dependencies);
199   }
200
201 }