View source
<?php
namespace Drupal\Tests\commerce_cart\FunctionalJavascript;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\language\Entity\ConfigurableLanguage;
class AddToCartMultilingualTest extends CartWebDriverTestBase {
protected $variations;
protected $product;
protected $colorAttributes;
protected $sizeAttributes;
public static $modules = [
'language',
'content_translation',
];
protected function setUp() : void {
parent::setUp();
$this
->setupMultilingual();
$variation_type = ProductVariationType::load($this->variation
->bundle());
$color_attributes = $this
->createAttributeSet($variation_type, 'color', [
'red' => 'Red',
'blue' => 'Blue',
]);
foreach ($color_attributes as $key => $color_attribute) {
$color_attribute
->addTranslation('fr', [
'name' => 'FR ' . $color_attribute
->label(),
]);
$color_attribute
->save();
}
$size_attributes = $this
->createAttributeSet($variation_type, 'size', [
'small' => 'Small',
'medium' => 'Medium',
'large' => 'Large',
]);
foreach ($size_attributes as $key => $size_attribute) {
$size_attribute
->addTranslation('fr', [
'name' => 'FR ' . $size_attribute
->label(),
]);
$size_attribute
->save();
}
$this->variation = ProductVariation::load($this->variation
->id());
$product = $this->variation
->getProduct();
$product
->setTitle('My Super Product');
$product
->addTranslation('fr', [
'title' => 'Mon super produit',
]);
$product
->save();
$this->variation
->get('attribute_color')
->setValue($color_attributes['red']);
$this->variation
->get('attribute_size')
->setValue($size_attributes['small']);
$this->variation
->save();
$attribute_values_matrix = [
[
'red',
'small',
],
[
'red',
'medium',
],
[
'red',
'large',
],
[
'blue',
'small',
],
[
'blue',
'medium',
],
];
foreach ($attribute_values_matrix as $key => $value) {
$variation = $this
->createEntity('commerce_product_variation', [
'type' => $variation_type
->id(),
'sku' => $this
->randomMachineName(),
'price' => [
'number' => 999,
'currency_code' => 'USD',
],
]);
$variation
->get('attribute_color')
->setValue($color_attributes[$value[0]]);
$variation
->get('attribute_size')
->setValue($size_attributes[$value[1]]);
$variation
->save();
$product
->addVariation($variation);
}
$product
->save();
$this->product = Product::load($product
->id());
foreach ($this->product
->getVariations() as $variation) {
$variation
->addTranslation('fr')
->save();
}
$this->variations = $product
->getVariations();
$this->colorAttributes = $color_attributes;
$this->sizeAttributes = $size_attributes;
}
protected function setupMultilingual() {
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this->container
->get('content_translation.manager')
->setEnabled('commerce_product', $this->variation
->bundle(), TRUE);
$this->container
->get('content_translation.manager')
->setEnabled('commerce_product_variation', $this->variation
->bundle(), TRUE);
$this->container
->get('entity_type.manager')
->clearCachedDefinitions();
$this->container
->get('router.builder')
->rebuild();
$this
->rebuildContainer();
}
public function testProductVariationAttributesWidget() {
$this
->drupalGet($this->product
->toUrl());
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_color]', 'Red');
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_size]', 'Small');
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_color]', $this->colorAttributes['blue']
->id());
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_size]', $this->sizeAttributes['medium']
->id());
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_size]', $this->sizeAttributes['large']
->id());
$this
->getSession()
->getPage()
->pressButton('Add to cart');
$this
->config('system.site')
->set('default_langcode', 'fr')
->save();
$this
->rebuildContainer();
$this
->drupalGet($this->product
->getTranslation('fr')
->toUrl());
$this
->getSession()
->getPage()
->selectFieldOption('purchased_entity[0][attributes][attribute_size]', 'FR Medium');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_color]', 'FR Red');
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_size]', 'FR Medium');
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_color]', $this->colorAttributes['blue']
->id());
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_size]', $this->sizeAttributes['small']
->id());
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_size]', $this->sizeAttributes['large']
->id());
$this
->getSession()
->getPage()
->selectFieldOption('purchased_entity[0][attributes][attribute_color]', 'FR Blue');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_color]', 'FR Blue');
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_size]', 'FR Medium');
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_color]', $this->colorAttributes['red']
->id());
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_size]', $this->sizeAttributes['small']
->id());
$this
->assertAttributeDoesNotExist('purchased_entity[0][attributes][attribute_size]', $this->sizeAttributes['large']
->id());
$this
->getSession()
->getPage()
->pressButton('Add to cart');
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertOrderItemInOrder($this->variations[0]
->getTranslation('fr'), $order_items[0]);
$this
->assertOrderItemInOrder($this->variations[5]
->getTranslation('fr'), $order_items[1]);
}
public function testProductVariationAttributesWidgetFromUrl() {
$variation = $this->variations[5];
$this
->drupalGet($variation
->toUrl());
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_color]', 'Blue');
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_size]', 'Medium');
$this
->getSession()
->getPage()
->pressButton('Add to cart');
$this
->config('system.site')
->set('default_langcode', 'fr')
->save();
$this
->rebuildContainer();
$variation = $variation
->getTranslation('fr');
$this
->drupalGet($variation
->toUrl());
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_color]', 'FR Blue');
$this
->assertAttributeSelected('purchased_entity[0][attributes][attribute_size]', 'FR Medium');
$this
->getSession()
->getPage()
->pressButton('Add to cart');
}
public function testProductVariationTitleWidget() {
$order_item_form_display = EntityFormDisplay::load('commerce_order_item.default.add_to_cart');
$order_item_form_display
->setComponent('purchased_entity', [
'type' => 'commerce_product_variation_title',
]);
$order_item_form_display
->save();
$this
->drupalGet($this->product
->toUrl());
$this
->assertSession()
->selectExists('purchased_entity[0][variation]');
$this
->assertAttributeSelected('purchased_entity[0][variation]', 'My Super Product - Red, Small');
$this
->getSession()
->getPage()
->pressButton('Add to cart');
$this
->config('system.site')
->set('default_langcode', 'fr')
->save();
$this
->rebuildContainer();
$this
->drupalGet($this->product
->getTranslation('fr')
->toUrl());
$this
->assertAttributeSelected('purchased_entity[0][variation]', 'Mon super produit - FR Red, FR Small');
$this
->getSession()
->getPage()
->selectFieldOption('purchased_entity[0][variation]', 'Mon super produit - FR Red, FR Medium');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertAttributeSelected('purchased_entity[0][variation]', 'Mon super produit - FR Red, FR Medium');
$this
->assertSession()
->pageTextContains('Mon super produit - FR Red, FR Medium');
$this
->getSession()
->getPage()
->selectFieldOption('purchased_entity[0][variation]', 'Mon super produit - FR Blue, FR Medium');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertAttributeSelected('purchased_entity[0][variation]', 'Mon super produit - FR Blue, FR Medium');
$this
->assertSession()
->pageTextContains('Mon super produit - FR Blue, FR Medium');
$this
->getSession()
->getPage()
->pressButton('Add to cart');
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertOrderItemInOrder($this->variations[0]
->getTranslation('fr'), $order_items[0]);
$this
->assertOrderItemInOrder($this->variations[5]
->getTranslation('fr'), $order_items[1]);
}
public function testProductVariationTitleWidgetFromUrl() {
$order_item_form_display = EntityFormDisplay::load('commerce_order_item.default.add_to_cart');
$order_item_form_display
->setComponent('purchased_entity', [
'type' => 'commerce_product_variation_title',
]);
$order_item_form_display
->save();
$variation = $this->variations[5];
$this
->drupalGet($variation
->toUrl());
$this
->assertSession()
->selectExists('purchased_entity[0][variation]');
$this
->assertAttributeSelected('purchased_entity[0][variation]', 'My Super Product - Blue, Medium');
$this
->config('system.site')
->set('default_langcode', 'fr')
->save();
$this
->rebuildContainer();
$variation = $variation
->getTranslation('fr');
$this
->drupalGet($variation
->toUrl());
$this
->assertSession()
->selectExists('purchased_entity[0][variation]');
$this
->assertAttributeSelected('purchased_entity[0][variation]', 'Mon super produit - FR Blue, FR Medium');
}
}