Newer
Older

Claudiu Cristea
committed
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
namespace Drupal\Tests\private_message_notify\Kernel;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\private_message\Traits\PrivateMessageTestTrait;
use Drupal\filter\Entity\FilterFormat;
use Drupal\message\Entity\MessageTemplate;
use Drupal\message\MessageTemplateInterface;
use Drupal\private_message\Entity\PrivateMessage;
use Drupal\private_message\Entity\PrivateMessageThread;
/**
* @coversDefaultClass \Drupal\private_message_notify\Service\PrivateMessageNotifier
* @group private_message
*/
class PrivateMessageNotifierTest extends KernelTestBase {
use AssertMailTrait;
use PrivateMessageTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'field',
'filter',
'mailsystem',
'message',
'message_notify',
'private_message',
'private_message_notify',
'private_message_notify_test',
'symfony_mailer_lite',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->config('system.site')->set('name', 'Aerosmith')->save();
// Theme us needed for mail body rendering.
$this->container->get('theme_installer')->install(['stark']);
$this->config('system.theme')
->set('admin', 'stark')
->set('default', 'stark')
->save();
$this->installEntitySchema('user');
$this->installSchema('user', ['users_data']);
$this->createTestingUsers();
$this->installEntitySchema('message');
$this->installEntitySchema('private_message_thread');
$this->installEntitySchema('private_message');
$this->installSchema('private_message', ['pm_thread_history']);
$this->installConfig([
'filter',
'message',
'message_notify',
'private_message_notify',
]);
// Enable notifications.
$this->config('private_message.settings')
->set('enable_notifications', TRUE)
->set('notify_by_default', TRUE)
->save();
// Use rich HTML in email body.
FilterFormat::create([
'format' => 'basic_html',
'name' => 'Basic HTML',
'filters' => [
'filter_html' => [
'status' => 1,
'settings' => [
'allowed_html' => '<p> <strong>',
],
],
],
])->save();
$messageTemplate = MessageTemplate::load('private_message_notification');
assert($messageTemplate instanceof MessageTemplateInterface);
$text = $messageTemplate->get('text');
$text[1]['format'] = 'basic_html';
$messageTemplate->set('text', $text)->save();
$this->config('mailsystem.settings')
->set('theme', 'stark')
->set('defaults', [
'sender' => 'test_mail_collector',
'formatter' => 'symfony_mailer_lite',
])->save();
}
/**
* @covers ::notify
* @covers \private_message_tokens
*/
public function testEmailMarkup(): void {
$thread = PrivateMessageThread::create([
'members' => [$this->users['a'], $this->users['b']],
'private_messages' => [],
]);
$privateMessage = PrivateMessage::create([
'owner' => $this->users['a'],
'message' => [
'value' => "<p><strong>Janie</strong>'s Got a Gun</p><script type=\"Danger!\"></script>",
'format' => 'basic_html',
],
]);
// Create a new.
$this->container->get('private_message.thread_manager')
->saveThread($privateMessage, $thread->getMembers(), $thread);
foreach ($this->getMails() as $mail) {
if ($mail['module'] !== 'message_notify' || $mail['key'] !== 'private_message_notification') {
continue;
}
$this->assertSame('Private message at Aerosmith', $mail['subject']);
// Cast from MarkupInterface to string.
$body = (string) $mail['body'];
$this->assertStringContainsString("<p><strong>Janie</strong>'s Got a Gun</p>",
$body);
// .No encoded HTML.
$this->assertStringNotContainsString('<', $body);
$this->assertStringNotContainsString('>', $body);
// Dangerous tags are stripped out.
$this->assertStringNotContainsString('Danger!', $body);
$this->assertStringNotContainsString('<script', $body);
$this->assertStringNotContainsString('</script>', $body);
}
}
}