3 namespace Drupal\Tests\views\Kernel\EventSubscriber;
5 use Drupal\Core\Entity\EntityTypeEvent;
6 use Drupal\Core\Entity\EntityTypeEvents;
7 use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
8 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
11 * Tests \Drupal\views\EventSubscriber\ViewsEntitySchemaSubscriber
15 class ViewsEntitySchemaSubscriberIntegrationTest extends ViewsKernelTestBase {
17 use EntityDefinitionTestTrait;
20 * The entity definition update manager.
22 * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
24 protected $entityDefinitionUpdateManager;
29 public static $modules = ['entity_test', 'entity_test_update', 'user', 'text'];
32 * Views used by this test.
36 public static $testViews = ['test_view_entity_test', 'test_view_entity_test_revision', 'test_view_entity_test_data', 'test_view_entity_test_additional_base_field'];
39 * The event dispatcher.
41 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
43 protected $eventDispatcher;
46 * The tested event subscriber of views.
48 * @var \Drupal\views\EventSubscriber\ViewsEntitySchemaSubscriber
50 protected $eventSubscriber;
53 * The entity manager service.
55 * @var \Drupal\Core\Entity\EntityManagerInterface
57 protected $entityManager;
62 * @var \Drupal\Core\State\StateInterface
69 protected function setUp($import_test_views = TRUE) {
72 $this->eventDispatcher = $this->container->get('event_dispatcher');
73 $this->eventSubscriber = $this->container->get('views.entity_schema_subscriber');
74 $this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager');
75 $this->entityManager = $this->container->get('entity.manager');
76 $this->state = $this->container->get('state');
78 $this->database = $this->container->get('database');
80 // Install every entity type's schema that wasn't installed in the parent
82 foreach (array_diff_key($this->entityManager->getDefinitions(), array_flip(['user', 'entity_test'])) as $entity_type_id => $entity_type) {
83 $this->installEntitySchema($entity_type_id);
88 * Tests that views are disabled when an entity type is deleted.
90 public function testDeleteEntityType() {
91 $entity_storage = $this->entityManager->getStorage('view');
93 // Make the test entity type revisionable.
94 $this->updateEntityTypeToRevisionable();
95 $this->entityDefinitionUpdateManager->applyUpdates();
97 $views = $entity_storage->loadMultiple();
99 // Ensure that all test views exists.
100 $this->assertTrue(isset($views['test_view_entity_test']));
101 $this->assertTrue(isset($views['test_view_entity_test_revision']));
102 $this->assertTrue(isset($views['test_view_entity_test_data']));
103 $this->assertTrue(isset($views['test_view_entity_test_additional_base_field']));
105 $event = new EntityTypeEvent($this->entityManager->getDefinition('entity_test_update'));
106 $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, $event);
108 // We expect that views which use 'entity_test_update' as base tables are
110 $views = $entity_storage->loadMultiple();
112 // Ensure that all test views still exists after the deletion of the
114 $this->assertTrue(isset($views['test_view_entity_test']));
115 $this->assertTrue(isset($views['test_view_entity_test_revision']));
116 $this->assertTrue(isset($views['test_view_entity_test_data']));
117 $this->assertTrue(isset($views['test_view_entity_test_additional_base_field']));
119 // Ensure that they are all disabled.
120 $this->assertFalse($views['test_view_entity_test']->status());
121 $this->assertFalse($views['test_view_entity_test_revision']->status());
122 $this->assertFalse($views['test_view_entity_test_data']->status());
123 $this->assertFalse($views['test_view_entity_test_additional_base_field']->status());
127 * Tests that renaming base tables adapts the views.
129 public function testBaseTableRename() {
130 $this->renameBaseTable();
131 $this->entityDefinitionUpdateManager->applyUpdates();
133 /** @var \Drupal\views\Entity\View $view */
134 $entity_storage = $this->entityManager->getStorage('view');
135 $view = $entity_storage->load('test_view_entity_test');
137 // Ensure the base table got renamed, so also the views fields.
138 $this->assertEqual('entity_test_update_new', $view->get('base_table'));
139 $display = $view->getDisplay('default');
140 $this->assertEqual('entity_test_update_new', $display['display_options']['fields']['id']['table']);
141 $this->assertEqual('entity_test_update_new', $display['display_options']['fields']['name']['table']);
145 * Tests that renaming data tables adapts the views.
147 public function testDataTableRename() {
148 $this->updateEntityTypeToTranslatable();
149 $this->entityDefinitionUpdateManager->applyUpdates();
151 $entity_storage = $this->entityManager->getStorage('view');
152 $view = $entity_storage->load('test_view_entity_test_data');
153 $this->assertEqual('entity_test_update', $view->get('base_table'));
154 $display = $view->getDisplay('default');
155 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
156 // Ensure that the data table is used.
157 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
159 $this->renameDataTable();
160 $this->entityDefinitionUpdateManager->applyUpdates();
162 /** @var \Drupal\views\Entity\View $view */
163 $entity_storage = $this->entityManager->getStorage('view');
164 $view = $entity_storage->load('test_view_entity_test_data');
166 // Ensure the data table got renamed, so also the views fields.
167 $this->assertEqual('entity_test_update', $view->get('base_table'));
168 $display = $view->getDisplay('default');
169 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
170 $this->assertEqual('entity_test_update_data_new', $display['display_options']['fields']['name']['table']);
174 * Tests that renaming revision tables adapts the views.
176 public function testRevisionBaseTableRename() {
177 $this->updateEntityTypeToRevisionable();
178 $this->entityDefinitionUpdateManager->applyUpdates();
180 /** @var \Drupal\views\Entity\View $view */
181 $entity_storage = $this->entityManager->getStorage('view');
182 $view = $entity_storage->load('test_view_entity_test_revision');
183 $this->assertEqual('entity_test_update_revision', $view->get('base_table'));
184 $display = $view->getDisplay('default');
185 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
186 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']);
188 $this->renameRevisionBaseTable();
189 $this->entityDefinitionUpdateManager->applyUpdates();
191 /** @var \Drupal\views\Entity\View $view */
192 $entity_storage = $this->entityManager->getStorage('view');
193 $view = $entity_storage->load('test_view_entity_test_revision');
195 // Ensure the base table got renamed, so also the views fields.
196 $this->assertEqual('entity_test_update_revision_new', $view->get('base_table'));
197 $display = $view->getDisplay('default');
198 $this->assertEqual('entity_test_update_revision_new', $display['display_options']['fields']['id']['table']);
199 $this->assertEqual('entity_test_update_revision_new', $display['display_options']['fields']['name']['table']);
203 * Tests that renaming revision tables adapts the views.
205 public function testRevisionDataTableRename() {
206 $this->updateEntityTypeToRevisionable();
207 // Multiple changes, so we have to invalidate the caches, otherwise
208 // the second update will revert the first.
209 $this->entityManager->clearCachedDefinitions();
210 $this->updateEntityTypeToTranslatable();
211 $this->entityDefinitionUpdateManager->applyUpdates();
213 /** @var \Drupal\views\Entity\View $view */
214 $entity_storage = $this->entityManager->getStorage('view');
215 $view = $entity_storage->load('test_view_entity_test_revision');
216 $this->assertEqual('entity_test_update_revision', $view->get('base_table'));
217 $display = $view->getDisplay('default');
218 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
219 $this->assertEqual('entity_test_update_revision_data', $display['display_options']['fields']['name']['table']);
221 $this->renameRevisionDataTable();
222 $this->entityDefinitionUpdateManager->applyUpdates();
224 /** @var \Drupal\views\Entity\View $view */
225 $entity_storage = $this->entityManager->getStorage('view');
226 $view = $entity_storage->load('test_view_entity_test_revision');
228 // Ensure the base table got renamed, so also the views fields.
229 $this->assertEqual('entity_test_update_revision', $view->get('base_table'));
230 $display = $view->getDisplay('default');
231 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
232 $this->assertEqual('entity_test_update_revision_data_new', $display['display_options']['fields']['name']['table']);
236 * Tests that adding data tables adapts the views.
238 public function testDataTableAddition() {
239 $this->updateEntityTypeToTranslatable();
240 $this->entityDefinitionUpdateManager->applyUpdates();
242 /** @var \Drupal\views\Entity\View $view */
243 $entity_storage = $this->entityManager->getStorage('view');
244 $view = $entity_storage->load('test_view_entity_test');
246 // Ensure the data table got renamed, so also the views fields.
247 $this->assertEqual('entity_test_update', $view->get('base_table'));
248 $display = $view->getDisplay('default');
249 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
250 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
254 * Tests that enabling revisions doesn't do anything.
256 public function testRevisionEnabling() {
257 $this->updateEntityTypeToRevisionable();
258 $this->entityDefinitionUpdateManager->applyUpdates();
260 /** @var \Drupal\views\Entity\View $view */
261 $entity_storage = $this->entityManager->getStorage('view');
262 $view = $entity_storage->load('test_view_entity_test');
264 // Ensure that nothing happens.
265 $this->assertEqual('entity_test_update', $view->get('base_table'));
266 $display = $view->getDisplay('default');
267 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
268 $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
272 * Tests that removing revision support disables the view.
274 public function testRevisionDisabling() {
275 $this->updateEntityTypeToRevisionable();
276 $this->entityDefinitionUpdateManager->applyUpdates();
278 $this->updateEntityTypeToNotRevisionable();
279 $this->entityDefinitionUpdateManager->applyUpdates();
281 /** @var \Drupal\views\Entity\View $view */
282 $entity_storage = $this->entityManager->getStorage('view');
283 $view = $entity_storage->load('test_view_entity_test_revision');
285 $this->assertFalse($view->status());
289 * Tests a bunch possible entity definition table updates.
291 public function testVariousTableUpdates() {
292 // We want to test the following permutations of entity definition updates:
293 // base <-> base + translation
294 // base + translation <-> base + translation + revision
295 // base + revision <-> base + translation + revision
296 // base <-> base + revision
297 // base <-> base + translation + revision
299 // base <-> base + translation
300 $this->updateEntityTypeToTranslatable();
301 $this->entityDefinitionUpdateManager->applyUpdates();
302 list($view, $display) = $this->getUpdatedViewAndDisplay();
304 $this->assertEqual('entity_test_update', $view->get('base_table'));
305 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
306 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
308 $this->updateEntityTypeToNotTranslatable();
309 $this->entityDefinitionUpdateManager->applyUpdates();
310 list($view, $display) = $this->getUpdatedViewAndDisplay();
312 $this->assertEqual('entity_test_update', $view->get('base_table'));
313 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
314 $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
316 $this->resetEntityType();
318 // base + translation <-> base + translation + revision
319 $this->updateEntityTypeToTranslatable();
320 $this->entityDefinitionUpdateManager->applyUpdates();
321 list($view, $display) = $this->getUpdatedViewAndDisplay();
323 $this->assertEqual('entity_test_update', $view->get('base_table'));
324 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
325 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
327 $this->updateEntityTypeToRevisionable();
328 $this->entityDefinitionUpdateManager->applyUpdates();
329 list($view, $display) = $this->getUpdatedViewAndDisplay();
331 $this->assertEqual('entity_test_update', $view->get('base_table'));
332 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
333 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
335 $this->updateEntityTypeToNotRevisionable();
336 $this->entityDefinitionUpdateManager->applyUpdates();
337 list($view, $display) = $this->getUpdatedViewAndDisplay();
339 $this->assertEqual('entity_test_update', $view->get('base_table'));
340 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
341 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
343 $this->resetEntityType();
345 // base + revision <-> base + translation + revision
346 $this->updateEntityTypeToRevisionable();
347 list($view, $display) = $this->getUpdatedViewAndDisplay();
349 $this->assertEqual('entity_test_update', $view->get('base_table'));
350 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
351 $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
353 $this->updateEntityTypeToTranslatable();
354 $this->entityDefinitionUpdateManager->applyUpdates();
355 list($view, $display) = $this->getUpdatedViewAndDisplay();
357 $this->assertEqual('entity_test_update', $view->get('base_table'));
358 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
359 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
361 $this->updateEntityTypeToNotTranslatable();
362 $this->entityDefinitionUpdateManager->applyUpdates();
363 list($view, $display) = $this->getUpdatedViewAndDisplay();
365 $this->assertEqual('entity_test_update', $view->get('base_table'));
366 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
367 $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
369 $this->resetEntityType();
371 // base <-> base + revision
372 $this->updateEntityTypeToRevisionable();
373 $this->entityDefinitionUpdateManager->applyUpdates();
374 list($view, $display) = $this->getUpdatedViewAndDisplay();
376 $this->assertEqual('entity_test_update', $view->get('base_table'));
377 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
378 $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
380 $this->updateEntityTypeToNotRevisionable();
381 $this->entityDefinitionUpdateManager->applyUpdates();
382 list($view, $display) = $this->getUpdatedViewAndDisplay();
384 $this->assertEqual('entity_test_update', $view->get('base_table'));
385 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
386 $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
388 $this->resetEntityType();
390 // base <-> base + translation + revision
391 $this->updateEntityTypeToRevisionable();
392 $this->updateEntityTypeToTranslatable();
393 $this->entityDefinitionUpdateManager->applyUpdates();
394 list($view, $display) = $this->getUpdatedViewAndDisplay();
396 $this->assertEqual('entity_test_update', $view->get('base_table'));
397 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
398 $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
400 $this->updateEntityTypeToNotRevisionable();
401 $this->updateEntityTypeToNotTranslatable();
402 $this->entityDefinitionUpdateManager->applyUpdates();
403 list($view, $display) = $this->getUpdatedViewAndDisplay();
405 $this->assertEqual('entity_test_update', $view->get('base_table'));
406 $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
407 $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
411 * Tests some possible entity table updates for a revision view.
413 public function testVariousTableUpdatesForRevisionView() {
414 // base + revision <-> base + translation + revision
415 $this->updateEntityTypeToRevisionable();
416 // Multiple changes, so we have to invalidate the caches, otherwise
417 // the second update will revert the first.
418 $this->entityManager->clearCachedDefinitions();
420 list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE);
422 $this->assertEqual('entity_test_update_revision', $view->get('base_table'));
423 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
424 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']);
426 $this->updateEntityTypeToTranslatable();
427 $this->entityDefinitionUpdateManager->applyUpdates();
428 list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE);
430 $this->assertEqual('entity_test_update_revision', $view->get('base_table'));
431 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
432 $this->assertEqual('entity_test_update_revision_data', $display['display_options']['fields']['name']['table']);
434 $this->updateEntityTypeToNotTranslatable();
435 $this->entityDefinitionUpdateManager->applyUpdates();
436 list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE);
438 $this->assertEqual('entity_test_update_revision', $view->get('base_table'));
439 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
440 $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']);
442 $this->resetEntityType();
446 * Gets a view and its display.
448 * @param bool $revision
449 * (optional) TRUE if we want to get a revision view.
452 * An array with the view as first item, and the display as second.
454 protected function getUpdatedViewAndDisplay($revision = FALSE) {
455 $entity_storage = $this->entityManager->getStorage('view');
456 /** @var \Drupal\views\Entity\View $view */
457 $view = $entity_storage->load($revision ? 'test_view_entity_test_revision' : 'test_view_entity_test');
458 $display = $view->getDisplay('default');
460 return [$view, $display];