You are here

class EmailTest in Message Notify 8

Unit tests for the Email notifier.

@coversDefaultClass \Drupal\message_notify\Plugin\Notifier\Email

@group message_notify

Hierarchy

Expanded class hierarchy of EmailTest

File

tests/src/Unit/Plugin/Notifier/EmailTest.php, line 25

Namespace

Drupal\Tests\message_notify\Unit\Plugin\Notifier
View source
class EmailTest extends UnitTestCase {

  /**
   * Digest configuration.
   *
   * @var array
   */
  protected $configuration = [];

  /**
   * Mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The mocked mail manager service.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * The rendering service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Plugin definition.
   *
   * @var array
   */
  protected $pluginDefinition = [
    'viewModes' => [
      'mail_subject',
      'mail_body',
    ],
  ];

  /**
   * Plugin ID.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * Test the send method.
   *
   * @covers ::send
   * @covers ::setMessage
   */
  public function testSend() {

    // Mock a message object.
    $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);

    // Mock view builder.
    $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());
  }

  /**
   * Test sending without a message.
   *
   * @covers ::send
   */
  public function testSendNoMessage() {
    $this
      ->expectException(\AssertionError::class);
    $this
      ->expectExceptionMessage('No message is set for this notifier.');
    $notifier = $this
      ->getNotifier();
    $notifier
      ->send();
  }

  /**
   * Test sending without an email.
   *
   * @covers ::deliver
   */
  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([]);
  }

  /**
   * Constructs an email notifier.
   *
   * @param \Drupal\message\MessageInterface $message
   *   (optional) The message to construct the notifier with.
   *
   * @return \Drupal\message_notify\Plugin\Notifier\Email
   *   The email notifier plugin.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmailTest::$configuration protected property Digest configuration.
EmailTest::$entityTypeManager protected property Mocked entity type manager.
EmailTest::$mailManager protected property The mocked mail manager service.
EmailTest::$pluginDefinition protected property Plugin definition.
EmailTest::$pluginId protected property Plugin ID.
EmailTest::$renderer protected property The rendering service.
EmailTest::getNotifier protected function Constructs an email notifier.
EmailTest::setUp public function Overrides UnitTestCase::setUp
EmailTest::testSend public function Test the send method.
EmailTest::testSendNoEmail public function Test sending without an email.
EmailTest::testSendNoMessage public function Test sending without a message.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.