View source
<?php
namespace Drupal\Tests\commerce_cart\FunctionalJavascript;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
class MultipleCartFormsTest extends CartWebDriverTestBase {
public static $modules = [
'commerce_cart_test',
'commerce_cart_big_pipe',
];
protected $colorAttributes = [];
protected $sizeAttributes = [];
protected $products = [];
protected function setUp() : void {
parent::setUp();
$this->maximumMetaRefreshCount = 0;
$this->variation
->getProduct()
->setUnpublished();
$this->variation
->getProduct()
->save();
$order_item_form_display = EntityFormDisplay::load('commerce_order_item.default.add_to_cart');
$order_item_form_display
->setComponent('quantity', [
'type' => 'commerce_quantity',
]);
$order_item_form_display
->save();
$variation_type = ProductVariationType::load('default');
$color_attributes = $this
->createAttributeSet($variation_type, 'color', [
'red' => 'Red',
'blue' => 'Blue',
]);
$this->colorAttributes = $color_attributes;
$size_attributes = $this
->createAttributeSet($variation_type, 'size', [
'small' => 'Small',
'medium' => 'Medium',
'large' => 'Large',
]);
$this->sizeAttributes = $size_attributes;
$attribute_values_matrix = [
[
'red',
'small',
],
[
'red',
'medium',
],
[
'red',
'large',
],
[
'blue',
'small',
],
[
'blue',
'medium',
],
[
'blue',
'large',
],
];
for ($i = 1; $i < 5; $i++) {
$variations = [];
foreach ($attribute_values_matrix as $key => $value) {
$variation = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => $this
->randomMachineName(),
'price' => [
'number' => (string) 3 * $i,
'currency_code' => 'USD',
],
'attribute_color' => $color_attributes[$value[0]],
'attribute_size' => $size_attributes[$value[1]],
]);
$variations[] = $variation;
}
$this->products[] = $this
->createEntity('commerce_product', [
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => $variations,
]);
}
}
public function testUniqueAddToCartFormIds() {
$this
->drupalGet('/test-multiple-cart-forms');
$seen_ids = [];
$forms = $this
->getSession()
->getPage()
->findAll('css', '.commerce-order-item-add-to-cart-form');
$this
->assertCount(4, $forms);
foreach ($forms as $form) {
$form_id = $form
->find('xpath', '//input[@type="hidden" and @name="form_id"]')
->getValue();
$this
->assertFalse(in_array($form_id, $seen_ids));
$seen_ids[] = $form_id;
}
$forms[1]
->selectFieldOption('Size', 'Large');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$forms = $this
->getSession()
->getPage()
->findAll('css', '.commerce-order-item-add-to-cart-form');
$this
->assertCount(4, $forms);
$ajax_seen_ids = [];
foreach ($forms as $form) {
$form_id = $form
->find('xpath', '//input[@type="hidden" and @name="form_id"]')
->getValue();
$this
->assertFalse(in_array($form_id, $ajax_seen_ids));
$ajax_seen_ids[] = $form_id;
}
$this
->assertEquals($seen_ids, $ajax_seen_ids);
}
public function testMultipleRenderedProducts() {
$this
->drupalGet('/test-multiple-cart-forms');
$forms = $this
->getSession()
->getPage()
->findAll('css', '.commerce-order-item-add-to-cart-form');
$current_form = $forms[2];
$current_form
->fillField('quantity[0][value]', '3');
$current_form
->selectFieldOption('Color', 'Blue');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->selectFieldOption('Size', 'Medium');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->selectFieldOption('Color', 'Red');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->pressButton('Add to cart');
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertEquals(3, $order_items[0]
->getQuantity());
$variation = $order_items[0]
->getPurchasedEntity();
$this
->assertEquals($this->sizeAttributes['medium']
->id(), $variation
->getAttributeValueId('attribute_size'));
$this
->assertEquals($this->colorAttributes['red']
->id(), $variation
->getAttributeValueId('attribute_color'));
$forms = $this
->getSession()
->getPage()
->findAll('css', '.commerce-order-item-add-to-cart-form');
$current_form = $forms[0];
$current_form
->fillField('quantity[0][value]', '2');
$current_form
->selectFieldOption('Color', 'Red');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->selectFieldOption('Size', 'Small');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->selectFieldOption('Color', 'Blue');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$forms[1]
->selectFieldOption('Size', 'Large');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$forms[1]
->pressButton('Add to cart');
$this->container
->get('entity_type.manager')
->getStorage('commerce_order')
->resetCache([
$this->cart
->id(),
]);
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertEquals(1, $order_items[1]
->getQuantity());
$variation = $order_items[1]
->getPurchasedEntity();
$this
->assertEquals($this->sizeAttributes['large']
->id(), $variation
->getAttributeValueId('attribute_size'));
$this
->assertEquals($this->colorAttributes['red']
->id(), $variation
->getAttributeValueId('attribute_color'));
}
public function testMultipleRenderedProductsWithTitleWidget() {
$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('/test-multiple-cart-forms');
$forms = $this
->getSession()
->getPage()
->findAll('css', '.commerce-order-item-add-to-cart-form');
$current_form = $forms[2];
$current_form
->fillField('quantity[0][value]', '3');
$current_form
->selectFieldOption('purchased_entity[0][variation]', $this->products[2]
->getVariations()[1]
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->pressButton('Add to cart');
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertEquals(3, $order_items[0]
->getQuantity());
$variation = $order_items[0]
->getPurchasedEntity();
$this
->assertEquals($this->products[2]
->getVariations()[1]
->id(), $variation
->id());
$current_form = $forms[0];
$current_form
->fillField('quantity[0][value]', '2');
$current_form
->selectFieldOption('purchased_entity[0][variation]', $this->products[0]
->getVariations()[2]
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$forms[1]
->selectFieldOption('purchased_entity[0][variation]', $this->products[1]
->getVariations()[3]
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$forms[1]
->pressButton('Add to cart');
$this->container
->get('entity_type.manager')
->getStorage('commerce_order')
->resetCache([
$this->cart
->id(),
]);
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertEquals(1, $order_items[1]
->getQuantity());
$variation = $order_items[1]
->getPurchasedEntity();
$this
->assertEquals($this->products[1]
->getVariations()[3]
->id(), $variation
->id());
}
public function testMultipleRenderedFields() {
$this
->drupalGet('/test-multiple-cart-forms-fields');
$forms = $this
->getSession()
->getPage()
->findAll('css', '.commerce-order-item-add-to-cart-form');
$current_form = $forms[3];
$current_form
->fillField('Quantity', '10');
$current_form
->selectFieldOption('Size', 'Large');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->selectFieldOption('Color', 'Blue');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$current_form
->pressButton('Add to cart');
\Drupal::entityTypeManager()
->getStorage('commerce_order')
->resetCache();
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertEquals(10, $order_items[0]
->getQuantity());
$variation = $order_items[0]
->getPurchasedEntity();
$this
->assertEquals($this->sizeAttributes['large']
->id(), $variation
->getAttributeValueId('attribute_size'));
$this
->assertEquals($this->colorAttributes['blue']
->id(), $variation
->getAttributeValueId('attribute_color'));
}
}