You are here

class EntityAccessEventTest in Hook Event Dispatcher 8

Class EntityAccessEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Entity

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of EntityAccessEventTest

File

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

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Entity
View source
class EntityAccessEventTest extends UnitTestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    \Drupal::setContainer($builder);
  }

  /**
   * Deprecated setEntity method test.
   *
   * @deprecated should be removed when setEntity() method is removed.
   */
  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());
  }

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

    /** @var \Drupal\hook_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 deprecated setAccessResult test.
   *
   * @deprecated should be removed when setAccessResult method is removed.
   */
  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());
  }

  /**
   * EntityAccessEvent with neutral result test.
   */
  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());
  }

  /**
   * EntityAccessEvent with allowed result test.
   */
  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());
  }

  /**
   * EntityAccessEvent with forbidden result test.
   */
  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());
  }

  /**
   * EntityAccessEvent with combined results test.
   *
   * This simulates multiple event listeners adding their own access results to
   * this event.
   */
  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());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityAccessEventTest::$manager private property The manager.
EntityAccessEventTest::setUp public function Overrides UnitTestCase::setUp
EntityAccessEventTest::testDeprecatedSetEntityMethod Deprecated public function Deprecated setEntity method test.
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::testEntityAccessEventWithDeprecatedSetAccessResult Deprecated public function EntityAccessEvent with deprecated setAccessResult test.
EntityAccessEventTest::testEntityAccessEventWithNoChanges public function EntityAccessEvent with no changes test.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.