function DatabaseBackendTagTest::testTagInvalidations

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php \Drupal\KernelTests\Core\Cache\DatabaseBackendTagTest::testTagInvalidations()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php \Drupal\KernelTests\Core\Cache\DatabaseBackendTagTest::testTagInvalidations()
  3. 10 core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php \Drupal\KernelTests\Core\Cache\DatabaseBackendTagTest::testTagInvalidations()

File

core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php, line 39

Class

DatabaseBackendTagTest
Tests DatabaseBackend cache tag implementation.

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testTagInvalidations() : void {
    // Create cache entry in multiple bins.
    $tags = [
        'test_tag:1',
        'test_tag:2',
        'test_tag:3',
    ];
    $bins = [
        'data',
        'bootstrap',
        'render',
    ];
    foreach ($bins as $bin) {
        $bin = \Drupal::cache($bin);
        $bin->set('test', 'value', Cache::PERMANENT, $tags);
        $this->assertNotEmpty($bin->get('test'), 'Cache item was set in bin.');
    }
    $connection = Database::getConnection();
    $invalidations_before = intval($connection->select('cachetags')
        ->fields('cachetags', [
        'invalidations',
    ])
        ->condition('tag', 'test_tag:2')
        ->execute()
        ->fetchField());
    Cache::invalidateTags([
        'test_tag:2',
    ]);
    // Test that cache entry has been invalidated in multiple bins.
    foreach ($bins as $bin) {
        $bin = \Drupal::cache($bin);
        $this->assertFalse($bin->get('test'), 'Tag invalidation affected item in bin.');
    }
    // Test that only one tag invalidation has occurred.
    $invalidations_after = intval($connection->select('cachetags')
        ->fields('cachetags', [
        'invalidations',
    ])
        ->condition('tag', 'test_tag:2')
        ->execute()
        ->fetchField());
    $this->assertEquals($invalidations_before + 1, $invalidations_after, 'Only one addition cache tag invalidation has occurred after invalidating a tag used in multiple bins.');
}

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