You are here

public function GenericCacheBackendUnitTestBase::testInvalidateTags in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testInvalidateTags()
  2. 9 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testInvalidateTags()

Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().

File

core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php, line 538

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\KernelTests\Core\Cache

Code

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
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate1')->data);
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate2')->data);

  // 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.');

  // Verify that cache items have not been deleted after invalidation.
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate1', TRUE)->data);
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate2', TRUE)->data);

  // 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
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate1')->data);
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate2')->data);

  // 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.');

  // Verify that cache items have not been deleted after invalidation.
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate1', TRUE)->data);
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate2', TRUE)->data);

  // 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
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate1')->data);
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate2')->data);
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate3')->data);
  Cache::invalidateTags([
    'test_tag_foo:3',
  ]);

  // Verify that cache items not matching the tag were not invalidated.
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate1')->data);
  $this
    ->assertSame($this->defaultValue, $backend
    ->get('test_cid_invalidate2')->data);
  $this
    ->assertFalse($backend
    ->get('test_cid_invalidate3'), '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
      ->assertNotEmpty($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
    ->assertNotEmpty($this
    ->getCacheBackend($bin)
    ->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
}