function AccessResultTest::testInheritCacheability

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

@covers ::inheritCacheability

File

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

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 testInheritCacheability() {
    // andIf(); 1st has defaults, 2nd has custom tags, contexts and max-age.
    $access = AccessResult::allowed();
    $other = AccessResult::allowed()->setCacheMaxAge(1500)
        ->cachePerPermissions()
        ->addCacheTags([
        'node:20011988',
    ]);
    $this->assertInstanceOf(AccessResult::class, $access->inheritCacheability($other));
    $this->assertSame([
        'user.permissions',
    ], $access->getCacheContexts());
    $this->assertSame([
        'node:20011988',
    ], $access->getCacheTags());
    $this->assertSame(1500, $access->getCacheMaxAge());
    // andIf(); 1st has custom tags, max-age, 2nd has custom contexts and max-age.
    $access = AccessResult::allowed()->cachePerUser()
        ->setCacheMaxAge(43200);
    $other = AccessResult::forbidden()->addCacheTags([
        'node:14031991',
    ])
        ->setCacheMaxAge(86400);
    $this->assertInstanceOf(AccessResult::class, $access->inheritCacheability($other));
    $this->assertSame([
        'user',
    ], $access->getCacheContexts());
    $this->assertSame([
        'node:14031991',
    ], $access->getCacheTags());
    $this->assertSame(43200, $access->getCacheMaxAge());
}

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