Skip to content
Snippets Groups Projects
Commit fc6b0cf6 authored by Luhur Abdi Rizal's avatar Luhur Abdi Rizal
Browse files

Issue #3240892 by el7cosmos: Convert deprecated path hooks implementations

parent 4de64cb2
No related branches found
Tags 3.2.0
No related merge requests found
......@@ -4,5 +4,5 @@ description: Dispatches events for multiple path hooks
core_version_requirement: ^8 || ^9
package: Hook events
dependencies:
- hook_event_dispatcher:hook_event_dispatcher
- hook_event_dispatcher:core_event_dispatcher
- drupal:path
......@@ -5,42 +5,37 @@
* Path event dispatcher submodule.
*/
use Drupal\path_alias\PathAliasInterface;
use Drupal\path_event_dispatcher\Event\Path\PathDeleteEvent;
use Drupal\path_event_dispatcher\Event\Path\PathInsertEvent;
use Drupal\path_event_dispatcher\Event\Path\PathUpdateEvent;
/**
* Implements hook_path_insert().
*
* {@inheritdoc}
* Implements hook_ENTITY_TYPE_insert().
*/
function path_event_dispatcher_path_insert(array $path) {
function path_event_dispatcher_path_alias_insert(PathAliasInterface $pathAlias) {
/** @var \Drupal\hook_event_dispatcher\Manager\HookEventDispatcherManagerInterface $manager */
$manager = Drupal::service('hook_event_dispatcher.manager');
$event = new PathInsertEvent($path);
$event = new PathInsertEvent($pathAlias);
$manager->register($event);
}
/**
* Implements hook_path_update().
*
* {@inheritdoc}
* Implements hook_ENTITY_TYPE_update().
*/
function path_event_dispatcher_path_update(array $path) {
function path_event_dispatcher_path_alias_update(PathAliasInterface $pathAlias) {
/** @var \Drupal\hook_event_dispatcher\Manager\HookEventDispatcherManagerInterface $manager */
$manager = Drupal::service('hook_event_dispatcher.manager');
$event = new PathUpdateEvent($path);
$event = new PathUpdateEvent($pathAlias);
$manager->register($event);
}
/**
* Implements hook_path_delete().
*
* {@inheritdoc}
* Implements hook_ENTITY_TYPE_delete().
*/
function path_event_dispatcher_path_delete(array $path) {
function path_event_dispatcher_path_alias_delete(PathAliasInterface $pathAlias) {
/** @var \Drupal\hook_event_dispatcher\Manager\HookEventDispatcherManagerInterface $manager */
$manager = Drupal::service('hook_event_dispatcher.manager');
$event = new PathDeleteEvent($path);
$event = new PathDeleteEvent($pathAlias);
$manager->register($event);
}
......@@ -2,52 +2,20 @@
namespace Drupal\path_event_dispatcher\Event\Path;
use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent;
use Drupal\hook_event_dispatcher\Event\EventInterface;
use Drupal\Component\EventDispatcher\Event;
/**
* Class AbstractPathEvent.
*/
abstract class AbstractPathEvent extends Event implements EventInterface {
abstract class AbstractPathEvent extends AbstractEntityEvent implements EventInterface {
/**
* The source like '/node/1'.
* The path alias entity.
*
* @var string
* @var \Drupal\path_alias\PathAliasInterface
*/
private $source;
/**
* The alias for the source.
*
* @var string
*/
private $alias;
/**
* Lang code.
*
* @var string
*/
private $langcode;
/**
* The path id.
*
* @var int
*/
private $pid;
/**
* AbstractPathEvent constructor.
*
* @param array $path
* The array structure is identical to that of the return value of
* \Drupal\Core\Path\AliasStorageInterface::save().
*/
public function __construct(array $path) {
$this->source = $path['source'];
$this->alias = $path['alias'];
$this->langcode = $path['langcode'];
$this->pid = (int) $path['pid'];
}
protected $entity;
/**
* Getter.
......@@ -56,7 +24,7 @@ abstract class AbstractPathEvent extends Event implements EventInterface {
* The path id.
*/
public function getPid(): int {
return $this->pid;
return $this->entity->id();
}
/**
......@@ -66,7 +34,7 @@ abstract class AbstractPathEvent extends Event implements EventInterface {
* The source like '/node/1'.
*/
public function getSource(): string {
return $this->source;
return $this->entity->getPath();
}
/**
......@@ -76,7 +44,7 @@ abstract class AbstractPathEvent extends Event implements EventInterface {
* The alias.
*/
public function getAlias(): string {
return $this->alias;
return $this->entity->getAlias();
}
/**
......@@ -86,7 +54,7 @@ abstract class AbstractPathEvent extends Event implements EventInterface {
* The langcode like 'nl'.
*/
public function getLangcode(): string {
return $this->langcode;
return $this->entity->language()->getId();
}
}
......@@ -9,25 +9,6 @@ use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
*/
final class PathDeleteEvent extends AbstractPathEvent {
/**
* The redirect.
*
* @var bool
*/
private $redirect;
/**
* Constructor.
*
* @param array $path
* The array structure is identical to that of the return value of
* \Drupal\Core\Path\AliasStorageInterface::save().
*/
public function __construct(array $path) {
parent::__construct($path);
$this->redirect = $path['redirect'] ?? FALSE;
}
/**
* Getter.
*
......@@ -35,7 +16,7 @@ final class PathDeleteEvent extends AbstractPathEvent {
* If it's a redirect.
*/
public function isRedirect(): bool {
return $this->redirect;
return FALSE;
}
/**
......
<?php
namespace Drupal\Tests\path_event_dispatcher\Kernel;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\path_event_dispatcher\Event\Path\PathDeleteEvent;
use Drupal\path_event_dispatcher\Event\Path\PathInsertEvent;
use Drupal\path_event_dispatcher\Event\Path\PathUpdateEvent;
use Drupal\Tests\hook_event_dispatcher\Kernel\ListenerTrait;
/**
* Test description.
*
* @group hook_event_dispatcher
* @group path_event_dispatcher
*/
class PathEventTest extends KernelTestBase {
use ListenerTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'path_alias',
'hook_event_dispatcher',
'path_event_dispatcher',
];
/**
* The path alias entity.
*
* @var \Drupal\path_alias\PathAliasInterface
*/
protected $pathAlias;
/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('path_alias');
$this->pathAlias = $this->container->get('entity_type.manager')
->getStorage('path_alias')
->create([
'id' => rand(),
'path' => 'testPath',
'alias' => 'testAlias',
]);
}
/**
* Test PathInsertEvent.
*
* @throws \Exception
*/
public function testPathInsertEvent(): void {
$this->listen(HookEventDispatcherInterface::PATH_INSERT, 'onPathInsert');
$this->listen(HookEventDispatcherInterface::PATH_UPDATE, 'onPathUpdate');
$this->listen(HookEventDispatcherInterface::PATH_DELETE, 'onPathDelete');
$this->pathAlias->save();
$this->pathAlias->setPath('updatedPath');
$this->pathAlias->setAlias('updatedAlias');
$this->pathAlias->save();
$this->pathAlias->delete();
}
/**
* Callback for PathInsertEvent.
*
* @param \Drupal\path_event_dispatcher\Event\Path\PathInsertEvent $event
* The event.
*/
public function onPathInsert(PathInsertEvent $event) {
$this->assertEquals($this->pathAlias->id(), $event->getPid());
$this->assertEquals($this->pathAlias->getPath(), $event->getSource());
$this->assertEquals($this->pathAlias->getAlias(), $event->getAlias());
$this->assertEquals($this->pathAlias->language()->getId(), $event->getLangcode());
}
/**
* Callback for PathUpdateEvent.
*
* @param \Drupal\path_event_dispatcher\Event\Path\PathUpdateEvent $event
* The event.
*/
public function onPathUpdate(PathUpdateEvent $event) {
$this->assertEquals('updatedPath', $event->getSource());
$this->assertEquals('updatedAlias', $event->getAlias());
}
/**
* Callback for PathDeleteEvent.
*
* @param \Drupal\path_event_dispatcher\Event\Path\PathDeleteEvent $event
* The event.
*/
public function onPathDelete(PathDeleteEvent $event) {
$this->assertFalse($event->isRedirect());
}
}
<?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.
*
* @group path_event_dispatcher
*/
class PathEventTest 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 PathDeleteEvent.
*/
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);
/** @var \Drupal\path_event_dispatcher\Event\Path\PathDeleteEvent $event */
$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());
}
/**
* Test PathDeleteEvent.
*/
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);
/** @var \Drupal\path_event_dispatcher\Event\Path\PathDeleteEvent $event */
$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());
}
/**
* Test PathInsertEvent.
*/
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);
/** @var \Drupal\path_event_dispatcher\Event\Path\PathInsertEvent $event */
$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());
}
/**
* Test PathUpdateEvent.
*/
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);
/** @var \Drupal\path_event_dispatcher\Event\Path\PathUpdateEvent $event */
$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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment