function GenericCacheBackendUnitTestBase::testInvalidateTags in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase::testInvalidateTags()
Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
1 call to GenericCacheBackendUnitTestBase::testInvalidateTags()
- ApcuBackendUnitTest::testInvalidateTags in core/
modules/ system/ src/ Tests/ Cache/ ApcuBackendUnitTest.php - Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
1 method overrides GenericCacheBackendUnitTestBase::testInvalidateTags()
- ApcuBackendUnitTest::testInvalidateTags in core/
modules/ system/ src/ Tests/ Cache/ ApcuBackendUnitTest.php - Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
File
- core/
modules/ system/ src/ Tests/ Cache/ GenericCacheBackendUnitTestBase.php, line 531 - Contains \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase.
Class
- GenericCacheBackendUnitTestBase
- Tests any cache backend.
Namespace
Drupal\system\Tests\CacheCode
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, array(
'test_tag:2',
));
$backend
->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array(
'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(array(
'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, array(
'test_tag:1',
));
$backend
->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array(
'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(array(
'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, array(
'test_tag:1',
));
$backend
->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array(
'test_tag:2',
));
$backend
->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, array(
'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(array(
'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 = array(
'test_tag:1',
'test_tag:2',
'test_tag:3',
);
$bins = array(
'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(array(
'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.');
}