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\user\RoleInterface;
use Drupal\user\UserInterface;
use Drupal\user\UserStorageInterface;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
use Prophecy\Argument;
class SystemEmailToUsersOfRoleTest extends RulesEntityIntegrationTestBase {
protected $logger;
protected $mailManager;
protected $accounts;
protected $userStorage;
protected $action;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('user');
$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->mailManager = $this
->prophesize(MailManagerInterface::class);
$this->container
->set('plugin.manager.mail', $this->mailManager
->reveal());
$this->accounts = [];
for ($i = 0; $i < 3; $i++) {
$account = $this
->prophesizeEntity(UserInterface::class);
$account
->getPreferredLangcode()
->willReturn('site_default');
$account
->getEmail()
->willReturn('user' . $i . '@example.com');
$account
->addRole('recipient');
if ($i == 0) {
$account
->addRole('moderator');
}
$this->accounts[] = $account
->reveal();
}
$this->userStorage = $this
->prophesize(UserStorageInterface::class);
$this->entityTypeManager
->getStorage('user')
->willReturn($this->userStorage
->reveal());
$this->action = $this->actionManager
->createInstance('rules_email_to_users_of_role');
}
public function testSummary() {
$this
->assertEquals('Send email to all users of a role', $this->action
->summary());
}
public function testSendMailToOneRoles() {
$recipient = $this
->prophesize(RoleInterface::class);
$recipient
->id()
->willReturn('recipient');
$roles = [
$recipient
->reveal(),
];
$this->action
->setContextValue('roles', $roles)
->setContextValue('subject', 'subject')
->setContextValue('message', 'hello');
$params = [
'subject' => 'subject',
'message' => 'hello',
];
$rids = [
'recipient',
];
$this->userStorage
->loadByProperties([
'roles' => $rids,
'status' => 1,
])
->willReturn($this->accounts);
$this->mailManager
->mail('rules', 'rules_action_mail_' . $this->action
->getPluginId(), Argument::any(), LanguageInterface::LANGCODE_SITE_DEFAULT, $params, NULL)
->willReturn([
'result' => TRUE,
])
->shouldBeCalledTimes(3);
$this->action
->execute();
$this->logger
->notice('Successfully sent email to %number out of %count users having the role(s) %roles', [
'%number' => 3,
'%count' => count($this->accounts),
'%roles' => implode(', ', $rids),
])
->shouldHaveBeenCalled();
}
public function testSendMailToTwoRoles() {
$recipient = $this
->prophesize(RoleInterface::class);
$recipient
->id()
->willReturn('recipient');
$moderator = $this
->prophesize(RoleInterface::class);
$moderator
->id()
->willReturn('moderator');
$roles = [
$recipient
->reveal(),
$moderator
->reveal(),
];
$this->action
->setContextValue('roles', $roles)
->setContextValue('subject', 'subject')
->setContextValue('message', 'hello');
$params = [
'subject' => 'subject',
'message' => 'hello',
];
$rids = [
'recipient',
'moderator',
];
$this->userStorage
->loadByProperties([
'roles' => $rids,
'status' => 1,
])
->willReturn([
$this->accounts[0],
]);
$this->mailManager
->mail('rules', 'rules_action_mail_' . $this->action
->getPluginId(), Argument::any(), LanguageInterface::LANGCODE_SITE_DEFAULT, $params, NULL)
->willReturn([
'result' => TRUE,
])
->shouldBeCalledTimes(1);
$this->action
->execute();
$this->logger
->notice('Successfully sent email to %number out of %count users having the role(s) %roles', [
'%number' => 1,
'%count' => 1,
'%roles' => implode(', ', $rids),
])
->shouldHaveBeenCalled();
}
}