You are here

public function ProductVariationFieldRendererTest::testRenderFieldsMultilingual in Commerce Core 8.2

Tests renderFields in multilingual context.

@covers ::renderFields

File

modules/product/tests/src/Kernel/ProductVariationFieldRendererTest.php, line 196

Class

ProductVariationFieldRendererTest
Tests the product variation field renderer.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testRenderFieldsMultilingual() {
  $this->secondVariationType
    ->setGenerateTitle(TRUE);
  $this->secondVariationType
    ->save();
  $this->container
    ->get('content_translation.manager')
    ->setEnabled('commerce_product_variation', $this->secondVariationType
    ->id(), TRUE);
  $this->container
    ->get('content_translation.manager')
    ->setEnabled('commerce_product', 'default', TRUE);
  $this->container
    ->get('content_translation.manager')
    ->setEnabled('commerce_product_attribute_value', 'color', TRUE);
  $blue = ProductAttributeValue::create([
    'attribute' => 'color',
    'name' => 'Blue',
  ]);
  $blue
    ->addTranslation('fr', [
    'name' => 'Bleu',
  ]);
  $blue
    ->save();
  $black = ProductAttributeValue::create([
    'attribute' => 'color',
    'name' => 'Black',
    'weight' => 3,
  ]);
  $black
    ->addTranslation('fr', [
    'name' => 'Noir',
  ]);
  $black
    ->save();
  $variation1 = ProductVariation::create([
    'type' => $this->secondVariationType
      ->id(),
    'sku' => strtolower($this
      ->randomMachineName()),
    'attribute_color' => $blue
      ->id(),
    'status' => 1,
  ]);
  $variation1
    ->save();
  $variation2 = ProductVariation::create([
    'type' => $this->secondVariationType
      ->id(),
    'sku' => strtolower($this
      ->randomMachineName()),
    'attribute_color' => $black
      ->id(),
    'status' => 1,
  ]);
  $variation2
    ->save();
  $product = Product::create([
    'type' => 'default',
    'title' => 'My Super Product',
    'variations' => [
      $variation1,
      $variation2,
    ],
  ]);
  $product
    ->addTranslation('fr', [
    'title' => 'Mon super produit',
  ]);
  $product
    ->save();
  $variation1
    ->addTranslation('fr', [])
    ->save();
  $variation2
    ->addTranslation('fr', [])
    ->save();
  $this
    ->assertEquals('Mon super produit - Bleu', $variation1
    ->getTranslation('fr')
    ->label());
  $this
    ->assertEquals('Mon super produit - Noir', $variation2
    ->getTranslation('fr')
    ->label());
  $entity_display = commerce_get_entity_display('commerce_product_variation', $this->secondVariationType
    ->id(), 'view');
  $entity_display
    ->setComponent('attribute_color', [
    'label' => 'above',
    'type' => 'entity_reference_label',
  ]);
  $entity_display
    ->setComponent('title', [
    'label' => 'above',
    'type' => 'string',
  ]);
  $entity_display
    ->save();

  // Make sure loadFromContext does not return the default variation, which is
  // always translated via ::getDefaultVariation on the product entity.
  $request = Request::create('');
  $request->query
    ->add([
    'v' => $variation2
      ->id(),
  ]);

  // Push the request to the request stack so `current_route_match` works.
  $this->container
    ->get('request_stack')
    ->push($request);
  $this
    ->assertNotEquals($request->query
    ->get('v'), $product
    ->getDefaultVariation()
    ->id());
  $this
    ->config('system.site')
    ->set('default_langcode', 'fr')
    ->save();
  $product_view_builder = $this->container
    ->get('entity_type.manager')
    ->getViewBuilder('commerce_product');
  $product_build = $product_view_builder
    ->view($product);
  $this
    ->render($product_build);
  $this
    ->assertEscaped('Mon super produit');
  $this
    ->assertEscaped('Mon super produit - Noir');
}