View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Tests\Traits\Core\GeneratePermutationsTrait;
class NodeRevisionPermissionsTest extends NodeTestBase {
use GeneratePermutationsTrait;
protected $defaultTheme = 'stark';
protected $nodeRevisions = [];
protected $accounts = [];
protected $map = [
'view' => 'view all revisions',
'update' => 'revert all revisions',
'delete' => 'delete all revisions',
];
protected $typeMap = [
'view' => 'view page revisions',
'update' => 'revert page revisions',
'delete' => 'delete page revisions',
];
protected function setUp() : void {
parent::setUp();
$types = [
'page',
'article',
];
foreach ($types as $type) {
$nodes[$type] = $this
->drupalCreateNode([
'type' => $type,
]);
$this->nodeRevisions[$type][] = $nodes[$type];
for ($i = 0; $i < 3; $i++) {
$revision = clone $nodes[$type];
$revision
->setNewRevision();
$revision->revision_log = $this
->randomMachineName(32);
$revision
->save();
$this->nodeRevisions[$type][] = $revision;
}
}
}
public function testNodeRevisionAccessAnyType() {
foreach ($this->map as $op => $permission) {
$account = $this
->drupalCreateUser([
'access content',
'edit any page content',
'delete any page content',
$permission,
]);
$account->op = $op;
$this->accounts[] = $account;
}
$admin_account = $this
->drupalCreateUser([
'access content',
'administer nodes',
]);
$admin_account->is_admin = TRUE;
$this->accounts['admin'] = $admin_account;
$accounts['admin'] = $admin_account;
$normal_account = $this
->drupalCreateUser();
$normal_account->op = FALSE;
$this->accounts[] = $normal_account;
$accounts[] = $normal_account;
$revision = $this->nodeRevisions['page'][1];
$parameters = [
'op' => array_keys($this->map),
'account' => $this->accounts,
];
$permutations = $this
->generatePermutations($parameters);
$node_revision_access = \Drupal::service('access_check.node.revision');
$vids = \Drupal::entityQuery('node')
->allRevisions()
->accessCheck(FALSE)
->condition('nid', $revision
->id())
->execute();
foreach ($permutations as $case) {
if (!($revision
->isDefaultRevision() && (count($vids) == 1 || $case['op'] == 'update' || $case['op'] == 'delete'))) {
if (!empty($case['account']->is_admin) || $case['account']
->hasPermission($this->map[$case['op']])) {
$this
->assertTrue($node_revision_access
->checkAccess($revision, $case['account'], $case['op']), "{$this->map[$case['op']]} granted.");
}
else {
$this
->assertFalse($node_revision_access
->checkAccess($revision, $case['account'], $case['op']), "{$this->map[$case['op']]} not granted.");
}
}
}
$admin_account = $accounts['admin'];
$this
->assertFalse($node_revision_access
->checkAccess($revision, $admin_account, 'invalid-op'), 'NodeRevisionAccessCheck() returns FALSE with an invalid op.');
}
public function testNodeRevisionAccessPerType() {
foreach ($this->typeMap as $op => $permission) {
$account = $this
->drupalCreateUser([
'access content',
'edit any page content',
'delete any page content',
$permission,
]);
$account->op = $op;
$accounts[] = $account;
}
$parameters = [
'op' => array_keys($this->typeMap),
'account' => $accounts,
];
$revision = $this->nodeRevisions['page'][1];
$permutations = $this
->generatePermutations($parameters);
$node_revision_access = \Drupal::service('access_check.node.revision');
$vids = \Drupal::entityQuery('node')
->allRevisions()
->accessCheck(FALSE)
->condition('nid', $revision
->id())
->execute();
foreach ($permutations as $case) {
if (!($revision
->isDefaultRevision() && (count($vids) == 1 || $case['op'] == 'update' || $case['op'] == 'delete'))) {
if (!empty($case['account']->is_admin) || $case['account']
->hasPermission($this->typeMap[$case['op']])) {
$this
->assertTrue($node_revision_access
->checkAccess($revision, $case['account'], $case['op']), "{$this->typeMap[$case['op']]} granted.");
}
else {
$this
->assertFalse($node_revision_access
->checkAccess($revision, $case['account'], $case['op']), "{$this->typeMap[$case['op']]} not granted.");
}
}
}
$revision = $this->nodeRevisions['article'][1];
foreach ($permutations as $case) {
$this
->assertFalse($node_revision_access
->checkAccess($revision, $case['account'], $case['op']), "{$this->typeMap[$case['op']]} did not grant revision permission for articles.");
}
}
}