You are here

public function OrderReceiptTest::testOrderReceipt in Commerce Core 8.2

Tests that the email is sent and translated to the customer's language.

The email is sent in the customer's langcode if the user is not anonymous, otherwise it is the site's default langcode. In #2603482 this could be changed to use the order's langcode.

@dataProvider providerOrderReceiptMultilingualData

Parameters

string $langcode: The langcode to test with.

string $expected_langcode: The expected langcode.

string $expected_order_total: The expected order total.

File

modules/order/tests/src/Kernel/OrderReceiptTest.php, line 216

Class

OrderReceiptTest
Tests the sending of multilingual order receipt emails.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testOrderReceipt($langcode, $expected_langcode, $expected_order_total) {
  $customer = $this->order
    ->getCustomer();
  $customer
    ->set('preferred_langcode', $langcode);
  $customer
    ->save();
  $this->order
    ->setOrderNumber('123456789');
  $this->order
    ->getState()
    ->applyTransitionById('place');
  $this->order
    ->save();
  if (isset($this->translations[$expected_langcode])) {
    $strings = $this->translations[$expected_langcode];
  }
  else {

    // Use the untranslated strings.
    $strings = array_keys($this->translations['fr']);
    $strings = array_combine($strings, $strings);
  }
  $subject = new FormattableMarkup($strings['Order #@number confirmed'], [
    '@number' => $this->order
      ->getOrderNumber(),
  ]);
  $emails = $this
    ->getMails();
  $email = reset($emails);
  $this
    ->assertEquals($this->order
    ->getEmail(), $email['to']);
  $this
    ->assertEquals('bcc@example.com', $email['headers']['Bcc']);
  $this
    ->assertEquals($expected_langcode, $email['langcode']);
  $this
    ->assertStringContainsString((string) $subject, $email['subject']);
  $this
    ->assertStringContainsString($strings['Thank you for your order!'], $email['body']);
  $this
    ->assertStringContainsString($strings['Default store'], $email['body']);
  $this
    ->assertStringContainsString($strings['Cash on delivery'], $email['body']);
  $this
    ->assertStringContainsString('Order Total: ' . $expected_order_total, $email['body']);
}