View source
<?php
namespace Drupal\Tests\workbench_moderation_state_access\Functional;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
class StateAccessTest extends BrowserTestBase {
public static $modules = [
'workbench_moderation_state_access',
'node',
'options',
'user',
'system',
];
public function testEditPermissions() {
$node_type_id = 'test';
$node_type = $this
->createNodeType('Test', $node_type_id);
$permissions = [
'access content',
'edit content in the draft state',
'edit any ' . $node_type_id . ' content',
'view all revisions',
'view moderation states',
];
$author = $this
->drupalCreateUser($permissions);
$permissions = [
'access content',
'edit content in the needs_review state',
'edit any ' . $node_type_id . ' content',
'view all revisions',
'view moderation states',
];
$approver = $this
->drupalCreateUser($permissions);
$draft_node = Node::create([
'type' => $node_type_id,
'title' => 'Draft node',
'uid' => $author
->id(),
]);
$draft_node->moderation_state->target_id = 'draft';
$draft_node
->save();
$review_node = Node::create([
'type' => $node_type_id,
'title' => 'Review node',
'uid' => $approver
->id(),
]);
$review_node->moderation_state->target_id = 'needs_review';
$review_node
->save();
$access_denied = 'Access denied';
$this
->drupalLogin($author);
$this
->drupalGet('node/' . $draft_node
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertFalse($page
->hasContent($access_denied));
$this
->drupalGet('node/' . $review_node
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->hasContent($access_denied));
$this
->drupalLogin($approver);
$this
->drupalGet('node/' . $draft_node
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->hasContent($access_denied));
$this
->drupalGet('node/' . $review_node
->id() . '/edit');
$page = $this
->getSession()
->getPage();
$this
->assertFalse($page
->hasContent($access_denied));
}
protected function createNodeType($label, $machine_name) {
$node_type = NodeType::create([
'type' => $machine_name,
'label' => $label,
]);
$node_type
->setThirdPartySetting('workbench_moderation', 'enabled', TRUE);
$node_type
->save();
return $node_type;
}
}