function MailTest::testCancelMessage

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testCancelMessage()
  2. 10 core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testCancelMessage()
  3. 11.x 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.