You are here

public function MailHandlerTest::testBasicEmail in Commerce Core 8.2

Tests sending a basic email, without any custom parameters.

File

tests/src/Kernel/MailHandlerTest.php, line 37

Class

MailHandlerTest
Tests the sending of customer emails.

Namespace

Drupal\Tests\commerce\Kernel

Code

public function testBasicEmail() {
  $body = [
    '#markup' => '<p>' . $this
      ->t('Mail Handler Test') . '</p>',
  ];
  $result = $this->mailHandler
    ->sendMail('customer@example.com', 'Test subject', $body);
  $this
    ->assertTrue($result);
  $language_manager = $this->container
    ->get('language_manager');
  $emails = $this
    ->getMails();
  $this
    ->assertEquals(1, count($emails));
  $email = reset($emails);
  $this
    ->assertEquals('text/html; charset=UTF-8;', $email['headers']['Content-Type']);
  $this
    ->assertEquals('commerce_mail', $email['id']);
  $this
    ->assertEquals('customer@example.com', $email['to']);
  $this
    ->assertFalse(isset($email['headers']['Cc']));
  $this
    ->assertFalse(isset($email['headers']['Bcc']));
  $this
    ->assertFalse(isset($email['headers']['Reply-to']));
  $this
    ->assertEquals($this->store
    ->getEmail(), $email['from']);
  $this
    ->assertEquals('Test subject', $email['subject']);
  $this
    ->assertStringContainsString('Mail Handler Test', $email['body']);
  $this
    ->assertEquals($language_manager
    ->getCurrentLanguage()
    ->getId(), $email['langcode']);

  // No email should be sent if the recipient is empty.
  $result = $this->mailHandler
    ->sendMail('', 'Test subject', $body);
  $this
    ->assertFalse($result);
}