You are here

public function AccessResultTest::testInheritCacheability in Drupal 9

Same name and namespace in other branches
  1. 8 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 535
Contains \Drupal\Tests\Core\Access\AccessResultTest.

Class

AccessResultTest
@coversDefaultClass \Drupal\Core\Access\AccessResult @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());
}