function AccessResultTest::testCacheTags

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheTags()
  2. 10 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheTags()
  3. 11.x 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 479

Class

AccessResultTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Access%21AccessResult.php/class/AccessResult/8.9.x" title="Value object for passing an access result with cacheability metadata." class="local">\Drupal\Core\Access\AccessResult</a> @group Access

Namespace

Drupal\Tests\Core\Access

Code

public function testCacheTags() {
    $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->assertSame($contexts, $access->getCacheContexts());
        $this->assertSame($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')
        ->will($this->returnValue([
        '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.