View source
<?php
namespace Drupal\Tests\field_permissions\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_permissions\Plugin\FieldPermissionTypeInterface;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\views\Entity\View;
use Drupal\views\Views;
class ViewsFieldAccessTest extends ViewsKernelTestBase {
protected $userWithAccess;
protected $userWithoutAccess;
protected $roleWithAccess;
protected $roleWithoutAccess;
public static $modules = [
'field_permissions',
'entity_test',
'text',
'field',
'filter',
];
protected $fieldStorage;
protected $field;
protected $entity;
public function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('user');
$this
->installConfig('filter');
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'test_field',
'type' => 'text',
'entity_type' => 'entity_test',
]);
$this->fieldStorage
->setThirdPartySetting('field_permissions', 'permission_type', FieldPermissionTypeInterface::ACCESS_PUBLIC);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_name' => 'test_field',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$this->field
->save();
$role_with_access = Role::create([
'id' => 'with_access',
'permissions' => [
'view test entity',
],
]);
$role_with_access
->save();
$this->roleWithAccess = $role_with_access;
$role_without_access = Role::create([
'id' => 'without_access',
'permissions' => [
'view test entity',
],
]);
$role_without_access
->save();
$this->roleWithoutAccess = $role_without_access;
$this->userWithAccess = User::create([
'name' => $this
->randomMachineName(),
'roles' => [
$role_with_access
->id(),
],
]);
$this->userWithAccess
->save();
$this->userWithoutAccess = User::create([
'name' => $this
->randomMachineName(),
'roles' => [
$role_without_access
->id(),
],
]);
$this->userWithoutAccess
->save();
$this->entity = EntityTest::create([
$this->fieldStorage
->getName() => [
'value' => 'Test value',
'format' => filter_default_format(),
],
]);
$this->entity
->save();
}
public function testPublicPermissions() {
$this
->assertFieldAccess();
}
public function testCustomPermissions() {
$this->fieldStorage
->setThirdPartySetting('field_permissions', 'permission_type', FieldPermissionTypeInterface::ACCESS_CUSTOM)
->save();
$this->roleWithAccess
->grantPermission('view ' . $this->fieldStorage
->getName())
->save();
$this
->assertFieldAccess();
}
public function testPrivatePermissions() {
$this->fieldStorage
->setThirdPartySetting('field_permissions', 'permission_type', FieldPermissionTypeInterface::ACCESS_PRIVATE)
->save();
$this->roleWithAccess
->grantPermission('access private fields')
->save();
$this
->assertFieldAccess();
$this->roleWithAccess
->revokePermission('access private fields')
->save();
$this->entity
->setOwner($this->userWithAccess)
->save();
$this
->assertFieldAccess();
}
protected function assertFieldAccess() {
$entity_type = $this->container
->get('entity_type.manager')
->getDefinition('entity_test');
$view_id = $this
->randomMachineName();
$data_table = $entity_type
->getDataTable();
$base_table = $data_table ?: $entity_type
->getBaseTable();
$field_name = $this->fieldStorage
->getName();
$field_content = $this->entity->{$field_name}->value;
$entity = View::create([
'id' => $view_id,
'base_table' => $base_table,
'display' => [
'default' => [
'display_plugin' => 'default',
'id' => 'default',
'display_options' => [
'fields' => [
$field_name => [
'table' => $base_table . '__' . $field_name,
'field' => $field_name,
'id' => $field_name,
'plugin_id' => 'field',
'type' => 'text_default',
],
],
],
],
],
]);
$entity
->save();
$account_switcher = $this->container
->get('account_switcher');
$renderer = $this->container
->get('renderer');
$account_switcher
->switchTo($this->userWithAccess);
$executable = Views::getView($view_id);
$build = $executable
->preview();
$this
->setRawContent($renderer
->renderRoot($build));
$this
->assertText($field_content);
$this
->assertArrayHasKey($field_name, $executable->field);
$account_switcher
->switchTo($this->userWithoutAccess);
$executable = Views::getView($view_id);
$build = $executable
->preview();
$this
->setRawContent($renderer
->renderRoot($build));
if ($this->fieldStorage
->getThirdPartySetting('field_permissions', 'permission_type') === FieldPermissionTypeInterface::ACCESS_PUBLIC) {
$this
->assertText($field_content);
}
else {
$this
->assertNoText($field_content);
}
}
}