Upgraded drupal core with security updates
[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
240     // The cache should only be called once (before the clear() call) as get
241     // will get all table data in the first get().
242     $this->cacheBackend->expects($this->at(0))
243       ->method('get')
244       ->with("views_data:en")
245       ->will($this->returnValue(FALSE));
246     $this->cacheBackend->expects($this->at(1))
247       ->method('set')
248       ->with("views_data:en", $expected_views_data);
249     $this->cacheBackend->expects($this->at(2))
250       ->method('get')
251       ->with("views_data:$random_table_name:en")
252       ->will($this->returnValue(FALSE));
253     $this->cacheBackend->expects($this->at(3))
254       ->method('set')
255       ->with("views_data:$random_table_name:en", []);
256     $this->cacheTagsInvalidator->expects($this->once())
257       ->method('invalidateTags')
258       ->with(['views_data']);
259     $this->cacheBackend->expects($this->at(4))
260       ->method('get')
261       ->with("views_data:en")
262       ->will($this->returnValue(FALSE));
263     $this->cacheBackend->expects($this->at(5))
264       ->method('set')
265       ->with("views_data:en", $expected_views_data);
266     $this->cacheBackend->expects($this->at(6))
267       ->method('get')
268       ->with("views_data:$random_table_name:en")
269       ->will($this->returnValue(FALSE));
270     $this->cacheBackend->expects($this->at(7))
271       ->method('set')
272       ->with("views_data:$random_table_name:en", []);
273
274     $views_data = $this->viewsData->getAll();
275     $this->assertSame($expected_views_data, $views_data);
276
277     // Request a specific table should be static cached.
278     $views_data = $this->viewsData->get($table_name);
279     $this->assertSame($expected_views_data[$table_name], $views_data);
280
281     // Another table being requested should also come from the static cache.
282     $views_data = $this->viewsData->get($table_name_2);
283     $this->assertSame($expected_views_data[$table_name_2], $views_data);
284
285     $views_data = $this->viewsData->get($random_table_name);
286     $this->assertSame([], $views_data);
287
288     $this->viewsData->clear();
289
290     // Get the views data again.
291     $this->viewsData->getAll();
292     $this->viewsData->get($table_name);
293     $this->viewsData->get($table_name_2);
294     $this->viewsData->get($random_table_name);
295   }
296
297   /**
298    * Tests the caching of the full views data.
299    */
300   public function testFullGetCache() {
301     $expected_views_data = $this->viewsDataWithProvider();
302
303     // Views data should be invoked once.
304     $this->setupMockedModuleHandler();
305
306     $this->moduleHandler->expects($this->once())
307       ->method('alter')
308       ->with('views_data', $expected_views_data);
309
310     $this->cacheBackend->expects($this->once())
311       ->method('get')
312       ->with("views_data:en")
313       ->will($this->returnValue(FALSE));
314
315     $views_data = $this->viewsData->getAll();
316     $this->assertSame($expected_views_data, $views_data);
317
318     $views_data = $this->viewsData->getAll();
319     $this->assertSame($expected_views_data, $views_data);
320   }
321
322   /**
323    * Tests the caching of the views data for a specific table.
324    */
325   public function testSingleTableGetCache() {
326     $table_name = 'views_test_data';
327     $expected_views_data = $this->viewsDataWithProvider();
328
329     // Views data should be invoked once.
330     $this->setupMockedModuleHandler();
331
332     $this->moduleHandler->expects($this->once())
333       ->method('alter')
334       ->with('views_data', $this->viewsDataWithProvider());
335
336     $this->cacheBackend->expects($this->at(0))
337       ->method('get')
338       ->with("views_data:$table_name:en")
339       ->will($this->returnValue(FALSE));
340     $this->cacheBackend->expects($this->at(1))
341       ->method('get')
342       ->with("views_data:en")
343       ->will($this->returnValue(FALSE));
344
345     $views_data = $this->viewsData->get($table_name);
346     $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching views data by table works as expected.');
347
348     $views_data = $this->viewsData->get($table_name);
349     $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching cached views data by table works as expected.');
350
351     // Test that this data is present if all views data is returned.
352     $views_data = $this->viewsData->getAll();
353
354     $this->assertArrayHasKey($table_name, $views_data, 'Make sure the views_test_data info appears in the total views data.');
355     $this->assertSame($expected_views_data[$table_name], $views_data[$table_name], 'Make sure the views_test_data has the expected values.');
356   }
357
358   /**
359    * Tests building the views data with a non existing table.
360    */
361   public function testNonExistingTableGetCache() {
362     $random_table_name = $this->randomMachineName();
363
364     // Views data should be invoked once.
365     $this->setupMockedModuleHandler();
366
367     $this->moduleHandler->expects($this->once())
368       ->method('alter')
369       ->with('views_data', $this->viewsDataWithProvider());
370
371     $this->cacheBackend->expects($this->at(0))
372       ->method('get')
373       ->with("views_data:$random_table_name:en")
374       ->will($this->returnValue(FALSE));
375     $this->cacheBackend->expects($this->at(1))
376       ->method('get')
377       ->with("views_data:en")
378       ->will($this->returnValue(FALSE));
379
380     // All views data should be requested on the first try.
381     $views_data = $this->viewsData->get($random_table_name);
382     $this->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');
383
384     // Test no data is rebuilt when requesting an invalid table again.
385     $views_data = $this->viewsData->get($random_table_name);
386     $this->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');
387   }
388
389   /**
390    * Tests the cache backend behavior with requesting the same table multiple
391    */
392   public function testCacheCallsWithSameTableMultipleTimes() {
393     $expected_views_data = $this->viewsDataWithProvider();
394
395     $this->setupMockedModuleHandler();
396
397     $this->cacheBackend->expects($this->at(0))
398       ->method('get')
399       ->with('views_data:views_test_data:en');
400     $this->cacheBackend->expects($this->at(1))
401       ->method('get')
402       ->with('views_data:en');
403     $this->cacheBackend->expects($this->at(2))
404       ->method('set')
405       ->with('views_data:en', $expected_views_data);
406     $this->cacheBackend->expects($this->at(3))
407       ->method('set')
408       ->with('views_data:views_test_data:en', $expected_views_data['views_test_data']);
409
410     // Request the same table 5 times. The caches are empty at this point, so
411     // what will happen is that it will first check for a cache entry for the
412     // given table, get a cache miss, then try the cache entry for all tables,
413     // which does not exist yet either. As a result, it rebuilds the information
414     // and writes a cache entry for all tables and the requested table.
415     $table_name = 'views_test_data';
416     for ($i = 0; $i < 5; $i++) {
417       $views_data = $this->viewsData->get($table_name);
418       $this->assertSame($expected_views_data['views_test_data'], $views_data);
419     }
420   }
421
422   /**
423    * Tests the cache calls for a single table and warm cache for:
424    *   - all tables
425    *   - views_test_data
426    */
427   public function testCacheCallsWithSameTableMultipleTimesAndWarmCache() {
428     $expected_views_data = $this->viewsDataWithProvider();
429     $this->moduleHandler->expects($this->never())
430       ->method('getImplementations');
431
432     // Setup a warm cache backend for a single table.
433     $this->cacheBackend->expects($this->once())
434       ->method('get')
435       ->with('views_data:views_test_data:en')
436       ->will($this->returnValue((object) ['data' => $expected_views_data['views_test_data']]));
437     $this->cacheBackend->expects($this->never())
438       ->method('set');
439
440     // We have a warm cache now, so this will only request the tables-specific
441     // cache entry and return that.
442     for ($i = 0; $i < 5; $i++) {
443       $views_data = $this->viewsData->get('views_test_data');
444       $this->assertSame($expected_views_data['views_test_data'], $views_data);
445     }
446   }
447
448   /**
449    * Tests the cache calls for a different table than the one in cache:
450    *
451    * Warm cache:
452    *   - all tables
453    *   - views_test_data
454    * Not warm cache:
455    *   - views_test_data_2
456    */
457   public function testCacheCallsWithWarmCacheAndDifferentTable() {
458     $expected_views_data = $this->viewsDataWithProvider();
459     $this->moduleHandler->expects($this->never())
460       ->method('getImplementations');
461
462     // Setup a warm cache backend for a single table.
463     $this->cacheBackend->expects($this->at(0))
464       ->method('get')
465       ->with('views_data:views_test_data_2:en');
466     $this->cacheBackend->expects($this->at(1))
467       ->method('get')
468       ->with('views_data:en')
469       ->will($this->returnValue((object) ['data' => $expected_views_data]));
470     $this->cacheBackend->expects($this->at(2))
471       ->method('set')
472       ->with('views_data:views_test_data_2:en', $expected_views_data['views_test_data_2']);
473
474     // Requests a different table as the cache contains. This will fail to get a
475     // table specific cache entry, load the cache entry for all tables and save
476     // a cache entry for this table but not all.
477     for ($i = 0; $i < 5; $i++) {
478       $views_data = $this->viewsData->get('views_test_data_2');
479       $this->assertSame($expected_views_data['views_test_data_2'], $views_data);
480     }
481   }
482
483   /**
484    * Tests the cache calls for an not existing table:
485    *
486    * Warm cache:
487    *   - all tables
488    *   - views_test_data
489    * Not warm cache:
490    *   - $non_existing_table
491    */
492   public function testCacheCallsWithWarmCacheAndInvalidTable() {
493     $expected_views_data = $this->viewsDataWithProvider();
494     $non_existing_table = $this->randomMachineName();
495     $this->moduleHandler->expects($this->never())
496       ->method('getImplementations');
497
498     // Setup a warm cache backend for a single table.
499     $this->cacheBackend->expects($this->at(0))
500       ->method('get')
501       ->with("views_data:$non_existing_table:en");
502     $this->cacheBackend->expects($this->at(1))
503       ->method('get')
504       ->with('views_data:en')
505       ->will($this->returnValue((object) ['data' => $expected_views_data]));
506     $this->cacheBackend->expects($this->at(2))
507       ->method('set')
508       ->with("views_data:$non_existing_table:en", []);
509
510     // Initialize the views data cache and request a non-existing table. This
511     // will result in the same cache requests as we explicitly write an empty
512     // cache entry for non-existing tables to avoid unnecessary requests in
513     // those situations. We do have to load the cache entry for all tables to
514     // check if the table does exist or not.
515     for ($i = 0; $i < 5; $i++) {
516       $views_data = $this->viewsData->get($non_existing_table);
517       $this->assertSame([], $views_data);
518     }
519   }
520
521   /**
522    * Tests the cache calls for an not existing table:
523    *
524    * Warm cache:
525    *   - all tables
526    *   - views_test_data
527    *   - $non_existing_table
528    */
529   public function testCacheCallsWithWarmCacheForInvalidTable() {
530     $non_existing_table = $this->randomMachineName();
531     $this->moduleHandler->expects($this->never())
532       ->method('getImplementations');
533
534     // Setup a warm cache backend for a single table.
535     $this->cacheBackend->expects($this->once())
536       ->method('get')
537       ->with("views_data:$non_existing_table:en")
538       ->will($this->returnValue((object) ['data' => []]));
539     $this->cacheBackend->expects($this->never())
540       ->method('set');
541
542     // Initialize the views data cache and request a non-existing table. This
543     // will result in the same cache requests as we explicitly write an empty
544     // cache entry for non-existing tables to avoid unnecessary requests in
545     // those situations. We do have to load the cache entry for all tables to
546     // check if the table does exist or not.
547     for ($i = 0; $i < 5; $i++) {
548       $views_data = $this->viewsData->get($non_existing_table);
549       $this->assertSame([], $views_data);
550     }
551   }
552
553   /**
554    * Tests the cache calls for all views data without a warm cache.
555    */
556   public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
557     $expected_views_data = $this->viewsDataWithProvider();
558     $this->setupMockedModuleHandler();
559
560     // Setup a warm cache backend for a single table.
561     $this->cacheBackend->expects($this->once())
562       ->method('get')
563       ->with("views_data:en");
564     $this->cacheBackend->expects($this->once())
565       ->method('set')
566       ->with('views_data:en', $expected_views_data);
567
568     // Initialize the views data cache and repeat with no specified table. This
569     // should only load the cache entry for all tables.
570     for ($i = 0; $i < 5; $i++) {
571       $views_data = $this->viewsData->getAll();
572       $this->assertSame($expected_views_data, $views_data);
573     }
574   }
575
576   /**
577    * Tests the cache calls for all views data.
578    *
579    * Warm cache:
580    *   - all tables
581    */
582   public function testCacheCallsWithWarmCacheAndGetAllTables() {
583     $expected_views_data = $this->viewsDataWithProvider();
584     $this->moduleHandler->expects($this->never())
585       ->method('getImplementations');
586
587     // Setup a warm cache backend for a single table.
588     $this->cacheBackend->expects($this->once())
589       ->method('get')
590       ->with("views_data:en")
591       ->will($this->returnValue((object) ['data' => $expected_views_data]));
592     $this->cacheBackend->expects($this->never())
593       ->method('set');
594
595     // Initialize the views data cache and repeat with no specified table. This
596     // should only load the cache entry for all tables.
597     for ($i = 0; $i < 5; $i++) {
598       $views_data = $this->viewsData->getAll();
599       $this->assertSame($expected_views_data, $views_data);
600     }
601   }
602
603   /**
604    * Tests the cache calls for multiple tables without warm caches.
605    *
606    * @covers ::get
607    */
608   public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
609     $expected_views_data = $this->viewsDataWithProvider();
610     $table_name = 'views_test_data';
611     $table_name_2 = 'views_test_data_2';
612
613     // Setup a warm cache backend for all table data, but not single tables.
614     $this->cacheBackend->expects($this->at(0))
615       ->method('get')
616       ->with("views_data:$table_name:en")
617       ->will($this->returnValue(FALSE));
618     $this->cacheBackend->expects($this->at(1))
619       ->method('get')
620       ->with('views_data:en')
621       ->will($this->returnValue((object) ['data' => $expected_views_data]));
622     $this->cacheBackend->expects($this->at(2))
623       ->method('set')
624       ->with("views_data:$table_name:en", $expected_views_data[$table_name]);
625     $this->cacheBackend->expects($this->at(3))
626       ->method('get')
627       ->with("views_data:$table_name_2:en")
628       ->will($this->returnValue(FALSE));
629     $this->cacheBackend->expects($this->at(4))
630       ->method('set')
631       ->with("views_data:$table_name_2:en", $expected_views_data[$table_name_2]);
632
633     $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
634     $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
635
636     // Should only be invoked the first time.
637     $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
638     $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
639   }
640
641   /**
642    * Tests that getting all data has same results as getting data with NULL
643    * logic.
644    *
645    * @covers ::getAll
646    */
647   public function testGetAllEqualsToGetNull() {
648     $expected_views_data = $this->viewsDataWithProvider();
649     $this->setupMockedModuleHandler();
650
651     // Setup a warm cache backend for a single table.
652     $this->cacheBackend->expects($this->once())
653       ->method('get')
654       ->with("views_data:en");
655     $this->cacheBackend->expects($this->once())
656       ->method('set')
657       ->with('views_data:en', $expected_views_data);
658
659     // Initialize the views data cache and repeat with no specified table. This
660     // should only load the cache entry for all tables.
661     for ($i = 0; $i < 5; $i++) {
662       $this->assertSame($expected_views_data, $this->viewsData->getAll());
663       $this->assertSame($expected_views_data, $this->viewsData->get());
664     }
665   }
666
667 }