View source
<?php
namespace Drupal\Tests\field_permissions\Unit\Plugin\FieldPermissionType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field_permissions\Plugin\FieldPermissionType\CustomAccess;
use Drupal\Tests\UnitTestCase;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\UserInterface;
class CustomAccessTest extends UnitTestCase {
protected $plugin;
public function setUp() {
parent::setUp();
$storage = $this
->prophesize(FieldStorageConfigInterface::class);
$storage
->getName()
->willReturn('foo_field');
$this->plugin = new CustomAccess([], 'custom', [], $storage
->reveal());
}
public function testHasFieldAccess($operation, EntityInterface $entity, AccountInterface $account, $access) {
$this
->assertEquals($access, $this->plugin
->hasFieldAccess($operation, $entity, $account));
}
public function testInvalidOperation() {
$account = $this
->prophesize(AccountInterface::class);
$entity = $this
->prophesize(EntityInterface::class);
$this
->expectException(\AssertionError::class, 'The operation is either "edit" or "view", "bad operation" given instead.');
$this->plugin
->hasFieldAccess('bad operation', $entity
->reveal(), $account
->reveal());
}
public function providerTestHasFieldAccess() {
$cases = [];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission('create foo_field')
->willReturn(TRUE);
$entity = $this
->prophesize(EntityInterface::class);
$entity
->isNew()
->willReturn(TRUE);
$cases[] = [
'edit',
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission('create foo_field')
->willReturn(FALSE);
$cases[] = [
'edit',
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
foreach ([
'edit',
'view',
] as $operation) {
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(TRUE);
$entity = $this
->prophesize(EntityInterface::class);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(FALSE);
$entity = $this
->prophesize(EntityInterface::class);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(FALSE);
$account
->hasPermission($operation . ' own foo_field')
->willReturn(TRUE);
$account
->id()
->willReturn(42);
$entity = $this
->prophesize(UserInterface::class);
$entity
->id()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(FALSE);
$account
->hasPermission($operation . ' own foo_field')
->willReturn(FALSE);
$account
->id()
->willReturn(42);
$entity = $this
->prophesize(UserInterface::class);
$entity
->id()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(FALSE);
$account
->hasPermission($operation . ' own foo_field')
->willReturn(TRUE);
$account
->id()
->willReturn(42);
$entity = $this
->prophesize(UserInterface::class);
$entity
->id()
->willReturn(27);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(FALSE);
$account
->hasPermission($operation . ' own foo_field')
->willReturn(TRUE);
$account
->id()
->willReturn(42);
$entity = $this
->prophesize(EntityInterface::class);
$entity
->willImplement(EntityOwnerInterface::class);
$entity
->getOwnerId()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(FALSE);
$account
->hasPermission($operation . ' own foo_field')
->willReturn(FALSE);
$account
->id()
->willReturn(42);
$entity = $this
->prophesize(EntityInterface::class);
$entity
->willImplement(EntityOwnerInterface::class);
$entity
->getOwnerId()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission($operation . ' foo_field')
->willReturn(FALSE);
$account
->hasPermission($operation . ' own foo_field')
->willReturn(TRUE);
$account
->id()
->willReturn(27);
$entity = $this
->prophesize(EntityInterface::class);
$entity
->willImplement(EntityOwnerInterface::class);
$entity
->getOwnerId()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
$operation,
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
}
return $cases;
}
}