3 namespace Drupal\Tests\views\Unit;
5 use Drupal\Core\Language\Language;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\views\ViewsData;
8 use Drupal\views\Tests\ViewTestData;
11 * @coversDefaultClass \Drupal\views\ViewsData
14 class ViewsDataTest extends UnitTestCase {
17 * The mocked cache backend.
19 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
21 protected $cacheBackend;
24 * The mocked cache tags invalidator.
26 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject
28 protected $cacheTagsInvalidator;
31 * The mocked module handler.
33 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
35 protected $moduleHandler;
38 * The mocked config factory.
40 * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
42 protected $configFactory;
45 * The mocked language manager.
47 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
49 protected $languageManager;
52 * The tested views data class.
54 * @var \Drupal\views\ViewsData
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);
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'])));
75 $this->viewsData = new ViewsData($this->cacheBackend, $this->configFactory, $this->moduleHandler, $this->languageManager);
79 * Returns the views data definition.
81 protected function viewsData() {
82 $data = ViewTestData::viewsData();
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'];
95 // Duplicate the example views test data for different weight, different title,
97 $data['views_test_data_2'] = $data['views_test_data'];
98 $data['views_test_data_2']['table']['base']['weight'] = 50;
100 $data['views_test_data_3'] = $data['views_test_data'];
101 $data['views_test_data_3']['table']['base']['weight'] = -50;
103 $data['views_test_data_4'] = $data['views_test_data'];
104 $data['views_test_data_4']['table']['base']['title'] = 'A different title';
106 $data['views_test_data_5'] = $data['views_test_data'];
107 $data['views_test_data_5']['table']['base']['title'] = 'Z different title';
109 $data['views_test_data_6'] = $data['views_test_data'];
115 * Returns the views data definition with the provider key.
119 * @see static::viewsData()
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';
130 * Mocks the basic module handler used for the test.
132 * @return \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
134 protected function setupMockedModuleHandler() {
135 $views_data = $this->viewsData();
136 $this->moduleHandler->expects($this->at(0))
137 ->method('getImplementations')
139 ->willReturn(['views_test_data']);
140 $this->moduleHandler->expects($this->at(1))
142 ->with('views_test_data', 'views_data')
143 ->willReturn($views_data);
147 * Tests the fetchBaseTables() method.
149 public function testFetchBaseTables() {
150 $this->setupMockedModuleHandler();
151 $data = $this->viewsData->getAll();
153 $base_tables = $this->viewsData->fetchBaseTables();
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']);
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.');
169 // Test the values returned for each base table.
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]);
185 * Tests fetching all the views data without a static cache.
187 public function testGetOnFirstCall() {
188 // Ensure that the hooks are just invoked once.
189 $this->setupMockedModuleHandler();
191 $this->moduleHandler->expects($this->at(2))
193 ->with('views_data', $this->viewsDataWithProvider());
195 $this->cacheBackend->expects($this->once())
197 ->with("views_data:en")
198 ->will($this->returnValue(FALSE));
200 $expected_views_data = $this->viewsDataWithProvider();
201 $views_data = $this->viewsData->getAll();
202 $this->assertSame($expected_views_data, $views_data);
206 * Tests the cache of the full and single table data.
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();
214 // Views data should be invoked twice due to the clear call.
215 $this->moduleHandler->expects($this->at(0))
216 ->method('getImplementations')
218 ->willReturn(['views_test_data']);
219 $this->moduleHandler->expects($this->at(1))
221 ->with('views_test_data', 'views_data')
222 ->willReturn($this->viewsData());
223 $this->moduleHandler->expects($this->at(2))
225 ->with('views_data', $expected_views_data);
227 $this->moduleHandler->expects($this->at(3))
228 ->method('getImplementations')
230 ->willReturn(['views_test_data']);
231 $this->moduleHandler->expects($this->at(4))
233 ->with('views_test_data', 'views_data')
234 ->willReturn($this->viewsData());
235 $this->moduleHandler->expects($this->at(5))
237 ->with('views_data', $expected_views_data);
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))
244 ->with("views_data:en")
245 ->will($this->returnValue(FALSE));
246 $this->cacheBackend->expects($this->at(1))
248 ->with("views_data:en", $expected_views_data);
249 $this->cacheBackend->expects($this->at(2))
251 ->with("views_data:$random_table_name:en")
252 ->will($this->returnValue(FALSE));
253 $this->cacheBackend->expects($this->at(3))
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))
261 ->with("views_data:en")
262 ->will($this->returnValue(FALSE));
263 $this->cacheBackend->expects($this->at(5))
265 ->with("views_data:en", $expected_views_data);
266 $this->cacheBackend->expects($this->at(6))
268 ->with("views_data:$random_table_name:en")
269 ->will($this->returnValue(FALSE));
270 $this->cacheBackend->expects($this->at(7))
272 ->with("views_data:$random_table_name:en", []);
274 $views_data = $this->viewsData->getAll();
275 $this->assertSame($expected_views_data, $views_data);
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);
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);
285 $views_data = $this->viewsData->get($random_table_name);
286 $this->assertSame([], $views_data);
288 $this->viewsData->clear();
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);
298 * Tests the caching of the full views data.
300 public function testFullGetCache() {
301 $expected_views_data = $this->viewsDataWithProvider();
303 // Views data should be invoked once.
304 $this->setupMockedModuleHandler();
306 $this->moduleHandler->expects($this->once())
308 ->with('views_data', $expected_views_data);
310 $this->cacheBackend->expects($this->once())
312 ->with("views_data:en")
313 ->will($this->returnValue(FALSE));
315 $views_data = $this->viewsData->getAll();
316 $this->assertSame($expected_views_data, $views_data);
318 $views_data = $this->viewsData->getAll();
319 $this->assertSame($expected_views_data, $views_data);
323 * Tests the caching of the views data for a specific table.
325 public function testSingleTableGetCache() {
326 $table_name = 'views_test_data';
327 $expected_views_data = $this->viewsDataWithProvider();
329 // Views data should be invoked once.
330 $this->setupMockedModuleHandler();
332 $this->moduleHandler->expects($this->once())
334 ->with('views_data', $this->viewsDataWithProvider());
336 $this->cacheBackend->expects($this->at(0))
338 ->with("views_data:$table_name:en")
339 ->will($this->returnValue(FALSE));
340 $this->cacheBackend->expects($this->at(1))
342 ->with("views_data:en")
343 ->will($this->returnValue(FALSE));
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.');
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.');
351 // Test that this data is present if all views data is returned.
352 $views_data = $this->viewsData->getAll();
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.');
359 * Tests building the views data with a non existing table.
361 public function testNonExistingTableGetCache() {
362 $random_table_name = $this->randomMachineName();
364 // Views data should be invoked once.
365 $this->setupMockedModuleHandler();
367 $this->moduleHandler->expects($this->once())
369 ->with('views_data', $this->viewsDataWithProvider());
371 $this->cacheBackend->expects($this->at(0))
373 ->with("views_data:$random_table_name:en")
374 ->will($this->returnValue(FALSE));
375 $this->cacheBackend->expects($this->at(1))
377 ->with("views_data:en")
378 ->will($this->returnValue(FALSE));
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.');
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.');
390 * Tests the cache backend behavior with requesting the same table multiple
392 public function testCacheCallsWithSameTableMultipleTimes() {
393 $expected_views_data = $this->viewsDataWithProvider();
395 $this->setupMockedModuleHandler();
397 $this->cacheBackend->expects($this->at(0))
399 ->with('views_data:views_test_data:en');
400 $this->cacheBackend->expects($this->at(1))
402 ->with('views_data:en');
403 $this->cacheBackend->expects($this->at(2))
405 ->with('views_data:en', $expected_views_data);
406 $this->cacheBackend->expects($this->at(3))
408 ->with('views_data:views_test_data:en', $expected_views_data['views_test_data']);
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);
423 * Tests the cache calls for a single table and warm cache for:
427 public function testCacheCallsWithSameTableMultipleTimesAndWarmCache() {
428 $expected_views_data = $this->viewsDataWithProvider();
429 $this->moduleHandler->expects($this->never())
430 ->method('getImplementations');
432 // Setup a warm cache backend for a single table.
433 $this->cacheBackend->expects($this->once())
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())
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);
449 * Tests the cache calls for a different table than the one in cache:
455 * - views_test_data_2
457 public function testCacheCallsWithWarmCacheAndDifferentTable() {
458 $expected_views_data = $this->viewsDataWithProvider();
459 $this->moduleHandler->expects($this->never())
460 ->method('getImplementations');
462 // Setup a warm cache backend for a single table.
463 $this->cacheBackend->expects($this->at(0))
465 ->with('views_data:views_test_data_2:en');
466 $this->cacheBackend->expects($this->at(1))
468 ->with('views_data:en')
469 ->will($this->returnValue((object) ['data' => $expected_views_data]));
470 $this->cacheBackend->expects($this->at(2))
472 ->with('views_data:views_test_data_2:en', $expected_views_data['views_test_data_2']);
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);
484 * Tests the cache calls for an not existing table:
490 * - $non_existing_table
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');
498 // Setup a warm cache backend for a single table.
499 $this->cacheBackend->expects($this->at(0))
501 ->with("views_data:$non_existing_table:en");
502 $this->cacheBackend->expects($this->at(1))
504 ->with('views_data:en')
505 ->will($this->returnValue((object) ['data' => $expected_views_data]));
506 $this->cacheBackend->expects($this->at(2))
508 ->with("views_data:$non_existing_table:en", []);
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);
522 * Tests the cache calls for an not existing table:
527 * - $non_existing_table
529 public function testCacheCallsWithWarmCacheForInvalidTable() {
530 $non_existing_table = $this->randomMachineName();
531 $this->moduleHandler->expects($this->never())
532 ->method('getImplementations');
534 // Setup a warm cache backend for a single table.
535 $this->cacheBackend->expects($this->once())
537 ->with("views_data:$non_existing_table:en")
538 ->will($this->returnValue((object) ['data' => []]));
539 $this->cacheBackend->expects($this->never())
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);
554 * Tests the cache calls for all views data without a warm cache.
556 public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
557 $expected_views_data = $this->viewsDataWithProvider();
558 $this->setupMockedModuleHandler();
560 // Setup a warm cache backend for a single table.
561 $this->cacheBackend->expects($this->once())
563 ->with("views_data:en");
564 $this->cacheBackend->expects($this->once())
566 ->with('views_data:en', $expected_views_data);
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);
577 * Tests the cache calls for all views data.
582 public function testCacheCallsWithWarmCacheAndGetAllTables() {
583 $expected_views_data = $this->viewsDataWithProvider();
584 $this->moduleHandler->expects($this->never())
585 ->method('getImplementations');
587 // Setup a warm cache backend for a single table.
588 $this->cacheBackend->expects($this->once())
590 ->with("views_data:en")
591 ->will($this->returnValue((object) ['data' => $expected_views_data]));
592 $this->cacheBackend->expects($this->never())
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);
604 * Tests the cache calls for multiple tables without warm caches.
608 public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
609 $expected_views_data = $this->viewsDataWithProvider();
610 $table_name = 'views_test_data';
611 $table_name_2 = 'views_test_data_2';
613 // Setup a warm cache backend for all table data, but not single tables.
614 $this->cacheBackend->expects($this->at(0))
616 ->with("views_data:$table_name:en")
617 ->will($this->returnValue(FALSE));
618 $this->cacheBackend->expects($this->at(1))
620 ->with('views_data:en')
621 ->will($this->returnValue((object) ['data' => $expected_views_data]));
622 $this->cacheBackend->expects($this->at(2))
624 ->with("views_data:$table_name:en", $expected_views_data[$table_name]);
625 $this->cacheBackend->expects($this->at(3))
627 ->with("views_data:$table_name_2:en")
628 ->will($this->returnValue(FALSE));
629 $this->cacheBackend->expects($this->at(4))
631 ->with("views_data:$table_name_2:en", $expected_views_data[$table_name_2]);
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));
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));
642 * Tests that getting all data has same results as getting data with NULL
647 public function testGetAllEqualsToGetNull() {
648 $expected_views_data = $this->viewsDataWithProvider();
649 $this->setupMockedModuleHandler();
651 // Setup a warm cache backend for a single table.
652 $this->cacheBackend->expects($this->once())
654 ->with("views_data:en");
655 $this->cacheBackend->expects($this->once())
657 ->with('views_data:en', $expected_views_data);
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());