ContentModerationAccessTest.php 2.9 KB

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