View source
<?php
namespace Drupal\Tests\content_moderation\Functional;
use Drupal\node\Entity\NodeType;
class NodeAccessTest extends ModerationStateTestBase {
protected static $modules = [
'content_moderation',
'block',
'block_content',
'node',
'node_access_test',
];
protected $defaultTheme = 'stark';
protected $permissions = [
'administer workflows',
'access administration pages',
'administer content types',
'administer nodes',
'view latest version',
'view any unpublished content',
'access content overview',
'use editorial transition create_new_draft',
'use editorial transition publish',
'bypass node access',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this
->createContentTypeFromUi('Moderated content', 'moderated_content', FALSE);
$this
->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
node_access_test_add_field(NodeType::load('moderated_content'));
node_access_rebuild();
}
public function testPageAccess() {
\Drupal::state()
->set('node_access_test.private', TRUE);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/add/moderated_content');
$this
->assertSession()
->fieldExists('Published');
$this
->enableModerationThroughUi('moderated_content', 'editorial');
$this
->drupalGet('node/add/moderated_content');
$this
->assertSession()
->fieldNotExists('Published');
$this
->submitForm([
'title[0][value]' => 'moderated content',
'moderation_state[0][state]' => 'draft',
], 'Save');
$node = $this
->getNodeByTitle('moderated content');
if (!$node) {
$this
->fail('Test node was not saved correctly.');
}
$view_path = 'node/' . $node
->id();
$edit_path = 'node/' . $node
->id() . '/edit';
$latest_path = 'node/' . $node
->id() . '/latest';
$user = $this
->createUser([
'use editorial transition create_new_draft',
'view latest version',
'view any unpublished content',
]);
$this
->drupalLogin($user);
$this
->drupalGet($edit_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($latest_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($view_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($edit_path);
$this
->submitForm([
'moderation_state[0][state]' => 'published',
], 'Save');
$this
->drupalLogout();
$this
->drupalGet($edit_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($latest_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($view_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($edit_path);
$this
->submitForm([
'title[0][value]' => 'moderated content revised',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->drupalLogin($user);
$this
->drupalGet($edit_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($latest_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet($view_path);
$this
->assertSession()
->statusCodeEquals(200);
$user = $this
->createUser([
'use editorial transition create_new_draft',
]);
$this
->drupalLogin($user);
$this
->drupalGet($edit_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($latest_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($view_path);
$this
->assertSession()
->statusCodeEquals(200);
$node = $this
->createNode([
'type' => 'moderated_content',
'private' => TRUE,
'uid' => $this->adminUser
->id(),
]);
$user = $this
->createUser([
'use editorial transition publish',
]);
$this
->drupalLogin($user);
\Drupal::state()
->set('node_access_test.allow_uid', $user
->id());
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->submitForm([], 'Apply');
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node
->id());
$this
->assertEquals('published', $node->moderation_state->value);
}
}