function ViewsDataTest::testSingleTableGetCache

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

Tests the caching of the views data for a specific table.

File

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

Class

ViewsDataTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21views%21src%21ViewsData.php/class/ViewsData/10" 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 testSingleTableGetCache() : void {
    $table_name = 'views_test_data';
    $expected_views_data = $this->viewsDataWithProvider();
    // Views data should be invoked once.
    $this->setupMockedModuleHandler();
    $this->moduleHandler
        ->expects($this->once())
        ->method('alter')
        ->with('views_data', $this->viewsDataWithProvider());
    $gets = [
        "views_data:{$table_name}:en",
        'views_data: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);
    $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.
    $views_data = $this->viewsData
        ->getAll();
    $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.');
}

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