function ViewsDataTest::testFullAndTableGetCache

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testFullAndTableGetCache()
  2. 10 core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testFullAndTableGetCache()
  3. 11.x core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testFullAndTableGetCache()

Tests the cache of the full and single table data.

File

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

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->exactly(2))
        ->method('invokeAllWith')
        ->with('views_data')
        ->willReturnCallback(function ($hook, $callback) {
        $callback(\Closure::fromCallable([
            $this,
            'viewsData',
        ]), 'views_test_data');
    });
    $this->moduleHandler
        ->expects($this->exactly(2))
        ->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->exactly(4))
        ->method('get')
        ->withConsecutive([
        'views_data:en',
    ], [
        "views_data:{$random_table_name}:en",
    ], [
        'views_data:en',
    ], [
        "views_data:{$random_table_name}:en",
    ])
        ->willReturn(FALSE);
    $this->cacheBackend
        ->expects($this->exactly(4))
        ->method('set')
        ->withConsecutive([
        'views_data:en',
        $expected_views_data,
    ], [
        "views_data:{$random_table_name}:en",
        [],
    ], [
        'views_data:en',
        $expected_views_data,
    ], [
        "views_data:{$random_table_name}:en",
        [],
    ]);
    $this->cacheTagsInvalidator
        ->expects($this->once())
        ->method('invalidateTags')
        ->with([
        'views_data',
    ]);
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.