function ViewsDataTest::testFullAndTableGetCache

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testFullAndTableGetCache()
  2. 8.9.x core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testFullAndTableGetCache()
  3. 10 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 204

Class

ViewsDataTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21views%21src%21ViewsData.php/class/ViewsData/11.x" title="Class to manage and lazy load cached views data." class="local">\Drupal\views\ViewsData</a> @group views

Namespace

Drupal\Tests\views\Unit

Code

public function testFullAndTableGetCache() : void {
    $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().
    $gets = [
        'views_data:en',
        "views_data:{$random_table_name}:en",
        'views_data:en',
        "views_data:{$random_table_name}:en",
    ];
    $this->cacheBackend
        ->expects($this->exactly(count($gets)))
        ->method('get')
        ->with($this->callback(function (string $key) use (&$gets) : bool {
        return $key === array_shift($gets);
    }))
        ->willReturn(FALSE);
    $sets = [
        '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->cacheBackend
        ->expects($this->exactly(count($sets) / 2))
        ->method('set')
        ->with($this->callback(function (string $key) use (&$sets) : bool {
        return $key === array_shift($sets);
    }), $this->callback(function (array $data) use (&$sets) : bool {
        return $data === array_shift($sets);
    }));
    $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.