View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
class SystemSendEmailTest extends RulesIntegrationTestBase {
protected $logger;
protected $mailManager;
protected $action;
protected function setUp() : void {
parent::setUp();
$this->mailManager = $this
->prophesize(MailManagerInterface::class);
$this->container
->set('plugin.manager.mail', $this->mailManager
->reveal());
$this->logger = $this
->prophesize(LoggerChannelInterface::class);
$logger_factory = $this
->prophesize(LoggerChannelFactoryInterface::class);
$logger_factory
->get('rules')
->willReturn($this->logger
->reveal());
$this->container
->set('logger.factory', $logger_factory
->reveal());
$this->action = $this->actionManager
->createInstance('rules_send_email');
}
public function testSummary() {
$this
->assertEquals('Send email', $this->action
->summary());
}
public function testSendMailToOneRecipient() {
$to = [
'mail@example.com',
];
$this->action
->setContextValue('to', $to)
->setContextValue('subject', 'subject')
->setContextValue('message', 'hello');
$params = [
'subject' => 'subject',
'message' => 'hello',
];
$this->mailManager
->mail('rules', 'rules_action_mail_' . $this->action
->getPluginId(), implode(', ', $to), LanguageInterface::LANGCODE_SITE_DEFAULT, $params, NULL)
->willReturn([
'result' => TRUE,
])
->shouldBeCalledTimes(1);
$this->action
->execute();
$this->logger
->notice('Successfully sent email to %recipient', [
'%recipient' => implode(', ', $to),
])
->shouldHaveBeenCalled();
}
public function testSendMailToTwoRecipients() {
$to = [
'mail@example.com',
'mail2@example.com',
];
$this->action
->setContextValue('to', $to)
->setContextValue('subject', 'subject')
->setContextValue('message', 'hello');
$params = [
'subject' => 'subject',
'message' => 'hello',
];
$this->mailManager
->mail('rules', 'rules_action_mail_' . $this->action
->getPluginId(), implode(', ', $to), LanguageInterface::LANGCODE_SITE_DEFAULT, $params, NULL)
->willReturn([
'result' => TRUE,
])
->shouldBeCalledTimes(1);
$this->action
->execute();
$this->logger
->notice('Successfully sent email to %recipient', [
'%recipient' => implode(', ', $to),
])
->shouldHaveBeenCalled();
}
}