DatabaseBackendTagTest.php in Zircon Profile 8.0
File
core/modules/system/src/Tests/Cache/DatabaseBackendTagTest.php
View source
<?php
namespace Drupal\system\Tests\Cache;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\DependencyInjection\Reference;
class DatabaseBackendTagTest extends KernelTestBase {
public static $modules = array(
'system',
);
public function containerBuild(ContainerBuilder $container) {
parent::containerBuild($container);
$container
->register('cache_factory', 'Drupal\\Core\\Cache\\CacheFactory')
->addArgument(new Reference('settings'))
->addMethodCall('setContainer', array(
new Reference('service_container'),
));
}
public function testTagInvalidations() {
$tags = array(
'test_tag:1',
'test_tag:2',
'test_tag:3',
);
$bins = array(
'data',
'bootstrap',
'render',
);
foreach ($bins as $bin) {
$bin = \Drupal::cache($bin);
$bin
->set('test', 'value', Cache::PERMANENT, $tags);
$this
->assertTrue($bin
->get('test'), 'Cache item was set in bin.');
}
$invalidations_before = intval(db_select('cachetags')
->fields('cachetags', array(
'invalidations',
))
->condition('tag', 'test_tag:2')
->execute()
->fetchField());
Cache::invalidateTags(array(
'test_tag:2',
));
foreach ($bins as $bin) {
$bin = \Drupal::cache($bin);
$this
->assertFalse($bin
->get('test'), 'Tag invalidation affected item in bin.');
}
$invalidations_after = intval(db_select('cachetags')
->fields('cachetags', array(
'invalidations',
))
->condition('tag', 'test_tag:2')
->execute()
->fetchField());
$this
->assertEqual($invalidations_after, $invalidations_before + 1, 'Only one addition cache tag invalidation has occurred after invalidating a tag used in multiple bins.');
}
}