ViewsDataTest.php 23.5 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Unit;
4 5 6 7

use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
use Drupal\views\ViewsData;
8
use Drupal\views\Tests\ViewTestData;
9 10

/**
11
 * @coversDefaultClass \Drupal\views\ViewsData
12
 * @group views
13 14 15 16 17 18 19 20 21 22
 */
class ViewsDataTest extends UnitTestCase {

  /**
   * The mocked cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheBackend;

23 24 25 26 27 28 29
  /**
   * The mocked cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheTagsInvalidator;

30 31 32 33 34 35 36 37 38 39
  /**
   * The mocked module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The mocked config factory.
   *
40
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
41 42 43 44 45 46
   */
  protected $configFactory;

  /**
   * The mocked language manager.
   *
47
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
48 49 50 51 52 53 54 55 56 57 58 59 60 61
   */
  protected $languageManager;

  /**
   * The tested views data class.
   *
   * @var \Drupal\views\ViewsData
   */
  protected $viewsData;

  /**
   * {@inheritdoc}
   */
  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);
65 66 67 68 69

    $configs = array();
    $configs['views.settings']['skip_cache'] = FALSE;
    $this->configFactory = $this->getConfigFactoryStub($configs);
    $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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      ->will($this->returnValue(new Language(array('id' => 'en'))));

    $this->viewsData = new ViewsData($this->cacheBackend, $this->configFactory, $this->moduleHandler, $this->languageManager);
  }

  /**
   * Returns the views data definition.
   */
  protected function viewsData() {
    $data = ViewTestData::viewsData();

    // Tweak the views data to have a base for testing.
    unset($data['views_test_data']['id']['field']);
    unset($data['views_test_data']['name']['argument']);
    unset($data['views_test_data']['age']['filter']);
    unset($data['views_test_data']['job']['sort']);
    $data['views_test_data']['created']['area']['id'] = 'text';
    $data['views_test_data']['age']['area']['id'] = 'text';
    $data['views_test_data']['age']['area']['sub_type'] = 'header';
    $data['views_test_data']['job']['area']['id'] = 'text';
    $data['views_test_data']['job']['area']['sub_type'] = array('header', 'footer');

95
    // Duplicate the example views test data for different weight, different title,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    // and matching data.
    $data['views_test_data_2'] = $data['views_test_data'];
    $data['views_test_data_2']['table']['base']['weight'] = 50;

    $data['views_test_data_3'] = $data['views_test_data'];
    $data['views_test_data_3']['table']['base']['weight'] = -50;

    $data['views_test_data_4'] = $data['views_test_data'];
    $data['views_test_data_4']['table']['base']['title'] = 'A different title';

    $data['views_test_data_5'] = $data['views_test_data'];
    $data['views_test_data_5']['table']['base']['title'] = 'Z different title';

    $data['views_test_data_6'] = $data['views_test_data'];

    return $data;
  }

  /**
115 116 117 118 119
   * Returns the views data definition with the provider key.
   *
   * @return array
   *
   * @see static::viewsData()
120
   */
121 122 123 124 125 126 127
  protected function viewsDataWithProvider() {
    $views_data = static::viewsData();
    foreach (array_keys($views_data) as $table) {
      $views_data[$table]['table']['provider'] = 'views_test_data';
    }
    return $views_data;
  }
128

