You are here

public function ProductTranslationTest::testSingleVariationProductTranslation in Commerce Core 8.2

Test translating a single-variation product.

File

modules/product/tests/src/Functional/ProductTranslationTest.php, line 108

Class

ProductTranslationTest
Tests translating products and variations.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testSingleVariationProductTranslation() {
  $this
    ->drupalGet('admin/commerce/config/product-types/default/edit');
  $edit = [
    'multipleVariations' => FALSE,
    'language_configuration[language_alterable]' => TRUE,
  ];
  $this
    ->submitForm($edit, t('Save'));
  $this
    ->drupalGet('admin/commerce/config/product-variation-types/default/edit');
  $edit = [
    'generateTitle' => FALSE,
  ];
  $this
    ->submitForm($edit, t('Save'));
  $product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'Translation test product',
    'stores' => $this->stores,
  ]);
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'default',
    'product_id' => $product
      ->id(),
    'title' => 'Hat',
    'sku' => $this
      ->randomMachineName(),
    'price' => new Price('9.99', 'USD'),
  ]);
  $this
    ->drupalGet($product
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Translate');
  $this
    ->assertSession()
    ->linkByHrefExists("/product/{$product->id()}/translations/add/en/fr");
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add');
  $this
    ->getSession()
    ->getPage()
    ->fillField('title[0][value]', 'Produit de test de traduction');
  $this
    ->getSession()
    ->getPage()
    ->fillField('variations[entity][title][0][value]', 'Le Chapeau');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save (this translation)');
  $this
    ->assertSession()
    ->pageTextContains('The product Produit de test de traduction has been successfully saved.');

  // Confirm that the variation was translated together with the product.
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product_variation')
    ->resetCache([
    1,
  ]);
  $variation = ProductVariation::load(1);
  $this
    ->assertEquals('en', $variation
    ->language()
    ->getId());
  $this
    ->assertEquals('Hat', $variation
    ->getTitle());
  $this
    ->assertTrue($variation
    ->hasTranslation('fr'));
  $translation = $variation
    ->getTranslation('fr');
  $this
    ->assertEquals('Le Chapeau', $translation
    ->getTitle());

  // Edit the product and change the language to German.
  $this
    ->drupalGet($product
    ->toUrl('edit-form', [
    'language' => new Language([
      'id' => 'en',
    ]),
  ]));
  $this
    ->submitForm([
    'langcode[0][value]' => 'de',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The product Translation test product has been successfully saved.');
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product')
    ->resetCache([
    1,
  ]);
  $product = Product::load(1);
  $this
    ->assertEquals('de', $product
    ->language()
    ->getId());
  $this
    ->assertTrue($product
    ->hasTranslation('fr'));

  // Confirm that the variation language was changed as well.
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product_variation')
    ->resetCache([
    1,
  ]);
  $variation = ProductVariation::load(1);
  $this
    ->assertEquals('de', $variation
    ->language()
    ->getId());
  $this
    ->assertEquals('Hat', $variation
    ->getTitle());
  $this
    ->assertTrue($variation
    ->hasTranslation('fr'));
}