You are here

public function InvoiceConfirmationTest::testInvoiceConfirmation in Commerce Invoice 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 invoice's langcode.

@dataProvider providerInvoiceConfirmationMultilingualData

Parameters

string $langcode: The langcode to test with.

string $expected_langcode: The expected langcode.

File

tests/src/Kernel/InvoiceConfirmationTest.php, line 188

Class

InvoiceConfirmationTest
Tests the sending of multilingual invoice confirmation emails.

Namespace

Drupal\Tests\commerce_invoice\Kernel

Code

public function testInvoiceConfirmation($langcode, $expected_langcode) {
  $customer = $this->order
    ->getCustomer();
  $customer
    ->set('preferred_langcode', $langcode);
  $customer
    ->save();
  $invoice_type = InvoiceType::load('default');
  $invoice_type
    ->setSendConfirmation(TRUE);
  $invoice_type
    ->setConfirmationBcc('bcc@example.com');
  $invoice_type
    ->save();
  if ($langcode) {
    $config = ContentLanguageSettings::loadByEntityTypeBundle('commerce_invoice', 'default');
    $config
      ->setDefaultLangcode($langcode);
    $config
      ->setLanguageAlterable(FALSE);
    $config
      ->save();
  }
  $invoice = Invoice::create([
    'type' => 'default',
    'mail' => $this->user
      ->getEmail(),
    'invoice_number' => '10',
    'orders' => [
      $this->order,
    ],
    'store_id' => $this->store
      ->id(),
    'billing_profile' => $this->profile,
    'state' => 'draft',
    'uid' => $this->user
      ->id(),
  ]);
  $invoice
    ->getState()
    ->applyTransitionById('confirm');
  $invoice
    ->save();
  $this->container
    ->get('commerce_invoice.invoice_confirmation_subscriber')
    ->destruct();
  $invoice = $this
    ->reloadEntity($invoice);
  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['Invoice #@number'], [
    '@number' => $invoice
      ->getInvoiceNumber(),
  ]);
  $emails = $this
    ->getMails();
  $email = reset($emails);
  $this
    ->assertEquals($invoice
    ->getEmail(), $email['to']);
  $this
    ->assertEquals('bcc@example.com', $email['headers']['Bcc']);
  $this
    ->assertEquals($expected_langcode, $email['langcode']);
  $this
    ->assertEquals((string) $subject, $email['subject']);
  $this
    ->assertStringContainsString('A new invoice has been created for you.', $email['body']);
  $this
    ->assertNotEmpty($email['params']['attachments']);

  // Confirm that pdf file has been created for the invoice.
  $file = $invoice
    ->getFile();
  $this
    ->assertNotEmpty($file);
  $this
    ->assertRegExp('#private://(.*)\\.pdf#', $file
    ->getFileUri());
  $this
    ->assertEquals('10-' . $expected_langcode . '-pending.pdf', $file
    ->getFilename());
  $this
    ->assertEquals('application/pdf', $file
    ->getMimeType());
  $attachments = $email['params']['attachments'];
  $attachment = reset($attachments);
  $this
    ->assertEquals($file
    ->getFileUri(), $attachment['filepath']);
}