class EntityFieldAccessEventTest in Hook Event Dispatcher 3.x
Same name and namespace in other branches
- 8.2 modules/core_event_dispatcher/tests/src/Unit/Entity/EntityFieldAccessEventTest.php \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityFieldAccessEventTest
Class EntityFieldAccessEventTest.
@group core_event_dispatcher
Hierarchy
- class \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityFieldAccessEventTest extends \PHPUnit\Framework\TestCase
Expanded class hierarchy of EntityFieldAccessEventTest
File
- modules/
core_event_dispatcher/ tests/ src/ Unit/ Entity/ EntityFieldAccessEventTest.php, line 22
Namespace
Drupal\Tests\core_event_dispatcher\Unit\EntityView source
class EntityFieldAccessEventTest extends TestCase {
/**
* The manager.
*
* @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
*/
private $manager;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
/**
* Field access event with result set.
*/
public function testEntityFieldAccessEventWithoutResult() : void {
$operation = 'test';
$fieldDefinition = $this
->createMock(FieldDefinitionInterface::class);
$account = $this
->createMock(AccountInterface::class);
$items = $this
->createMock(FieldItemListInterface::class);
$hookAccessResult = core_event_dispatcher_entity_field_access($operation, $fieldDefinition, $account, $items);
/** @var \Drupal\core_event_dispatcher\Event\Entity\EntityFieldAccessEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_FIELD_ACCESS);
self::assertSame($operation, $event
->getOperation());
self::assertSame($fieldDefinition, $event
->getFieldDefinition());
self::assertSame($account, $event
->getAccount());
self::assertSame($items, $event
->getItems());
self::assertSame($hookAccessResult, $event
->getAccessResult());
self::assertTrue($event
->getAccessResult()
->isNeutral());
}
/**
* Field access event with result set.
*/
public function testEntityFieldAccessEventWithResultSet() : void {
$accessResult = $this
->createMock(AccessResultInterface::class);
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_FIELD_ACCESS => static 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 = core_event_dispatcher_entity_field_access($operation, $fieldDefinition, $account, $items);
/** @var \Drupal\core_event_dispatcher\Event\Entity\EntityFieldAccessEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_FIELD_ACCESS);
self::assertSame($operation, $event
->getOperation());
self::assertSame($fieldDefinition, $event
->getFieldDefinition());
self::assertSame($account, $event
->getAccount());
self::assertSame($items, $event
->getItems());
self::assertSame($accessResult, $event
->getAccessResult());
self::assertSame($accessResult, $hookAccessResult);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityFieldAccessEventTest:: |
private | property | The manager. | |
EntityFieldAccessEventTest:: |
public | function | ||
EntityFieldAccessEventTest:: |
public | function | Field access event with result set. | |
EntityFieldAccessEventTest:: |
public | function | Field access event with result set. |