diff --git a/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php b/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php index ea79c2d..5cd0f4c 100644 --- a/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php +++ b/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php @@ -1,6 +1,7 @@ createContentTypeFromUi('Moderated content', 'moderated_content', FALSE); $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content'); + // Add the private field to the node type. + node_access_test_add_field(NodeType::load('moderated_content')); + // Rebuild permissions because hook_node_grants() is implemented by the // node_access_test_empty module. node_access_rebuild(); @@ -58,6 +62,10 @@ protected function setUp() { * Verifies that a non-admin user can still access the appropriate pages. */ public function testPageAccess() { + // Initially disable access grant records in + // node_access_test_node_access_records(). + \Drupal::state()->set('node_access_test.private', TRUE); + $this->drupalLogin($this->adminUser); // Access the node form before moderation is enabled, the publication state @@ -149,6 +157,30 @@ public function testPageAccess() { $this->assertResponse(403); $this->drupalGet($view_path); $this->assertResponse(200); + + // Now create a private node that the user is not granted access to by the + // node grants, but is granted access via hook_node_access(). + // @see node_access_test_node_access + $node = $this->createNode([ + 'type' => 'moderated_content', + 'private' => TRUE, + 'uid' => $this->adminUser->id(), + ]); + $user = $this->createUser([ + 'use editorial transition publish', + ]); + $this->drupalLogin($user); + + // Grant access to the node via node_access_test_node_access(). + \Drupal::state()->set('node_access_test.allow_uid', $user->id()); + + $this->drupalGet($node->toUrl()); + $this->assertResponse(200); + + // Verify the moderation form is in place by publishing the node. + $this->drupalPostForm(NULL, [], t('Apply')); + $node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($node->id()); + $this->assertEquals('published', $node->moderation_state->value); } } diff --git a/core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php b/core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php new file mode 100644 index 0000000..6716ca3 --- /dev/null +++ b/core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php @@ -0,0 +1,88 @@ +installEntitySchema('content_moderation_state'); + $this->installEntitySchema('node'); + $this->installEntitySchema('user'); + $this->installEntitySchema('workflow'); + $this->installConfig(['content_moderation', 'filter']); + $this->installSchema('system', ['sequences']); + $this->installSchema('node', ['node_access']); + + // Add a moderated node type. + $node_type = NodeType::create([ + 'type' => 'page', + 'label' => 'Page', + ]); + $node_type->save(); + $workflow = Workflow::load('editorial'); + $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page'); + $workflow->save(); + + $this->moderationInformation = \Drupal::service('content_moderation.moderation_information'); + } + + /** + * Tests for moderation information methods with node access. + */ + public function testModerationInformation() { + // Create an admin user. + $user = $this->createUser([], NULL, TRUE); + \Drupal::currentUser()->setAccount($user); + + // Create a node. + $node = $this->createNode(['type' => 'page']); + $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id())); + $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id())); + + // Create a non-admin user. + $user = $this->createUser(); + \Drupal::currentUser()->setAccount($user); + $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id())); + $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id())); + } + +} diff --git a/core/modules/node/tests/modules/node_access_test/node_access_test.module b/core/modules/node/tests/modules/node_access_test/node_access_test.module index 40baeb5..731ae6d 100644 --- a/core/modules/node/tests/modules/node_access_test/node_access_test.module +++ b/core/modules/node/tests/modules/node_access_test/node_access_test.module @@ -79,7 +79,7 @@ function node_access_test_node_grants($account, $op) { function node_access_test_node_access_records(NodeInterface $node) { $grants = []; // For NodeAccessBaseTableTestCase, only set records for private nodes. - if (!\Drupal::state()->get('node_access_test.private') || $node->private->value) { + if (!\Drupal::state()->get('node_access_test.private') || (isset($node->private) && ($node->private->value))) { // Groups 8888 and 8889 for the node_access_test realm both receive a view // grant for all controlled nodes. See node_access_test_node_grants(). $grants[] = [ @@ -152,6 +152,12 @@ function node_access_test_node_access(NodeInterface $node, $op, AccountInterface // Make all Catalan content secret. return AccessResult::forbidden()->setCacheMaxAge(0); } + + // Grant access if a specific user is specified. + if (\Drupal::state()->get('node_access_test.allow_uid') == $account->id()) { + return AccessResult::allowed(); + } + // No opinion. return AccessResult::neutral()->setCacheMaxAge(0); }