function GenericCacheBackendUnitTestBase::testDeleteTags

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

File

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

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\system\Tests\Cache

Code

function testDeleteTags() {
  $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.');

  // Delete test_tag of value 1. This should delete both entries.
  $backend
    ->deleteTags(array(
    'test_tag' => 2,
  ));
  $this
    ->assertFalse($backend
    ->get('test_cid_invalidate1') || $backend
    ->get('test_cid_invalidate2'), 'Two cache items invalidated after deleting a cache tag.');
  $this
    ->assertFalse($backend
    ->get('test_cid_invalidate1', TRUE) || $backend
    ->get('test_cid_invalidate2', TRUE), 'Two cache items deleted after deleting 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.');

  // Delete test_tag of value 1. This should delete both entries.
  $backend
    ->deleteTags(array(
    'test_tag' => array(
      1,
    ),
  ));
  $this
    ->assertFalse($backend
    ->get('test_cid_invalidate1') || $backend
    ->get('test_cid_invalidate2'), 'Two cache items invalidated after deleted a cache tag.');
  $this
    ->assertFalse($backend
    ->get('test_cid_invalidate1', TRUE) || $backend
    ->get('test_cid_invalidate2', TRUE), 'Two cache items deleted after deleting 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
    ->deleteTags(array(
    'test_tag_foo' => array(
      3,
    ),
  ));
  $this
    ->assertTrue($backend
    ->get('test_cid_invalidate1') && $backend
    ->get('test_cid_invalidate2'), 'Cached items not matching the tag were not deleted.');
  $this
    ->assertFalse($backend
    ->get('test_cid_invalidated3', TRUE), 'Cache item matching the tag was deleted.');

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

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

  // Test that cache entry has been deleted in multple bins.
  foreach ($bins as $bin) {
    $this
      ->assertFalse($this
      ->getCacheBackend($bin)
      ->get('test', TRUE), 'Tag deletion 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', TRUE), 'Cache items matching tag were deleted.');

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