24e4e6af5105c88c2349329cb964f6c8fd5ccb10
[yaffs-website] / web / core / modules / views / tests / src / Kernel / EventSubscriber / ViewsEntitySchemaSubscriberIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\EventSubscriber;
4
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;
9
10 /**
11  * Tests \Drupal\views\EventSubscriber\ViewsEntitySchemaSubscriber
12  *
13  * @group Views
14  */
15 class ViewsEntitySchemaSubscriberIntegrationTest extends ViewsKernelTestBase {
16
17   use EntityDefinitionTestTrait;
18
19   /**
20    * The entity definition update manager.
21    *
22    * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
23    */
24   protected $entityDefinitionUpdateManager;
25
26   /**
27    * {@inheritdoc}
28    */
29   public static $modules = ['entity_test', 'entity_test_update', 'user', 'text'];
30
31   /**
32    * Views used by this test.
33    *
34    * @var array
35    */
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'];
37
38   /**
39    * The event dispatcher.
40    *
41    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
42    */
43   protected $eventDispatcher;
44
45   /**
46    * The tested event subscriber of views.
47    *
48    * @var \Drupal\views\EventSubscriber\ViewsEntitySchemaSubscriber
49    */
50   protected $eventSubscriber;
51
52   /**
53    * The entity manager service.
54    *
55    * @var \Drupal\Core\Entity\EntityManagerInterface
56    */
57   protected $entityManager;
58
59   /**
60    * The state service.
61    *
62    * @var \Drupal\Core\State\StateInterface
63    */
64   protected $state;
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function setUp($import_test_views = TRUE) {
70     parent::setUp();
71
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');
77
78     $this->database = $this->container->get('database');
79
80     // Install every entity type's schema that wasn't installed in the parent
81     // method.
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);
84     }
85   }
86
87   /**
88    * Tests that views are disabled when an entity type is deleted.
89    */
90   public function testDeleteEntityType() {
91     $entity_storage = $this->entityManager->getStorage('view');
92
93     // Make the test entity type revisionable.
94     $this->updateEntityTypeToRevisionable();
95     $this->entityDefinitionUpdateManager->applyUpdates();
96
97     $views = $entity_storage->loadMultiple();
98
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']));
104
105     $event = new EntityTypeEvent($this->entityManager->getDefinition('entity_test_update'));
106     $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, $event);
107
108     // We expect that views which use 'entity_test_update' as base tables are
109     // disabled.
110     $views = $entity_storage->loadMultiple();
111
112     // Ensure that all test views still exists after the deletion of the
113     // entity type.
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']));
118
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());
124   }
125
126   /**
127    * Tests that renaming base tables adapts the views.
128    */
129   public function testBaseTableRename() {
130     $this->renameBaseTable();
131     $this->entityDefinitionUpdateManager->applyUpdates();
132
133     /** @var \Drupal\views\Entity\View $view */
134     $entity_storage = $this->entityManager->getStorage('view');
135     $view = $entity_storage->load('test_view_entity_test');
136
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']);
142   }
143
144   /**
145    * Tests that renaming data tables adapts the views.
146    */
147   public function testDataTableRename() {
148     $this->updateEntityTypeToTranslatable();
149     $this->entityDefinitionUpdateManager->applyUpdates();
150
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']);
158
159     $this->renameDataTable();
160     $this->entityDefinitionUpdateManager->applyUpdates();
161
162     /** @var \Drupal\views\Entity\View $view */
163     $entity_storage = $this->entityManager->getStorage('view');
164     $view = $entity_storage->load('test_view_entity_test_data');
165
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']);
171   }
172
173   /**
174    * Tests that renaming revision tables adapts the views.
175    */
176   public function testRevisionBaseTableRename() {
177     $this->updateEntityTypeToRevisionable();
178     $this->entityDefinitionUpdateManager->applyUpdates();
179
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']);
187
188     $this->renameRevisionBaseTable();
189     $this->entityDefinitionUpdateManager->applyUpdates();
190
191     /** @var \Drupal\views\Entity\View $view */
192     $entity_storage = $this->entityManager->getStorage('view');
193     $view = $entity_storage->load('test_view_entity_test_revision');
194
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']);
200   }
201
202   /**
203    * Tests that renaming revision tables adapts the views.
204    */
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();
212
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']);
220
221     $this->renameRevisionDataTable();
222     $this->entityDefinitionUpdateManager->applyUpdates();
223
224     /** @var \Drupal\views\Entity\View $view */
225     $entity_storage = $this->entityManager->getStorage('view');
226     $view = $entity_storage->load('test_view_entity_test_revision');
227
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']);
233   }
234
235   /**
236    * Tests that adding data tables adapts the views.
237    */
238   public function testDataTableAddition() {
239     $this->updateEntityTypeToTranslatable();
240     $this->entityDefinitionUpdateManager->applyUpdates();
241
242     /** @var \Drupal\views\Entity\View $view */
243     $entity_storage = $this->entityManager->getStorage('view');
244     $view = $entity_storage->load('test_view_entity_test');
245
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']);
251   }
252
253   /**
254    * Tests that enabling revisions doesn't do anything.
255    */
256   public function testRevisionEnabling() {
257     $this->updateEntityTypeToRevisionable();
258     $this->entityDefinitionUpdateManager->applyUpdates();
259
260     /** @var \Drupal\views\Entity\View $view */
261     $entity_storage = $this->entityManager->getStorage('view');
262     $view = $entity_storage->load('test_view_entity_test');
263
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']);
269   }
270
271   /**
272    * Tests that removing revision support disables the view.
273    */
274   public function testRevisionDisabling() {
275     $this->updateEntityTypeToRevisionable();
276     $this->entityDefinitionUpdateManager->applyUpdates();
277
278     $this->updateEntityTypeToNotRevisionable();
279     $this->entityDefinitionUpdateManager->applyUpdates();
280
281     /** @var \Drupal\views\Entity\View $view */
282     $entity_storage = $this->entityManager->getStorage('view');
283     $view = $entity_storage->load('test_view_entity_test_revision');
284
285     $this->assertFalse($view->status());
286   }
287
288   /**
289    * Tests a bunch possible entity definition table updates.
290    */
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
298
299     // base <-> base + translation
300     $this->updateEntityTypeToTranslatable();
301     $this->entityDefinitionUpdateManager->applyUpdates();
302     list($view, $display) = $this->getUpdatedViewAndDisplay();
303
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']);
307
308     $this->updateEntityTypeToNotTranslatable();
309     $this->entityDefinitionUpdateManager->applyUpdates();
310     list($view, $display) = $this->getUpdatedViewAndDisplay();
311
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']);
315
316     $this->resetEntityType();
317
318     // base + translation <-> base + translation + revision
319     $this->updateEntityTypeToTranslatable();
320     $this->entityDefinitionUpdateManager->applyUpdates();
321     list($view, $display) = $this->getUpdatedViewAndDisplay();
322
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']);
326
327     $this->updateEntityTypeToRevisionable();
328     $this->entityDefinitionUpdateManager->applyUpdates();
329     list($view, $display) = $this->getUpdatedViewAndDisplay();
330
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']);
334
335     $this->updateEntityTypeToNotRevisionable();
336     $this->entityDefinitionUpdateManager->applyUpdates();
337     list($view, $display) = $this->getUpdatedViewAndDisplay();
338
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']);
342
343     $this->resetEntityType();
344
345     // base + revision <-> base + translation + revision
346     $this->updateEntityTypeToRevisionable();
347     list($view, $display) = $this->getUpdatedViewAndDisplay();
348
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']);
352
353     $this->updateEntityTypeToTranslatable();
354     $this->entityDefinitionUpdateManager->applyUpdates();
355     list($view, $display) = $this->getUpdatedViewAndDisplay();
356
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']);
360
361     $this->updateEntityTypeToNotTranslatable();
362     $this->entityDefinitionUpdateManager->applyUpdates();
363     list($view, $display) = $this->getUpdatedViewAndDisplay();
364
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']);
368
369     $this->resetEntityType();
370
371     // base <-> base + revision
372     $this->updateEntityTypeToRevisionable();
373     $this->entityDefinitionUpdateManager->applyUpdates();
374     list($view, $display) = $this->getUpdatedViewAndDisplay();
375
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']);
379
380     $this->updateEntityTypeToNotRevisionable();
381     $this->entityDefinitionUpdateManager->applyUpdates();
382     list($view, $display) = $this->getUpdatedViewAndDisplay();
383
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']);
387
388     $this->resetEntityType();
389
390     // base <-> base + translation + revision
391     $this->updateEntityTypeToRevisionable();
392     $this->updateEntityTypeToTranslatable();
393     $this->entityDefinitionUpdateManager->applyUpdates();
394     list($view, $display) = $this->getUpdatedViewAndDisplay();
395
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']);
399
400     $this->updateEntityTypeToNotRevisionable();
401     $this->updateEntityTypeToNotTranslatable();
402     $this->entityDefinitionUpdateManager->applyUpdates();
403     list($view, $display) = $this->getUpdatedViewAndDisplay();
404
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']);
408   }
409
410   /**
411    * Tests some possible entity table updates for a revision view.
412    */
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();
419
420     list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE);
421
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']);
425
426     $this->updateEntityTypeToTranslatable();
427     $this->entityDefinitionUpdateManager->applyUpdates();
428     list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE);
429
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']);
433
434     $this->updateEntityTypeToNotTranslatable();
435     $this->entityDefinitionUpdateManager->applyUpdates();
436     list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE);
437
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']);
441
442     $this->resetEntityType();
443   }
444
445   /**
446    * Gets a view and its display.
447    *
448    * @param bool $revision
449    *   (optional) TRUE if we want to get a revision view.
450    *
451    * @return array
452    *   An array with the view as first item, and the display as second.
453    */
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');
459
460     return [$view, $display];
461   }
462
463 }