function ViewsDataTest::testCacheCallsWithWarmCacheAndInvalidTable

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

Tests the cache calls for a non-existent table.

Warm cache:

  • all tables
  • views_test_data

Not warm cache:

  • $non_existing_table

File

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

Class

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

Namespace

Drupal\Tests\views\Unit

Code

public function testCacheCallsWithWarmCacheAndInvalidTable() : void {
    $expected_views_data = $this->viewsDataWithProvider();
    $non_existing_table = $this->randomMachineName();
    $this->moduleHandler
        ->expects($this->never())
        ->method('invokeAllWith');
    // Setup a warm cache backend for a single table.
    $gets = [
        "views_data:{$non_existing_table}: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);
    }))
        ->willReturnOnConsecutiveCalls(FALSE, (object) [
        'data' => $expected_views_data,
    ]);
    $this->cacheBackend
        ->expects($this->once())
        ->method('set')
        ->with("views_data:{$non_existing_table}:en", []);
    // 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
    // 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.
    for ($i = 0; $i < 5; $i++) {
        $views_data = $this->viewsData
            ->get($non_existing_table);
        $this->assertSame([], $views_data);
    }
}

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