You are here

public function MailTest::testCancelMessage in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testCancelMessage()

Test that message sending may be canceled.

See also

mail_cancel_test_mail_alter()

File

core/modules/system/tests/src/Kernel/Mail/MailTest.php, line 96

Class

MailTest
Performs tests on the pluggable mailing framework.

Namespace

Drupal\Tests\system\Kernel\Mail

Code

public function testCancelMessage() {
  $language_interface = \Drupal::languageManager()
    ->getCurrentLanguage();

  // Reset the state variable that holds sent messages.
  \Drupal::state()
    ->set('system.test_mail_collector', []);

  // Send a test message that mail_cancel_test_alter should cancel.
  \Drupal::service('plugin.manager.mail')
    ->mail('mail_cancel_test', 'cancel_test', 'cancel@example.com', $language_interface
    ->getId());

  // Retrieve sent message.
  $captured_emails = \Drupal::state()
    ->get('system.test_mail_collector');
  $sent_message = end($captured_emails);

  // Assert that the message was not actually sent.
  // Message was canceled.
  $this
    ->assertFalse($sent_message);
}