View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Entity;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\hook_event_dispatcher\Event\Entity\EntityAccessEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use Drupal\Tests\UnitTestCase;
class EntityAccessEventTest extends UnitTestCase {
private $manager;
public function setUp() {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
\Drupal::setContainer($builder);
}
public function testDeprecatedSetEntityMethod() {
$entity = $this
->createMock(EntityInterface::class);
$operation = 'test';
$account = $this
->createMock(AccountInterface::class);
$event = new EntityAccessEvent($entity, $operation, $account);
$otherEntity = $this
->createMock(EntityInterface::class);
$event
->setEntity($otherEntity);
self::assertEquals($otherEntity, $event
->getEntity());
}
public function testEntityAccessEventWithNoChanges() {
$entity = $this
->createMock(EntityInterface::class);
$operation = 'test';
$account = $this
->createMock(AccountInterface::class);
$hookAccessResult = hook_event_dispatcher_entity_access($entity, $operation, $account);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_ACCESS);
self::assertSame($entity, $event
->getEntity());
self::assertSame($operation, $event
->getOperation());
self::assertSame($account, $event
->getAccount());
self::assertTrue($hookAccessResult
->isNeutral());
self::assertFalse($hookAccessResult
->isAllowed());
self::assertFalse($hookAccessResult
->isForbidden());
}
public function testEntityAccessEventWithDeprecatedSetAccessResult() {
$accessResult = new AccessResultForbidden();
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_ACCESS => function (EntityAccessEvent $event) use ($accessResult) {
$event
->setAccessResult($accessResult);
},
]);
$entity = $this
->createMock(EntityInterface::class);
$operation = 'test';
$account = $this
->createMock(AccountInterface::class);
$hookAccessResult = hook_event_dispatcher_entity_access($entity, $operation, $account);
self::assertFalse($hookAccessResult
->isNeutral());
self::assertFalse($hookAccessResult
->isAllowed());
self::assertTrue($hookAccessResult
->isForbidden());
}
public function testEntityAccessEventNeutralResult() {
$accessResult = new AccessResultNeutral();
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_ACCESS => function (EntityAccessEvent $event) use ($accessResult) {
$event
->addAccessResult($accessResult);
},
]);
$entity = $this
->createMock(EntityInterface::class);
$operation = 'test';
$account = $this
->createMock(AccountInterface::class);
$hookAccessResult = hook_event_dispatcher_entity_access($entity, $operation, $account);
self::assertTrue($hookAccessResult
->isNeutral());
self::assertFalse($hookAccessResult
->isAllowed());
self::assertFalse($hookAccessResult
->isForbidden());
}
public function testEntityAccessEventAllowedResult() {
$accessResult = new AccessResultAllowed();
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_ACCESS => function (EntityAccessEvent $event) use ($accessResult) {
$event
->addAccessResult($accessResult);
},
]);
$entity = $this
->createMock(EntityInterface::class);
$operation = 'test';
$account = $this
->createMock(AccountInterface::class);
$hookAccessResult = hook_event_dispatcher_entity_access($entity, $operation, $account);
self::assertFalse($hookAccessResult
->isNeutral());
self::assertTrue($hookAccessResult
->isAllowed());
self::assertFalse($hookAccessResult
->isForbidden());
}
public function testEntityAccessEventForbiddenResult() {
$accessResult = new AccessResultForbidden();
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_ACCESS => function (EntityAccessEvent $event) use ($accessResult) {
$event
->addAccessResult($accessResult);
},
]);
$entity = $this
->createMock(EntityInterface::class);
$operation = 'test';
$account = $this
->createMock(AccountInterface::class);
$hookAccessResult = hook_event_dispatcher_entity_access($entity, $operation, $account);
self::assertFalse($hookAccessResult
->isNeutral());
self::assertFalse($hookAccessResult
->isAllowed());
self::assertTrue($hookAccessResult
->isForbidden());
}
public function testEntityAccessEventCombinedResults() {
$accessResults = [
new AccessResultNeutral(),
new AccessResultAllowed(),
new AccessResultForbidden(),
];
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_ACCESS => function (EntityAccessEvent $event) use ($accessResults) {
foreach ($accessResults as $accessResult) {
$event
->addAccessResult($accessResult);
}
},
]);
$entity = $this
->createMock(EntityInterface::class);
$operation = 'test';
$account = $this
->createMock(AccountInterface::class);
$hookAccessResult = hook_event_dispatcher_entity_access($entity, $operation, $account);
self::assertFalse($hookAccessResult
->isNeutral());
self::assertFalse($hookAccessResult
->isAllowed());
self::assertTrue($hookAccessResult
->isForbidden());
}
}