FieldPermissionsNodeTest.php in Field Permissions 8
File
tests/src/Functional/FieldPermissionsNodeTest.php
View source
<?php
namespace Drupal\Tests\field_permissions\Functional;
use Drupal\Core\Url;
use Drupal\field_permissions\Plugin\FieldPermissionTypeInterface;
class FieldPermissionsNodeTest extends FieldPermissionsTestBase {
public function setUp() {
parent::setUp();
$this->webUserRole
->grantPermission('administer content types')
->grantPermission('administer node fields')
->save();
}
public function testNodeFieldPermissions() {
$this
->checkPermissionPage();
$this
->checkFieldPermissionConfigurationEdit();
$this
->checkInitAddNode();
$this
->checkChengeToPrivateField();
$this
->checkViewOwnField();
$this
->checkViewEditOwnField();
$this
->checkViewEditAllField();
}
protected function setNodeFieldPermissions($perm, array $custom_permission = []) {
$current_user = $this->loggedInUser;
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/types/manage/article/fields/node.article.body');
if ($perm === FieldPermissionTypeInterface::ACCESS_PUBLIC || $perm === FieldPermissionTypeInterface::ACCESS_PRIVATE) {
$edit = [
'type' => $perm,
];
$this
->submitForm($edit, 'Save settings');
}
elseif ($perm === FieldPermissionTypeInterface::ACCESS_CUSTOM && !empty($custom_permission)) {
$custom_permission['type'] = $perm;
$this
->submitForm($custom_permission, 'Save settings');
}
if ($current_user) {
$this
->drupalLogin($current_user);
}
}
protected function addNode() {
$this->node = $this
->drupalCreateNode([
'type' => 'article',
'uid' => $this->limitedUser
->id(),
]);
$this
->drupalGet('node/' . $this->node
->id());
$node_body = $this->node
->getFields()['body']
->getValue();
$this
->assertSession()
->responseContains($node_body[0]['value']);
}
protected function addNodeUi() {
$this
->drupalGet('node/add/article');
$this
->assertSession()
->pageTextContains('Body');
$edit = [];
$node_name = $this
->randomMachineName();
$edit['body[0][value]'] = $this
->randomString();
$edit['title[0][value]'] = $node_name;
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains(t('Article @name has been created.', [
'@name' => $node_name,
]));
}
protected function assertNodeFieldVisible() {
$field_value = $this->node
->getFields()['body']
->getValue();
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextContains($field_value[0]['value']);
}
protected function assertNodeFieldHidden() {
$field_value = $this->node
->getFields()['body']
->getValue();
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains($field_value[0]['value']);
}
protected function assertNodeFieldEditAccess() {
$this
->drupalGet('node/' . $this->node
->id() . '/edit');
$this
->assertSession()
->pageTextContains('Title');
$this
->assertSession()
->pageTextContains('Body');
}
protected function assertNodeFieldEditNoAccess() {
$this
->drupalGet('node/' . $this->node
->id() . '/edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Title');
$this
->assertSession()
->pageTextNotContains('Body');
}
protected function checkFieldPermissionConfigurationEdit() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('admin/structure/types/manage/article/fields/node.article.body');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Field visibility and permissions');
$this->webUserRole
->grantPermission('administer field permissions')
->save();
$this
->drupalGet('admin/structure/types/manage/article/fields/node.article.body');
$this
->assertSession()
->pageTextContains('Field visibility and permissions');
$this
->drupalLogout();
}
protected function checkPermissionPage() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet(Url::fromRoute('user.admin_permissions'));
$this
->assertSession()
->pageTextContains('Access other users private fields');
$this
->assertSession()
->pageTextContains('Administer field permissions');
$this
->drupalLogout();
}
protected function checkInitAddNode() {
$this
->drupalLogin($this->limitedUser);
$this
->addNodeUi();
$this
->addNode();
$this
->drupalLogout();
}
protected function checkChengeToPrivateField() {
$this
->drupalLogin($this->webUser);
$this
->assertNodeFieldVisible();
$this->webUserRole
->grantPermission('administer field permissions')
->save();
$this
->setNodeFieldPermissions(FieldPermissionTypeInterface::ACCESS_PRIVATE);
$this
->assertNodeFieldHidden();
$this->webUserRole
->grantPermission('access private fields')
->save();
$this
->assertNodeFieldVisible();
$this
->drupalLogout();
}
protected function checkViewOwnField() {
$permission = [];
$permission = $this
->grantCustomPermissions($this->limitUserRole, [
'view own body',
], $permission);
$this
->setNodeFieldPermissions(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission);
$this
->drupalLogin($this->limitedUser);
$this
->assertNodeFieldVisible();
$this
->assertNodeFieldEditNoAccess();
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this
->assertNodeFieldHidden();
$this
->assertNodeFieldEditNoAccess();
$this
->drupalLogout();
}
protected function checkViewEditOwnField() {
$permission = [];
$permission = $this
->grantCustomPermissions($this->limitUserRole, [
'view own body',
'edit own body',
], $permission);
$this
->setNodeFieldPermissions(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission);
$this
->drupalLogin($this->limitedUser);
$this
->assertNodeFieldVisible();
$this
->assertNodeFieldEditAccess();
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this
->assertNodeFieldHidden();
$this
->assertNodeFieldEditNoAccess();
$this
->drupalLogout();
}
protected function checkViewEditAllField() {
$this
->drupalLogin($this->webUser);
$this
->assertNodeFieldHidden();
$this
->assertNodeFieldEditNoAccess();
$this
->drupalLogout();
$permission = [];
$permission = $this
->grantCustomPermissions($this->webUserRole, [
'view body',
'edit body',
], $permission);
$this
->setNodeFieldPermissions(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission);
$this
->drupalLogin($this->webUser);
$this
->assertNodeFieldVisible();
$this
->assertNodeFieldEditAccess();
$this
->drupalLogout();
}
}