annotate core/modules/contact/tests/src/Unit/MailHandlerTest.php @ 0:c75dbcec494b

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