You are here

class ViewEventTest in Hook Event Dispatcher 8

Class ViewEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Views

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of ViewEventTest

File

tests/src/Unit/Views/ViewEventTest.php, line 23

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Views
View source
class ViewEventTest 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);
  }

  /**
   * Pre view event.
   */
  public function testPreViewEvent() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    $displayId = 'test';
    $arguments = [
      'test',
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::VIEWS_PRE_VIEW => function (ViewsPreViewEvent $event) {
        $arguments =& $event
          ->getArguments();
        $arguments[0] = 'test2';
      },
    ]);
    hook_event_dispatcher_views_pre_view($view, $displayId, $arguments);
    self::assertEquals('test2', $arguments[0]);
  }

  /**
   * Pre build event.
   */
  public function testPreBuildEvent() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    hook_event_dispatcher_views_pre_build($view);

    /** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_BUILD);
    self::assertEquals($view, $event
      ->getView());
  }

  /**
   * Post build event.
   */
  public function testPostBuildEvent() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    hook_event_dispatcher_views_post_build($view);

    /** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_POST_BUILD);
    self::assertEquals($view, $event
      ->getView());
  }

  /**
   * Pre execute event.
   */
  public function testPreExecuteEvent() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    hook_event_dispatcher_views_pre_execute($view);

    /** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_EXECUTE);
    self::assertEquals($view, $event
      ->getView());
  }

  /**
   * Post execute event.
   */
  public function testPostExecuteEvent() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    hook_event_dispatcher_views_post_execute($view);

    /** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_POST_EXECUTE);
    self::assertEquals($view, $event
      ->getView());
  }

  /**
   * Pre render event.
   */
  public function testPreRender() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    hook_event_dispatcher_views_pre_render($view);

    /** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_RENDER);
    self::assertEquals($view, $event
      ->getView());
  }

  /**
   * Post render event.
   */
  public function testPostRenderEvent() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    $output = "<h1>test</h1>";

    /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
    $cache = $this
      ->createMock(CachePluginBase::class);
    $cache->options['results_lifespan'] = 0;
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::VIEWS_POST_RENDER => function (ViewsPostRenderEvent $event) {
        $output =& $event
          ->getOutput();
        $output = "<h2>Test</h2>";
        $cache = $event
          ->getCache();
        $cache->options['results_lifespan'] = 10;
      },
    ]);
    hook_event_dispatcher_views_post_render($view, $output, $cache);
    self::assertEquals("<h2>Test</h2>", $output);
    self::assertEquals(10, $cache->options['results_lifespan']);
  }

  /**
   * Query alter event.
   */
  public function testQueryAlterEvent() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);

    /** @var \Drupal\views\Plugin\views\query\QueryPluginBase $query */
    $query = $this
      ->createMock(QueryPluginBase::class);
    hook_event_dispatcher_views_query_alter($view, $query);

    /** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsQueryAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_QUERY_ALTER);
    self::assertSame($view, $event
      ->getView());
    self::assertSame($query, $event
      ->getQuery());
  }

  /**
   * Query substitutions event.
   */
  public function testQuerySubstitions() {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $this
      ->createMock(ViewExecutable::class);
    $expected = [
      "test" => 1,
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::VIEWS_QUERY_SUBSTITUTIONS => function (ViewsQuerySubstitutionsEvent $event) use ($expected) {
        $event
          ->setSubstitutions($expected);
      },
    ]);
    $result = hook_event_dispatcher_views_query_substitutions($view);
    self::assertSame($expected, $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ViewEventTest::$manager private property The manager.
ViewEventTest::setUp public function Overrides UnitTestCase::setUp
ViewEventTest::testPostBuildEvent public function Post build event.
ViewEventTest::testPostExecuteEvent public function Post execute event.
ViewEventTest::testPostRenderEvent public function Post render event.
ViewEventTest::testPreBuildEvent public function Pre build event.
ViewEventTest::testPreExecuteEvent public function Pre execute event.
ViewEventTest::testPreRender public function Pre render event.
ViewEventTest::testPreViewEvent public function Pre view event.
ViewEventTest::testQueryAlterEvent public function Query alter event.
ViewEventTest::testQuerySubstitions public function Query substitutions event.