function ContentModerationAccessTest::testAccessCacheability

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/ContentModerationAccessTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationAccessTest::testAccessCacheability()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/ContentModerationAccessTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationAccessTest::testAccessCacheability()
  3. 11.x 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 65

Class

ContentModerationAccessTest
Tests content moderation access.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testAccessCacheability() : void {
  $node = $this->createNode([
    'type' => 'page',
  ]);
  /** @var \Drupal\user\RoleInterface $authenticated */
  $authenticated = Role::create([
    'id' => 'authenticated',
    'label' => '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());
}

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