You are here

public function ContentModerationAccessTest::testAccessCacheability in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/tests/src/Kernel/ContentModerationAccessTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationAccessTest::testAccessCacheability()

Tests access cacheability.

File

core/modules/content_moderation/tests/src/Kernel/ContentModerationAccessTest.php, line 64

Class

ContentModerationAccessTest
Tests content moderation access.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testAccessCacheability() {
  $node = $this
    ->createNode([
    'type' => 'page',
  ]);

  /** @var \Drupal\user\RoleInterface $authenticated */
  $authenticated = Role::create([
    'id' => 'authenticated',
  ]);
  $authenticated
    ->grantPermission('access content');
  $authenticated
    ->grantPermission('edit any page content');
  $authenticated
    ->save();
  $account = new UserSession([
    'uid' => 2,
    'roles' => [
      'authenticated',
    ],
  ]);
  $result = $node
    ->access('update', $account, TRUE);
  $this
    ->assertFalse($result
    ->isAllowed());
  $this
    ->assertEqualsCanonicalizing([
    'user.permissions',
  ], $result
    ->getCacheContexts());
  $this
    ->assertEqualsCanonicalizing([
    'config:workflows.workflow.editorial',
    'node:' . $node
      ->id(),
  ], $result
    ->getCacheTags());
  $this
    ->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result
    ->getCacheMaxAge());
  $authenticated
    ->grantPermission('use editorial transition create_new_draft');
  $authenticated
    ->save();
  \Drupal::entityTypeManager()
    ->getAccessControlHandler('node')
    ->resetCache();
  $result = $node
    ->access('update', $account, TRUE);
  $this
    ->assertTrue($result
    ->isAllowed());
  $this
    ->assertEqualsCanonicalizing([
    'user.permissions',
  ], $result
    ->getCacheContexts());
  $this
    ->assertEqualsCanonicalizing([
    'config:workflows.workflow.editorial',
    'node:' . $node
      ->id(),
  ], $result
    ->getCacheTags());
  $this
    ->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result
    ->getCacheMaxAge());
}