forked from chapter-three/next-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityRevalidatedEventTest.php
More file actions
128 lines (110 loc) · 3.74 KB
/
EntityRevalidatedEventTest.php
File metadata and controls
128 lines (110 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Drupal\Tests\next\Kernel\Event;
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;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Tests the EntityRevalidatedEvent.
*
* @group next
*/
class EntityRevalidatedEventTest extends KernelTestBase {
use NodeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'filter',
'next',
'next_tests',
'node',
'system',
'user',
'dblog',
'content_translation',
'language',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installEntitySchema('next_entity_type_config');
$this->installConfig(['filter', 'next', 'system', 'user']);
$this->installSchema('dblog', ['watchdog']);
$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.
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
'draft_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
'blog' => 'blog',
],
],
'revalidator' => 'path',
'revalidator_configuration' => [
'revalidate_page' => TRUE,
],
]);
$entity_type_config->save();
}
/**
* Test entity revalidated events.
*/
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.');
// 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.');
// 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.');
// 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.');
}
/**
* Helper to assert logs.
*
* @param string $message
* The message to assert in the logs.
*/
protected function assertLogsContains(string $message): void {
$logs = $this->container->get('database')
->select('watchdog', 'wd')
->fields('wd', ['message', 'variables'])
->condition('type', 'system')
->execute()
->fetchAll();
$controller = DbLogController::create($this->container);
$messages = array_map(function ($log) use ($controller) {
return (string) $controller->formatMessage($log);
}, $logs);
$this->assertContains($message, $messages);
}
}