3 namespace Drupal\Tests\contact\Unit;
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;
13 * @coversDefaultClass \Drupal\contact\MailHandler
16 class MailHandlerTest extends UnitTestCase {
19 * Language manager service.
21 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
23 protected $languageManager;
28 * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
33 * Mail manager service.
35 * @var \Drupal\Core\Mail\MailManagerInterface|\PHPUnit_Framework_MockObject_MockObject
37 protected $mailManager;
40 * Contact mail messages service.
42 * @var \Drupal\contact\MailHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
44 protected $contactMailHandler;
47 * The contact form entity.
49 * @var \Drupal\contact\ContactFormInterface|\PHPUnit_Framework_MockObject_MockObject
51 protected $contactForm;
54 * The entity manager service.
56 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
58 protected $entityManager;
61 * The user storage handler.
63 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
65 protected $userStorage;
70 protected function 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')
80 ->willReturn($this->userStorage);
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']);
86 $this->languageManager->expects($this->any())
87 ->method('getDefaultLanguage')
88 ->will($this->returnValue($language));
90 $this->languageManager->expects($this->any())
91 ->method('getCurrentLanguage')
92 ->will($this->returnValue($language));
96 * Tests the children() method with an invalid key.
98 * @covers ::sendMailMessages
100 public function testInvalidRecipient() {
101 $message = $this->getMock('\Drupal\contact\MessageInterface');
102 $message->expects($this->once())
103 ->method('isPersonal')
105 $message->expects($this->once())
106 ->method('getPersonalRecipient')
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())
114 ->willReturn($sender);
115 // User IDs 1 and 0 have special implications, use 3 instead.
116 $sender->expects($this->any())
119 $sender->expects($this->once())
120 ->method('isAnonymous')
122 $this->setExpectedException(MailHandlerException::class, 'Unable to determine message recipient');
123 $this->contactMailHandler->sendMailMessages($message, $sender);
127 * Tests the sendMailMessages method.
129 * @dataProvider getSendMailMessages
131 * @covers ::sendMailMessages
133 public function testSendMailMessages(MessageInterface $message, AccountInterface $sender, $results) {
134 $this->logger->expects($this->once())
136 $this->mailManager->expects($this->any())
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']);
148 $this->userStorage->expects($this->any())
150 ->willReturn(clone $sender);
151 $this->contactMailHandler->sendMailMessages($message, $sender);
155 * Data provider for ::testSendMailMessages.
157 public function getSendMailMessages() {
159 $recipients = ['admin@drupal.org', 'user@drupal.org'];
161 'module' => 'contact',
163 'to' => implode(', ', $recipients),
166 'from' => 'anonymous@drupal.org',
169 $message = $this->getAnonymousMockMessage($recipients, '');
170 $sender = $this->getMockSender();
172 'key' => 'page_mail',
174 'contact_message' => $message,
176 'contact_form' => $message->getContactForm(),
179 $results[] = $result + $default_result;
180 $data[] = [$message, $sender, $results];
183 $message = $this->getAnonymousMockMessage($recipients, 'reply');
184 $sender = $this->getMockSender();
186 'key' => 'page_mail',
188 'contact_message' => $message,
190 'contact_form' => $message->getContactForm(),
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];
201 $message = $this->getAnonymousMockMessage($recipients, '', TRUE);
202 $sender = $this->getMockSender();
204 'key' => 'page_mail',
206 'contact_message' => $message,
208 'contact_form' => $message->getContactForm(),
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];
218 $message = $this->getAnonymousMockMessage($recipients, 'reply', TRUE);
219 $sender = $this->getMockSender();
221 'key' => 'page_mail',
223 'contact_message' => $message,
225 'contact_form' => $message->getContactForm(),
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];
237 // For authenticated user.
239 $message = $this->getAuthenticatedMockMessage();
240 $sender = $this->getMockSender(FALSE, 'user@drupal.org');
242 'module' => 'contact',
243 'key' => 'user_mail',
244 'to' => 'user2@drupal.org',
247 'contact_message' => $message,
249 'recipient' => $message->getPersonalRecipient(),
251 'from' => 'user@drupal.org',
253 $results[] = $result;
254 $data[] = [$message, $sender, $results];
257 $message = $this->getAuthenticatedMockMessage(TRUE);
258 $sender = $this->getMockSender(FALSE, 'user@drupal.org');
260 'module' => 'contact',
261 'key' => 'user_mail',
262 'to' => 'user2@drupal.org',
265 'contact_message' => $message,
267 'recipient' => $message->getPersonalRecipient(),
269 'from' => 'user@drupal.org',
271 $results[] = $result;
273 $result['key'] = 'user_copy';
274 $result['to'] = $result['from'];
275 $results[] = $result;
276 $data[] = [$message, $sender, $results];
282 * Builds a mock sender on given scenario.
284 * @param bool $anonymous
285 * TRUE if the sender is anonymous.
286 * @param string $mail_address
287 * The mail address of the user.
289 * @return \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
290 * Mock sender for testing.
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())
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())
306 ->willReturn($anonymous ? 0 : 3);
308 // Anonymous user values set in params include updated values for name and
310 $sender->name = 'Anonymous (not verified)';
311 $sender->mail = 'anonymous@drupal.org';
317 * Builds a mock message from anonymous user.
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.
326 * @return \Drupal\contact\MessageInterface|\PHPUnit_Framework_MockObject_MockObject
327 * Mock message for testing.
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')
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));
350 * Builds a mock message from authenticated user.
352 * @param bool $copy_sender
353 * TRUE if a copy should be sent, FALSE if not.
355 * @return \Drupal\contact\MessageInterface|\PHPUnit_Framework_MockObject_MockObject
356 * Mock message for testing.
358 protected function getAuthenticatedMockMessage($copy_sender = FALSE) {
359 $message = $this->getMock('\Drupal\contact\MessageInterface');
360 $message->expects($this->any())
361 ->method('isPersonal')
363 $message->expects($this->once())
364 ->method('copySender')
365 ->willReturn($copy_sender);
366 $recipient = $this->getMock('\Drupal\user\UserInterface');
367 $recipient->expects($this->once())
369 ->willReturn('user2@drupal.org');
370 $recipient->expects($this->any())
371 ->method('getDisplayName')
372 ->willReturn('user2');
373 $recipient->expects($this->once())
374 ->method('getPreferredLangcode')
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));
386 * Builds a mock message on given scenario.
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.
393 * @return \Drupal\contact\ContactFormInterface|\PHPUnit_Framework_MockObject_MockObject
394 * Mock message for testing.
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())
403 ->willReturn($auto_reply);
405 return $contact_form;