129 130 131 132 133 134 135 136 137
  /**
   * Mocks the basic module handler used for the test.
   *
   * @return \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected function setupMockedModuleHandler() {
    $views_data = $this->viewsData();
    $this->moduleHandler->expects($this->at(0))
      ->method('getImplementations')
138
      ->with('views_data')
139 140 141 142 143 144
      ->willReturn(array('views_test_data'));
    $this->moduleHandler->expects($this->at(1))
      ->method('invoke')
      ->with('views_test_data', 'views_data')
      ->willReturn($views_data);
  }
145

146 147 148 149 150
  /**
   * Tests the fetchBaseTables() method.
   */
  public function testFetchBaseTables() {
    $this->setupMockedModuleHandler();
151
    $data = $this->viewsData->getAll();
152 153 154

    $base_tables = $this->viewsData->fetchBaseTables();

155 156 157 158 159
    // Ensure that 'provider' is set for each base table.
    foreach (array_keys($base_tables) as $base_table) {
      $this->assertEquals('views_test_data', $data[$base_table]['table']['provider']);
    }

160 161
    // Test the number of tables returned and their order.
    $this->assertCount(6, $base_tables, 'The correct amount of base tables were returned.');
162 163
    $base_tables_keys = array_keys($base_tables);
    for ($i = 1; $i < count($base_tables); ++$i) {
164
      $prev = $base_tables[$base_tables_keys[$i - 1]];
165 166 167
      $current = $base_tables[$base_tables_keys[$i]];
      $this->assertTrue($prev['weight'] <= $current['weight'] && $prev['title'] <= $prev['title'], 'The tables are sorted as expected.');
    }
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    // Test the values returned for each base table.
    $defaults = array(
      'title' => '',
      'help' => '',
      'weight' => 0,
    );
    foreach ($base_tables as $base_table => $info) {
      // Merge in default values as in fetchBaseTables().
      $expected = $data[$base_table]['table']['base'] += $defaults;
      foreach ($defaults as $key => $default) {
        $this->assertSame($info[$key], $expected[$key]);
      }
    }
  }

  /**
   * Tests fetching all the views data without a static cache.
   */
  public function testGetOnFirstCall() {
    // Ensure that the hooks are just invoked once.
189
    $this->setupMockedModuleHandler();
190

191
    $this->moduleHandler->expects($this->at(2))
192
      ->method('alter')
193
      ->with('views_data', $this->viewsDataWithProvider());
194 195 196 197 198 199

    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with("views_data:en")
      ->will($this->returnValue(FALSE));

200
    $expected_views_data = $this->viewsDataWithProvider();
201
    $views_data = $this->viewsData->getAll();
202 203 204 205 206 207 208
    $this->assertSame($expected_views_data, $views_data);
  }

  /**
   * Tests the cache of the full and single table data.
   */
  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();
213 214

    // Views data should be invoked twice due to the clear call.
215 216
    $this->moduleHandler->expects($this->at(0))
      ->method('getImplementations')
217
      ->with('views_data')
218 219 220 221 222 223 224 225
      ->willReturn(array('views_test_data'));
    $this->moduleHandler->expects($this->at(1))
      ->method('invoke')
      ->with('views_test_data', 'views_data')
      ->willReturn($this->viewsData());
    $this->moduleHandler->expects($this->at(2))
      ->method('alter')
      ->with('views_data', $expected_views_data);
226

227 228 229 230 231 232 233 234 235
    $this->moduleHandler->expects($this->at(3))
      ->method('getImplementations')
      ->with('views_data')
      ->willReturn(array('views_test_data'));
    $this->moduleHandler->expects($this->at(4))
      ->method('invoke')
      ->with('views_test_data', 'views_data')
      ->willReturn($this->viewsData());
    $this->moduleHandler->expects($this->at(5))
236 237 238
      ->method('alter')
      ->with('views_data', $expected_views_data);

239

240 241 242 243 244 245 246 247
    // The cache should only be called once (before the clear() call) as get
    // will get all table data in the first get().
    $this->cacheBackend->expects($this->at(0))
      ->method('get')
      ->with("views_data:en")
      ->will($this->returnValue(FALSE));
    $this->cacheBackend->expects($this->at(1))
      ->method('set')
248
      ->with("views_data:en", $expected_views_data);
249 250 251 252 253 254 255
    $this->cacheBackend->expects($this->at(2))
      ->method('get')
      ->with("views_data:$random_table_name:en")
      ->will($this->returnValue(FALSE));
    $this->cacheBackend->expects($this->at(3))
      ->method('set')
      ->with("views_data:$random_table_name:en", array());
256 257 258
    $this->cacheTagsInvalidator->expects($this->once())
      ->method('invalidateTags')
      ->with(['views_data']);
259 260 261 262
    $this->cacheBackend->expects($this->at(4))
      ->method('get')
      ->with("views_data:en")
      ->will($this->returnValue(FALSE));
263
    $this->cacheBackend->expects($this->at(5))
264
      ->method('set')
265
      ->with("views_data:en", $expected_views_data);
266
    $this->cacheBackend->expects($this->at(6))
267 268 269
      ->method('get')
      ->with("views_data:$random_table_name:en")
      ->will($this->returnValue(FALSE));
270
    $this->cacheBackend->expects($this->at(7))
271 272 273
      ->method('set')
      ->with("views_data:$random_table_name:en", array());

274
    $views_data = $this->viewsData->getAll();
275 276 277 278 279 280
    $this->assertSame($expected_views_data, $views_data);

    // Request a specific table should be static cached.
    $views_data = $this->viewsData->get($table_name);
    $this->assertSame($expected_views_data[$table_name], $views_data);

