You are here

public function SystemEmailToUsersOfRoleTest::testSendMailToTwoRoles in Rules 8.3

Tests sending an email to two roles.

@covers ::execute

File

tests/src/Unit/Integration/RulesAction/SystemEmailToUsersOfRoleTest.php, line 145

Class

SystemEmailToUsersOfRoleTest
@coversDefaultClass \Drupal\rules\Plugin\RulesAction\SystemEmailToUsersOfRole @group RulesAction

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testSendMailToTwoRoles() {

  // Mock the 'recipient' and 'moderator' roles.
  $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();
}