View source
<?php
namespace Drupal\Tests\commerce_cart\Functional;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_product\Entity\ProductAttribute;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
class AddToCartFormTest extends CartBrowserTestBase {
public static $modules = [
'commerce_test',
];
public function testProductAddToCartForm() {
$this
->postAddToCart($this->variation
->getProduct());
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertOrderItemInOrder($this->variation, $order_items[0]);
$this
->postAddToCart($this->variation
->getProduct());
\Drupal::entityTypeManager()
->getStorage('commerce_order')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('commerce_order_item')
->resetCache();
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertNotEmpty(count($order_items) == 1, 'No additional order items were created');
$this
->assertOrderItemInOrder($this->variation, $order_items[0], 2);
}
public function testProductAddToCartFormValidations() {
$this->variation
->setSku('TEST_SKU1234')
->save();
$this
->postAddToCart($this->variation
->getProduct());
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertCount(0, $order_items);
$this
->assertSession()
->pageTextContains(sprintf('%s is not available with a quantity of %s.', $this->variation
->label(), 1));
}
public function testCartAssignment() {
$this
->drupalLogout();
$this
->postAddToCart($this->variation
->getProduct());
$query = \Drupal::entityTypeManager()
->getStorage('commerce_order')
->getQuery()
->condition('cart', TRUE)
->condition('uid', 0)
->accessCheck(FALSE);
$result = $query
->execute();
$cart_id = reset($result);
$cart = Order::load($cart_id);
$this
->assertEquals(0, $cart
->getCustomerId());
$this
->assertNotEmpty($cart
->hasItems());
$this
->drupalLogin($this->adminUser);
\Drupal::entityTypeManager()
->getStorage('commerce_order')
->resetCache();
$cart = Order::load($cart
->id());
$this
->assertEquals($this->adminUser
->id(), $cart
->getCustomerId());
}
public function testVariationCanonicalLinkAddToCartForm() {
$variation_type = ProductVariationType::load($this->variation
->bundle());
$color_attribute_values = $this
->createAttributeSet($variation_type, 'color', [
'cyan' => 'Cyan',
'magenta' => 'Magenta',
]);
$variation1 = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => 'not-canonical',
'price' => [
'number' => '5.00',
'currency_code' => 'USD',
],
'attribute_color' => $color_attribute_values['cyan'],
]);
$variation2 = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => 'canonical-test',
'price' => [
'number' => '9.99',
'currency_code' => 'USD',
],
'attribute_color' => $color_attribute_values['magenta'],
]);
$this
->createEntity('commerce_product', [
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$variation1,
$variation2,
],
]);
$this
->drupalGet($variation2
->toUrl());
$this
->assertSession()
->pageTextContains('$9.99');
$this
->submitForm([], 'Add to cart');
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertEquals($variation2
->getSku(), $order_items[0]
->getPurchasedEntity()
->getSku());
}
public function testExposedOrderItemFields() {
$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();
$this
->postAddToCart($this->variation
->getProduct(), [
'quantity[0][value]' => 3,
]);
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertOrderItemInOrder($this->variation, $order_items[0], 3);
$this
->postAddToCart($this->variation
->getProduct(), [
'quantity[0][value]' => 0,
]);
$this
->assertSession()
->pageTextContains('Quantity must be higher than or equal to 1.');
}
public function testRenderedAttributeElement() {
$variation_type = ProductVariationType::load($this->variation
->bundle());
$color_attribute_values = $this
->createAttributeSet($variation_type, 'color', [
'cyan' => 'Cyan',
'magenta' => 'Magenta',
], TRUE);
$color_attribute_values['cyan']
->set('rendered_test', 'Cyan (Rendered)')
->save();
$color_attribute_values['cyan']
->save();
$color_attribute_values['magenta']
->set('rendered_test', 'Magenta (Rendered)')
->save();
$color_attribute_values['magenta']
->save();
$color_attribute = ProductAttribute::load($color_attribute_values['cyan']
->getAttributeId());
$variation1 = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => $this
->randomMachineName(),
'price' => [
'number' => 999,
'currency_code' => 'USD',
],
'attribute_color' => $color_attribute_values['cyan'],
]);
$variation2 = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => $this
->randomMachineName(),
'price' => [
'number' => 999,
'currency_code' => 'USD',
],
'attribute_color' => $color_attribute_values['magenta'],
]);
$product = $this
->createEntity('commerce_product', [
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$variation1,
$variation2,
],
]);
$this
->drupalGet($product
->toUrl());
$this
->assertAttributeExists('purchased_entity[0][attributes][attribute_color]', $color_attribute_values['cyan']
->id());
$color_attribute
->set('elementType', 'commerce_product_rendered_attribute')
->save();
$this
->drupalGet($product
->toUrl());
$this
->assertSession()
->pageTextContains('Cyan (Rendered)');
$this
->assertSession()
->pageTextContains('Magenta (Rendered)');
}
public function testOptionalProductAttribute() {
$variation_type = ProductVariationType::load($this->variation
->bundle());
$size_attributes = $this
->createAttributeSet($variation_type, 'size', [
'small' => 'Small',
'medium' => 'Medium',
'large' => 'Large',
]);
$color_attributes = $this
->createAttributeSet($variation_type, 'color', [
'red' => 'Red',
]);
$color_field = FieldConfig::loadByName('commerce_product_variation', 'default', 'attribute_color');
$color_field
->setRequired(TRUE);
$color_field
->save();
$this->variation = ProductVariation::load($this->variation
->id());
$product = $this->variation
->getProduct();
$this->variation->attribute_size = $size_attributes['small']
->id();
$this->variation->attribute_color = $color_attributes['red']
->id();
$this->variation
->save();
$attribute_values_matrix = [
[
'medium',
'red',
],
[
'large',
'red',
],
];
$variations = [
$this->variation,
];
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',
],
'attribute_size' => $size_attributes[$value[0]]
->id(),
'attribute_color' => $color_attributes[$value[1]]
->id(),
]);
$variations[] = $variation;
$product->variations
->appendItem($variation);
}
$product
->save();
$this
->drupalGet($product
->toUrl());
$this
->assertSession()
->fieldExists('purchased_entity[0][attributes][attribute_size]');
$this
->assertSession()
->elementExists('xpath', '//select[@id="edit-purchased-entity-0-attributes-attribute-color" and @required]');
foreach ($variations as $variation) {
$variation->attribute_color = NULL;
$this->variation
->save();
}
$this
->drupalGet($product
->toUrl());
$this
->assertSession()
->fieldExists('purchased_entity[0][attributes][attribute_size]');
$this
->assertSession()
->fieldNotExists('purchased_entity[0][attributes][attribute_color]');
}
}