function DatabaseBackendTagTest::testTagsPurge

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

Test cache tag purging.

File

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

Class

DatabaseBackendTagTest
Tests DatabaseBackend cache tag implementation.

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testTagsPurge() : void {
  $tags = [
    'test_tag:1',
    'test_tag:2',
    'test_tag:3',
  ];
  /** @var \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_invalidator */
  $checksum_invalidator = \Drupal::service('cache_tags.invalidator.checksum');
  // Assert that initial current tag checksum is 0. This also ensures that the
  // 'cachetags' table is created, which at this point does not exist yet.
  $this->assertEquals(0, $checksum_invalidator->getCurrentChecksum($tags));
  /** @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface $invalidator */
  $invalidator = \Drupal::service('cache_tags.invalidator');
  $invalidator->invalidateTags($tags);
  // Checksum should be incremented by 1 by the invalidation for each tag.
  $this->assertEquals(3, $checksum_invalidator->getCurrentChecksum($tags));
  // After purging, confirm checksum is 0 and the 'cachetags' table is empty.
  $this->assertInstanceOf(CacheTagsPurgeInterface::class, $invalidator);
  $invalidator->purge();
  $this->assertEquals(0, $checksum_invalidator->getCurrentChecksum($tags));
  $rows = Database::getConnection()->select('cachetags')
    ->fields('cachetags')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this->assertEmpty($rows, 'cachetags table is empty.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.