EmailTest.php in Message Notify 8
File
tests/src/Unit/Plugin/Notifier/EmailTest.php
View source
<?php
namespace Drupal\Tests\message_notify\Unit\Plugin\Notifier;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityViewBuilderInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\message\MessageInterface;
use Drupal\message\MessageTemplateInterface;
use Drupal\message_notify\Exception\MessageNotifyException;
use Drupal\message_notify\Plugin\Notifier\Email;
use Drupal\Tests\UnitTestCase;
use Drupal\user\UserInterface;
class EmailTest extends UnitTestCase {
protected $configuration = [];
protected $entityTypeManager;
protected $mailManager;
protected $renderer;
protected $pluginDefinition = [
'viewModes' => [
'mail_subject',
'mail_body',
],
];
protected $pluginId;
public function setUp() {
parent::setUp();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class)
->reveal();
$this->mailManager = $this
->prophesize(MailManagerInterface::class)
->reveal();
$this->renderer = $this
->prophesize(RendererInterface::class)
->reveal();
$this->pluginId = $this
->randomMachineName();
$this->pluginDefinition['title'] = $this
->randomMachineName();
}
public function testSend() {
$message = $this
->prophesize(MessageInterface::class);
$account = $this
->prophesize(UserInterface::class);
$account
->id()
->willReturn(42);
$account
->getEmail()
->willReturn('foo@foo.com');
$account
->getPreferredLangcode()
->willReturn(Language::LANGCODE_DEFAULT);
$message
->getOwner()
->willReturn($account
->reveal());
$message
->getOwnerId()
->willReturn(42);
$template = $this
->prophesize(MessageTemplateInterface::class)
->reveal();
$message
->getTemplate()
->willReturn($template);
$message
->save()
->willReturn(1);
$view_builder = $this
->prophesize(EntityViewBuilderInterface::class)
->reveal();
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getViewBuilder('message')
->willReturn($view_builder);
$this->entityTypeManager = $entity_type_manager
->reveal();
$notifier = $this
->getNotifier();
$notifier
->setMessage($message
->reveal());
$this
->assertNull($notifier
->send());
}
public function testSendNoMessage() {
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage('No message is set for this notifier.');
$notifier = $this
->getNotifier();
$notifier
->send();
}
public function testSendNoEmail() {
$this
->expectException(MessageNotifyException::class);
$this
->expectExceptionMessage('It is not possible to send a Message to an anonymous owner. You may set an owner using ::setOwner() or pass a "mail" to the $options array.');
$message = $this
->prophesize(MessageInterface::class);
$account = $this
->prophesize(UserInterface::class)
->reveal();
$message
->getOwner()
->willReturn($account);
$notifier = $this
->getNotifier($message
->reveal());
$notifier
->deliver([]);
}
protected function getNotifier(MessageInterface $message = NULL) {
$logger = $this
->prophesize(LoggerChannelInterface::class)
->reveal();
return new Email($this->configuration, $this->pluginId, $this->pluginDefinition, $logger, $this->entityTypeManager, $this->renderer, $message, $this->mailManager);
}
}