NodeAccessTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Functional;
  3. use Drupal\node\Entity\NodeType;
  4. /**
  5. * Tests permission access control around nodes.
  6. *
  7. * @group content_moderation
  8. */
  9. class NodeAccessTest extends ModerationStateTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = [
  16. 'content_moderation',
  17. 'block',
  18. 'block_content',
  19. 'node',
  20. 'node_access_test',
  21. ];
  22. /**
  23. * Permissions to grant admin user.
  24. *
  25. * @var array
  26. */
  27. protected $permissions = [
  28. 'administer workflows',
  29. 'access administration pages',
  30. 'administer content types',
  31. 'administer nodes',
  32. 'view latest version',
  33. 'view any unpublished content',
  34. 'access content overview',
  35. 'use editorial transition create_new_draft',
  36. 'use editorial transition publish',
  37. 'bypass node access',
  38. ];
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->drupalLogin($this->adminUser);
  45. $this->createContentTypeFromUi('Moderated content', 'moderated_content', FALSE);
  46. $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
  47. // Add the private field to the node type.
  48. node_access_test_add_field(NodeType::load('moderated_content'));
  49. // Rebuild permissions because hook_node_grants() is implemented by the
  50. // node_access_test_empty module.
  51. node_access_rebuild();
  52. }
  53. /**
  54. * Verifies that a non-admin user can still access the appropriate pages.
  55. */
  56. public function testPageAccess() {
  57. // Initially disable access grant records in
  58. // node_access_test_node_access_records().
  59. \Drupal::state()->set('node_access_test.private', TRUE);
  60. $this->drupalLogin($this->adminUser);
  61. // Access the node form before moderation is enabled, the publication state
  62. // should now be visible.
  63. $this->drupalGet('node/add/moderated_content');
  64. $this->assertSession()->fieldExists('Published');
  65. // Now enable the workflow.
  66. $this->enableModerationThroughUi('moderated_content', 'editorial');
  67. // Access that the status field is no longer visible.
  68. $this->drupalGet('node/add/moderated_content');
  69. $this->assertSession()->fieldNotExists('Published');
  70. // Create a node to test with.
  71. $this->drupalPostForm(NULL, [
  72. 'title[0][value]' => 'moderated content',
  73. 'moderation_state[0][state]' => 'draft',
  74. ], t('Save'));
  75. $node = $this->getNodeByTitle('moderated content');
  76. if (!$node) {
  77. $this->fail('Test node was not saved correctly.');
  78. }
  79. $view_path = 'node/' . $node->id();
  80. $edit_path = 'node/' . $node->id() . '/edit';
  81. $latest_path = 'node/' . $node->id() . '/latest';
  82. // Now make a new user and verify that the new user's access is correct.
  83. $user = $this->createUser([
  84. 'use editorial transition create_new_draft',
  85. 'view latest version',
  86. 'view any unpublished content',
  87. ]);
  88. $this->drupalLogin($user);
  89. $this->drupalGet($edit_path);
  90. $this->assertResponse(403);
  91. $this->drupalGet($latest_path);
  92. $this->assertResponse(403);
  93. $this->drupalGet($view_path);
  94. $this->assertResponse(200);
  95. // Publish the node.
  96. $this->drupalLogin($this->adminUser);
  97. $this->drupalPostForm($edit_path, [
  98. 'moderation_state[0][state]' => 'published',
  99. ], t('Save'));
  100. // Ensure access works correctly for anonymous users.
  101. $this->drupalLogout();
  102. $this->drupalGet($edit_path);
  103. $this->assertResponse(403);
  104. $this->drupalGet($latest_path);
  105. $this->assertResponse(403);
  106. $this->drupalGet($view_path);
  107. $this->assertResponse(200);
  108. // Create a pending revision for the 'Latest revision' tab.
  109. $this->drupalLogin($this->adminUser);
  110. $this->drupalPostForm($edit_path, [
  111. 'title[0][value]' => 'moderated content revised',
  112. 'moderation_state[0][state]' => 'draft',
  113. ], t('Save'));
  114. $this->drupalLogin($user);
  115. $this->drupalGet($edit_path);
  116. $this->assertResponse(403);
  117. $this->drupalGet($latest_path);
  118. $this->assertResponse(200);
  119. $this->drupalGet($view_path);
  120. $this->assertResponse(200);
  121. // Now make another user, who should not be able to see pending revisions.
  122. $user = $this->createUser([
  123. 'use editorial transition create_new_draft',
  124. ]);
  125. $this->drupalLogin($user);
  126. $this->drupalGet($edit_path);
  127. $this->assertResponse(403);
  128. $this->drupalGet($latest_path);
  129. $this->assertResponse(403);
  130. $this->drupalGet($view_path);
  131. $this->assertResponse(200);
  132. // Now create a private node that the user is not granted access to by the
  133. // node grants, but is granted access via hook_node_access().
  134. // @see node_access_test_node_access
  135. $node = $this->createNode([
  136. 'type' => 'moderated_content',
  137. 'private' => TRUE,
  138. 'uid' => $this->adminUser->id(),
  139. ]);
  140. $user = $this->createUser([
  141. 'use editorial transition publish',
  142. ]);
  143. $this->drupalLogin($user);
  144. // Grant access to the node via node_access_test_node_access().
  145. \Drupal::state()->set('node_access_test.allow_uid', $user->id());
  146. $this->drupalGet($node->toUrl());
  147. $this->assertResponse(200);
  148. // Verify the moderation form is in place by publishing the node.
  149. $this->drupalPostForm(NULL, [], t('Apply'));
  150. $node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($node->id());
  151. $this->assertEquals('published', $node->moderation_state->value);
  152. }
  153. }