Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Cache / DatabaseBackendTagTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Cache;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\KernelTests\KernelTestBase;
8 use Symfony\Component\DependencyInjection\Reference;
9
10 /**
11  * Tests DatabaseBackend cache tag implementation.
12  *
13  * @group Cache
14  */
15 class DatabaseBackendTagTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['system'];
23
24   /**
25    * {@inheritdoc}
26    */
27   public function register(ContainerBuilder $container) {
28     parent::register($container);
29     // Change container to database cache backends.
30     $container
31       ->register('cache_factory', 'Drupal\Core\Cache\CacheFactory')
32       ->addArgument(new Reference('settings'))
33       ->addMethodCall('setContainer', [new Reference('service_container')]);
34   }
35
36   public function testTagInvalidations() {
37     // Create cache entry in multiple bins.
38     $tags = ['test_tag:1', 'test_tag:2', 'test_tag:3'];
39     $bins = ['data', 'bootstrap', 'render'];
40     foreach ($bins as $bin) {
41       $bin = \Drupal::cache($bin);
42       $bin->set('test', 'value', Cache::PERMANENT, $tags);
43       $this->assertTrue($bin->get('test'), 'Cache item was set in bin.');
44     }
45
46     $invalidations_before = intval(db_select('cachetags')->fields('cachetags', ['invalidations'])->condition('tag', 'test_tag:2')->execute()->fetchField());
47     Cache::invalidateTags(['test_tag:2']);
48
49     // Test that cache entry has been invalidated in multiple bins.
50     foreach ($bins as $bin) {
51       $bin = \Drupal::cache($bin);
52       $this->assertFalse($bin->get('test'), 'Tag invalidation affected item in bin.');
53     }
54
55     // Test that only one tag invalidation has occurred.
56     $invalidations_after = intval(db_select('cachetags')->fields('cachetags', ['invalidations'])->condition('tag', 'test_tag:2')->execute()->fetchField());
57     $this->assertEqual($invalidations_after, $invalidations_before + 1, 'Only one addition cache tag invalidation has occurred after invalidating a tag used in multiple bins.');
58   }
59
60 }