View source
<?php
namespace Drupal\Tests\contact\Unit;
use Drupal\contact\MailHandler;
use Drupal\contact\MailHandlerException;
use Drupal\contact\MessageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
class MailHandlerTest extends UnitTestCase {
protected $languageManager;
protected $logger;
protected $mailManager;
protected $contactMailHandler;
protected $contactForm;
protected $entityTypeManager;
protected $userStorage;
protected function setUp() : void {
parent::setUp();
$this->mailManager = $this
->createMock('\\Drupal\\Core\\Mail\\MailManagerInterface');
$this->languageManager = $this
->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$this->logger = $this
->createMock('\\Psr\\Log\\LoggerInterface');
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->userStorage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->with('user')
->willReturn($this->userStorage);
$string_translation = $this
->getStringTranslationStub();
$this->contactMailHandler = new MailHandler($this->mailManager, $this->languageManager, $this->logger, $string_translation, $this->entityTypeManager);
$language = new Language([
'id' => 'en',
]);
$this->languageManager
->expects($this
->any())
->method('getDefaultLanguage')
->will($this
->returnValue($language));
$this->languageManager
->expects($this
->any())
->method('getCurrentLanguage')
->will($this
->returnValue($language));
}
public function testInvalidRecipient() {
$message = $this
->createMock('\\Drupal\\contact\\MessageInterface');
$message
->expects($this
->once())
->method('isPersonal')
->willReturn(TRUE);
$message
->expects($this
->once())
->method('getPersonalRecipient')
->willReturn(NULL);
$message
->expects($this
->once())
->method('getContactForm')
->willReturn($this
->createMock('\\Drupal\\contact\\ContactFormInterface'));
$sender = $this
->createMock('\\Drupal\\Core\\Session\\AccountInterface');
$this->userStorage
->expects($this
->any())
->method('load')
->willReturn($sender);
$sender
->expects($this
->any())
->method('id')
->willReturn(3);
$sender
->expects($this
->once())
->method('isAnonymous')
->willReturn(FALSE);
$this
->expectException(MailHandlerException::class);
$this
->expectExceptionMessage('Unable to determine message recipient');
$this->contactMailHandler
->sendMailMessages($message, $sender);
}
public function testSendMailMessages(MessageInterface $message, AccountInterface $sender, $results) {
$this->logger
->expects($this
->once())
->method('notice');
$this->mailManager
->expects($this
->any())
->method('mail')
->willReturnCallback(function ($module, $key, $to, $langcode, $params, $from) use (&$results) {
$result = array_shift($results);
$this
->assertEquals($module, $result['module']);
$this
->assertEquals($key, $result['key']);
$this
->assertEquals($to, $result['to']);
$this
->assertEquals($langcode, $result['langcode']);
$this
->assertEquals($params, $result['params']);
$this
->assertEquals($from, $result['from']);
});
$this->userStorage
->expects($this
->any())
->method('load')
->willReturn(clone $sender);
$this->contactMailHandler
->sendMailMessages($message, $sender);
}
public function getSendMailMessages() {
$data = [];
$recipients = [
'admin@drupal.org',
'user@drupal.org',
];
$default_result = [
'module' => 'contact',
'key' => '',
'to' => implode(', ', $recipients),
'langcode' => 'en',
'params' => [],
'from' => 'anonymous@drupal.org',
];
$results = [];
$message = $this
->getAnonymousMockMessage($recipients, '');
$sender = $this
->getMockSender();
$result = [
'key' => 'page_mail',
'params' => [
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message
->getContactForm(),
],
];
$results[] = $result + $default_result;
$data[] = [
$message,
$sender,
$results,
];
$results = [];
$message = $this
->getAnonymousMockMessage($recipients, 'reply');
$sender = $this
->getMockSender();
$result = [
'key' => 'page_mail',
'params' => [
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message
->getContactForm(),
],
];
$results[] = $result + $default_result;
$result['key'] = 'page_autoreply';
$result['to'] = 'anonymous@drupal.org';
$result['from'] = NULL;
$results[] = $result + $default_result;
$data[] = [
$message,
$sender,
$results,
];
$results = [];
$message = $this
->getAnonymousMockMessage($recipients, '', TRUE);
$sender = $this
->getMockSender();
$result = [
'key' => 'page_mail',
'params' => [
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message
->getContactForm(),
],
];
$results[] = $result + $default_result;
$result['key'] = 'page_copy';
$result['to'] = 'anonymous@drupal.org';
$results[] = $result + $default_result;
$data[] = [
$message,
$sender,
$results,
];
$results = [];
$message = $this
->getAnonymousMockMessage($recipients, 'reply', TRUE);
$sender = $this
->getMockSender();
$result = [
'key' => 'page_mail',
'params' => [
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message
->getContactForm(),
],
];
$results[] = $result + $default_result;
$result['key'] = 'page_copy';
$result['to'] = 'anonymous@drupal.org';
$results[] = $result + $default_result;
$result['key'] = 'page_autoreply';
$result['from'] = NULL;
$results[] = $result + $default_result;
$data[] = [
$message,
$sender,
$results,
];
$results = [];
$message = $this
->getAuthenticatedMockMessage();
$sender = $this
->getMockSender(FALSE, 'user@drupal.org');
$result = [
'module' => 'contact',
'key' => 'user_mail',
'to' => 'user2@drupal.org',
'langcode' => 'en',
'params' => [
'contact_message' => $message,
'sender' => $sender,
'recipient' => $message
->getPersonalRecipient(),
],
'from' => 'user@drupal.org',
];
$results[] = $result;
$data[] = [
$message,
$sender,
$results,
];
$results = [];
$message = $this
->getAuthenticatedMockMessage(TRUE);
$sender = $this
->getMockSender(FALSE, 'user@drupal.org');
$result = [
'module' => 'contact',
'key' => 'user_mail',
'to' => 'user2@drupal.org',
'langcode' => 'en',
'params' => [
'contact_message' => $message,
'sender' => $sender,
'recipient' => $message
->getPersonalRecipient(),
],
'from' => 'user@drupal.org',
];
$results[] = $result;
$result['key'] = 'user_copy';
$result['to'] = $result['from'];
$results[] = $result;
$data[] = [
$message,
$sender,
$results,
];
return $data;
}
protected function getMockSender($anonymous = TRUE, $mail_address = 'anonymous@drupal.org') {
$sender = $this
->createMock('\\Drupal\\Core\\Session\\AccountInterface');
$sender
->expects($this
->once())
->method('isAnonymous')
->willReturn($anonymous);
$sender
->expects($this
->any())
->method('getEmail')
->willReturn($mail_address);
$sender
->expects($this
->any())
->method('getDisplayName')
->willReturn('user');
$sender
->expects($this
->any())
->method('id')
->willReturn($anonymous ? 0 : 3);
if ($anonymous) {
$sender->name = 'Anonymous (not verified)';
$sender->mail = 'anonymous@drupal.org';
}
return $sender;
}
protected function getAnonymousMockMessage($recipients, $auto_reply, $copy_sender = FALSE) {
$message = $this
->createMock('\\Drupal\\contact\\MessageInterface');
$message
->expects($this
->any())
->method('getSenderName')
->willReturn('Anonymous');
$message
->expects($this
->once())
->method('getSenderMail')
->willReturn('anonymous@drupal.org');
$message
->expects($this
->any())
->method('isPersonal')
->willReturn(FALSE);
$message
->expects($this
->once())
->method('copySender')
->willReturn($copy_sender);
$message
->expects($this
->any())
->method('getContactForm')
->willReturn($this
->getMockContactForm($recipients, $auto_reply));
return $message;
}
protected function getAuthenticatedMockMessage($copy_sender = FALSE) {
$message = $this
->createMock('\\Drupal\\contact\\MessageInterface');
$message
->expects($this
->any())
->method('isPersonal')
->willReturn(TRUE);
$message
->expects($this
->once())
->method('copySender')
->willReturn($copy_sender);
$recipient = $this
->createMock('\\Drupal\\user\\UserInterface');
$recipient
->expects($this
->once())
->method('getEmail')
->willReturn('user2@drupal.org');
$recipient
->expects($this
->any())
->method('getDisplayName')
->willReturn('user2');
$recipient
->expects($this
->once())
->method('getPreferredLangcode')
->willReturn('en');
$message
->expects($this
->any())
->method('getPersonalRecipient')
->willReturn($recipient);
$message
->expects($this
->any())
->method('getContactForm')
->willReturn($this
->getMockContactForm('user2@drupal.org', FALSE));
return $message;
}
protected function getMockContactForm($recipients, $auto_reply) {
$contact_form = $this
->createMock('\\Drupal\\contact\\ContactFormInterface');
$contact_form
->expects($this
->once())
->method('getRecipients')
->willReturn($recipients);
$contact_form
->expects($this
->once())
->method('getReply')
->willReturn($auto_reply);
return $contact_form;
}
}