annotate core/modules/views/tests/src/Unit/ViewsDataTest.php @ 19:fa3358dc1485 tip

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