3 namespace Drupal\Tests\views\Kernel\Handler;
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;
13 * Tests the generic entity area handler.
16 * @see \Drupal\views\Plugin\views\area\Entity
18 class AreaEntityTest extends ViewsKernelTestBase {
20 use BlockCreationTrait;
27 public static $modules = ['entity_test', 'user', 'block'];
30 * Views used by this test.
34 public static $testViews = ['test_entity_area'];
39 protected function setUp($import_test_views = TRUE) {
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();
51 $this->installEntitySchema('user');
52 $this->installEntitySchema('entity_test');
53 $this->installConfig(['entity_test']);
55 $this->placeBlock('system_main_block', ['id' => 'test_block']);
57 parent::setUpFixtures();
61 * Tests views data for entity area handlers.
63 public function testEntityAreaData() {
64 $data = $this->container->get('views.views_data')->get('views');
65 $entity_types = $this->container->get('entity.manager')->getDefinitions();
67 $expected_entities = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
68 return $entity_type->hasViewBuilderClass();
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]));
78 $expected_entities = array_filter($entity_types, function (EntityTypeInterface $type) {
79 return !$type->hasViewBuilderClass();
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]));
89 * Tests the area handler.
91 public function testEntityArea() {
92 /** @var \Drupal\Core\Entity\EntityInterface[] $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')
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];
106 $entity_test->save();
107 $entities[] = $entity_test;
109 ->set('entity_test_entity_access.view.' . $entity_test->id(), $i != 2);
112 $this->doTestCalculateDependencies();
113 $this->doTestRender($entities);
117 * Tests rendering the entity area handler.
119 * @param \Drupal\Core\Entity\EntityInterface[] $entities
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]';
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.');
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.');
140 $preview = $view->preview('default', [$entities[1]->id()]);
141 $this->setRawContent($renderer->renderRoot($preview));
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.');
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.');
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();
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);
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.');
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.');
177 // Test the available view mode options.
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.');
187 * Tests the calculation of the rendered dependencies.
189 public function doTestCalculateDependencies() {
190 $view = View::load('test_entity_area');
192 $dependencies = $view->calculateDependencies()->getDependencies();
193 // Ensure that both config and content entity dependencies are calculated.
195 'config' => ['block.block.test_block'],
196 'content' => ['entity_test:entity_test:aa0c61cb-b7bb-4795-972a-493dabcf529c'],