annotate core/modules/contact/tests/src/Unit/MailHandlerTest.php @ 19:fa3358dc1485 tip

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