You are here

class EntityAccessEventTest in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/core_event_dispatcher/tests/src/Unit/Entity/EntityAccessEventTest.php \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityAccessEventTest

Class EntityAccessEventTest.

@group core_event_dispatcher

Hierarchy

  • class \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityAccessEventTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of EntityAccessEventTest

File

modules/core_event_dispatcher/tests/src/Unit/Entity/EntityAccessEventTest.php, line 23

Namespace

Drupal\Tests\core_event_dispatcher\Unit\Entity
View source
class EntityAccessEventTest 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);
  }

  /**
   * EntityAccessEvent with no changes test.
   */
  public function testEntityAccessEventWithNoChanges() : void {
    $entity = $this
      ->createMock(EntityInterface::class);
    $operation = 'test';
    $account = $this
      ->createMock(AccountInterface::class);
    $hookAccessResult = core_event_dispatcher_entity_access($entity, $operation, $account);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityAccessEvent $event */
    $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());
  }

  /**
   * EntityAccessEvent with neutral result test.
   */
  public function testEntityAccessEventNeutralResult() : void {
    $accessResult = new AccessResultNeutral();
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_ACCESS => static function (EntityAccessEvent $event) use ($accessResult) {
        $event
          ->addAccessResult($accessResult);
      },
    ]);
    $entity = $this
      ->createMock(EntityInterface::class);
    $operation = 'test';
    $account = $this
      ->createMock(AccountInterface::class);
    $hookAccessResult = core_event_dispatcher_entity_access($entity, $operation, $account);
    self::assertTrue($hookAccessResult
      ->isNeutral());
    self::assertFalse($hookAccessResult
      ->isAllowed());
    self::assertFalse($hookAccessResult
      ->isForbidden());
  }

  /**
   * EntityAccessEvent with allowed result test.
   */
  public function testEntityAccessEventAllowedResult() : void {
    $accessResult = new AccessResultAllowed();
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_ACCESS => static function (EntityAccessEvent $event) use ($accessResult) {
        $event
          ->addAccessResult($accessResult);
      },
    ]);
    $entity = $this
      ->createMock(EntityInterface::class);
    $operation = 'test';
    $account = $this
      ->createMock(AccountInterface::class);
    $hookAccessResult = core_event_dispatcher_entity_access($entity, $operation, $account);
    self::assertFalse($hookAccessResult
      ->isNeutral());
    self::assertTrue($hookAccessResult
      ->isAllowed());
    self::assertFalse($hookAccessResult
      ->isForbidden());
  }

  /**
   * EntityAccessEvent with forbidden result test.
   */
  public function testEntityAccessEventForbiddenResult() : void {
    $accessResult = new AccessResultForbidden();
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_ACCESS => static function (EntityAccessEvent $event) use ($accessResult) {
        $event
          ->addAccessResult($accessResult);
      },
    ]);
    $entity = $this
      ->createMock(EntityInterface::class);
    $operation = 'test';
    $account = $this
      ->createMock(AccountInterface::class);
    $hookAccessResult = core_event_dispatcher_entity_access($entity, $operation, $account);
    self::assertFalse($hookAccessResult
      ->isNeutral());
    self::assertFalse($hookAccessResult
      ->isAllowed());
    self::assertTrue($hookAccessResult
      ->isForbidden());
  }

  /**
   * EntityAccessEvent with combined results test.
   *
   * This simulates multiple event listeners adding their own access results to
   * this event.
   */
  public function testEntityAccessEventCombinedResults() : void {
    $accessResults = [
      new AccessResultNeutral(),
      new AccessResultAllowed(),
      new AccessResultForbidden(),
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_ACCESS => static 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 = core_event_dispatcher_entity_access($entity, $operation, $account);
    self::assertFalse($hookAccessResult
      ->isNeutral());
    self::assertFalse($hookAccessResult
      ->isAllowed());
    self::assertTrue($hookAccessResult
      ->isForbidden());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityAccessEventTest::$manager private property The manager.
EntityAccessEventTest::setUp public function
EntityAccessEventTest::testEntityAccessEventAllowedResult public function EntityAccessEvent with allowed result test.
EntityAccessEventTest::testEntityAccessEventCombinedResults public function EntityAccessEvent with combined results test.
EntityAccessEventTest::testEntityAccessEventForbiddenResult public function EntityAccessEvent with forbidden result test.
EntityAccessEventTest::testEntityAccessEventNeutralResult public function EntityAccessEvent with neutral result test.
EntityAccessEventTest::testEntityAccessEventWithNoChanges public function EntityAccessEvent with no changes test.