function AccessResultTest::testCacheTags

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheTags()
  2. 8.9.x core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheTags()
  3. 10 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheTags()

@covers ::addCacheTags @covers ::addCacheableDependency @covers ::getCacheTags @covers ::resetCacheTags

File

core/tests/Drupal/Tests/Core/Access/AccessResultTest.php, line 477

Class

AccessResultTest
@coversDefaultClass \Drupal\Core\Access\AccessResult @group Access

Namespace

Drupal\Tests\Core\Access

Code

public function testCacheTags() : void {
    $verify = function (AccessResult $access, array $tags, array $contexts = [], $max_age = Cache::PERMANENT) {
        $this->assertFalse($access->isAllowed());
        $this->assertFalse($access->isForbidden());
        $this->assertTrue($access->isNeutral());
        $this->assertSame($max_age, $access->getCacheMaxAge());
        $this->assertEqualsCanonicalizing($contexts, $access->getCacheContexts());
        $this->assertEqualsCanonicalizing($tags, $access->getCacheTags());
    };
    $access = AccessResult::neutral()->addCacheTags([
        'foo:bar',
    ]);
    $verify($access, [
        'foo:bar',
    ]);
    // Verify resetting works.
    $access->resetCacheTags();
    $verify($access, []);
    // Verify idempotency.
    $access->addCacheTags([
        'foo:bar',
    ])
        ->addCacheTags([
        'foo:bar',
    ]);
    $verify($access, [
        'foo:bar',
    ]);
    // Verify same values in different call order yields the same result.
    $access->resetCacheTags()
        ->addCacheTags([
        'bar:baz',
    ])
        ->addCacheTags([
        'bar:qux',
    ])
        ->addCacheTags([
        'foo:bar',
    ])
        ->addCacheTags([
        'foo:baz',
    ]);
    $verify($access, [
        'bar:baz',
        'bar:qux',
        'foo:bar',
        'foo:baz',
    ]);
    $access->resetCacheTags()
        ->addCacheTags([
        'foo:bar',
    ])
        ->addCacheTags([
        'bar:qux',
    ])
        ->addCacheTags([
        'foo:baz',
    ])
        ->addCacheTags([
        'bar:baz',
    ]);
    $verify($access, [
        'bar:baz',
        'bar:qux',
        'foo:bar',
        'foo:baz',
    ]);
    // ::addCacheableDependency() convenience method.
    $node = $this->createMock('\\Drupal\\node\\NodeInterface');
    $node->expects($this->any())
        ->method('getCacheTags')
        ->willReturn([
        'node:20011988',
    ]);
    $node->expects($this->any())
        ->method('getCacheMaxAge')
        ->willReturn(600);
    $node->expects($this->any())
        ->method('getCacheContexts')
        ->willReturn([
        'user',
    ]);
    $tags = [
        'node:20011988',
    ];
    $a = AccessResult::neutral()->addCacheTags($tags);
    $verify($a, $tags);
    $b = AccessResult::neutral()->addCacheableDependency($node);
    $verify($b, $tags, [
        'user',
    ], 600);
    $non_cacheable_dependency = new \stdClass();
    $non_cacheable = AccessResult::neutral()->addCacheableDependency($non_cacheable_dependency);
    $verify($non_cacheable, [], [], 0);
}

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