comparison core/modules/contact/tests/src/Unit/MailHandlerTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\contact\Unit;
4
5 use Drupal\contact\MailHandler;
6 use Drupal\contact\MailHandlerException;
7 use Drupal\contact\MessageInterface;
8 use Drupal\Core\Language\Language;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Tests\UnitTestCase;
11
12 /**
13 * @coversDefaultClass \Drupal\contact\MailHandler
14 * @group contact
15 */
16 class MailHandlerTest extends UnitTestCase {
17
18 /**
19 * Language manager service.
20 *
21 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
22 */
23 protected $languageManager;
24
25 /**
26 * Logger service.
27 *
28 * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
29 */
30 protected $logger;
31
32 /**
33 * Mail manager service.
34 *
35 * @var \Drupal\Core\Mail\MailManagerInterface|\PHPUnit_Framework_MockObject_MockObject
36 */
37 protected $mailManager;
38
39 /**
40 * Contact mail messages service.
41 *
42 * @var \Drupal\contact\MailHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
43 */
44 protected $contactMailHandler;
45
46 /**
47 * The contact form entity.
48 *
49 * @var \Drupal\contact\ContactFormInterface|\PHPUnit_Framework_MockObject_MockObject
50 */
51 protected $contactForm;
52
53 /**
54 * The entity manager service.
55 *
56 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
57 */
58 protected $entityManager;
59
60 /**
61 * The user storage handler.
62 *
63 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
64 */
65 protected $userStorage;
66
67 /**
68 * {@inheritdoc}
69 */
70 protected function setUp() {
71 parent::setUp();
72 $this->mailManager = $this->getMock('\Drupal\Core\Mail\MailManagerInterface');
73 $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
74 $this->logger = $this->getMock('\Psr\Log\LoggerInterface');
75 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
76 $this->userStorage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
77 $this->entityManager->expects($this->any())
78 ->method('getStorage')
79 ->with('user')
80 ->willReturn($this->userStorage);
81
82 $string_translation = $this->getStringTranslationStub();
83 $this->contactMailHandler = new MailHandler($this->mailManager, $this->languageManager, $this->logger, $string_translation, $this->entityManager);
84 $language = new Language(['id' => 'en']);
85
86 $this->languageManager->expects($this->any())
87 ->method('getDefaultLanguage')
88 ->will($this->returnValue($language));
89
90 $this->languageManager->expects($this->any())
91 ->method('getCurrentLanguage')
92 ->will($this->returnValue($language));
93 }
94
95 /**
96 * Tests the children() method with an invalid key.
97 *
98 * @covers ::sendMailMessages
99 */
100 public function testInvalidRecipient() {
101 $message = $this->getMock('\Drupal\contact\MessageInterface');
102 $message->expects($this->once())
103 ->method('isPersonal')
104 ->willReturn(TRUE);
105 $message->expects($this->once())
106 ->method('getPersonalRecipient')
107 ->willReturn(NULL);
108 $message->expects($this->once())
109 ->method('getContactForm')
110 ->willReturn($this->getMock('\Drupal\contact\ContactFormInterface'));
111 $sender = $this->getMock('\Drupal\Core\Session\AccountInterface');
112 $this->userStorage->expects($this->any())
113 ->method('load')
114 ->willReturn($sender);
115 // User IDs 1 and 0 have special implications, use 3 instead.
116 $sender->expects($this->any())
117 ->method('id')
118 ->willReturn(3);
119 $sender->expects($this->once())
120 ->method('isAnonymous')
121 ->willReturn(FALSE);
122 $this->setExpectedException(MailHandlerException::class, 'Unable to determine message recipient');
123 $this->contactMailHandler->sendMailMessages($message, $sender);
124 }
125
126 /**
127 * Tests the sendMailMessages method.
128 *
129 * @dataProvider getSendMailMessages
130 *
131 * @covers ::sendMailMessages
132 */
133 public function testSendMailMessages(MessageInterface $message, AccountInterface $sender, $results) {
134 $this->logger->expects($this->once())
135 ->method('notice');
136 $this->mailManager->expects($this->any())
137 ->method('mail')
138 ->willReturnCallback(
139 function ($module, $key, $to, $langcode, $params, $from) use (&$results) {
140 $result = array_shift($results);
141 $this->assertEquals($module, $result['module']);
142 $this->assertEquals($key, $result['key']);
143 $this->assertEquals($to, $result['to']);
144 $this->assertEquals($langcode, $result['langcode']);
145 $this->assertArrayEquals($params, $result['params']);
146 $this->assertEquals($from, $result['from']);
147 });
148 $this->userStorage->expects($this->any())
149 ->method('load')
150 ->willReturn(clone $sender);
151 $this->contactMailHandler->sendMailMessages($message, $sender);
152 }
153
154 /**
155 * Data provider for ::testSendMailMessages.
156 */
157 public function getSendMailMessages() {
158 $data = [];
159 $recipients = ['admin@drupal.org', 'user@drupal.org'];
160 $default_result = [
161 'module' => 'contact',
162 'key' => '',
163 'to' => implode(', ', $recipients),
164 'langcode' => 'en',
165 'params' => [],
166 'from' => 'anonymous@drupal.org',
167 ];
168 $results = [];
169 $message = $this->getAnonymousMockMessage($recipients, '');
170 $sender = $this->getMockSender();
171 $result = [
172 'key' => 'page_mail',
173 'params' => [
174 'contact_message' => $message,
175 'sender' => $sender,
176 'contact_form' => $message->getContactForm(),
177 ],
178 ];
179 $results[] = $result + $default_result;
180 $data[] = [$message, $sender, $results];
181
182 $results = [];
183 $message = $this->getAnonymousMockMessage($recipients, 'reply');
184 $sender = $this->getMockSender();
185 $result = [
186 'key' => 'page_mail',
187 'params' => [
188 'contact_message' => $message,
189 'sender' => $sender,
190 'contact_form' => $message->getContactForm(),
191 ],
192 ];
193 $results[] = $result + $default_result;
194 $result['key'] = 'page_autoreply';
195 $result['to'] = 'anonymous@drupal.org';
196 $result['from'] = NULL;
197 $results[] = $result + $default_result;
198 $data[] = [$message, $sender, $results];
199
200 $results = [];
201 $message = $this->getAnonymousMockMessage($recipients, '', TRUE);
202 $sender = $this->getMockSender();
203 $result = [
204 'key' => 'page_mail',
205 'params' => [
206 'contact_message' => $message,
207 'sender' => $sender,
208 'contact_form' => $message->getContactForm(),
209 ],
210 ];
211 $results[] = $result + $default_result;
212 $result['key'] = 'page_copy';
213 $result['to'] = 'anonymous@drupal.org';
214 $results[] = $result + $default_result;
215 $data[] = [$message, $sender, $results];
216
217 $results = [];
218 $message = $this->getAnonymousMockMessage($recipients, 'reply', TRUE);
219 $sender = $this->getMockSender();
220 $result = [
221 'key' => 'page_mail',
222 'params' => [
223 'contact_message' => $message,
224 'sender' => $sender,
225 'contact_form' => $message->getContactForm(),
226 ],
227 ];
228 $results[] = $result + $default_result;
229 $result['key'] = 'page_copy';
230 $result['to'] = 'anonymous@drupal.org';
231 $results[] = $result + $default_result;
232 $result['key'] = 'page_autoreply';
233 $result['from'] = NULL;
234 $results[] = $result + $default_result;
235 $data[] = [$message, $sender, $results];
236
237 // For authenticated user.
238 $results = [];
239 $message = $this->getAuthenticatedMockMessage();
240 $sender = $this->getMockSender(FALSE, 'user@drupal.org');
241 $result = [
242 'module' => 'contact',
243 'key' => 'user_mail',
244 'to' => 'user2@drupal.org',
245 'langcode' => 'en',
246 'params' => [
247 'contact_message' => $message,
248 'sender' => $sender,
249 'recipient' => $message->getPersonalRecipient(),
250 ],
251 'from' => 'user@drupal.org',
252 ];
253 $results[] = $result;
254 $data[] = [$message, $sender, $results];
255
256 $results = [];
257 $message = $this->getAuthenticatedMockMessage(TRUE);
258 $sender = $this->getMockSender(FALSE, 'user@drupal.org');
259 $result = [
260 'module' => 'contact',
261 'key' => 'user_mail',
262 'to' => 'user2@drupal.org',
263 'langcode' => 'en',
264 'params' => [
265 'contact_message' => $message,
266 'sender' => $sender,
267 'recipient' => $message->getPersonalRecipient(),
268 ],
269 'from' => 'user@drupal.org',
270 ];
271 $results[] = $result;
272
273 $result['key'] = 'user_copy';
274 $result['to'] = $result['from'];
275 $results[] = $result;
276 $data[] = [$message, $sender, $results];
277
278 return $data;
279 }
280
281 /**
282 * Builds a mock sender on given scenario.
283 *
284 * @param bool $anonymous
285 * TRUE if the sender is anonymous.
286 * @param string $mail_address
287 * The mail address of the user.
288 *
289 * @return \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
290 * Mock sender for testing.
291 */
292 protected function getMockSender($anonymous = TRUE, $mail_address = 'anonymous@drupal.org') {
293 $sender = $this->getMock('\Drupal\Core\Session\AccountInterface');
294 $sender->expects($this->once())
295 ->method('isAnonymous')
296 ->willReturn($anonymous);
297 $sender->expects($this->any())
298 ->method('getEmail')
299 ->willReturn($mail_address);
300 $sender->expects($this->any())
301 ->method('getDisplayName')
302 ->willReturn('user');
303 // User ID 1 has special implications, use 3 instead.
304 $sender->expects($this->any())
305 ->method('id')
306 ->willReturn($anonymous ? 0 : 3);
307 if ($anonymous) {
308 // Anonymous user values set in params include updated values for name and
309 // mail.
310 $sender->name = 'Anonymous (not verified)';
311 $sender->mail = 'anonymous@drupal.org';
312 }
313 return $sender;
314 }
315
316 /**
317 * Builds a mock message from anonymous user.
318 *
319 * @param array $recipients
320 * An array of recipient email addresses.
321 * @param bool $auto_reply
322 * TRUE if auto reply is enable.
323 * @param bool $copy_sender
324 * TRUE if a copy should be sent, FALSE if not.
325 *
326 * @return \Drupal\contact\MessageInterface|\PHPUnit_Framework_MockObject_MockObject
327 * Mock message for testing.
328 */
329 protected function getAnonymousMockMessage($recipients, $auto_reply, $copy_sender = FALSE) {
330 $message = $this->getMock('\Drupal\contact\MessageInterface');
331 $message->expects($this->any())
332 ->method('getSenderName')
333 ->willReturn('Anonymous');
334 $message->expects($this->once())
335 ->method('getSenderMail')
336 ->willReturn('anonymous@drupal.org');
337 $message->expects($this->any())
338 ->method('isPersonal')
339 ->willReturn(FALSE);
340 $message->expects($this->once())
341 ->method('copySender')
342 ->willReturn($copy_sender);
343 $message->expects($this->any())
344 ->method('getContactForm')
345 ->willReturn($this->getMockContactForm($recipients, $auto_reply));
346 return $message;
347 }
348
349 /**
350 * Builds a mock message from authenticated user.
351 *
352 * @param bool $copy_sender
353 * TRUE if a copy should be sent, FALSE if not.
354 *
355 * @return \Drupal\contact\MessageInterface|\PHPUnit_Framework_MockObject_MockObject
356 * Mock message for testing.
357 */
358 protected function getAuthenticatedMockMessage($copy_sender = FALSE) {
359 $message = $this->getMock('\Drupal\contact\MessageInterface');
360 $message->expects($this->any())
361 ->method('isPersonal')
362 ->willReturn(TRUE);
363 $message->expects($this->once())
364 ->method('copySender')
365 ->willReturn($copy_sender);
366 $recipient = $this->getMock('\Drupal\user\UserInterface');
367 $recipient->expects($this->once())
368 ->method('getEmail')
369 ->willReturn('user2@drupal.org');
370 $recipient->expects($this->once())
371 ->method('getDisplayName')
372 ->willReturn('user2');
373 $recipient->expects($this->once())
374 ->method('getPreferredLangcode')
375 ->willReturn('en');
376 $message->expects($this->any())
377 ->method('getPersonalRecipient')
378 ->willReturn($recipient);
379 $message->expects($this->any())
380 ->method('getContactForm')
381 ->willReturn($this->getMockContactForm('user2@drupal.org', FALSE));
382 return $message;
383 }
384
385 /**
386 * Builds a mock message on given scenario.
387 *
388 * @param array $recipients
389 * An array of recipient email addresses.
390 * @param string $auto_reply
391 * An auto-reply message to send to the message author.
392 *
393 * @return \Drupal\contact\ContactFormInterface|\PHPUnit_Framework_MockObject_MockObject
394 * Mock message for testing.
395 */
396 protected function getMockContactForm($recipients, $auto_reply) {
397 $contact_form = $this->getMock('\Drupal\contact\ContactFormInterface');
398 $contact_form->expects($this->once())
399 ->method('getRecipients')
400 ->willReturn($recipients);
401 $contact_form->expects($this->once())
402 ->method('getReply')
403 ->willReturn($auto_reply);
404
405 return $contact_form;
406 }
407
408 }