function GenericCacheBackendUnitTestBase::testInvalidateTags

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

File

drupal/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php, line 468
Definition of Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase.

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\system\Tests\Cache

Code

function testInvalidateTags() {
  $backend = $this
    ->getCacheBackend();

  // Create two cache entries with the same tag and tag value.
  $backend
    ->set('test_cid_invalidate1', $this->defaultValue, CacheBackendInterface::CACHE_PERMANENT, array(
    'test_tag' => 2,
  ));
  $backend
    ->set('test_cid_invalidate2', $this->defaultValue, CacheBackendInterface::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.
  $backend
    ->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, CacheBackendInterface::CACHE_PERMANENT, array(
    'test_tag' => array(
      1,
    ),
  ));
  $backend
    ->set('test_cid_invalidate2', $this->defaultValue, CacheBackendInterface::CACHE_PERMANENT, array(
    'test_tag' => array(
      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.
  $backend
    ->invalidateTags(array(
    'test_tag' => array(
      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, CacheBackendInterface::CACHE_PERMANENT, array(
    'test_tag' => array(
      1,
    ),
  ));
  $backend
    ->set('test_cid_invalidate2', $this->defaultValue, CacheBackendInterface::CACHE_PERMANENT, array(
    'test_tag' => array(
      2,
    ),
  ));
  $backend
    ->set('test_cid_invalidate3', $this->defaultValue, CacheBackendInterface::CACHE_PERMANENT, array(
    'test_tag_foo' => array(
      3,
    ),
  ));
  $this
    ->assertTrue($backend
    ->get('test_cid_invalidate1') && $backend
    ->get('test_cid_invalidate2') && $backend
    ->get('test_cid_invalidate3'), 'Three cached items were created.');
  $backend
    ->invalidateTags(array(
    'test_tag_foo' => array(
      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' => array(
      1,
      2,
      3,
    ),
  );
  $bins = array(
    'path',
    'bootstrap',
    'page',
  );
  foreach ($bins as $bin) {
    $this
      ->getCacheBackend($bin)
      ->set('test', $this->defaultValue, CacheBackendInterface::CACHE_PERMANENT, $tags);
    $this
      ->assertTrue($this
      ->getCacheBackend($bin)
      ->get('test'), 'Cache item was set in bin.');
  }

  // Invalidate tag in mulitple bins.
  foreach ($bins as $bin) {
    $this
      ->getCacheBackend($bin)
      ->invalidateTags(array(
      'test_tag' => array(
        2,
      ),
    ));
  }

  // Test that cache entry has been invalidated in multple 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.');
}