annotate core/modules/views/tests/src/Unit/ViewsDataTest.php @ 2:92f882872392

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