public function DatabaseBackendTagTest::testTagInvalidations in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php \Drupal\KernelTests\Core\Cache\DatabaseBackendTagTest::testTagInvalidations()
- 9 core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php \Drupal\KernelTests\Core\Cache\DatabaseBackendTagTest::testTagInvalidations()
File
- core/
tests/ Drupal/ KernelTests/ Core/ Cache/ DatabaseBackendTagTest.php, line 37
Class
- DatabaseBackendTagTest
- Tests DatabaseBackend cache tag implementation.
Namespace
Drupal\KernelTests\Core\CacheCode
public function testTagInvalidations() {
// 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.');
}