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);
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))
243 ->with("views_data:en")
244 ->will($this->returnValue(FALSE));
245 $this->cacheBackend->expects($this->at(1))
247 ->with("views_data:en", $expected_views_data);
248 $this->cacheBackend->expects($this->at(2))
250 ->with("views_data:$random_table_name:en")
251 ->will($this->returnValue(FALSE));
252 $this->cacheBackend->expects($this->at(3))
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))
260 ->with("views_data:en")
261 ->will($this->returnValue(FALSE));
262 $this->cacheBackend->expects($this->at(5))
264 ->with("views_data:en", $expected_views_data);
265 $this->cacheBackend->expects($this->at(6))
267 ->with("views_data:$random_table_name:en")
268 ->will($this->returnValue(FALSE));
269 $this->cacheBackend->expects($this->at(7))
271 ->with("views_data:$random_table_name:en", []);
273 $views_data = $this->viewsData->getAll();
274 $this->assertSame($expected_views_data, $views_data);
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);
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);
284 $views_data = $this->viewsData->get($random_table_name);
285 $this->assertSame([], $views_data);
287 $this->viewsData->clear();
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);
297 * Tests the caching of the full views data.
299 public function testFullGetCache() {
300 $expected_views_data = $this->viewsDataWithProvider();
302 // Views data should be invoked once.
303 $this->setupMockedModuleHandler();
305 $this->moduleHandler->expects($this->once())
307 ->with('views_data', $expected_views_data);
309 $this->cacheBackend->expects($this->once())
311 ->with("views_data:en")
312 ->will($this->returnValue(FALSE));
314 $views_data = $this->viewsData->getAll();
315 $this->assertSame($expected_views_data, $views_data);
317 $views_data = $this->viewsData->getAll();
318 $this->assertSame($expected_views_data, $views_data);
322 * Tests the caching of the views data for a specific table.
324 public function testSingleTableGetCache() {
325 $table_name = 'views_test_data';
326 $expected_views_data = $this->viewsDataWithProvider();
328 // Views data should be invoked once.
329 $this->setupMockedModuleHandler();
331 $this->moduleHandler->expects($this->once())
333 ->with('views_data', $this->viewsDataWithProvider());
335 $this->cacheBackend->expects($this->at(0))
337 ->with("views_data:$table_name:en")
338 ->will($this->returnValue(FALSE));
339 $this->cacheBackend->expects($this->at(1))
341 ->with("views_data:en")
342 ->will($this->returnValue(FALSE));
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.');
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.');
350 // Test that this data is present if all views data is returned.
351 $views_data = $this->viewsData->getAll();
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.');
358 * Tests building the views data with a non existing table.
360 public function testNonExistingTableGetCache() {
361 $random_table_name = $this->randomMachineName();
363 // Views data should be invoked once.
364 $this->setupMockedModuleHandler();
366 $this->moduleHandler->expects($this->once())
368 ->with('views_data', $this->viewsDataWithProvider());
370 $this->cacheBackend->expects($this->at(0))
372 ->with("views_data:$random_table_name:en")
373 ->will($this->returnValue(FALSE));
374 $this->cacheBackend->expects($this->at(1))
376 ->with("views_data:en")
377 ->will($this->returnValue(FALSE));
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.');
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.');
389 * Tests the cache backend behavior with requesting the same table multiple
391 public function testCacheCallsWithSameTableMultipleTimes() {
392 $expected_views_data = $this->viewsDataWithProvider();
394 $this->setupMockedModuleHandler();
396 $this->cacheBackend->expects($this->at(0))
398 ->with('views_data:views_test_data:en');
399 $this->cacheBackend->expects($this->at(1))
401 ->with('views_data:en');
402 $this->cacheBackend->expects($this->at(2))
404 ->with('views_data:en', $expected_views_data);
405 $this->cacheBackend->expects($this->at(3))
407 ->with('views_data:views_test_data:en', $expected_views_data['views_test_data']);
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);
422 * Tests the cache calls for a single table and warm cache for:
426 public function testCacheCallsWithSameTableMultipleTimesAndWarmCache() {
427 $expected_views_data = $this->viewsDataWithProvider();
428 $this->moduleHandler->expects($this->never())
429 ->method('getImplementations');
431 // Setup a warm cache backend for a single table.
432 $this->cacheBackend->expects($this->once())
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())
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);
448 * Tests the cache calls for a different table than the one in cache:
454 * - views_test_data_2
456 public function testCacheCallsWithWarmCacheAndDifferentTable() {
457 $expected_views_data = $this->viewsDataWithProvider();
458 $this->moduleHandler->expects($this->never())
459 ->method('getImplementations');
461 // Setup a warm cache backend for a single table.
462 $this->cacheBackend->expects($this->at(0))
464 ->with('views_data:views_test_data_2:en');
465 $this->cacheBackend->expects($this->at(1))
467 ->with('views_data:en')
468 ->will($this->returnValue((object) ['data' => $expected_views_data]));
469 $this->cacheBackend->expects($this->at(2))
471 ->with('views_data:views_test_data_2:en', $expected_views_data['views_test_data_2']);
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);
483 * Tests the cache calls for an not existing table:
489 * - $non_existing_table
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');
497 // Setup a warm cache backend for a single table.
498 $this->cacheBackend->expects($this->at(0))
500 ->with("views_data:$non_existing_table:en");
501 $this->cacheBackend->expects($this->at(1))
503 ->with('views_data:en')
504 ->will($this->returnValue((object) ['data' => $expected_views_data]));
505 $this->cacheBackend->expects($this->at(2))
507 ->with("views_data:$non_existing_table:en", []);
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);
521 * Tests the cache calls for an not existing table:
526 * - $non_existing_table
528 public function testCacheCallsWithWarmCacheForInvalidTable() {
529 $non_existing_table = $this->randomMachineName();
530 $this->moduleHandler->expects($this->never())
531 ->method('getImplementations');
533 // Setup a warm cache backend for a single table.
534 $this->cacheBackend->expects($this->once())
536 ->with("views_data:$non_existing_table:en")
537 ->will($this->returnValue((object) ['data' => []]));
538 $this->cacheBackend->expects($this->never())
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);
553 * Tests the cache calls for all views data without a warm cache.
555 public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
556 $expected_views_data = $this->viewsDataWithProvider();
557 $this->setupMockedModuleHandler();
559 // Setup a warm cache backend for a single table.
560 $this->cacheBackend->expects($this->once())
562 ->with("views_data:en");
563 $this->cacheBackend->expects($this->once())
565 ->with('views_data:en', $expected_views_data);
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);
576 * Tests the cache calls for all views data.
581 public function testCacheCallsWithWarmCacheAndGetAllTables() {
582 $expected_views_data = $this->viewsDataWithProvider();
583 $this->moduleHandler->expects($this->never())
584 ->method('getImplementations');
586 // Setup a warm cache backend for a single table.
587 $this->cacheBackend->expects($this->once())
589 ->with("views_data:en")
590 ->will($this->returnValue((object) ['data' => $expected_views_data]));
591 $this->cacheBackend->expects($this->never())
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);
603 * Tests the cache calls for multiple tables without warm caches.
607 public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
608 $expected_views_data = $this->viewsDataWithProvider();
609 $table_name = 'views_test_data';
610 $table_name_2 = 'views_test_data_2';
612 // Setup a warm cache backend for all table data, but not single tables.
613 $this->cacheBackend->expects($this->at(0))
615 ->with("views_data:$table_name:en")
616 ->will($this->returnValue(FALSE));
617 $this->cacheBackend->expects($this->at(1))
619 ->with('views_data:en')
620 ->will($this->returnValue((object) ['data' => $expected_views_data]));
621 $this->cacheBackend->expects($this->at(2))
623 ->with("views_data:$table_name:en", $expected_views_data[$table_name]);
624 $this->cacheBackend->expects($this->at(3))
626 ->with("views_data:$table_name_2:en")
627 ->will($this->returnValue(FALSE));
628 $this->cacheBackend->expects($this->at(4))
630 ->with("views_data:$table_name_2:en", $expected_views_data[$table_name_2]);
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));
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));
641 * Tests that getting all data has same results as getting data with NULL
646 public function testGetAllEqualsToGetNull() {
647 $expected_views_data = $this->viewsDataWithProvider();
648 $this->setupMockedModuleHandler();
650 // Setup a warm cache backend for a single table.
651 $this->cacheBackend->expects($this->once())
653 ->with("views_data:en");
654 $this->cacheBackend->expects($this->once())
656 ->with('views_data:en', $expected_views_data);
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());