Skip to content

Commit

Permalink
Refractor entityEventTests for next.
Browse files Browse the repository at this point in the history
  • Loading branch information
apathak18 committed Aug 6, 2024
1 parent 861477a commit 71616c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
42 changes: 19 additions & 23 deletions modules/next/tests/src/Kernel/Event/EntityActionEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit 71616c3

Please sign in to comment.