You are here

class EntityFieldAccessEventTest in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 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

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\Entity
View 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

Namesort descending Modifiers Type Description Overrides
EntityFieldAccessEventTest::$manager private property The manager.
EntityFieldAccessEventTest::setUp public function
EntityFieldAccessEventTest::testEntityFieldAccessEventWithoutResult public function Field access event with result set.
EntityFieldAccessEventTest::testEntityFieldAccessEventWithResultSet public function Field access event with result set.