View source
<?php
namespace Drupal\Tests\commerce_order\Kernel;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\language\Entity\ConfigurableLanguage;
class OrderMultilingualTest extends OrderKernelTestBase {
public static $modules = [
'language',
'content_translation',
];
protected function setUp() : void {
parent::setUp();
ConfigurableLanguage::createFromLangcode('fr')
->save();
ConfigurableLanguage::createFromLangcode('sr')
->save();
}
public function testOrderStoreTranslated() {
$this->container
->get('content_translation.manager')
->setEnabled('commerce_store', 'online', TRUE);
$this->store = $this
->reloadEntity($this->store);
$this->store
->addTranslation('fr', [
'name' => 'Magasin par défaut',
])
->save();
$user = $this
->createUser([
'mail' => $this
->randomString() . '@example.com',
]);
$order = Order::create([
'type' => 'default',
'store_id' => $this->store
->id(),
'state' => 'draft',
'mail' => 'text@example.com',
'uid' => $user
->id(),
'ip_address' => '127.0.0.1',
'order_items' => [],
]);
$this
->assertEquals('Default store', $order
->getStore()
->label());
$this
->config('system.site')
->set('default_langcode', 'fr')
->save();
$this
->assertEquals('Magasin par défaut', $order
->getStore()
->label());
$this
->config('system.site')
->set('default_langcode', 'sr')
->save();
$this
->assertEquals('Default store', $order
->getStore()
->label());
}
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);
$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());
$this
->config('system.site')
->set('default_langcode', 'sr')
->save();
$this
->assertEquals('My Super Product', $order_item
->getPurchasedEntity()
->label());
}
}