281 282 283 284
    // Another table being requested should also come from the static cache.
    $views_data = $this->viewsData->get($table_name_2);
    $this->assertSame($expected_views_data[$table_name_2], $views_data);

285 286 287 288 289 290
    $views_data = $this->viewsData->get($random_table_name);
    $this->assertSame(array(), $views_data);

    $this->viewsData->clear();

    // Get the views data again.
291
    $this->viewsData->getAll();
292
    $this->viewsData->get($table_name);
293
    $this->viewsData->get($table_name_2);
294 295 296 297 298 299 300
    $this->viewsData->get($random_table_name);
  }

  /**
   * Tests the caching of the full views data.
   */
  public function testFullGetCache() {
301
    $expected_views_data = $this->viewsDataWithProvider();
302 303

    // Views data should be invoked once.
304
    $this->setupMockedModuleHandler();
305 306 307 308 309 310 311 312 313 314

    $this->moduleHandler->expects($this->once())
      ->method('alter')
      ->with('views_data', $expected_views_data);

    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with("views_data:en")
      ->will($this->returnValue(FALSE));

315
    $views_data = $this->viewsData->getAll();
316 317
    $this->assertSame($expected_views_data, $views_data);

318
    $views_data = $this->viewsData->getAll();
319 320 321 322 323 324 325 326
    $this->assertSame($expected_views_data, $views_data);
  }

  /**
   * Tests the caching of the views data for a specific table.
   */
  public function testSingleTableGetCache() {
    $table_name = 'views_test_data';
327
    $expected_views_data = $this->viewsDataWithProvider();
328 329

    // Views data should be invoked once.
330
    $this->setupMockedModuleHandler();
331 332 333

    $this->moduleHandler->expects($this->once())
      ->method('alter')
334
      ->with('views_data', $this->viewsDataWithProvider());
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

    $this->cacheBackend->expects($this->at(0))
      ->method('get')
      ->with("views_data:$table_name:en")
      ->will($this->returnValue(FALSE));
    $this->cacheBackend->expects($this->at(1))
      ->method('get')
      ->with("views_data:en")
      ->will($this->returnValue(FALSE));

    $views_data = $this->viewsData->get($table_name);
    $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching views data by table works as expected.');

    $views_data = $this->viewsData->get($table_name);
    $this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching cached views data by table works as expected.');

    // Test that this data is present if all views data is returned.
352
    $views_data = $this->viewsData->getAll();
353 354 355 356 357 358 359 360 361

    $this->assertArrayHasKey($table_name, $views_data, 'Make sure the views_test_data info appears in the total views data.');
    $this->assertSame($expected_views_data[$table_name], $views_data[$table_name], 'Make sure the views_test_data has the expected values.');
  }

  /**
   * Tests building the views data with a non existing table.
   */
  public function testNonExistingTableGetCache() {
362
    $random_table_name = $this->randomMachineName();
363 364

    // Views data should be invoked once.
365
    $this->setupMockedModuleHandler();
366 367 368

    $this->moduleHandler->expects($this->once())
      ->method('alter')
369
      ->with('views_data', $this->viewsDataWithProvider());
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

    $this->cacheBackend->expects($this->at(0))
      ->method('get')
      ->with("views_data:$random_table_name:en")
      ->will($this->returnValue(FALSE));
    $this->cacheBackend->expects($this->at(1))
      ->method('get')
      ->with("views_data:en")
      ->will($this->returnValue(FALSE));

    // All views data should be requested on the first try.
    $views_data = $this->viewsData->get($random_table_name);
    $this->assertSame(array(), $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');

    // Test no data is rebuilt when requesting an invalid table again.
    $views_data = $this->viewsData->get($random_table_name);
    $this->assertSame(array(), $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');
  }

  /**
390
   * Tests the cache backend behavior with requesting the same table multiple
391 392
   */
  public function testCacheCallsWithSameTableMultipleTimes() {
393
    $expected_views_data = $this->viewsDataWithProvider();
394

395
    $this->setupMockedModuleHandler();
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

    $this->cacheBackend->expects($this->at(0))
      ->method('get')
      ->with('views_data:views_test_data:en');
    $this->cacheBackend->expects($this->at(1))
      ->method('get')
      ->with('views_data:en');
    $this->cacheBackend->expects($this->at(2))
      ->method('set')
      ->with('views_data:en', $expected_views_data);
    $this->cacheBackend->expects($this->at(3))
      ->method('set')
      ->with('views_data:views_test_data:en', $expected_views_data['views_test_data']);

    // Request the same table 5 times. The caches are empty at this point, so
    // what will happen is that it will first check for a cache entry for the
    // given table, get a cache miss, then try the cache entry for all tables,
    // which does not exist yet either. As a result, it rebuilds the information
    // and writes a cache entry for all tables and the requested table.
    $table_name = 'views_test_data';
    for ($i = 0; $i < 5; $i++) {
      $views_data = $this->viewsData->get($table_name);
      $this->assertSame($expected_views_data['views_test_data'], $views_data);
    }
  }

  /**
   * Tests the cache calls for a single table and warm cache for:
   *   - all tables
   *   - views_test_data
   */
  public function testCacheCallsWithSameTableMultipleTimesAndWarmCache() {
428
    $expected_views_data = $this->viewsDataWithProvider();
429
    $this->moduleHandler->expects($this->never())
430
      ->method('getImplementations');
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

    // Setup a warm cache backend for a single table.
    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with('views_data:views_test_data:en')
      ->will($this->returnValue((object) array('data' => $expected_views_data['views_test_data'])));
    $this->cacheBackend->expects($this->never())
      ->method('set');

    // We have a warm cache now, so this will only request the tables-specific
    // cache entry and return that.
    for ($i = 0; $i < 5; $i++) {
      $views_data = $this->viewsData->get('views_test_data');
      $this->assertSame($expected_views_data['views_test_data'], $views_data);
    }
  }

  /**
   * Tests the cache calls for a different table than the one in cache:
   *
   * Warm cache:
   *   - all tables
   *   - views_test_data
   * Not warm cache:
   *   - views_test_data_2
   */
  public function testCacheCallsWithWarmCacheAndDifferentTable() {
458
    $expected_views_data = $this->viewsDataWithProvider();
459
    $this->moduleHandler->expects($this->never())
460
      ->method('getImplementations');
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

    // Setup a warm cache backend for a single table.
    $this->cacheBackend->expects($this->at(0))
      ->method('get')
      ->with('views_data:views_test_data_2:en');
    $this->cacheBackend->expects($this->at(1))
      ->method('get')
      ->with('views_data:en')
      ->will($this->returnValue((object) array('data' => $expected_views_data)));
    $this->cacheBackend->expects($this->at(2))
      ->method('set')
      ->with('views_data:views_test_data_2:en', $expected_views_data['views_test_data_2']);

    // Requests a different table as the cache contains. This will fail to get a
    // table specific cache entry, load the cache entry for all tables and save
    // a cache entry for this table but not all.
    for ($i = 0; $i < 5; $i++) {
      $views_data = $this->viewsData->get('views_test_data_2');
      $this->assertSame($expected_views_data['views_test_data_2'], $views_data);
    }
  }

  /**
   * Tests the cache calls for an not existing table:
   *
   * Warm cache:
   *   - all tables
   *   - views_test_data
   * Not warm cache:
   *   - $non_existing_table
   */
  public function testCacheCallsWithWarmCacheAndInvalidTable() {
493
    $expected_views_data = $this->viewsDataWithProvider();
494
    $non_existing_table = $this->randomMachineName();
495
    $this->moduleHandler->expects($this->never())
496
      ->method('getImplementations');
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

    // Setup a warm cache backend for a single table.
    $this->cacheBackend->expects($this->at(0))
      ->method('get')
      ->with("views_data:$non_existing_table:en");
    $this->cacheBackend->expects($this->at(1))
      ->method('get')
      ->with('views_data:en')
      ->will($this->returnValue((object) array('data' => $expected_views_data)));
    $this->cacheBackend->expects($this->at(2))
      ->method('set')
      ->with("views_data:$non_existing_table:en", array());

    // Initialize the views data cache and request a non-existing table. This
    // will result in the same cache requests as we explicitly write an empty
512 513 514
    // cache entry for non-existing tables to avoid unnecessary requests in
    // those situations. We do have to load the cache entry for all tables to
    // check if the table does exist or not.
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    for ($i = 0; $i < 5; $i++) {
      $views_data = $this->viewsData->get($non_existing_table);
      $this->assertSame(array(), $views_data);
    }
  }

  /**
   * Tests the cache calls for an not existing table:
   *
   * Warm cache:
   *   - all tables
   *   - views_test_data
   *   - $non_existing_table
   */
  public function testCacheCallsWithWarmCacheForInvalidTable() {
530
    $non_existing_table = $this->randomMachineName();
531
    $this->moduleHandler->expects($this->never())
532
      ->method('getImplementations');
533 534 535 536 537 538 539 540 541 542 543

    // Setup a warm cache backend for a single table.
    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with("views_data:$non_existing_table:en")
      ->will($this->returnValue((object) array('data' => array())));
    $this->cacheBackend->expects($this->never())
      ->method('set');

    // Initialize the views data cache and request a non-existing table. This
    // will result in the same cache requests as we explicitly write an empty
544 545 546
    // cache entry for non-existing tables to avoid unnecessary requests in
    // those situations. We do have to load the cache entry for all tables to
    // check if the table does exist or not.
547 548 549 550 551 552 553 554 555 556
    for ($i = 0; $i < 5; $i++) {
      $views_data = $this->viewsData->get($non_existing_table);
      $this->assertSame(array(), $views_data);
    }
  }

  /**
   * Tests the cache calls for all views data without a warm cache.
   */
  public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
557 558
    $expected_views_data = $this->viewsDataWithProvider();
    $this->setupMockedModuleHandler();
559 560 561 562 563 564 565 566 567 568 569 570

    // Setup a warm cache backend for a single table.
    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with("views_data:en");
    $this->cacheBackend->expects($this->once())
      ->method('set')
      ->with('views_data:en', $expected_views_data);

    // Initialize the views data cache and repeat with no specified table. This
    // should only load the cache entry for all tables.
    for ($i = 0; $i < 5; $i++) {
571
      $views_data = $this->viewsData->getAll();
572 573 574 575 576 577 578 579 580 581 582
      $this->assertSame($expected_views_data, $views_data);
    }
  }

  /**
   * Tests the cache calls for all views data.
   *
   * Warm cache:
   *   - all tables
   */
  public function testCacheCallsWithWarmCacheAndGetAllTables() {
583
    $expected_views_data = $this->viewsDataWithProvider();
584
    $this->moduleHandler->expects($this->never())
585
      ->method('getImplementations');
586 587 588 589 590 591 592 593 594 595 596 597

    // Setup a warm cache backend for a single table.
    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with("views_data:en")
      ->will($this->returnValue((object) array('data' => $expected_views_data)));
    $this->cacheBackend->expects($this->never())
      ->method('set');

    // Initialize the views data cache and repeat with no specified table. This
    // should only load the cache entry for all tables.
    for ($i = 0; $i < 5; $i++) {
598
      $views_data = $this->viewsData->getAll();
599 600 601 602
      $this->assertSame($expected_views_data, $views_data);
    }
  }

603 604 605 606 607 608
  /**
   * Tests the cache calls for multiple tables without warm caches.
   *
   * @covers ::get
   */
  public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
609
    $expected_views_data = $this->viewsDataWithProvider();
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    $table_name = 'views_test_data';
    $table_name_2 = 'views_test_data_2';

    // Setup a warm cache backend for all table data, but not single tables.
    $this->cacheBackend->expects($this->at(0))
      ->method('get')
      ->with("views_data:$table_name:en")
      ->will($this->returnValue(FALSE));
    $this->cacheBackend->expects($this->at(1))
      ->method('get')
      ->with('views_data:en')
      ->will($this->returnValue((object) array('data' => $expected_views_data)));
    $this->cacheBackend->expects($this->at(2))
      ->method('set')
      ->with("views_data:$table_name:en", $expected_views_data[$table_name]);
    $this->cacheBackend->expects($this->at(3))
      ->method('get')
      ->with("views_data:$table_name_2:en")
      ->will($this->returnValue(FALSE));
    $this->cacheBackend->expects($this->at(4))
      ->method('set')
      ->with("views_data:$table_name_2:en", $expected_views_data[$table_name_2]);

    $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
    $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));

    // Should only be invoked the first time.
    $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
    $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
  }

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
  /**
   * Tests that getting all data has same results as getting data with NULL
   * logic.
   *
   * @covers ::getAll
   */
  public function testGetAllEqualsToGetNull() {
    $expected_views_data = $this->viewsDataWithProvider();
    $this->setupMockedModuleHandler();

    // Setup a warm cache backend for a single table.
    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with("views_data:en");
    $this->cacheBackend->expects($this->once())
      ->method('set')
      ->with('views_data:en', $expected_views_data);

    // Initialize the views data cache and repeat with no specified table. This
    // should only load the cache entry for all tables.
    for ($i = 0; $i < 5; $i++) {
      $this->assertSame($expected_views_data, $this->viewsData->getAll());
      $this->assertSame($expected_views_data, $this->viewsData->get());
    }
  }

667
}