Chris@0: cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); Chris@0: $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); Chris@0: $this->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator); Chris@0: Chris@0: $configs = []; Chris@0: $configs['views.settings']['skip_cache'] = FALSE; Chris@0: $this->configFactory = $this->getConfigFactoryStub($configs); Chris@0: $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); Chris@0: $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); Chris@0: $this->languageManager->expects($this->any()) Chris@0: ->method('getCurrentLanguage') Chris@0: ->will($this->returnValue(new Language(['id' => 'en']))); Chris@0: Chris@0: $this->viewsData = new ViewsData($this->cacheBackend, $this->configFactory, $this->moduleHandler, $this->languageManager); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the views data definition. Chris@0: */ Chris@0: protected function viewsData() { Chris@0: $data = ViewTestData::viewsData(); Chris@0: Chris@0: // Tweak the views data to have a base for testing. Chris@0: unset($data['views_test_data']['id']['field']); Chris@0: unset($data['views_test_data']['name']['argument']); Chris@0: unset($data['views_test_data']['age']['filter']); Chris@0: unset($data['views_test_data']['job']['sort']); Chris@0: $data['views_test_data']['created']['area']['id'] = 'text'; Chris@0: $data['views_test_data']['age']['area']['id'] = 'text'; Chris@0: $data['views_test_data']['age']['area']['sub_type'] = 'header'; Chris@0: $data['views_test_data']['job']['area']['id'] = 'text'; Chris@0: $data['views_test_data']['job']['area']['sub_type'] = ['header', 'footer']; Chris@0: Chris@0: // Duplicate the example views test data for different weight, different title, Chris@0: // and matching data. Chris@0: $data['views_test_data_2'] = $data['views_test_data']; Chris@0: $data['views_test_data_2']['table']['base']['weight'] = 50; Chris@0: Chris@0: $data['views_test_data_3'] = $data['views_test_data']; Chris@0: $data['views_test_data_3']['table']['base']['weight'] = -50; Chris@0: Chris@0: $data['views_test_data_4'] = $data['views_test_data']; Chris@0: $data['views_test_data_4']['table']['base']['title'] = 'A different title'; Chris@0: Chris@0: $data['views_test_data_5'] = $data['views_test_data']; Chris@0: $data['views_test_data_5']['table']['base']['title'] = 'Z different title'; Chris@0: Chris@0: $data['views_test_data_6'] = $data['views_test_data']; Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the views data definition with the provider key. Chris@0: * Chris@0: * @return array Chris@0: * Chris@0: * @see static::viewsData() Chris@0: */ Chris@0: protected function viewsDataWithProvider() { Chris@0: $views_data = static::viewsData(); Chris@0: foreach (array_keys($views_data) as $table) { Chris@0: $views_data[$table]['table']['provider'] = 'views_test_data'; Chris@0: } Chris@0: return $views_data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Mocks the basic module handler used for the test. Chris@0: * Chris@0: * @return \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject Chris@0: */ Chris@0: protected function setupMockedModuleHandler() { Chris@0: $views_data = $this->viewsData(); Chris@0: $this->moduleHandler->expects($this->at(0)) Chris@0: ->method('getImplementations') Chris@0: ->with('views_data') Chris@0: ->willReturn(['views_test_data']); Chris@0: $this->moduleHandler->expects($this->at(1)) Chris@0: ->method('invoke') Chris@0: ->with('views_test_data', 'views_data') Chris@0: ->willReturn($views_data); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the fetchBaseTables() method. Chris@0: */ Chris@0: public function testFetchBaseTables() { Chris@0: $this->setupMockedModuleHandler(); Chris@0: $data = $this->viewsData->getAll(); Chris@0: Chris@0: $base_tables = $this->viewsData->fetchBaseTables(); Chris@0: Chris@0: // Ensure that 'provider' is set for each base table. Chris@0: foreach (array_keys($base_tables) as $base_table) { Chris@0: $this->assertEquals('views_test_data', $data[$base_table]['table']['provider']); Chris@0: } Chris@0: Chris@0: // Test the number of tables returned and their order. Chris@0: $this->assertCount(6, $base_tables, 'The correct amount of base tables were returned.'); Chris@0: $base_tables_keys = array_keys($base_tables); Chris@0: for ($i = 1; $i < count($base_tables); ++$i) { Chris@0: $prev = $base_tables[$base_tables_keys[$i - 1]]; Chris@0: $current = $base_tables[$base_tables_keys[$i]]; Chris@0: $this->assertTrue($prev['weight'] <= $current['weight'] && $prev['title'] <= $prev['title'], 'The tables are sorted as expected.'); Chris@0: } Chris@0: Chris@0: // Test the values returned for each base table. Chris@0: $defaults = [ Chris@0: 'title' => '', Chris@0: 'help' => '', Chris@0: 'weight' => 0, Chris@0: ]; Chris@0: foreach ($base_tables as $base_table => $info) { Chris@0: // Merge in default values as in fetchBaseTables(). Chris@0: $expected = $data[$base_table]['table']['base'] += $defaults; Chris@0: foreach ($defaults as $key => $default) { Chris@0: $this->assertSame($info[$key], $expected[$key]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests fetching all the views data without a static cache. Chris@0: */ Chris@0: public function testGetOnFirstCall() { Chris@0: // Ensure that the hooks are just invoked once. Chris@0: $this->setupMockedModuleHandler(); Chris@0: Chris@0: $this->moduleHandler->expects($this->at(2)) Chris@0: ->method('alter') Chris@0: ->with('views_data', $this->viewsDataWithProvider()); Chris@0: Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with("views_data:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $views_data = $this->viewsData->getAll(); Chris@0: $this->assertSame($expected_views_data, $views_data); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache of the full and single table data. Chris@0: */ Chris@0: public function testFullAndTableGetCache() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $table_name = 'views_test_data'; Chris@0: $table_name_2 = 'views_test_data_2'; Chris@0: $random_table_name = $this->randomMachineName(); Chris@0: Chris@0: // Views data should be invoked twice due to the clear call. Chris@0: $this->moduleHandler->expects($this->at(0)) Chris@0: ->method('getImplementations') Chris@0: ->with('views_data') Chris@0: ->willReturn(['views_test_data']); Chris@0: $this->moduleHandler->expects($this->at(1)) Chris@0: ->method('invoke') Chris@0: ->with('views_test_data', 'views_data') Chris@0: ->willReturn($this->viewsData()); Chris@0: $this->moduleHandler->expects($this->at(2)) Chris@0: ->method('alter') Chris@0: ->with('views_data', $expected_views_data); Chris@0: Chris@0: $this->moduleHandler->expects($this->at(3)) Chris@0: ->method('getImplementations') Chris@0: ->with('views_data') Chris@0: ->willReturn(['views_test_data']); Chris@0: $this->moduleHandler->expects($this->at(4)) Chris@0: ->method('invoke') Chris@0: ->with('views_test_data', 'views_data') Chris@0: ->willReturn($this->viewsData()); Chris@0: $this->moduleHandler->expects($this->at(5)) Chris@0: ->method('alter') Chris@0: ->with('views_data', $expected_views_data); Chris@0: Chris@0: // The cache should only be called once (before the clear() call) as get Chris@0: // will get all table data in the first get(). Chris@0: $this->cacheBackend->expects($this->at(0)) Chris@0: ->method('get') Chris@0: ->with("views_data:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(1)) Chris@0: ->method('set') Chris@0: ->with("views_data:en", $expected_views_data); Chris@0: $this->cacheBackend->expects($this->at(2)) Chris@0: ->method('get') Chris@0: ->with("views_data:$random_table_name:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(3)) Chris@0: ->method('set') Chris@0: ->with("views_data:$random_table_name:en", []); Chris@0: $this->cacheTagsInvalidator->expects($this->once()) Chris@0: ->method('invalidateTags') Chris@0: ->with(['views_data']); Chris@0: $this->cacheBackend->expects($this->at(4)) Chris@0: ->method('get') Chris@0: ->with("views_data:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(5)) Chris@0: ->method('set') Chris@0: ->with("views_data:en", $expected_views_data); Chris@0: $this->cacheBackend->expects($this->at(6)) Chris@0: ->method('get') Chris@0: ->with("views_data:$random_table_name:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(7)) Chris@0: ->method('set') Chris@0: ->with("views_data:$random_table_name:en", []); Chris@0: Chris@0: $views_data = $this->viewsData->getAll(); Chris@0: $this->assertSame($expected_views_data, $views_data); Chris@0: Chris@0: // Request a specific table should be static cached. Chris@0: $views_data = $this->viewsData->get($table_name); Chris@0: $this->assertSame($expected_views_data[$table_name], $views_data); Chris@0: Chris@0: // Another table being requested should also come from the static cache. Chris@0: $views_data = $this->viewsData->get($table_name_2); Chris@0: $this->assertSame($expected_views_data[$table_name_2], $views_data); Chris@0: Chris@0: $views_data = $this->viewsData->get($random_table_name); Chris@0: $this->assertSame([], $views_data); Chris@0: Chris@0: $this->viewsData->clear(); Chris@0: Chris@0: // Get the views data again. Chris@0: $this->viewsData->getAll(); Chris@0: $this->viewsData->get($table_name); Chris@0: $this->viewsData->get($table_name_2); Chris@0: $this->viewsData->get($random_table_name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the caching of the full views data. Chris@0: */ Chris@0: public function testFullGetCache() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: Chris@0: // Views data should be invoked once. Chris@0: $this->setupMockedModuleHandler(); Chris@0: Chris@0: $this->moduleHandler->expects($this->once()) Chris@0: ->method('alter') Chris@0: ->with('views_data', $expected_views_data); Chris@0: Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with("views_data:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: Chris@0: $views_data = $this->viewsData->getAll(); Chris@0: $this->assertSame($expected_views_data, $views_data); Chris@0: Chris@0: $views_data = $this->viewsData->getAll(); Chris@0: $this->assertSame($expected_views_data, $views_data); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the caching of the views data for a specific table. Chris@0: */ Chris@0: public function testSingleTableGetCache() { Chris@0: $table_name = 'views_test_data'; Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: Chris@0: // Views data should be invoked once. Chris@0: $this->setupMockedModuleHandler(); Chris@0: Chris@0: $this->moduleHandler->expects($this->once()) Chris@0: ->method('alter') Chris@0: ->with('views_data', $this->viewsDataWithProvider()); Chris@0: Chris@0: $this->cacheBackend->expects($this->at(0)) Chris@0: ->method('get') Chris@0: ->with("views_data:$table_name:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(1)) Chris@0: ->method('get') Chris@0: ->with("views_data:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: Chris@0: $views_data = $this->viewsData->get($table_name); Chris@0: $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching views data by table works as expected.'); Chris@0: Chris@0: $views_data = $this->viewsData->get($table_name); Chris@0: $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching cached views data by table works as expected.'); Chris@0: Chris@0: // Test that this data is present if all views data is returned. Chris@0: $views_data = $this->viewsData->getAll(); Chris@0: Chris@0: $this->assertArrayHasKey($table_name, $views_data, 'Make sure the views_test_data info appears in the total views data.'); Chris@0: $this->assertSame($expected_views_data[$table_name], $views_data[$table_name], 'Make sure the views_test_data has the expected values.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests building the views data with a non existing table. Chris@0: */ Chris@0: public function testNonExistingTableGetCache() { Chris@0: $random_table_name = $this->randomMachineName(); Chris@0: Chris@0: // Views data should be invoked once. Chris@0: $this->setupMockedModuleHandler(); Chris@0: Chris@0: $this->moduleHandler->expects($this->once()) Chris@0: ->method('alter') Chris@0: ->with('views_data', $this->viewsDataWithProvider()); Chris@0: Chris@0: $this->cacheBackend->expects($this->at(0)) Chris@0: ->method('get') Chris@0: ->with("views_data:$random_table_name:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(1)) Chris@0: ->method('get') Chris@0: ->with("views_data:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: Chris@0: // All views data should be requested on the first try. Chris@0: $views_data = $this->viewsData->get($random_table_name); Chris@0: $this->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.'); Chris@0: Chris@0: // Test no data is rebuilt when requesting an invalid table again. Chris@0: $views_data = $this->viewsData->get($random_table_name); Chris@0: $this->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache backend behavior with requesting the same table multiple Chris@0: */ Chris@0: public function testCacheCallsWithSameTableMultipleTimes() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: Chris@0: $this->setupMockedModuleHandler(); Chris@0: Chris@0: $this->cacheBackend->expects($this->at(0)) Chris@0: ->method('get') Chris@0: ->with('views_data:views_test_data:en'); Chris@0: $this->cacheBackend->expects($this->at(1)) Chris@0: ->method('get') Chris@0: ->with('views_data:en'); Chris@0: $this->cacheBackend->expects($this->at(2)) Chris@0: ->method('set') Chris@0: ->with('views_data:en', $expected_views_data); Chris@0: $this->cacheBackend->expects($this->at(3)) Chris@0: ->method('set') Chris@0: ->with('views_data:views_test_data:en', $expected_views_data['views_test_data']); Chris@0: Chris@0: // Request the same table 5 times. The caches are empty at this point, so Chris@0: // what will happen is that it will first check for a cache entry for the Chris@0: // given table, get a cache miss, then try the cache entry for all tables, Chris@0: // which does not exist yet either. As a result, it rebuilds the information Chris@0: // and writes a cache entry for all tables and the requested table. Chris@0: $table_name = 'views_test_data'; Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $views_data = $this->viewsData->get($table_name); Chris@0: $this->assertSame($expected_views_data['views_test_data'], $views_data); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache calls for a single table and warm cache for: Chris@0: * - all tables Chris@0: * - views_test_data Chris@0: */ Chris@0: public function testCacheCallsWithSameTableMultipleTimesAndWarmCache() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $this->moduleHandler->expects($this->never()) Chris@0: ->method('getImplementations'); Chris@0: Chris@0: // Setup a warm cache backend for a single table. Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with('views_data:views_test_data:en') Chris@0: ->will($this->returnValue((object) ['data' => $expected_views_data['views_test_data']])); Chris@0: $this->cacheBackend->expects($this->never()) Chris@0: ->method('set'); Chris@0: Chris@0: // We have a warm cache now, so this will only request the tables-specific Chris@0: // cache entry and return that. Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $views_data = $this->viewsData->get('views_test_data'); Chris@0: $this->assertSame($expected_views_data['views_test_data'], $views_data); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache calls for a different table than the one in cache: Chris@0: * Chris@0: * Warm cache: Chris@0: * - all tables Chris@0: * - views_test_data Chris@0: * Not warm cache: Chris@0: * - views_test_data_2 Chris@0: */ Chris@0: public function testCacheCallsWithWarmCacheAndDifferentTable() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $this->moduleHandler->expects($this->never()) Chris@0: ->method('getImplementations'); Chris@0: Chris@0: // Setup a warm cache backend for a single table. Chris@0: $this->cacheBackend->expects($this->at(0)) Chris@0: ->method('get') Chris@0: ->with('views_data:views_test_data_2:en'); Chris@0: $this->cacheBackend->expects($this->at(1)) Chris@0: ->method('get') Chris@0: ->with('views_data:en') Chris@0: ->will($this->returnValue((object) ['data' => $expected_views_data])); Chris@0: $this->cacheBackend->expects($this->at(2)) Chris@0: ->method('set') Chris@0: ->with('views_data:views_test_data_2:en', $expected_views_data['views_test_data_2']); Chris@0: Chris@0: // Requests a different table as the cache contains. This will fail to get a Chris@0: // table specific cache entry, load the cache entry for all tables and save Chris@0: // a cache entry for this table but not all. Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $views_data = $this->viewsData->get('views_test_data_2'); Chris@0: $this->assertSame($expected_views_data['views_test_data_2'], $views_data); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache calls for an not existing table: Chris@0: * Chris@0: * Warm cache: Chris@0: * - all tables Chris@0: * - views_test_data Chris@0: * Not warm cache: Chris@0: * - $non_existing_table Chris@0: */ Chris@0: public function testCacheCallsWithWarmCacheAndInvalidTable() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $non_existing_table = $this->randomMachineName(); Chris@0: $this->moduleHandler->expects($this->never()) Chris@0: ->method('getImplementations'); Chris@0: Chris@0: // Setup a warm cache backend for a single table. Chris@0: $this->cacheBackend->expects($this->at(0)) Chris@0: ->method('get') Chris@0: ->with("views_data:$non_existing_table:en"); Chris@0: $this->cacheBackend->expects($this->at(1)) Chris@0: ->method('get') Chris@0: ->with('views_data:en') Chris@0: ->will($this->returnValue((object) ['data' => $expected_views_data])); Chris@0: $this->cacheBackend->expects($this->at(2)) Chris@0: ->method('set') Chris@0: ->with("views_data:$non_existing_table:en", []); Chris@0: Chris@0: // Initialize the views data cache and request a non-existing table. This Chris@0: // will result in the same cache requests as we explicitly write an empty Chris@0: // cache entry for non-existing tables to avoid unnecessary requests in Chris@0: // those situations. We do have to load the cache entry for all tables to Chris@0: // check if the table does exist or not. Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $views_data = $this->viewsData->get($non_existing_table); Chris@0: $this->assertSame([], $views_data); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache calls for an not existing table: Chris@0: * Chris@0: * Warm cache: Chris@0: * - all tables Chris@0: * - views_test_data Chris@0: * - $non_existing_table Chris@0: */ Chris@0: public function testCacheCallsWithWarmCacheForInvalidTable() { Chris@0: $non_existing_table = $this->randomMachineName(); Chris@0: $this->moduleHandler->expects($this->never()) Chris@0: ->method('getImplementations'); Chris@0: Chris@0: // Setup a warm cache backend for a single table. Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with("views_data:$non_existing_table:en") Chris@0: ->will($this->returnValue((object) ['data' => []])); Chris@0: $this->cacheBackend->expects($this->never()) Chris@0: ->method('set'); Chris@0: Chris@0: // Initialize the views data cache and request a non-existing table. This Chris@0: // will result in the same cache requests as we explicitly write an empty Chris@0: // cache entry for non-existing tables to avoid unnecessary requests in Chris@0: // those situations. We do have to load the cache entry for all tables to Chris@0: // check if the table does exist or not. Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $views_data = $this->viewsData->get($non_existing_table); Chris@0: $this->assertSame([], $views_data); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache calls for all views data without a warm cache. Chris@0: */ Chris@0: public function testCacheCallsWithoutWarmCacheAndGetAllTables() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $this->setupMockedModuleHandler(); Chris@0: Chris@0: // Setup a warm cache backend for a single table. Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with("views_data:en"); Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('set') Chris@0: ->with('views_data:en', $expected_views_data); Chris@0: Chris@0: // Initialize the views data cache and repeat with no specified table. This Chris@0: // should only load the cache entry for all tables. Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $views_data = $this->viewsData->getAll(); Chris@0: $this->assertSame($expected_views_data, $views_data); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache calls for all views data. Chris@0: * Chris@0: * Warm cache: Chris@0: * - all tables Chris@0: */ Chris@0: public function testCacheCallsWithWarmCacheAndGetAllTables() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $this->moduleHandler->expects($this->never()) Chris@0: ->method('getImplementations'); Chris@0: Chris@0: // Setup a warm cache backend for a single table. Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with("views_data:en") Chris@0: ->will($this->returnValue((object) ['data' => $expected_views_data])); Chris@0: $this->cacheBackend->expects($this->never()) Chris@0: ->method('set'); Chris@0: Chris@0: // Initialize the views data cache and repeat with no specified table. This Chris@0: // should only load the cache entry for all tables. Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $views_data = $this->viewsData->getAll(); Chris@0: $this->assertSame($expected_views_data, $views_data); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache calls for multiple tables without warm caches. Chris@0: * Chris@0: * @covers ::get Chris@0: */ Chris@0: public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $table_name = 'views_test_data'; Chris@0: $table_name_2 = 'views_test_data_2'; Chris@0: Chris@0: // Setup a warm cache backend for all table data, but not single tables. Chris@0: $this->cacheBackend->expects($this->at(0)) Chris@0: ->method('get') Chris@0: ->with("views_data:$table_name:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(1)) Chris@0: ->method('get') Chris@0: ->with('views_data:en') Chris@0: ->will($this->returnValue((object) ['data' => $expected_views_data])); Chris@0: $this->cacheBackend->expects($this->at(2)) Chris@0: ->method('set') Chris@0: ->with("views_data:$table_name:en", $expected_views_data[$table_name]); Chris@0: $this->cacheBackend->expects($this->at(3)) Chris@0: ->method('get') Chris@0: ->with("views_data:$table_name_2:en") Chris@0: ->will($this->returnValue(FALSE)); Chris@0: $this->cacheBackend->expects($this->at(4)) Chris@0: ->method('set') Chris@0: ->with("views_data:$table_name_2:en", $expected_views_data[$table_name_2]); Chris@0: Chris@0: $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name)); Chris@0: $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2)); Chris@0: Chris@0: // Should only be invoked the first time. Chris@0: $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name)); Chris@0: $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that getting all data has same results as getting data with NULL Chris@0: * logic. Chris@0: * Chris@0: * @covers ::getAll Chris@0: */ Chris@0: public function testGetAllEqualsToGetNull() { Chris@0: $expected_views_data = $this->viewsDataWithProvider(); Chris@0: $this->setupMockedModuleHandler(); Chris@0: Chris@0: // Setup a warm cache backend for a single table. Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with("views_data:en"); Chris@0: $this->cacheBackend->expects($this->once()) Chris@0: ->method('set') Chris@0: ->with('views_data:en', $expected_views_data); Chris@0: Chris@0: // Initialize the views data cache and repeat with no specified table. This Chris@0: // should only load the cache entry for all tables. Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $this->assertSame($expected_views_data, $this->viewsData->getAll()); Chris@0: $this->assertSame($expected_views_data, $this->viewsData->get()); Chris@0: } Chris@0: } Chris@0: Chris@0: }