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\PrivateAccess;
use Drupal\Tests\UnitTestCase;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\UserInterface;
class PrivateAccessTest extends UnitTestCase {
protected $plugin;
public function setUp() {
parent::setUp();
$storage = $this
->prophesize(FieldStorageConfigInterface::class);
$this->plugin = new PrivateAccess([], 'private', [], $storage
->reveal());
}
public function testHasFieldAccess($operation, EntityInterface $entity, AccountInterface $account, $access) {
$this
->assertEquals($access, $this->plugin
->hasFieldAccess($operation, $entity, $account));
}
public function providerTestHasFieldAccess() {
$cases = [];
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission('access private fields')
->willReturn(TRUE);
$entity = $this
->prophesize(EntityInterface::class)
->reveal();
$cases[] = [
'view',
$entity,
$account
->reveal(),
TRUE,
];
$cases[] = [
'edit',
$entity,
$account
->reveal(),
TRUE,
];
$account = $this
->prophesize(AccountInterface::class)
->reveal();
$entity = $this
->prophesize(EntityInterface::class);
$entity
->isNew()
->willReturn(TRUE);
$cases[] = [
'view',
$entity
->reveal(),
$account,
TRUE,
];
$cases[] = [
'edit',
$entity
->reveal(),
$account,
TRUE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn(42);
$account
->hasPermission('access private fields')
->willReturn(FALSE);
$entity = $this
->prophesize(UserInterface::class);
$entity
->id()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
'view',
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$cases[] = [
'edit',
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn(27);
$account
->hasPermission('access private fields')
->willReturn(FALSE);
$entity = $this
->prophesize(UserInterface::class);
$entity
->id()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
'view',
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$cases[] = [
'edit',
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn(42);
$account
->hasPermission('access private fields')
->willReturn(FALSE);
$entity = $this
->prophesize(EntityInterface::class)
->willImplement(EntityOwnerInterface::class);
$entity
->getOwnerId()
->willReturn(42);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
'view',
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$cases[] = [
'edit',
$entity
->reveal(),
$account
->reveal(),
TRUE,
];
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn(42);
$account
->hasPermission('access private fields')
->willReturn(FALSE);
$entity = $this
->prophesize(EntityInterface::class)
->willImplement(EntityOwnerInterface::class);
$entity
->getOwnerId()
->willReturn(27);
$entity
->isNew()
->willReturn(FALSE);
$cases[] = [
'view',
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$cases[] = [
'edit',
$entity
->reveal(),
$account
->reveal(),
FALSE,
];
$account = $this
->prophesize(AccountInterface::class)
->reveal();
$entity = $this
->prophesize(EntityInterface::class)
->reveal();
$cases[] = [
'view',
$entity,
$account,
TRUE,
];
$cases[] = [
'edit',
$entity,
$account,
TRUE,
];
return $cases;
}
}