Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions modules/next/next.module
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
38 changes: 30 additions & 8 deletions modules/next/tests/src/Kernel/Event/EntityActionEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\Tests\next\Kernel\Event;

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;
Expand All @@ -28,6 +29,8 @@ class EntityActionEventTest extends KernelTestBase {
'system',
'user',
'dblog',
'content_translation',
'language',
];

/**
Expand All @@ -38,7 +41,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('node', ['node_access']);
$this->installSchema('user', ['users_data']);
Expand All @@ -49,36 +52,55 @@ protected function setUp(): void {
'label' => 'Page',
]);
$page_type->save();

// Set up multilingual.
ConfigurableLanguage::createFromLangcode('nl')->save();
}

/**
* Test entity action events.
*/
public function testEntityActionEvents() {
$page = $this->createNode(['type' => 'page', 'title' => 'A page']);
public function testEntityActionEvents(): void {
$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($translated_title, 'delete');

// Delete.
$page->delete();
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
$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) {
$message = "Event @event dispatched for entity @label and action @action.";
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,
];

Expand Down
23 changes: 18 additions & 5 deletions modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\dblog\Controller\DbLogController;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\next\Entity\NextEntityTypeConfig;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait;
Expand All @@ -30,6 +31,8 @@ class EntityRevalidatedEventTest extends KernelTestBase {
'system',
'user',
'dblog',
'content_translation',
'language',
];

/**
Expand All @@ -46,6 +49,9 @@ protected function setUp(): void {
$this->installSchema('node', ['node_access']);
$this->installSchema('user', ['users_data']);

// Set up multilingual.
ConfigurableLanguage::createFromLangcode('nl')->save();

NodeType::create(['type' => 'page', 'name' => 'Page'])->save();

// Create entity type config.
Expand All @@ -71,24 +77,31 @@ protected function setUp(): void {
*/
public function testEntityRevalidatedEvents() {
$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->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.');

// 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.');
}

/**
Expand All @@ -97,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'])
Expand Down