function EntityUnitTest::testCacheTags

Same name and namespace in other branches
  1. 8.9.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. 11.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 <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityBase.php/class/EntityBase/9" title="Defines a base entity class." class="local">\Drupal\Core\Entity\EntityBase</a> @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.