Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
18694ba
Issue #2860840 by mgoncalves, Regnoy, mglaman, bojanz, GoZ, vasike: P…
vasike Mar 20, 2017
56ee51f
Some updates for PR reviews.
vasike Mar 21, 2017
405c350
Make sure we use translated variations for Product variation title wi…
vasike Mar 21, 2017
00af086
Some updates for travis errors - code standards.
vasike Mar 21, 2017
ddd266b
Add some tests.
vasike Mar 21, 2017
6e0c5d0
Merge branch '8.x-2.x' of https://git.drupal.org/project/commerce int…
vasike Mar 25, 2017
879e28c
Travis error messages.
vasike Mar 25, 2017
83cd03e
Fix for tests.
vasike Mar 28, 2017
eaf9ebf
Merge branch '8.x-2.x' of https://git.drupal.org/project/commerce int…
vasike Mar 28, 2017
6839c14
Merge branch '8.x-2.x' of https://git.drupal.org/project/commerce int…
vasike Apr 5, 2017
4cdbc6a
Merge branch '8.x-2.x' of https://git.drupal.org/project/commerce int…
vasike Apr 24, 2017
29c13a4
Fix for tests.
vasike Apr 25, 2017
daae24b
Merge branch '8.x-2.x' of https://git.drupal.org/project/commerce int…
vasike Aug 2, 2017
2c3e55c
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
vasike Oct 3, 2017
a9d98cc
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Oct 11, 2017
6be8313
Fix VariationStoreage::loadEnabled
mglaman Oct 11, 2017
1730dba
Use loadEnabled
mglaman Oct 11, 2017
5327961
Use entity repository
mglaman Oct 18, 2017
71768ab
Add more explicit testing.
mglaman Oct 18, 2017
f7a2183
Ensure titles are translate if generated.
mglaman Oct 18, 2017
6f8ba42
Merge remote-tracking branch 'upstream/8.x-2.x' into 2860840-add-to-c…
mglaman Oct 18, 2017
dbcf16b
Fix kernel test
mglaman Oct 23, 2017
6e700d5
Test title generation, multilingual
mglaman Oct 23, 2017
98983e6
Work to fix functional JS test
mglaman Oct 23, 2017
71d22bc
Issue #2877632 by smaz: Redirect to Order view instead of Orders list…
Oct 23, 2017
5a53548
Issue #2916252 by caseylau, skyredwang: Order's Adjustment can't be n…
Oct 23, 2017
fe115a3
Try flushing caches
mglaman Oct 23, 2017
8cc10b4
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Oct 23, 2017
a4fe890
Link to proper product
mglaman Oct 23, 2017
3ac4f0f
Fix failing test
mglaman Oct 23, 2017
27e0860
Few nits and fixes.
mglaman Oct 23, 2017
b9e601c
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Oct 30, 2017
0ac8eac
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Nov 6, 2017
e5ba637
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Nov 6, 2017
6d7bd73
Remove unneeded changes
mglaman Nov 6, 2017
12939a6
Undo changes to variation title widget
mglaman Nov 6, 2017
36af66c
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Nov 15, 2017
d97acd9
Remove changes to variation storage
mglaman Nov 15, 2017
7df708f
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Dec 1, 2017
aab1185
Merge branch '8.x-2.x' into 2860840-add-to-cart-multilingual
mglaman Dec 11, 2017
787278b
Widgets not loading translated variations..
mglaman Dec 11, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?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;
use Drupal\Tests\commerce\FunctionalJavascript\JavascriptTestTrait;
use Drupal\Tests\commerce_cart\Functional\CartBrowserTestBase;

/**
* Tests the add to cart form for multilingual.
*
* @group commerce
*/
class AddToCartMultilingualTest extends CartBrowserTestBase {

use JavascriptTestTrait;

/**
* The variations to test with.
*
* @var \Drupal\commerce_product\Entity\ProductVariation[]
*/
protected $variations;

/**
* The product to test against.
*
* @var \Drupal\commerce_product\Entity\Product
*/
protected $product;

/**
* The color attributes to test with.
*
* @var \Drupal\commerce_product\Entity\ProductAttributeValueInterface[]
*/
protected $colorAttributes;

/**
* The size attributes to test with.
*
* @var \Drupal\commerce_product\Entity\ProductAttributeValueInterface[]
*/
protected $sizeAttributes;

/**
* {@inheritdoc}
*/
public static $modules = [
'language',
'content_translation',
];

/**
* {@inheritdoc}
*
* @see \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase
*/
public function setUp() {
parent::setUp();

$this->setupMultilingual();

/** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
$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();
}

// Reload the variation since we have new fields.
$this->variation = ProductVariation::load($this->variation->id());

// Translate the product's title.
$product = $this->variation->getProduct();
$product->setTitle('My Super Product');
$product->addTranslation('fr', [
'title' => 'Mon super produit',
]);
$product->save();

// Update first variation to have the attribute's value.
$this->variation->get('attribute_color')->setValue($color_attributes['red']);
$this->variation->get('attribute_size')->setValue($size_attributes['small']);
$this->variation->save();

// The matrix is intentionally uneven, blue / large is missing.
$attribute_values_matrix = [
['red', 'small'],
['red', 'medium'],
['red', 'large'],
['blue', 'small'],
['blue', 'medium'],
];

// Generate variations off of the attributes values matrix.
foreach ($attribute_values_matrix as $key => $value) {
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
$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());

// Create a translation for each variation on the product.
foreach ($this->product->getVariations() as $variation) {
$variation->addTranslation('fr')->save();
}

$this->variations = $product->getVariations();
$this->colorAttributes = $color_attributes;
$this->sizeAttributes = $size_attributes;
}

/**
* Sets up the multilingual items.
*/
protected function setupMultilingual() {
// Add a new language.
ConfigurableLanguage::createFromLangcode('fr')->save();

// Enable translation for the product and ensure the change is picked up.
$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.manager')->clearCachedDefinitions();
$this->container->get('router.builder')->rebuild();
$this->container->get('entity.definition_update_manager')->applyUpdates();

// Rebuild the container so that the new languages are picked up by services
// that hold a list of languages.
$this->rebuildContainer();
}

/**
* Tests that the attribute widget uses translated items.
*/
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');

// Change the site language.
$this->config('system.site')->set('default_langcode', 'fr')->save();
drupal_flush_all_caches();

$this->drupalGet($this->product->getTranslation('fr')->toUrl());
// Use AJAX to change the size to Medium, keeping the color on Red.
$this->getSession()->getPage()->selectFieldOption('purchased_entity[0][attributes][attribute_size]', 'FR Medium');
$this->waitForAjaxToFinish();
$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());

// Use AJAX to change the color to Blue, keeping the size on Medium.
$this->getSession()->getPage()->selectFieldOption('purchased_entity[0][attributes][attribute_color]', 'FR Blue');
$this->waitForAjaxToFinish();
$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]);
}

