EntityFieldAccessEventTest.php in Hook Event Dispatcher 8
File
tests/src/Unit/EntityField/EntityFieldAccessEventTest.php
View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\EntityField;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\hook_event_dispatcher\Event\EntityField\EntityFieldAccessEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use Drupal\Tests\UnitTestCase;
class EntityFieldAccessEventTest 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 testEntityFieldAccessEvent() {
$accessResult = $this
->createMock(AccessResultInterface::class);
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_FIELD_ACCESS => function (EntityFieldAccessEvent $event) use ($accessResult) {
$event
->setAccessResult($accessResult);
},
]);
$operation = 'test';
$fieldDefinition = $this
->createMock(FieldDefinitionInterface::class);
$account = $this
->createMock(AccountInterface::class);
$items = $this
->createMock(FieldItemListInterface::class);
$hookAccessResult = hook_event_dispatcher_entity_field_access($operation, $fieldDefinition, $account, $items);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_FIELD_ACCESS);
self::assertEquals($operation, $event
->getOperation());
self::assertEquals($fieldDefinition, $event
->getFieldDefinition());
self::assertEquals($account, $event
->getAccount());
self::assertEquals($items, $event
->getItems());
self::assertEquals($accessResult, $hookAccessResult);
}
}