1<?php 2 3namespace Drupal\Tests\content_moderation\Kernel; 4 5use Drupal\Core\Cache\CacheBackendInterface; 6use Drupal\Core\Session\UserSession; 7use Drupal\KernelTests\KernelTestBase; 8use Drupal\node\Entity\NodeType; 9use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait; 10use Drupal\Tests\node\Traits\NodeCreationTrait; 11use Drupal\Tests\user\Traits\UserCreationTrait; 12use Drupal\user\Entity\Role; 13 14/** 15 * Tests content moderation access. 16 * 17 * @group content_moderation 18 */ 19class ContentModerationAccessTest extends KernelTestBase { 20 21 use NodeCreationTrait; 22 use UserCreationTrait; 23 use ContentModerationTestTrait; 24 25 /** 26 * {@inheritdoc} 27 */ 28 public static $modules = [ 29 'content_moderation', 30 'filter', 31 'node', 32 'system', 33 'user', 34 'workflows', 35 ]; 36 37 /** 38 * {@inheritdoc} 39 */ 40 protected function setUp() { 41 parent::setUp(); 42 43 $this->installEntitySchema('content_moderation_state'); 44 $this->installEntitySchema('node'); 45 $this->installEntitySchema('user'); 46 $this->installConfig(['content_moderation', 'filter']); 47 $this->installSchema('system', ['sequences']); 48 $this->installSchema('node', ['node_access']); 49 50 // Add a moderated node type. 51 $node_type = NodeType::create([ 52 'type' => 'page', 53 'label' => 'Page', 54 ]); 55 $node_type->save(); 56 $workflow = $this->createEditorialWorkflow(); 57 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page'); 58 $workflow->save(); 59 } 60 61 /** 62 * Tests access cacheability. 63 */ 64 public function testAccessCacheability() { 65 $node = $this->createNode(['type' => 'page']); 66 67 /** @var \Drupal\user\RoleInterface $authenticated */ 68 $authenticated = Role::create([ 69 'id' => 'authenticated', 70 ]); 71 $authenticated->grantPermission('access content'); 72 $authenticated->grantPermission('edit any page content'); 73 $authenticated->save(); 74 75 $account = new UserSession([ 76 'uid' => 2, 77 'roles' => ['authenticated'], 78 ]); 79 80 $result = $node->access('update', $account, TRUE); 81 $this->assertFalse($result->isAllowed()); 82 $this->assertEquals(['user.permissions'], $result->getCacheContexts()); 83 $this->assertEquals(['config:workflows.workflow.editorial', 'node:' . $node->id()], $result->getCacheTags()); 84 $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result->getCacheMaxAge()); 85 86 $authenticated->grantPermission('use editorial transition create_new_draft'); 87 $authenticated->save(); 88 89 \Drupal::entityTypeManager()->getAccessControlHandler('node')->resetCache(); 90 $result = $node->access('update', $account, TRUE); 91 $this->assertTrue($result->isAllowed()); 92 $this->assertEquals(['user.permissions'], $result->getCacheContexts()); 93 $this->assertEquals(['config:workflows.workflow.editorial', 'node:' . $node->id()], $result->getCacheTags()); 94 $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result->getCacheMaxAge()); 95 } 96 97} 98