You are here

public function DatabaseBackendTagTest::testTagInvalidations in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Cache/DatabaseBackendTagTest.php \Drupal\system\Tests\Cache\DatabaseBackendTagTest::testTagInvalidations()

File

core/modules/system/src/Tests/Cache/DatabaseBackendTagTest.php, line 41
Contains \Drupal\system\Tests\Cache\DatabaseBackendTagTest.

Class

DatabaseBackendTagTest
Tests DatabaseBackend cache tag implementation.

Namespace

Drupal\system\Tests\Cache

Code

public function testTagInvalidations() {

  // Create cache entry in multiple bins.
  $tags = array(
    'test_tag:1',
    'test_tag:2',
    'test_tag:3',
  );
  $bins = array(
    'data',
    'bootstrap',
    'render',
  );
  foreach ($bins as $bin) {
    $bin = \Drupal::cache($bin);
    $bin
      ->set('test', 'value', Cache::PERMANENT, $tags);
    $this
      ->assertTrue($bin
      ->get('test'), 'Cache item was set in bin.');
  }
  $invalidations_before = intval(db_select('cachetags')
    ->fields('cachetags', array(
    'invalidations',
  ))
    ->condition('tag', 'test_tag:2')
    ->execute()
    ->fetchField());
  Cache::invalidateTags(array(
    'test_tag:2',
  ));

  // Test that cache entry has been invalidated in multiple bins.
  foreach ($bins as $bin) {
    $bin = \Drupal::cache($bin);
    $this
      ->assertFalse($bin
      ->get('test'), 'Tag invalidation affected item in bin.');
  }

  // Test that only one tag invalidation has occurred.
  $invalidations_after = intval(db_select('cachetags')
    ->fields('cachetags', array(
    'invalidations',
  ))
    ->condition('tag', 'test_tag:2')
    ->execute()
    ->fetchField());
  $this
    ->assertEqual($invalidations_after, $invalidations_before + 1, 'Only one addition cache tag invalidation has occurred after invalidating a tag used in multiple bins.');
}