View source
<?php
namespace Drupal\supercache\Tests\Generic\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Drupal\Core\Site\Settings;
abstract class CacheTagsChecksumTests extends KernelTestBase {
protected $backendFactory;
protected $tagInvalidator;
protected $tagChecksum;
public function testTagInvalidations() {
if ($this->tagInvalidator !== $this->tagInvalidator) {
$this
->fail('Incorrect setup for this test.');
}
$this->tagInvalidator
->resetTags();
$tags = [
'test_tag:1',
'test_tag:2',
'test_tag:3',
];
$bins = [
'data',
'bootstrap',
'render',
];
foreach ($bins as $bin) {
$bin = $this->backendFactory
->get($bin);
$bin
->set('test', 'value', Cache::PERMANENT, $tags);
$this
->assertTrue($bin
->get('test'), 'Cache item was set in bin.');
}
$checksum = $this->tagChecksum
->getCurrentChecksum($tags);
$this->tagInvalidator
->invalidateTags([
'test_tag:2',
]);
$this
->assertEquals($this->tagChecksum
->getCurrentChecksum($tags), $checksum + 1);
$checksum = $this->tagChecksum
->getCurrentChecksum($tags);
$this->tagInvalidator
->invalidateTags($tags);
$this
->assertEquals($this->tagChecksum
->getCurrentChecksum($tags), $checksum + 3);
foreach ($bins as $bin) {
$bin = $this->backendFactory
->get($bin);
$this
->assertFalse($bin
->get('test'), 'Tag invalidation affected item in bin.');
}
$this->tagInvalidator
->resetTags();
$this
->assertEquals($this->tagChecksum
->getCurrentChecksum($tags), 0);
$bin = $this->backendFactory
->get($bins[0]);
$bin
->setMultiple([
'test0' => [
'data' => 'data0',
'tags' => [
$tags[0],
$tags[1],
],
],
]);
$bin
->setMultiple([
'test1' => [
'data' => 'data1',
'tags' => [
$tags[2],
],
],
]);
$this->tagInvalidator
->invalidateTags([
$tags[0],
]);
$this
->assertFalse($bin
->get('test0'), 'Tag invalidation affected item in bin.');
$this
->assertEquals('data1', $bin
->get('test1')->data, 'Tag invalidation did not affect item in bin.');
$this->tagInvalidator
->invalidateTags([
$tags[2],
]);
$this
->assertFalse($bin
->get('test1'), 'Tag invalidation affected item in bin.');
}
}