View source
<?php
namespace Drupal\Tests\commerce_invoice\Kernel;
use Drupal\commerce_invoice\Entity\Invoice;
use Drupal\commerce_invoice\Entity\InvoiceType;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Site\Settings;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\profile\Entity\Profile;
class InvoiceConfirmationTest extends InvoiceKernelTestBase {
use AssertMailTrait;
protected $order;
protected $profile;
protected $user;
protected $translations = [
'fr' => [
'Invoice #@number' => 'Facture #@number',
'A new invoice has been created for you.' => 'Une nouvelle facture a été créée pour vous.',
'Default store' => 'Magasin par défaut',
],
'es' => [
'Invoice #@number' => 'Factura #@number',
'A new invoice has been created for you.' => 'Se ha creado una nueva factura para usted.',
'Default store' => 'Tienda por defecto',
],
];
public static $modules = [
'language',
'locale',
'content_translation',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'language',
]);
$this
->installSchema('locale', [
'locales_source',
'locales_target',
'locales_location',
]);
foreach (array_keys($this->translations) as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$settings = Settings::getAll();
foreach ($this->translations as $langcode => $custom_translation) {
foreach ($custom_translation as $untranslated => $translated) {
$settings['locale_custom_strings_' . $langcode][''][$untranslated] = $translated;
}
}
new Settings($settings);
$currency_importer = $this->container
->get('commerce_price.currency_importer');
$currency_importer
->importTranslations(array_keys($this->translations));
$language_manager = $this->container
->get('language_manager');
$fr_usd = $language_manager
->getLanguageConfigOverride('fr', 'commerce_price.commerce_currency.USD');
$fr_usd
->set('symbol', 'U$D');
$fr_usd
->save();
$this->store = $this
->reloadEntity($this->store);
$this->store
->addTranslation('es', [
'name' => $this->translations['es']['Default store'],
]);
$this->store
->addTranslation('fr', [
'name' => $this->translations['fr']['Default store'],
]);
$this->store
->save();
$user = $this
->createUser();
$this->user = $this
->reloadEntity($user);
$profile = Profile::create([
'type' => 'customer',
'address' => [
'country_code' => 'US',
'postal_code' => '53177',
'locality' => 'Milwaukee',
'address_line1' => 'Pabst Blue Ribbon Dr',
'administrative_area' => 'WI',
'given_name' => 'Frederick',
'family_name' => 'Pabst',
],
'uid' => $this->user
->id(),
]);
$profile
->save();
$this->profile = $this
->reloadEntity($profile);
$order_item = OrderItem::create([
'type' => 'test',
'quantity' => 1,
'title' => $this
->randomString(),
'unit_price' => new Price('12.00', 'USD'),
]);
$order_item
->save();
$this->order = Order::create([
'type' => 'default',
'mail' => $this->user
->getEmail(),
'state' => 'completed',
'store_id' => $this->store,
'billing_profile' => $this->profile,
'uid' => $this->user
->id(),
'order_items' => [
$order_item,
],
]);
$this->order
->save();
$this->order = $this
->reloadEntity($this->order);
}
public function testInvoiceConfirmationDisabled() {
$invoice = Invoice::create([
'type' => 'default',
'mail' => $this->user
->getEmail(),
'invoice_number' => '10',
'orders' => [
$this->order,
],
'store_id' => $this->store
->id(),
'billing_profile' => $this->profile,
'uid' => $this->user
->id(),
]);
$invoice
->getState()
->applyTransitionById('confirm');
$invoice
->save();
$this
->assertEmpty($invoice
->getFile());
$this
->assertCount(0, $this
->getMails());
}
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 {
$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']);
$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']);
}
public function providerInvoiceConfirmationMultilingualData() {
return [
[
NULL,
'en',
],
[
'es',
'es',
],
[
'fr',
'fr',
],
[
'en',
'en',
],
];
}
}