function EntityUnitTest::testCacheTags

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php \Drupal\Tests\Core\Entity\EntityUnitTest::testCacheTags()
  2. 10 core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php \Drupal\Tests\Core\Entity\EntityUnitTest::testCacheTags()
  3. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php \Drupal\Tests\Core\Entity\EntityUnitTest::testCacheTags()

@covers ::getCacheTags
@covers ::getCacheTagsToInvalidate
@covers ::addCacheTags

File

core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php, line 567

Class

EntityUnitTest
@coversDefaultClass \Drupal\Core\Entity\EntityBase[[api-linebreak]] @group Entity @group Access

Namespace

Drupal\Tests\Core\Entity

Code

public function testCacheTags() {
  // Ensure that both methods return the same by default.
  $this->assertEqualsCanonicalizing([
    $this->entityTypeId . ':' . 1,
  ], $this->entity
    ->getCacheTags());
  $this->assertEqualsCanonicalizing([
    $this->entityTypeId . ':' . 1,
  ], $this->entity
    ->getCacheTagsToInvalidate());
  // Add an additional cache tag and make sure only getCacheTags() returns
  // that.
  $this->entity
    ->addCacheTags([
    'additional_cache_tag',
  ]);
  // EntityTypeId is random so it can shift order. We need to duplicate the
  // sort from \Drupal\Core\Cache\Cache::mergeTags().
  $tags = [
    $this->entityTypeId . ':' . 1,
    'additional_cache_tag',
  ];
  $this->assertEqualsCanonicalizing($tags, $this->entity
    ->getCacheTags());
  $this->assertEqualsCanonicalizing([
    $this->entityTypeId . ':' . 1,
  ], $this->entity
    ->getCacheTagsToInvalidate());
}

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