public function ProductMultilingualTest::testProductVariationsTranslated in Commerce Core 8.2
Tests that the product's variations are translated to specified language.
File
- modules/
product/ tests/ src/ Kernel/ ProductMultilingualTest.php, line 90
Class
- ProductMultilingualTest
- Tests the product and variation entity in a multilingual context.
Namespace
Drupal\Tests\commerce_product\KernelCode
public function testProductVariationsTranslated() {
$default_variation_type = ProductVariationType::load('default');
$default_variation_type
->setGenerateTitle(FALSE);
$default_variation_type
->save();
$this->container
->get('content_translation.manager')
->setEnabled('commerce_product', 'default', TRUE);
$this->container
->get('content_translation.manager')
->setEnabled('commerce_product_variation', 'default', TRUE);
$product = Product::create([
'type' => 'default',
'title' => 'My Super Product',
'stores' => [
$this->store,
],
]);
$product
->addTranslation('fr', [
'title' => 'Mon super produit',
]);
$product
->addTranslation('sr', [
'title' => 'Мој супер производ',
]);
$variation1 = ProductVariation::create([
'type' => 'default',
'title' => 'Version one',
]);
$variation1
->addTranslation('fr', [
'title' => 'Version une',
]);
$product
->addVariation($variation1);
$variation2 = ProductVariation::create([
'type' => 'default',
'title' => 'Version two',
]);
$variation2
->addTranslation('fr', [
'title' => 'Version deux',
]);
$product
->addVariation($variation2);
$default_langcode = $this->container
->get('language_manager')
->getCurrentLanguage()
->getId();
foreach ($product
->getVariations() as $variation) {
$this
->assertEquals($default_langcode, $variation
->language()
->getId());
}
foreach ($product
->getTranslation('fr')
->getVariations() as $variation) {
$this
->assertEquals('fr', $variation
->language()
->getId());
}
foreach ($product
->getTranslation('en')
->getVariations() as $variation) {
$this
->assertEquals('en', $variation
->language()
->getId());
}
foreach ($product
->getTranslation('sr')
->getVariations() as $variation) {
$this
->assertEquals('en', $variation
->language()
->getId());
}
}