View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Path;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use PHPUnit\Framework\TestCase;
use function path_event_dispatcher_path_delete;
use function path_event_dispatcher_path_insert;
use function path_event_dispatcher_path_update;
class PathEventTest extends TestCase {
private $manager;
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
public function testPathDeleteEvent() : void {
$source = 'testSource';
$alias = 'testAlias';
$langcode = 'NL';
$pid = 1337;
$path = [
'source' => $source,
'alias' => $alias,
'langcode' => $langcode,
'pid' => $pid,
'redirect' => TRUE,
];
path_event_dispatcher_path_delete($path);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::PATH_DELETE);
self::assertSame($source, $event
->getSource());
self::assertSame($alias, $event
->getAlias());
self::assertSame($langcode, $event
->getLangcode());
self::assertSame($pid, $event
->getPid());
self::assertTrue($event
->isRedirect());
}
public function testPathDeleteEventWithoutRedirect() : void {
$source = 'testSource';
$alias = 'testAlias';
$langcode = 'NL';
$pid = 1337;
$path = [
'source' => $source,
'alias' => $alias,
'langcode' => $langcode,
'pid' => $pid,
];
path_event_dispatcher_path_delete($path);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::PATH_DELETE);
self::assertSame($source, $event
->getSource());
self::assertSame($alias, $event
->getAlias());
self::assertSame($langcode, $event
->getLangcode());
self::assertSame($pid, $event
->getPid());
self::assertFalse($event
->isRedirect());
}
public function testPathInsertEvent() : void {
$source = 'testSource';
$alias = 'testAlias';
$langcode = 'NL';
$pid = 1337;
$path = [
'source' => $source,
'alias' => $alias,
'langcode' => $langcode,
'pid' => $pid,
];
path_event_dispatcher_path_insert($path);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::PATH_INSERT);
self::assertSame($source, $event
->getSource());
self::assertSame($alias, $event
->getAlias());
self::assertSame($langcode, $event
->getLangcode());
self::assertSame($pid, $event
->getPid());
}
public function testPathUpdateEvent() : void {
$source = 'testSource';
$alias = 'testAlias';
$langcode = 'NL';
$pid = 1337;
$path = [
'source' => $source,
'alias' => $alias,
'langcode' => $langcode,
'pid' => $pid,
];
path_event_dispatcher_path_update($path);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::PATH_UPDATE);
self::assertSame($source, $event
->getSource());
self::assertSame($alias, $event
->getAlias());
self::assertSame($langcode, $event
->getLangcode());
self::assertSame($pid, $event
->getPid());
}
}