You are here

public function OrderMultilingualTest::testOrderItemPurchasedEntityTranslated in Commerce Core 8.2

Tests that the order item returns a translated order item.

File

modules/order/tests/src/Kernel/OrderMultilingualTest.php, line 74

Class

OrderMultilingualTest
Tests orders and order items in a multilingual context.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testOrderItemPurchasedEntityTranslated() {
  $variation_type = ProductVariationType::create([
    'id' => 'test',
    'label' => 'Test',
    'orderItemType' => 'default',
    'generateTitle' => FALSE,
  ]);
  $variation_type
    ->save();
  $this->container
    ->get('content_translation.manager')
    ->setEnabled('commerce_product_variation', 'default', TRUE);

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = ProductVariation::create([
    'type' => 'test',
    'title' => 'My Super Product',
  ]);
  $variation
    ->addTranslation('fr', [
    'title' => 'Mon super produit',
  ]);
  $order_item = OrderItem::create([
    'type' => 'default',
    'purchased_entity' => $variation,
  ]);
  $this
    ->assertEquals('My Super Product', $order_item
    ->getPurchasedEntity()
    ->label());
  $this
    ->config('system.site')
    ->set('default_langcode', 'fr')
    ->save();
  $this
    ->assertEquals('Mon super produit', $order_item
    ->getPurchasedEntity()
    ->label());

  // Change the default site language and ensure the purchased entity is
  // returned even if it has not been translated to that language.
  $this
    ->config('system.site')
    ->set('default_langcode', 'sr')
    ->save();
  $this
    ->assertEquals('My Super Product', $order_item
    ->getPurchasedEntity()
    ->label());
}