You are here

public function QueueMailFunctionalTest::sendEmailAndTest in Queue Mail 8

Send an email and ensure it is queued or sent immediately.

Parameters

string $mail_key: The key of the email to send.

bool $should_be_queued: Pass in TRUE to test if the email was queued, FALSE to test that it wasn't queued.

3 calls to QueueMailFunctionalTest::sendEmailAndTest()
QueueMailFunctionalTest::testNonQueuedEmail in tests/src/Functional/QueueMailFunctionalTest.php
Test that if we're not queuing any emails that they get sent as normal.
QueueMailFunctionalTest::testQueuedEmail in tests/src/Functional/QueueMailFunctionalTest.php
Test that if we are queuing emails, that they get queued.
QueueMailFunctionalTest::testQueuedEmailKeyMatching in tests/src/Functional/QueueMailFunctionalTest.php
This tests the matching of mailkeys to be queued.

File

tests/src/Functional/QueueMailFunctionalTest.php, line 133

Class

QueueMailFunctionalTest
Tests queue mail functionality.

Namespace

Drupal\Tests\queue_mail\Functional

Code

public function sendEmailAndTest($mail_key = 'basic', $should_be_queued = TRUE) {
  $queue = _queue_mail_get_queue();

  // Parameters before testing.
  $queue_count_before = $queue
    ->numberOfItems();
  $email_count_before = count($this
    ->getMails());
  $content = $this
    ->randomMachineName();

  // Send test email.
  $message = $this
    ->sendQueueMailTest($mail_key, 'info@example.com', [
    'content' => $content,
  ]);
  $queue_count_after = $queue
    ->numberOfItems();
  $email_count_after = count($this
    ->getMails());

  // Now do the desired assertions.
  if ($should_be_queued === TRUE) {
    $this
      ->assertEquals(1, $queue_count_after - $queue_count_before, 'Email is queued.');
    $this
      ->assertEquals(0, $email_count_after - $email_count_before, 'Queued email is not sent immediately.');

    // Now run cron and see if our email gets sent.
    $queue_count_before = $queue
      ->numberOfItems();
    $email_count_before = count($this
      ->getMails());
    $this
      ->cronRun();
    $this
      ->assertMailString('body', $content, 1);
    $queue_count_after = $queue
      ->numberOfItems();
    $email_count_after = count($this
      ->getMails());
    $this
      ->assertEquals(-1, $queue_count_after - $queue_count_before, 'Email is sent from the queue.');
    $this
      ->assertEquals(1, $email_count_after - $email_count_before, 'Queued email is sent on cron.');
    $this
      ->assertMail('current_langcode', $this->langcode, 'The mail language was respected');
    $this
      ->assertTrue($message['queued'], 'Message has been added to the queue.');
  }
  elseif ($should_be_queued === FALSE) {
    $this
      ->assertEquals(0, $queue_count_after - $queue_count_before, 'Email is not queued.');
    $this
      ->assertEquals(1, $email_count_after - $email_count_before, 'Email is sent immediately.');
    $this
      ->assertMailString('body', $content, 1);
    $this
      ->assertFalse($message['queued'], 'Message has not been added to the queue.');
  }
}