ViewEventTest.php in Hook Event Dispatcher 8
File
tests/src/Unit/Views/ViewEventTest.php
View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Views;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPostRenderEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPreViewEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsQuerySubstitutionsEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
class ViewEventTest extends UnitTestCase {
private $manager;
public function setUp() {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
\Drupal::setContainer($builder);
}
public function testPreViewEvent() {
$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]);
}
public function testPreBuildEvent() {
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_pre_build($view);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_BUILD);
self::assertEquals($view, $event
->getView());
}
public function testPostBuildEvent() {
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_post_build($view);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_POST_BUILD);
self::assertEquals($view, $event
->getView());
}
public function testPreExecuteEvent() {
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_pre_execute($view);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_EXECUTE);
self::assertEquals($view, $event
->getView());
}
public function testPostExecuteEvent() {
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_post_execute($view);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_POST_EXECUTE);
self::assertEquals($view, $event
->getView());
}
public function testPreRender() {
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_pre_render($view);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_RENDER);
self::assertEquals($view, $event
->getView());
}
public function testPostRenderEvent() {
$view = $this
->createMock(ViewExecutable::class);
$output = "<h1>test</h1>";
$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']);
}
public function testQueryAlterEvent() {
$view = $this
->createMock(ViewExecutable::class);
$query = $this
->createMock(QueryPluginBase::class);
hook_event_dispatcher_views_query_alter($view, $query);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_QUERY_ALTER);
self::assertSame($view, $event
->getView());
self::assertSame($query, $event
->getQuery());
}
public function testQuerySubstitions() {
$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);
}
}