You are here

public function AddToCartFormTest::testVariationCanonicalLinkAddToCartForm in Commerce Core 8.2

Test adding a product to the cart, via the variant's canonical link.

File

modules/cart/tests/src/Functional/AddToCartFormTest.php, line 90

Class

AddToCartFormTest
Tests the add to cart form.

Namespace

Drupal\Tests\commerce_cart\Functional

Code

public function testVariationCanonicalLinkAddToCartForm() {

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