View source
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class NodeFieldAccessTest extends EntityKernelTestBase {
protected static $modules = [
'node',
];
protected $administrativeFields = [
'status',
'promote',
'sticky',
'created',
'uid',
];
protected $readOnlyFields = [
'changed',
'revision_uid',
'revision_timestamp',
];
public function testAccessToAdministrativeFields() {
$page = NodeType::create([
'type' => 'page',
'new_revision' => FALSE,
]);
$page
->save();
$article = NodeType::create([
'type' => 'article',
'new_revision' => TRUE,
]);
$article
->save();
$content_admin_user = $this
->createUser([
'uid' => 2,
], [
'administer nodes',
]);
$page_creator_user = $this
->createUser([], [
'create page content',
'edit own page content',
'delete own page content',
]);
$page_manager_user = $this
->createUser([], [
'create page content',
'edit any page content',
'delete any page content',
]);
$page_unrelated_user = $this
->createUser([], [
'access content',
]);
$test_users = [
$content_admin_user,
$page_creator_user,
$page_manager_user,
$page_unrelated_user,
];
$node1 = Node::create([
'title' => $this
->randomMachineName(8),
'uid' => $page_creator_user
->id(),
'type' => 'page',
]);
$node2 = Node::create([
'title' => $this
->randomMachineName(8),
'uid' => $page_manager_user
->id(),
'type' => 'article',
]);
$node3 = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
]);
foreach ($this->administrativeFields as $field) {
foreach ($test_users as $account) {
$may_view = $node1->{$field}
->access('view', $account);
$this
->assertTrue($may_view, new FormattableMarkup('Any user may view the field @name.', [
'@name' => $field,
]));
}
$may_update = $node1->{$field}
->access('edit', $page_creator_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit own page content" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $node2->{$field}
->access('edit', $page_creator_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit own page content" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $node2->{$field}
->access('edit', $page_manager_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit any page content" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $node1->{$field}
->access('edit', $page_manager_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit any page content" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $node2->{$field}
->access('edit', $page_unrelated_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users not having permission "edit any page content" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $node1->{$field}
->access('edit', $content_admin_user) && $node3->status
->access('edit', $content_admin_user);
$this
->assertTrue($may_update, new FormattableMarkup('Users with permission "administer nodes" may edit @name fields on all nodes.', [
'@name' => $field,
]));
}
foreach ($this->readOnlyFields as $field) {
foreach ($test_users as $account) {
$may_view = $node1->{$field}
->access('view', $account);
$this
->assertTrue($may_view, new FormattableMarkup('Any user may view the field @name.', [
'@name' => $field,
]));
}
foreach ($test_users as $account) {
$may_view = $node1->{$field}
->access('edit', $account);
$this
->assertFalse($may_view, new FormattableMarkup('No user is not allowed to edit the field @name.', [
'@name' => $field,
]));
}
}
$may_update = $node1->revision_log
->access('edit', $content_admin_user);
$this
->assertTrue($may_update, 'A user with permission "administer nodes" can edit the revision_log field when revisions are disabled.');
$may_update = $node1->revision_log
->access('edit', $page_creator_user);
$this
->assertFalse($may_update, 'A user without permission "administer nodes" can not edit the revision_log field when revisions are disabled.');
$may_update = $node2->revision_log
->access('edit', $content_admin_user);
$this
->assertTrue($may_update, 'A user with permission "administer nodes" can edit the revision_log field when revisions are enabled.');
$may_update = $node2->revision_log
->access('edit', $page_creator_user);
$this
->assertTrue($may_update, 'A user without permission "administer nodes" can edit the revision_log field when revisions are enabled.');
}
}