/**
* Tests the title widget has translated variation title.
*/
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');

// Change the site language.
$this->config('system.site')->set('default_langcode', 'fr')->save();
drupal_flush_all_caches();

$this->drupalGet($this->product->getTranslation('fr')->toUrl());
// Use AJAX to change the size to Medium, keeping the color on Red.
$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->waitForAjaxToFinish();
$this->assertAttributeSelected('purchased_entity[0][variation]', 'Mon super produit - FR Red, FR Medium');
$this->assertSession()->pageTextContains('Mon super produit - FR Red, FR Medium');
// Use AJAX to change the color to Blue, keeping the size on Medium.
$this->getSession()->getPage()->selectFieldOption('purchased_entity[0][variation]', 'Mon super produit - FR Blue, FR Medium');
$this->waitForAjaxToFinish();
$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]);
}

}
2 changes: 1 addition & 1 deletion modules/product/commerce_product.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:

commerce_product.lazy_builders:
class: Drupal\commerce_product\ProductLazyBuilders
arguments: ['@entity_type.manager', '@form_builder']
arguments: ['@entity_type.manager', '@form_builder', '@entity.repository']

commerce_product.variation_field_renderer:
class: Drupal\commerce_product\ProductVariationFieldRenderer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
$items->getEntity()->id(),
$this->viewMode,
$this->getSetting('combine'),
$langcode,
],
],
'#create_placeholder' => TRUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\commerce_product\ProductAttributeFieldManagerInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
Expand Down Expand Up @@ -55,11 +56,13 @@ class ProductVariationAttributesWidget extends ProductVariationWidgetBase implem
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\commerce_product\ProductAttributeFieldManagerInterface $attribute_field_manager
* The attribute field manager.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, ProductAttributeFieldManagerInterface $attribute_field_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_type_manager);
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, ProductAttributeFieldManagerInterface $attribute_field_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_type_manager, $entity_repository);

$this->attributeFieldManager = $attribute_field_manager;
$this->attributeStorage = $entity_type_manager->getStorage('commerce_product_attribute');
Expand All @@ -76,6 +79,7 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('entity_type.manager'),
$container->get('entity.repository'),
$container->get('commerce_product.attribute_field_manager')
);
}
Expand All @@ -86,7 +90,7 @@ public static function create(ContainerInterface $container, array $configuratio
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
$product = $form_state->get('product');
$variations = $this->variationStorage->loadEnabled($product);
$variations = $this->loadEnabledVariations($product);
if (count($variations) === 0) {
// Nothing to purchase, tell the parent form to hide itself.
$form_state->set('hide_form', TRUE);
Expand Down Expand Up @@ -141,6 +145,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
}
}
}

$element['variation'] = [
'#type' => 'value',
'#value' => $selected_variation->id(),
Expand Down Expand Up @@ -264,10 +269,12 @@ protected function getAttributeInfo(ProductVariationInterface $selected_variatio
$field = $field_definitions[$field_name];
/** @var \Drupal\commerce_product\Entity\ProductAttributeInterface $attribute */
$attribute = $this->attributeStorage->load($attribute_ids[$index]);
// Make sure we have translation for attribute.
$attribute = $this->entityRepository->getTranslationFromContext($attribute, $selected_variation->language()->getId());

$attributes[$field_name] = [
'field_name' => $field_name,
'title' => $field->getLabel(),
'title' => $attribute->label(),
'required' => $field->isRequired(),
'element_type' => $attribute->getElementType(),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public function settingsSummary() {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
$product = $form_state->get('product');
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface[] $variations */
$variations = $this->variationStorage->loadEnabled($product);
$variations = $this->loadEnabledVariations($product);
if (count($variations) === 0) {
// Nothing to purchase, tell the parent form to hide itself.
$form_state->set('hide_form', TRUE);
Expand Down
Loading