From 71616c31eca18a6ce9cdeacac0c958038139cddd Mon Sep 17 00:00:00 2001 From: Ankit Pathak Date: Tue, 6 Aug 2024 14:28:54 +0530 Subject: [PATCH] Refractor entityEventTests for next. --- .../Kernel/Event/EntityActionEventTest.php | 42 +++++++++---------- .../Event/EntityRevalidatedEventTest.php | 4 +- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php index 4af09b25..5a2fad6d 100644 --- a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php @@ -3,7 +3,6 @@ namespace Drupal\Tests\next\Kernel\Event; use Drupal\Core\Database\Database; -use Drupal\dblog\Controller\DbLogController; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\NodeType; use Drupal\Tests\node\Traits\NodeCreationTrait; @@ -63,39 +62,36 @@ public function testEntityActionEvents() { // Insert. $page->save(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); - $this->assertLogsContains("Event next.entity.action dispatched for entity A page and action 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."); - - // 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"); } /** - * Helper to assert logs. + * Helper to assert log. * - * @param string $message - * The message to assert in the logs. + * @param string $action + * The action to perform. */ - protected function assertLogsContains(string $message) { - $logs = Database::getConnection() - ->select('watchdog', 'wd') - ->fields('wd', ['message', 'variables']) + protected function assertLogMessage(string $action) { + $message = "Event @event dispatched for entity @label and action @action."; + $variables = [ + '@event' => 'next.entity.action', + '@label' => 'A page', + '@action' => $action, + ]; + + $this->assertNotEmpty(Database::getConnection()->select('watchdog', 'w') ->condition('type', 'system') + ->condition('message', $message) + ->condition('variables', serialize($variables)) + ->countQuery() ->execute() - ->fetchAll(); - - $controller = DbLogController::create($this->container); - $messages = array_map(function ($log) use ($controller) { - return (string) $controller->formatMessage($log); - }, $logs); - - $this->assertContains($message, $messages); + ->fetchField() + ); } } diff --git a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php index e8c69d07..48af81fb 100644 --- a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php @@ -84,6 +84,9 @@ public function testEntityRevalidatedEvents() { $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $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."); } /** @@ -104,7 +107,6 @@ protected function assertLogsContains(string $message) { $messages = array_map(function ($log) use ($controller) { return (string) $controller->formatMessage($log); }, $logs); - $this->assertContains($message, $messages); }