You are here

public function ViewsDataTest::testFullAndTableGetCache in Drupal 8

Tests the cache of the full and single table data.

File

core/modules/views/tests/src/Unit/ViewsDataTest.php, line 208

Class

ViewsDataTest
@coversDefaultClass \Drupal\views\ViewsData @group views

Namespace

Drupal\Tests\views\Unit

Code

public function testFullAndTableGetCache() {
  $expected_views_data = $this
    ->viewsDataWithProvider();
  $table_name = 'views_test_data';
  $table_name_2 = 'views_test_data_2';
  $random_table_name = $this
    ->randomMachineName();

  // Views data should be invoked twice due to the clear call.
  $this->moduleHandler
    ->expects($this
    ->at(0))
    ->method('getImplementations')
    ->with('views_data')
    ->willReturn([
    '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);
  $this->moduleHandler
    ->expects($this
    ->at(3))
    ->method('getImplementations')
    ->with('views_data')
    ->willReturn([
    '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))
    ->method('alter')
    ->with('views_data', $expected_views_data);

  // 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')
    ->with("views_data:en", $expected_views_data);
  $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", []);
  $this->cacheTagsInvalidator
    ->expects($this
    ->once())
    ->method('invalidateTags')
    ->with([
    'views_data',
  ]);
  $this->cacheBackend
    ->expects($this
    ->at(4))
    ->method('get')
    ->with("views_data:en")
    ->will($this
    ->returnValue(FALSE));
  $this->cacheBackend
    ->expects($this
    ->at(5))
    ->method('set')
    ->with("views_data:en", $expected_views_data);
  $this->cacheBackend
    ->expects($this
    ->at(6))
    ->method('get')
    ->with("views_data:{$random_table_name}:en")
    ->will($this
    ->returnValue(FALSE));
  $this->cacheBackend
    ->expects($this
    ->at(7))
    ->method('set')
    ->with("views_data:{$random_table_name}:en", []);
  $views_data = $this->viewsData
    ->getAll();
  $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);

  // 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);
  $views_data = $this->viewsData
    ->get($random_table_name);
  $this
    ->assertSame([], $views_data);
  $this->viewsData
    ->clear();

  // Get the views data again.
  $this->viewsData
    ->getAll();
  $this->viewsData
    ->get($table_name);
  $this->viewsData
    ->get($table_name_2);
  $this->viewsData
    ->get($random_table_name);
}