public function GenericCacheBackendUnitTestBase::testInvalidateTags in Drupal 8
Same name in this branch
- 8 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testInvalidateTags()
- 8 core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase::testInvalidateTags()
Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
File
- core/
modules/ system/ src/ Tests/ Cache/ GenericCacheBackendUnitTestBase.php, line 536
Class
- GenericCacheBackendUnitTestBase
- Tests any cache backend.
Namespace
Drupal\system\Tests\CacheCode
public function testInvalidateTags() {
$backend = $this
->getCacheBackend();
// Create two cache entries with the same tag and tag value.
$backend
->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, [
'test_tag:2',
]);
$backend
->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, [
'test_tag:2',
]);
$this
->assertTrue($backend
->get('test_cid_invalidate1') && $backend
->get('test_cid_invalidate2'), 'Two cache items were created.');
// Invalidate test_tag of value 1. This should invalidate both entries.
Cache::invalidateTags([
'test_tag:2',
]);
$this
->assertFalse($backend
->get('test_cid_invalidate1') || $backend
->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.');
$this
->assertTrue($backend
->get('test_cid_invalidate1', TRUE) && $backend
->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
// Create two cache entries with the same tag and an array tag value.
$backend
->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, [
'test_tag:1',
]);
$backend
->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, [
'test_tag:1',
]);
$this
->assertTrue($backend
->get('test_cid_invalidate1') && $backend
->get('test_cid_invalidate2'), 'Two cache items were created.');
// Invalidate test_tag of value 1. This should invalidate both entries.
Cache::invalidateTags([
'test_tag:1',
]);
$this
->assertFalse($backend
->get('test_cid_invalidate1') || $backend
->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.');
$this
->assertTrue($backend
->get('test_cid_invalidate1', TRUE) && $backend
->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
// Create three cache entries with a mix of tags and tag values.
$backend
->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, [
'test_tag:1',
]);
$backend
->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, [
'test_tag:2',
]);
$backend
->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, [
'test_tag_foo:3',
]);
$this
->assertTrue($backend
->get('test_cid_invalidate1') && $backend
->get('test_cid_invalidate2') && $backend
->get('test_cid_invalidate3'), 'Three cached items were created.');
Cache::invalidateTags([
'test_tag_foo:3',
]);
$this
->assertTrue($backend
->get('test_cid_invalidate1') && $backend
->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.');
$this
->assertFalse($backend
->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.');
// Create cache entry in multiple bins. Two cache entries
// (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
// tests.
$tags = [
'test_tag:1',
'test_tag:2',
'test_tag:3',
];
$bins = [
'path',
'bootstrap',
'page',
];
foreach ($bins as $bin) {
$this
->getCacheBackend($bin)
->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
$this
->assertTrue($this
->getCacheBackend($bin)
->get('test'), 'Cache item was set in bin.');
}
Cache::invalidateTags([
'test_tag:2',
]);
// Test that the cache entry has been invalidated in multiple bins.
foreach ($bins as $bin) {
$this
->assertFalse($this
->getCacheBackend($bin)
->get('test'), 'Tag invalidation affected item in bin.');
}
// Test that the cache entry with a matching tag has been invalidated.
$this
->assertFalse($this
->getCacheBackend($bin)
->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.');
// Test that the cache entry with without a matching tag still exists.
$this
->assertTrue($this
->getCacheBackend($bin)
->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
}