View source
<?php
namespace Drupal\Tests\field_permissions\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_permissions\Plugin\FieldPermissionTypeInterface;
use Drupal\user\UserInterface;
class FieldPermissionsUserTest extends FieldPermissionsTestBase {
protected $entityDisplayRepository;
public function setUp() {
parent::setUp();
$this->fieldName = mb_strtolower($this
->randomMachineName());
$this->fieldText = str_replace('@', '', $this
->randomString(42));
$this->webUserRole
->grantPermission('access user profiles')
->grantPermission('administer users')
->save();
$this->entityDisplayRepository = $this->container
->get('entity_display.repository');
$this
->addUserField();
}
public function testUserFieldPermissions() {
$this
->drupalLogin($this->adminUser);
$this
->checkUserFieldEdit($this->adminUser);
$this
->drupalLogout();
$this
->drupalLogin($this->limitedUser);
$this
->assertUserFieldAccess($this->adminUser);
$this
->drupalLogout();
$this
->checkPrivateField();
$this
->checkUserViewEditOwnField();
$this
->checkUserViewEditField();
}
protected function addUserField() {
FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'user',
'type' => 'text',
])
->save();
FieldConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'user',
'label' => 'Textfield',
'bundle' => 'user',
])
->save();
$this->entityDisplayRepository
->getFormDisplay('user', 'user', 'default')
->setComponent($this->fieldName)
->save();
$this->entityDisplayRepository
->getFormDisplay('user', 'user', 'register')
->setComponent($this->fieldName)
->save();
$this->entityDisplayRepository
->getViewDisplay('user', 'user')
->setComponent($this->fieldName)
->save();
}
protected function checkUserFieldEdit(UserInterface $account) {
$this
->drupalGet($account
->toUrl('edit-form'));
$this
->assertSession()
->pageTextContains('Textfield');
$edit = [];
$edit[$this->fieldName . '[0][value]'] = $this->fieldText;
$this
->submitForm($edit, 'Save');
$this
->drupalGet($account
->toUrl());
$this
->assertSession()
->assertEscaped($this->fieldText);
}
protected function assertUserFieldAccess(UserInterface $account) {
$this
->drupalGet($account
->toUrl());
$this
->assertSession()
->pageTextContains('Textfield');
}
protected function assertUserFieldNoAccess(UserInterface $account) {
$this
->drupalGet($account
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Textfield');
}
protected function assertUserEditFieldAccess(UserInterface $account) {
$this
->drupalGet($account
->toUrl('edit-form'));
$this
->assertSession()
->pageTextContains('Textfield');
}
protected function assertUserEditFieldNoAccess(UserInterface $account) {
$this
->drupalGet($account
->toUrl('edit-form'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Textfield');
}
private function setUserFieldPermission($perm, array $custom_permission = []) {
$current_user = $this->loggedInUser;
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/people/accounts/fields/user.user.' . $this->fieldName);
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 checkUserViewEditOwnField() {
$permission = [];
$this
->drupalLogin($this->webUser);
$perm = [
'view own ' . $this->fieldName,
];
$permission = $this
->grantCustomPermissions($this->limitUserRole, $perm, $permission);
$this
->setUserFieldPermission(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission);
$this
->assertUserFieldNoAccess($this->limitedUser);
$this
->assertUserEditFieldNoAccess($this->limitedUser);
$this
->assertUserEditFieldNoAccess($this->adminUser);
$this
->assertUserFieldNoAccess($this->adminUser);
$this
->drupalLogout();
$this
->drupalLogin($this->limitedUser);
$this
->assertUserFieldAccess($this->limitedUser);
$this
->assertUserFieldNoAccess($this->adminUser);
$this
->assertUserEditFieldNoAccess($this->limitedUser);
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$permission = $this
->grantCustomPermissions($this->limitUserRole, [
'edit own ' . $this->fieldName,
], $permission);
$this
->setUserFieldPermission(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission);
$this
->assertUserEditFieldNoAccess($this->adminUser);
$this
->assertUserEditFieldNoAccess($this->limitedUser);
$this
->drupalLogout();
$this
->drupalLogin($this->limitedUser);
$this
->assertUserEditFieldAccess($this->limitedUser);
$this
->drupalLogout();
}
protected function checkUserViewEditField() {
$permission = [];
$this
->drupalLogin($this->webUser);
$perm = [
'view ' . $this->fieldName,
];
$permission = $this
->grantCustomPermissions($this->webUserRole, $perm, $permission);
$this
->setUserFieldPermission(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission);
$this
->assertUserFieldAccess($this->limitedUser);
$perm = [
'edit ' . $this->fieldName,
];
$permission = $this
->grantCustomPermissions($this->webUserRole, $perm, $permission);
$this
->setUserFieldPermission(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission);
$this
->assertUserEditFieldAccess($this->limitedUser);
$this
->drupalLogout();
}
protected function checkPrivateField() {
$this
->drupalLogin($this->webUser);
$this
->setUserFieldPermission(FieldPermissionTypeInterface::ACCESS_PRIVATE);
$this
->drupalLogout();
$this
->drupalLogin($this->limitedUser);
$this
->assertUserFieldNoAccess($this->adminUser);
$this
->checkUserFieldEdit($this->limitedUser);
$this
->assertUserFieldAccess($this->limitedUser);
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this
->assertUserFieldNoAccess($this->limitedUser);
$this
->assertUserEditFieldNoAccess($this->limitedUser);
$this
->drupalLogout();
}
}