You are here

public function DatabaseBackendTagTest::testTagInvalidations in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php \Drupal\KernelTests\Core\Cache\DatabaseBackendTagTest::testTagInvalidations()

File

core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTagTest.php, line 37

Class

DatabaseBackendTagTest
Tests DatabaseBackend cache tag implementation.

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testTagInvalidations() {

  // Create cache entry in multiple bins.
  $tags = [
    'test_tag:1',
    'test_tag:2',
    'test_tag:3',
  ];
  $bins = [
    'data',
    'bootstrap',
    'render',
  ];
  foreach ($bins as $bin) {
    $bin = \Drupal::cache($bin);
    $bin
      ->set('test', 'value', Cache::PERMANENT, $tags);
    $this
      ->assertNotEmpty($bin
      ->get('test'), 'Cache item was set in bin.');
  }
  $connection = Database::getConnection();
  $invalidations_before = intval($connection
    ->select('cachetags')
    ->fields('cachetags', [
    'invalidations',
  ])
    ->condition('tag', 'test_tag:2')
    ->execute()
    ->fetchField());
  Cache::invalidateTags([
    '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($connection
    ->select('cachetags')
    ->fields('cachetags', [
    '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.');
}