You are here

public function ProductAttributeTranslationTest::testMismatchedLanguages in Commerce Core 8.2

Tests the product attribute UI with mismatched languages.

File

modules/product/tests/src/Functional/ProductAttributeTranslationTest.php, line 129

Class

ProductAttributeTranslationTest
Tests translating product attributes and their values.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testMismatchedLanguages() {

  // Create a French attribute with two English (default language) values.
  $this
    ->createEntity('commerce_product_attribute', [
    'id' => 'color',
    'label' => 'Couleur',
    'langcode' => 'fr',
  ]);
  $red_value = $this
    ->createEntity('commerce_product_attribute_value', [
    'attribute' => 'color',
    'name' => 'Red',
    'weight' => 0,
  ]);
  $blue_value = $this
    ->createEntity('commerce_product_attribute_value', [
    'attribute' => 'color',
    'name' => 'Blue',
    'weight' => 1,
  ]);

  // Enable attribute value translations.
  $edit = [
    'enable_value_translation' => TRUE,
  ];
  $this
    ->drupalGet('admin/commerce/product-attributes/manage/color');
  $this
    ->submitForm($edit, t('Save'));

  // Translate the English values to French.
  $red_value_en = $red_value
    ->addTranslation('fr', [
    'name' => 'Rouge',
  ]);
  $red_value_en
    ->save();
  $blue_value_en = $blue_value
    ->addTranslation('fr', [
    'name' => 'Bleu',
  ]);
  $blue_value_en
    ->save();

  // Since the attribute language is French, the displayed values should
  // also be in French, not English.
  $this
    ->drupalGet('admin/commerce/product-attributes/manage/color');
  $this
    ->assertSession()
    ->elementExists('xpath', "//input[@value='Rouge']");
  $this
    ->assertSession()
    ->elementExists('xpath', "//input[@value='Bleu']");
  $this
    ->drupalGet('admin/commerce/product-attributes/manage/color/translate/en/add');
  $this
    ->assertSession()
    ->pageTextContains('Rouge');
  $this
    ->assertSession()
    ->pageTextContains('Bleu');
}