From f3dbfd94fe3c628c9615d4ac7e58bca24e789ff9 Mon Sep 17 00:00:00 2001 From: Bojan Bogdanovic Date: Tue, 18 Jun 2024 16:13:16 +0200 Subject: [PATCH 1/4] Add event action on entity translation delete --- modules/next/next.module | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/next/next.module b/modules/next/next.module index 815cb70dd..9a1787e13 100644 --- a/modules/next/next.module +++ b/modules/next/next.module @@ -87,3 +87,11 @@ function next_entity_predelete(EntityInterface $entity) { $event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::DELETE_ACTION); \Drupal::service('next.entity_action_event_dispatcher')->addEvent($event); } + +/** + * Implements hook_entity_translation_delete(). + */ +function next_entity_translation_delete(EntityInterface $translation) { + $event = EntityActionEvent::createFromEntity($translation, EntityActionEventInterface::DELETE_ACTION); + \Drupal::service('next.entity_action_event_dispatcher')->addEvent($event); +} From 9f2f14e315e6f369d8147ef584b236016f9f6543 Mon Sep 17 00:00:00 2001 From: Bojan Bogdanovic Date: Wed, 19 Jun 2024 08:48:55 +0200 Subject: [PATCH 2/4] Add test coverage --- .../src/Kernel/Event/EntityActionEventTest.php | 15 ++++++++++++++- .../Kernel/Event/EntityRevalidatedEventTest.php | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php index 4af09b250..a2636fc7d 100644 --- a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php @@ -5,6 +5,7 @@ use Drupal\Core\Database\Database; use Drupal\dblog\Controller\DbLogController; use Drupal\KernelTests\KernelTestBase; +use Drupal\language\Entity\ConfigurableLanguage; use Drupal\node\Entity\NodeType; use Drupal\Tests\node\Traits\NodeCreationTrait; use Symfony\Component\HttpFoundation\Request; @@ -30,6 +31,8 @@ class EntityActionEventTest extends KernelTestBase { 'system', 'user', 'dblog', + 'content_translation', + 'language', ]; /** @@ -40,7 +43,7 @@ protected function setUp(): void { $this->installEntitySchema('node'); $this->installEntitySchema('user'); - $this->installConfig(['filter', 'next', 'system', 'user']); + $this->installConfig(['filter', 'next', 'system', 'user', 'language']); $this->installSchema('dblog', ['watchdog']); $this->installSchema('system', ['sequences']); $this->installSchema('node', ['node_access']); @@ -52,6 +55,9 @@ protected function setUp(): void { 'label' => 'Page', ]); $page_type->save(); + + // Set up multilingual. + ConfigurableLanguage::createFromLangcode('nl')->save(); } /** @@ -59,6 +65,7 @@ protected function setUp(): void { */ public function testEntityActionEvents() { $page = $this->createNode(['type' => 'page', 'title' => 'A page']); + $page->addTranslation('nl', ['title' => 'Translation']); // Insert. $page->save(); @@ -70,6 +77,12 @@ public function testEntityActionEvents() { $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action update."); + // Delete translation. + $page->removeTranslation('nl'); + $page->save(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); + $this->assertLogsContains("Event next.entity.action dispatched for entity Translation and action delete."); + // Delete. $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); diff --git a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php index e8c69d079..928819eb5 100644 --- a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php @@ -5,6 +5,7 @@ use Drupal\Core\Database\Database; use Drupal\dblog\Controller\DbLogController; use Drupal\KernelTests\KernelTestBase; +use Drupal\language\Entity\ConfigurableLanguage; use Drupal\next\Entity\NextEntityTypeConfig; use Drupal\Tests\node\Traits\NodeCreationTrait; use Symfony\Component\HttpFoundation\Request; @@ -30,6 +31,8 @@ class EntityRevalidatedEventTest extends KernelTestBase { 'system', 'user', 'dblog', + 'content_translation', + 'language', ]; /** @@ -47,6 +50,9 @@ protected function setUp(): void { $this->installSchema('node', ['node_access']); $this->installSchema('user', ['users_data']); + // Set up multilingual. + ConfigurableLanguage::createFromLangcode('nl')->save(); + // Create entity type config. $entity_type_config = NextEntityTypeConfig::create([ 'id' => 'node.page', @@ -69,6 +75,7 @@ protected function setUp(): void { */ public function testEntityRevalidatedEvents() { $page = $this->createNode(['type' => 'page', 'title' => 'A page']); + $page->addTranslation('nl', ['title' => 'Translation']); // Insert. $page->save(); @@ -80,6 +87,12 @@ public function testEntityRevalidatedEvents() { $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Entity A page updated, action update, revalidated 0."); + // Delete translation. + $page->removeTranslation('nl'); + $page->save(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); + $this->assertLogsContains("Entity Translation, action delete, revalidated 0."); + // Delete. $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); From 2a3e3b2897a7011a91388a3f926973f5f0dd5bc7 Mon Sep 17 00:00:00 2001 From: Bojan Bogdanovic Date: Mon, 29 Dec 2025 11:05:22 +0100 Subject: [PATCH 3/4] Update tests --- .../src/Kernel/Event/EntityActionEventTest.php | 15 +++++++-------- .../Kernel/Event/EntityRevalidatedEventTest.php | 12 ++++++------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php index 9f3560470..37982ccfe 100644 --- a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php @@ -60,31 +60,30 @@ protected function setUp(): void { /** * Test entity action events. */ - public function testEntityActionEvents() { + public function testEntityActionEvents(): void { $page = $this->createNode(['type' => 'page', 'title' => 'A page']); $page->addTranslation('nl', ['title' => 'Translation']); // Insert. $page->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogMessage("insert"); + $this->assertLogMessage('insert'); // Update. $page->set('title', 'A page updated')->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action update."); + $this->assertLogMessage('update'); // Delete translation. $page->removeTranslation('nl'); $page->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Event next.entity.action dispatched for entity Translation and action delete."); + $this->assertLogMessage('update'); // Delete. $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action delete."); - $this->assertLogMessage("update"); + $this->assertLogMessage('update'); } /** @@ -93,8 +92,8 @@ public function testEntityActionEvents() { * @param string $action * The action to perform. */ - protected function assertLogMessage(string $action) { - $message = "Event @event dispatched for entity @label and action @action."; + protected function assertLogMessage(string $action): void { + $message = 'Event @event dispatched for entity @label and action @action.'; $variables = [ '@event' => 'next.entity.action', '@label' => 'A page', diff --git a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php index 2ca67df0d..daba888b6 100644 --- a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php @@ -82,26 +82,26 @@ public function testEntityRevalidatedEvents() { // Insert. $page->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Entity A page, action insert, revalidated 0."); + $this->assertLogsContains('Entity A page, action insert, revalidated 0.'); // Update. $page->set('title', 'A page updated')->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Entity A page updated, action update, revalidated 0."); + $this->assertLogsContains('Entity A page updated, action update, revalidated 0.'); // Delete translation. $page->removeTranslation('nl'); $page->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Entity Translation, action delete, revalidated 0."); + $this->assertLogsContains('Entity Translation, action delete, revalidated 0.'); // Delete. $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Entity A page updated, action delete, revalidated 0."); + $this->assertLogsContains('Entity A page updated, action delete, revalidated 0.'); // As hook_entity_predelete is used to perform revalidate // before delete action then it's ideal to check log after revalidate. - $this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action delete."); + $this->assertLogsContains('Event next.entity.action dispatched for entity A page updated and action delete.'); } /** @@ -110,7 +110,7 @@ public function testEntityRevalidatedEvents() { * @param string $message * The message to assert in the logs. */ - protected function assertLogsContains(string $message) { + protected function assertLogsContains(string $message): void { $logs = $this->container->get('database') ->select('watchdog', 'wd') ->fields('wd', ['message', 'variables']) From 3313c191cc2db73a2740972e4d32cad1d6c2f560 Mon Sep 17 00:00:00 2001 From: Bojan Bogdanovic Date: Mon, 29 Dec 2025 11:41:06 +0100 Subject: [PATCH 4/4] Update EntityActionEventTest --- .../Kernel/Event/EntityActionEventTest.php | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php index 37982ccfe..2ac75cb8f 100644 --- a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php @@ -61,42 +61,46 @@ protected function setUp(): void { * Test entity action events. */ public function testEntityActionEvents(): void { - $page = $this->createNode(['type' => 'page', 'title' => 'A page']); - $page->addTranslation('nl', ['title' => 'Translation']); + $title = 'A page'; + $translated_title = 'Translation'; + $page = $this->createNode(['type' => 'page', 'title' => $title]); + $page->addTranslation('nl', ['title' => $translated_title]); // Insert. $page->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogMessage('insert'); + $this->assertLogMessage($title, 'insert'); // Update. $page->set('title', 'A page updated')->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogMessage('update'); + $this->assertLogMessage($title, 'update'); // Delete translation. $page->removeTranslation('nl'); $page->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogMessage('update'); + $this->assertLogMessage($translated_title, 'delete'); // Delete. $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogMessage('update'); + $this->assertLogMessage('A page updated', 'delete'); } /** * Helper to assert log. * + * @param string $label + * The label of the entity. * @param string $action * The action to perform. */ - protected function assertLogMessage(string $action): void { + protected function assertLogMessage(string $label, string $action): void { $message = 'Event @event dispatched for entity @label and action @action.'; $variables = [ '@event' => 'next.entity.action', - '@label' => 'A page', + '@label' => $label, '@action' => $action, ];