You are here

protected function OrderReceiptTest::setUp in Commerce Core 8.2

Overrides OrderKernelTestBase::setUp

File

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

Class

OrderReceiptTest
Tests the sending of multilingual order receipt emails.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installConfig([
    'language',
  ]);
  $this
    ->installSchema('locale', [
    'locales_source',
    'locales_target',
    'locales_location',
  ]);
  $user = $this
    ->createUser([
    'mail' => $this
      ->randomString() . '@example.com',
  ]);
  foreach (array_keys($this->translations) as $langcode) {
    ConfigurableLanguage::createFromLangcode($langcode)
      ->save();
  }

  // Provide the translated strings by overriding in-memory settings.
  $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);

  /** @var \Drupal\commerce_price\CurrencyImporterInterface $currency_importer */
  $currency_importer = $this->container
    ->get('commerce_price.currency_importer');
  $currency_importer
    ->importTranslations(array_keys($this->translations));

  /** @var \Drupal\language\ConfigurableLanguageManagerInterface $language_manager */
  $language_manager = $this->container
    ->get('language_manager');

  // The translated USD symbol is $US in both French and Spanish.
  // Invent a new symbol translation for French, to test translations.
  $fr_usd = $language_manager
    ->getLanguageConfigOverride('fr', 'commerce_price.commerce_currency.USD');
  $fr_usd
    ->set('symbol', 'U$D');
  $fr_usd
    ->save();
  $order_type = OrderType::load('default');
  $order_type
    ->setReceiptBcc('bcc@example.com');
  $order_type
    ->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();

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = PaymentGateway::create([
    'id' => 'cod',
    'label' => 'Manual',
    'plugin' => 'manual',
    'configuration' => [
      'display_label' => 'Cash on delivery',
      'instructions' => [
        'value' => 'Sample payment instructions.',
        'format' => 'plain_text',
      ],
    ],
  ]);
  $payment_gateway
    ->save();
  $es_payment_gateway = $language_manager
    ->getLanguageConfigOverride('es', 'commerce_payment.commerce_payment_gateway.cod');
  $es_payment_gateway
    ->set('configuration', [
    'display_label' => $this->translations['es']['Cash on delivery'],
  ]);
  $es_payment_gateway
    ->save();
  $fr_payment_gateway = $language_manager
    ->getLanguageConfigOverride('fr', 'commerce_payment.commerce_payment_gateway.cod');
  $fr_payment_gateway
    ->set('configuration', [
    'display_label' => $this->translations['fr']['Cash on delivery'],
  ]);
  $fr_payment_gateway
    ->save();
  $product = Product::create([
    'type' => 'default',
    'title' => 'Default testing product',
  ]);
  $product
    ->save();
  $variation1 = ProductVariation::create([
    'type' => 'default',
    'product_id' => $product
      ->id(),
    'sku' => 'TEST_' . strtolower($this
      ->randomMachineName()),
    'status' => 1,
    'price' => new Price('12.00', 'USD'),
  ]);
  $variation1
    ->save();
  $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' => $user
      ->id(),
  ]);
  $profile
    ->save();
  $profile = $this
    ->reloadEntity($profile);

  /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
  $order_item_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_order_item');
  $order_item1 = $order_item_storage
    ->createFromPurchasableEntity($variation1);
  $order_item1
    ->save();
  $order = Order::create([
    'type' => 'default',
    'state' => 'draft',
    'mail' => $user
      ->getEmail(),
    'uid' => $user
      ->id(),
    'ip_address' => '127.0.0.1',
    'billing_profile' => $profile,
    'store_id' => $this->store
      ->id(),
    'order_items' => [
      $order_item1,
    ],
    'payment_gateway' => $payment_gateway
      ->id(),
  ]);
  $order
    ->save();
  $this->order = $this
    ->reloadEntity($order);
}