Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / tests / src / Unit / ViewsDataTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit;
4
5 use Drupal\Core\Language\Language;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\views\ViewsData;
8 use Drupal\views\Tests\ViewTestData;
9
10 /**
11  * @coversDefaultClass \Drupal\views\ViewsData
12  * @group views
13  */
14 class ViewsDataTest extends UnitTestCase {
15
16   /**
17    * The mocked cache backend.
18    *
19    * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
20    */
21   protected $cacheBackend;
22
23   /**
24    * The mocked cache tags invalidator.
25    *
26    * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $cacheTagsInvalidator;
29
30   /**
31    * The mocked module handler.
32    *
33    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
34    */
35   protected $moduleHandler;
36
37   /**
38    * The mocked config factory.
39    *
40    * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
41    */
42   protected $configFactory;
43
44   /**
45    * The mocked language manager.
46    *
47    * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
48    */
49   protected $languageManager;
50
51   /**
52    * The tested views data class.
53    *
54    * @var \Drupal\views\ViewsData
55    */
56   protected $viewsData;
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function setUp() {
62     $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
63     $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
64     $this->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator);
65
66     $configs = [];
67     $configs['views.settings']['skip_cache'] = FALSE;
68     $this->configFactory = $this->getConfigFactoryStub($configs);
69     $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
70     $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
71     $this->languageManager->expects($this->any())
72       ->method('getCurrentLanguage')
73       ->will($this->returnValue(new Language(['id' => 'en'])));
74
75     $this->viewsData = new ViewsData($this->cacheBackend, $this->configFactory, $this->moduleHandler, $this->languageManager);
76   }
77
78   /**
79    * Returns the views data definition.
80    */
81   protected function viewsData() {
82     $data = ViewTestData::viewsData();
83
84     // Tweak the views data to have a base for testing.
85     unset($data['views_test_data']['id']['field']);
86     unset($data['views_test_data']['name']['argument']);
87     unset($data['views_test_data']['age']['filter']);
88     unset($data['views_test_data']['job']['sort']);
89     $data['views_test_data']['created']['area']['id'] = 'text';
90     $data['views_test_data']['age']['area']['id'] = 'text';
91     $data['views_test_data']['age']['area']['sub_type'] = 'header';
92     $data['views_test_data']['job']['area']['id'] = 'text';
93     $data['views_test_data']['job']['area']['sub_type'] = ['header', 'footer'];
94
95     // Duplicate the example views test data for different weight, different title,
96     // and matching data.
97     $data['views_test_data_2'] = $data['views_test_data'];
98     $data['views_test_data_2']['table']['base']['weight'] = 50;
99
100     $data['views_test_data_3'] = $data['views_test_data'];
101     $data['views_test_data_3']['table']['base']['weight'] = -50;
102
103     $data['views_test_data_4'] = $data['views_test_data'];
104     $data['views_test_data_4']['table']['base']['title'] = 'A different title';
105
106     $data['views_test_data_5'] = $data['views_test_data'];
107     $data['views_test_data_5']['table']['base']['title'] = 'Z different title';
108
109     $data['views_test_data_6'] = $data['views_test_data'];
110
111     return $data;
112   }
113
114   /**
115    * Returns the views data definition with the provider key.
116    *
117    * @return array
118    *
119    * @see static::viewsData()
120    */
121   protected function viewsDataWithProvider() {
122     $views_data = static::viewsData();
123     foreach (array_keys($views_data) as $table) {
124       $views_data[$table]['table']['provider'] = 'views_test_data';
125     }
126     return $views_data;
127   }
128
129   /**
130    * Mocks the basic module handler used for the test.
131    *
132    * @return \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
133    */
134   protected function setupMockedModuleHandler() {
135     $views_data = $this->viewsData();
136     $this->moduleHandler->expects($this->at(0))
137       ->method('getImplementations')
138       ->with('views_data')
139       ->willReturn(['views_test_data']);
140     $this->moduleHandler->expects($this->at(1))
141       ->method('invoke')
142       ->with('views_test_data', 'views_data')
143       ->willReturn($views_data);
144   }
145
146   /**
147    * Tests the fetchBaseTables() method.
148    */
149   public function testFetchBaseTables() {
150     $this->setupMockedModuleHandler();
151     $data = $this->viewsData->getAll();
152
153     $base_tables = $this->viewsData->fetchBaseTables();
154
155     // Ensure that 'provider' is set for each base table.
156     foreach (array_keys($base_tables) as $base_table) {
157       $this->assertEquals('views_test_data', $data[$base_table]['table']['provider']);
158     }
159
160     // Test the number of tables returned and their order.
161     $this->assertCount(6, $base_tables, 'The correct amount of base tables were returned.');
162     $base_tables_keys = array_keys($base_tables);
163     for ($i = 1; $i < count($base_tables); ++$i) {
164       $prev = $base_tables[$base_tables_keys[$i - 1]];
165       $current = $base_tables[$base_tables_keys[$i]];
166       $this->assertTrue($prev['weight'] <= $current['weight'] && $prev['title'] <= $prev['title'], 'The tables are sorted as expected.');
167     }
168
169     // Test the values returned for each base table.
170     $defaults = [
171       'title' => '',
172       'help' => '',
173       'weight' => 0,
174     ];
175     foreach ($base_tables as $base_table => $info) {
176       // Merge in default values as in fetchBaseTables().
177       $expected = $data[$base_table]['table']['base'] += $defaults;
178       foreach ($defaults as $key => $default) {
179         $this->assertSame($info[$key], $expected[$key]);
180       }
181     }
182   }
183
184   /**
185    * Tests fetching all the views data without a static cache.
186    */
187   public function testGetOnFirstCall() {
188     // Ensure that the hooks are just invoked once.
189     $this->setupMockedModuleHandler();
190
191     $this->moduleHandler->expects($this->at(2))
192       ->method('alter')
193       ->with('views_data', $this->viewsDataWithProvider());
194
195     $this->cacheBackend->expects($this->once())
196       ->method('get')
197       ->with("views_data:en")
198       ->will($this->returnValue(FALSE));
199
200     $expected_views_data = $this->viewsDataWithProvider();
201     $views_data = $this->viewsData->getAll();
202     $this->assertSame($expected_views_data, $views_data);
203   }
204
205   /**
206    * Tests the cache of the full and single table data.
207    */
208   public function testFullAndTableGetCache() {
209     $expected_views_data = $this->viewsDataWithProvider();
210     $table_name = 'views_test_data';
211     $table_name_2 = 'views_test_data_2';
212     $random_table_name = $this->randomMachineName();
213
214     // Views data should be invoked twice due to the clear call.
215     $this->moduleHandler->expects($this->at(0))
216       ->method('getImplementations')
217       ->with('views_data')
218       ->willReturn(['views_test_data']);
219     $this->moduleHandler->expects($this->at(1))
220       ->method('invoke')
221       ->with('views_test_data', 'views_data')
222       ->willReturn($this->viewsData());
223     $this->moduleHandler->expects($this->at(2))
224       ->method('alter')
225       ->with('views_data', $expected_views_data);
226
227     $this->moduleHandler->expects($this->at(3))
228       ->method('getImplementations')
229       ->with('views_data')
230       ->willReturn(['views_test_data']);
231     $this->moduleHandler->expects($this->at(4))
232       ->method('invoke')
233       ->with('views_test_data', 'views_data')
234       ->willReturn($this->viewsData());
235     $this->moduleHandler->expects($this->at(5))
236       ->method('alter')
237       ->with('views_data', $expected_views_data);
238
239     // The cache should only be called once (before the clear() call) as get
240     // will get all table data in the first get().
241     $this->cacheBackend->expects($this->at(0))
242       ->method('get')
243       ->with("views_data:en")
244       ->will($this->returnValue(FALSE));
245     $this->cacheBackend->expects($this->at(1))
246       ->method('set')
247       ->with("views_data:en", $expected_views_data);
248     $this->cacheBackend->expects($this->at(2))
249       ->method('get')
250       ->with("views_data:$random_table_name:en")
251       ->will($this->returnValue(FALSE));
252     $this->cacheBackend->expects($this->at(3))
253       ->method('set')
254       ->with("views_data:$random_table_name:en", []);
255     $this->cacheTagsInvalidator->expects($this->once())
256       ->method('invalidateTags')
257       ->with(['views_data']);
258     $this->cacheBackend->expects($this->at(4))
259       ->method('get')
260       ->with("views_data:en")
261       ->will($this->returnValue(FALSE));
262     $this->cacheBackend->expects($this->at(5))
263       ->method('set')
264       ->with("views_data:en", $expected_views_data);
265     $this->cacheBackend->expects($this->at(6))
266       ->method('get')
267       ->with("views_data:$random_table_name:en")
268       ->will($this->returnValue(FALSE));
269     $this->cacheBackend->expects($this->at(7))
270       ->method('set')
271       ->with("views_data:$random_table_name:en", []);
272
273     $views_data = $this->viewsData->getAll();
274     $this->assertSame($expected_views_data, $views_data);
275
276     // Request a specific table should be static cached.
277     $views_data = $this->viewsData->get($table_name);
278     $this->assertSame($expected_views_data[$table_name], $views_data);
279
280     // Another table being requested should also come from the static cache.
281     $views_data = $this->viewsData->get($table_name_2);
282     $this->assertSame($expected_views_data[$table_name_2], $views_data);
283
284     $views_data = $this->viewsData->get($random_table_name);
285     $this->assertSame([], $views_data);
286
287     $this->viewsData->clear();
288
289     // Get the views data again.
290     $this->viewsData->getAll();
291     $this->viewsData->get($table_name);
292     $this->viewsData->get($table_name_2);
293     $this->viewsData->get($random_table_name);
294   }
295
296   /**
297    * Tests the caching of the full views data.
298    */
299   public function testFullGetCache() {
300     $expected_views_data = $this->viewsDataWithProvider();
301
302     // Views data should be invoked once.
303     $this->setupMockedModuleHandler();
304
305     $this->moduleHandler->expects($this->once())
306       ->method('alter')
307       ->with('views_data', $expected_views_data);
308
309     $this->cacheBackend->expects($this->once())
310       ->method('get')
311       ->with("views_data:en")
312       ->will($this->returnValue(FALSE));
313
314     $views_data = $this->viewsData->getAll();
315     $this->assertSame($expected_views_data, $views_data);
316
317     $views_data = $this->viewsData->getAll();
318     $this->assertSame($expected_views_data, $views_data);
319   }
320
321   /**
322    * Tests the caching of the views data for a specific table.
323    */
324   public function testSingleTableGetCache() {
325     $table_name = 'views_test_data';
326     $expected_views_data = $this->viewsDataWithProvider();
327
328     // Views data should be invoked once.
329     $this->setupMockedModuleHandler();
330
331     $this->moduleHandler->expects($this->once())
332       ->method('alter')
333       ->with('views_data', $this->viewsDataWithProvider());
334
335     $this->cacheBackend->expects($this->at(0))
336       ->method('get')
337       ->with("views_data:$table_name:en")
338       ->will($this->returnValue(FALSE));
339     $this->cacheBackend->expects($this->at(1))
340       ->method('get')
341       ->with("views_data:en")
342       ->will($this->returnValue(FALSE));
343
344     $views_data = $this->viewsData->get($table_name);
345     $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching views data by table works as expected.');
346
347     $views_data = $this->viewsData->get($table_name);
348     $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching cached views data by table works as expected.');
349
350     // Test that this data is present if all views data is returned.
351     $views_data = $this->viewsData->getAll();
352
353     $this->assertArrayHasKey($table_name, $views_data, 'Make sure the views_test_data info appears in the total views data.');
354     $this->assertSame($expected_views_data[$table_name], $views_data[$table_name], 'Make sure the views_test_data has the expected values.');
355   }
356
357   /**
358    * Tests building the views data with a non existing table.
359    */
360   public function testNonExistingTableGetCache() {
361     $random_table_name = $this->randomMachineName();
362
363     // Views data should be invoked once.
364     $this->setupMockedModuleHandler();
365
366     $this->moduleHandler->expects($this->once())
367       ->method('alter')
368       ->with('views_data', $this->viewsDataWithProvider());
369
370     $this->cacheBackend->expects($this->at(0))
371       ->method('get')
372       ->with("views_data:$random_table_name:en")
373       ->will($this->returnValue(FALSE));
374     $this->cacheBackend->expects($this->at(1))
375       ->method('get')
376       ->with("views_data:en")
377       ->will($this->returnValue(FALSE));
378
379     // All views data should be requested on the first try.
380     $views_data = $this->viewsData->get($random_table_name);
381     $this->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');
382
383     // Test no data is rebuilt when requesting an invalid table again.
384     $views_data = $this->viewsData->get($random_table_name);
385     $this->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');
386   }
387
388   /**
389    * Tests the cache backend behavior with requesting the same table multiple
390    */
391   public function testCacheCallsWithSameTableMultipleTimes() {
392     $expected_views_data = $this->viewsDataWithProvider();
393
394     $this->setupMockedModuleHandler();
395
396     $this->cacheBackend->expects($this->at(0))
397       ->method('get')
398       ->with('views_data:views_test_data:en');
399     $this->cacheBackend->expects($this->at(1))
400       ->method('get')
401       ->with('views_data:en');
402     $this->cacheBackend->expects($this->at(2))
403       ->method('set')
404       ->with('views_data:en', $expected_views_data);
405     $this->cacheBackend->expects($this->at(3))
406       ->method('set')
407       ->with('views_data:views_test_data:en', $expected_views_data['views_test_data']);
408
409     // Request the same table 5 times. The caches are empty at this point, so
410     // what will happen is that it will first check for a cache entry for the
411     // given table, get a cache miss, then try the cache entry for all tables,
412     // which does not exist yet either. As a result, it rebuilds the information
413     // and writes a cache entry for all tables and the requested table.
414     $table_name = 'views_test_data';
415     for ($i = 0; $i < 5; $i++) {
416       $views_data = $this->viewsData->get($table_name);
417       $this->assertSame($expected_views_data['views_test_data'], $views_data);
418     }
419   }
420
421   /**
422    * Tests the cache calls for a single table and warm cache for:
423    *   - all tables
424    *   - views_test_data
425    */
426   public function testCacheCallsWithSameTableMultipleTimesAndWarmCache() {
427     $expected_views_data = $this->viewsDataWithProvider();
428     $this->moduleHandler->expects($this->never())
429       ->method('getImplementations');
430
431     // Setup a warm cache backend for a single table.
432     $this->cacheBackend->expects($this->once())
433       ->method('get')
434       ->with('views_data:views_test_data:en')
435       ->will($this->returnValue((object) ['data' => $expected_views_data['views_test_data']]));
436     $this->cacheBackend->expects($this->never())
437       ->method('set');
438
439     // We have a warm cache now, so this will only request the tables-specific
440     // cache entry and return that.
441     for ($i = 0; $i < 5; $i++) {
442       $views_data = $this->viewsData->get('views_test_data');
443       $this->assertSame($expected_views_data['views_test_data'], $views_data);
444     }
445   }
446
447   /**
448    * Tests the cache calls for a different table than the one in cache:
449    *
450    * Warm cache:
451    *   - all tables
452    *   - views_test_data
453    * Not warm cache:
454    *   - views_test_data_2
455    */
456   public function testCacheCallsWithWarmCacheAndDifferentTable() {
457     $expected_views_data = $this->viewsDataWithProvider();
458     $this->moduleHandler->expects($this->never())
459       ->method('getImplementations');
460
461     // Setup a warm cache backend for a single table.
462     $this->cacheBackend->expects($this->at(0))
463       ->method('get')
464       ->with('views_data:views_test_data_2:en');
465     $this->cacheBackend->expects($this->at(1))
466       ->method('get')
467       ->with('views_data:en')
468       ->will($this->returnValue((object) ['data' => $expected_views_data]));
469     $this->cacheBackend->expects($this->at(2))
470       ->method('set')
471       ->with('views_data:views_test_data_2:en', $expected_views_data['views_test_data_2']);
472
473     // Requests a different table as the cache contains. This will fail to get a
474     // table specific cache entry, load the cache entry for all tables and save
475     // a cache entry for this table but not all.
476     for ($i = 0; $i < 5; $i++) {
477       $views_data = $this->viewsData->get('views_test_data_2');
478       $this->assertSame($expected_views_data['views_test_data_2'], $views_data);
479     }
480   }
481
482   /**
483    * Tests the cache calls for an not existing table:
484    *
485    * Warm cache:
486    *   - all tables
487    *   - views_test_data
488    * Not warm cache:
489    *   - $non_existing_table
490    */
491   public function testCacheCallsWithWarmCacheAndInvalidTable() {
492     $expected_views_data = $this->viewsDataWithProvider();
493     $non_existing_table = $this->randomMachineName();
494     $this->moduleHandler->expects($this->never())
495       ->method('getImplementations');
496
497     // Setup a warm cache backend for a single table.
498     $this->cacheBackend->expects($this->at(0))
499       ->method('get')
500       ->with("views_data:$non_existing_table:en");
501     $this->cacheBackend->expects($this->at(1))
502       ->method('get')
503       ->with('views_data:en')
504       ->will($this->returnValue((object) ['data' => $expected_views_data]));
505     $this->cacheBackend->expects($this->at(2))
506       ->method('set')
507       ->with("views_data:$non_existing_table:en", []);
508
509     // Initialize the views data cache and request a non-existing table. This
510     // will result in the same cache requests as we explicitly write an empty
511     // cache entry for non-existing tables to avoid unnecessary requests in
512     // those situations. We do have to load the cache entry for all tables to
513     // check if the table does exist or not.
514     for ($i = 0; $i < 5; $i++) {
515       $views_data = $this->viewsData->get($non_existing_table);
516       $this->assertSame([], $views_data);
517     }
518   }
519
520   /**
521    * Tests the cache calls for an not existing table:
522    *
523    * Warm cache:
524    *   - all tables
525    *   - views_test_data
526    *   - $non_existing_table
527    */
528   public function testCacheCallsWithWarmCacheForInvalidTable() {
529     $non_existing_table = $this->randomMachineName();
530     $this->moduleHandler->expects($this->never())
531       ->method('getImplementations');
532
533     // Setup a warm cache backend for a single table.
534     $this->cacheBackend->expects($this->once())
535       ->method('get')
536       ->with("views_data:$non_existing_table:en")
537       ->will($this->returnValue((object) ['data' => []]));
538     $this->cacheBackend->expects($this->never())
539       ->method('set');
540
541     // Initialize the views data cache and request a non-existing table. This
542     // will result in the same cache requests as we explicitly write an empty
543     // cache entry for non-existing tables to avoid unnecessary requests in
544     // those situations. We do have to load the cache entry for all tables to
545     // check if the table does exist or not.
546     for ($i = 0; $i < 5; $i++) {
547       $views_data = $this->viewsData->get($non_existing_table);
548       $this->assertSame([], $views_data);
549     }
550   }
551
552   /**
553    * Tests the cache calls for all views data without a warm cache.
554    */
555   public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
556     $expected_views_data = $this->viewsDataWithProvider();
557     $this->setupMockedModuleHandler();
558
559     // Setup a warm cache backend for a single table.
560     $this->cacheBackend->expects($this->once())
561       ->method('get')
562       ->with("views_data:en");
563     $this->cacheBackend->expects($this->once())
564       ->method('set')
565       ->with('views_data:en', $expected_views_data);
566
567     // Initialize the views data cache and repeat with no specified table. This
568     // should only load the cache entry for all tables.
569     for ($i = 0; $i < 5; $i++) {
570       $views_data = $this->viewsData->getAll();
571       $this->assertSame($expected_views_data, $views_data);
572     }
573   }
574
575   /**
576    * Tests the cache calls for all views data.
577    *
578    * Warm cache:
579    *   - all tables
580    */
581   public function testCacheCallsWithWarmCacheAndGetAllTables() {
582     $expected_views_data = $this->viewsDataWithProvider();
583     $this->moduleHandler->expects($this->never())
584       ->method('getImplementations');
585
586     // Setup a warm cache backend for a single table.
587     $this->cacheBackend->expects($this->once())
588       ->method('get')
589       ->with("views_data:en")
590       ->will($this->returnValue((object) ['data' => $expected_views_data]));
591     $this->cacheBackend->expects($this->never())
592       ->method('set');
593
594     // Initialize the views data cache and repeat with no specified table. This
595     // should only load the cache entry for all tables.
596     for ($i = 0; $i < 5; $i++) {
597       $views_data = $this->viewsData->getAll();
598       $this->assertSame($expected_views_data, $views_data);
599     }
600   }
601
602   /**
603    * Tests the cache calls for multiple tables without warm caches.
604    *
605    * @covers ::get
606    */
607   public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
608     $expected_views_data = $this->viewsDataWithProvider();
609     $table_name = 'views_test_data';
610     $table_name_2 = 'views_test_data_2';
611
612     // Setup a warm cache backend for all table data, but not single tables.
613     $this->cacheBackend->expects($this->at(0))
614       ->method('get')
615       ->with("views_data:$table_name:en")
616       ->will($this->returnValue(FALSE));
617     $this->cacheBackend->expects($this->at(1))
618       ->method('get')
619       ->with('views_data:en')
620       ->will($this->returnValue((object) ['data' => $expected_views_data]));
621     $this->cacheBackend->expects($this->at(2))
622       ->method('set')
623       ->with("views_data:$table_name:en", $expected_views_data[$table_name]);
624     $this->cacheBackend->expects($this->at(3))
625       ->method('get')
626       ->with("views_data:$table_name_2:en")
627       ->will($this->returnValue(FALSE));
628     $this->cacheBackend->expects($this->at(4))
629       ->method('set')
630       ->with("views_data:$table_name_2:en", $expected_views_data[$table_name_2]);
631
632     $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
633     $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
634
635     // Should only be invoked the first time.
636     $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
637     $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
638   }
639
640   /**
641    * Tests that getting all data has same results as getting data with NULL
642    * logic.
643    *
644    * @covers ::getAll
645    */
646   public function testGetAllEqualsToGetNull() {
647     $expected_views_data = $this->viewsDataWithProvider();
648     $this->setupMockedModuleHandler();
649
650     // Setup a warm cache backend for a single table.
651     $this->cacheBackend->expects($this->once())
652       ->method('get')
653       ->with("views_data:en");
654     $this->cacheBackend->expects($this->once())
655       ->method('set')
656       ->with('views_data:en', $expected_views_data);
657
658     // Initialize the views data cache and repeat with no specified table. This
659     // should only load the cache entry for all tables.
660     for ($i = 0; $i < 5; $i++) {
661       $this->assertSame($expected_views_data, $this->viewsData->getAll());
662       $this->assertSame($expected_views_data, $this->viewsData->get());
663     }
664   }
665
666 }