You are here

public function OrderIntegrationTest::testEmailLog in Commerce Core 8.2

Tests that a log is generated when any order email is sent.

File

modules/log/tests/src/Kernel/OrderIntegrationTest.php, line 192

Class

OrderIntegrationTest
Tests integration with order events.

Namespace

Drupal\Tests\commerce_log\Kernel

Code

public function testEmailLog() {
  $order_receipt_mail = $this->container
    ->get('commerce_order.order_receipt_mail');
  $order_receipt_mail
    ->send($this->order);
  $this->order
    ->setData('simulate_mail_failure', TRUE);
  $order_receipt_mail
    ->send($this->order);
  $this->order
    ->unsetData('simulate_mail_failure');
  $subject = sprintf('Order %s test mail', $this->order
    ->getOrderNumber());
  $params = [
    'id' => 'order_test',
    'from' => $this->order
      ->getStore()
      ->getEmail(),
    'order' => $this->order,
  ];
  $mail_handler = $this->container
    ->get('commerce.mail_handler');
  $mail_handler
    ->sendMail($this->order
    ->getEmail(), $subject, [], $params);
  $this->order
    ->setData('simulate_mail_failure', TRUE);
  $mail_handler
    ->sendMail($this->order
    ->getEmail(), $subject, [], $params);
  $logs = $this->logStorage
    ->loadMultipleByEntity($this->order);
  $this
    ->assertEquals(4, count($logs));
  $success_log = reset($logs);
  $build = $this->logViewBuilder
    ->view($success_log);
  $this
    ->render($build);
  $this
    ->assertText(new FormattableMarkup('Order receipt email sent to @mail.', [
    '@mail' => $this->order
      ->getEmail(),
  ]));
  $failure_log = $logs[2];
  $build = $this->logViewBuilder
    ->view($failure_log);
  $this
    ->render($build);
  $this
    ->assertText(new FormattableMarkup('Order receipt email failed to send to @mail.', [
    '@mail' => $this->order
      ->getEmail(),
  ]));
  $order_test_log = $logs[3];
  $build = $this->logViewBuilder
    ->view($order_test_log);
  $this
    ->render($build);
  $this
    ->assertText(new FormattableMarkup('Email "order_test" sent to @mail.', [
    '@mail' => $this->order
      ->getEmail(),
  ]));
  $order_test_failure_log = $logs[4];
  $build = $this->logViewBuilder
    ->view($order_test_failure_log);
  $this
    ->render($build);
  $this
    ->assertText(new FormattableMarkup('Failed to send "order_test" to @mail.', [
    '@mail' => $this->order
      ->getEmail(),
  ]));
}