public function ProductMultilingualTest::testVariationAttributeValuesTranslated in Commerce Core 8.2
Tests that a variation's attributes are returned in specified language.
File
- modules/
product/ tests/ src/ Kernel/ ProductMultilingualTest.php, line 239
Class
- ProductMultilingualTest
- Tests the product and variation entity in a multilingual context.
Namespace
Drupal\Tests\commerce_product\KernelCode
public function testVariationAttributeValuesTranslated() {
$this->container
->get('content_translation.manager')
->setEnabled('commerce_product', 'default', TRUE);
$this->container
->get('content_translation.manager')
->setEnabled('commerce_product_variation', 'default', TRUE);
$color = ProductAttribute::create([
'id' => 'color',
'label' => 'Color',
]);
$color
->save();
$this->container
->get('commerce_product.attribute_field_manager')
->createField($color, 'default');
$this->container
->get('content_translation.manager')
->setEnabled('commerce_product_attribute_value', 'color', TRUE);
$black = ProductAttributeValue::create([
'attribute' => 'color',
'name' => 'Black',
'weight' => 3,
]);
$black
->addTranslation('fr', [
'name' => 'Noir',
]);
$product = Product::create([
'type' => 'default',
'title' => 'My Super Product',
'stores' => [
$this->store,
],
]);
$product
->addTranslation('fr', [
'title' => 'Mon super produit',
]);
$variation1 = ProductVariation::create([
'type' => 'default',
'title' => 'Version one',
'product_id' => $product,
'attribute_color' => $black,
]);
$variation1
->addTranslation('fr', [
'title' => 'Version une',
]);
$variation1
->addTranslation('sr', [
'title' => 'Верзија два',
]);
$product
->addVariation($variation1);
$values = $variation1
->getAttributeValues();
$this
->assertEquals('Black', $values['attribute_color']
->label());
$values = $variation1
->getTranslation('fr')
->getAttributeValues();
$this
->assertEquals('Noir', $values['attribute_color']
->label());
$values = $variation1
->getTranslation('en')
->getAttributeValues();
$this
->assertEquals('Black', $values['attribute_color']
->label());
$values = $variation1
->getTranslation('sr')
->getAttributeValues();
$this
->assertEquals('Black', $values['attribute_color']
->label());
$this
->assertEquals('Black', $variation1
->getAttributeValue('attribute_color')
->label());
$this
->assertEquals('Noir', $variation1
->getTranslation('fr')
->getAttributeValue('attribute_color')
->label());
$this
->assertEquals('Black', $variation1
->getTranslation('en')
->getAttributeValue('attribute_color')
->label());
$this
->assertEquals('Black', $variation1
->getTranslation('sr')
->getAttributeValue('attribute_color')
->label());
}