You are here

final class EntityViewEventTest in Hook Event Dispatcher 8.2

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

Class EntityViewEventTest.

@group core_event_dispatcher

Hierarchy

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

Expanded class hierarchy of EntityViewEventTest

File

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

Namespace

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

  /**
   * Test EntityViewEvent by reference.
   */
  public function testEntityViewEventByReference() : void {
    $build = $expectedBuild = [
      'testBuild' => [
        'someBuild',
      ],
    ];
    $entity = $this
      ->createMock(EntityInterface::class);
    $display = $this
      ->createMock(EntityViewDisplayInterface::class);
    $viewMode = 'testViewMode';
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_VIEW => static function (EntityViewEvent $event) {
        $event
          ->getBuild()['otherBuild'] = [
          'aBuild',
        ];
      },
    ]);
    $expectedBuild['otherBuild'] = [
      'aBuild',
    ];
    core_event_dispatcher_entity_view($build, $entity, $display, $viewMode);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityViewEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_VIEW);
    self::assertSame($build, $event
      ->getBuild());
    self::assertSame($expectedBuild, $event
      ->getBuild());
    self::assertSame($entity, $event
      ->getEntity());
    self::assertSame($display, $event
      ->getDisplay());
    self::assertSame($viewMode, $event
      ->getViewMode());
  }

  /**
   * Test EntityViewAlterEvent.
   */
  public function testEntityViewAlterEvent() : void {
    $build = $expectedBuild = [
      'testBuild' => [
        'someBuild',
      ],
    ];
    $entity = $this
      ->createMock(EntityInterface::class);
    $display = $this
      ->createMock(EntityViewDisplayInterface::class);
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_VIEW_ALTER => static function (EntityViewAlterEvent $event) {
        $event
          ->getBuild()['otherBuild'] = [
          'aBuild',
        ];
      },
    ]);
    $expectedBuild['otherBuild'] = [
      'aBuild',
    ];
    core_event_dispatcher_entity_view_alter($build, $entity, $display);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityViewAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_VIEW_ALTER);
    self::assertSame($build, $event
      ->getBuild());
    self::assertSame($expectedBuild, $event
      ->getBuild());
    self::assertSame($entity, $event
      ->getEntity());
    self::assertSame($display, $event
      ->getDisplay());
  }

  /**
   * Test EntityBuildDefaultsAlter.
   */
  public function testEntityBuildDefaultsAlter() : void {
    $build = $expectedBuild = [
      'testBuild' => [
        'someBuild',
      ],
    ];
    $entity = $this
      ->createMock(EntityInterface::class);
    $viewMode = 'entity_view_mode';
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_BUILD_DEFAULTS_ALTER => static function (EntityBuildDefaultsAlterEvent $event) {
        $event
          ->getBuild()['otherBuild'] = [
          'aBuild',
        ];
      },
    ]);
    $expectedBuild['otherBuild'] = [
      'aBuild',
    ];
    core_event_dispatcher_entity_build_defaults_alter($build, $entity, $viewMode);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityBuildDefaultsAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_BUILD_DEFAULTS_ALTER);
    self::assertSame($build, $event
      ->getBuild());
    self::assertSame($expectedBuild, $event
      ->getBuild());
    self::assertSame($entity, $event
      ->getEntity());
    self::assertSame($viewMode, $event
      ->getViewMode());
  }

  /**
   * Test EntityBuildDefaultsAlter.
   */
  public function testEntityBuildDefaultsAlterWithNullViewMode() : void {
    $build = [
      'testBuild' => [
        'someBuild',
      ],
    ];
    $entity = $this
      ->createMock(EntityInterface::class);
    $viewMode = NULL;
    core_event_dispatcher_entity_build_defaults_alter($build, $entity, $viewMode);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityBuildDefaultsAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_BUILD_DEFAULTS_ALTER);
    self::assertSame($build, $event
      ->getBuild());
    self::assertSame($entity, $event
      ->getEntity());
    self::assertSame((string) $viewMode, $event
      ->getViewMode());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityViewEventTest::$manager private property The manager.
EntityViewEventTest::setUp public function
EntityViewEventTest::testEntityBuildDefaultsAlter public function Test EntityBuildDefaultsAlter.
EntityViewEventTest::testEntityBuildDefaultsAlterWithNullViewMode public function Test EntityBuildDefaultsAlter.
EntityViewEventTest::testEntityViewAlterEvent public function Test EntityViewAlterEvent.
EntityViewEventTest::testEntityViewEventByReference public function Test EntityViewEvent by reference.