You are here

public function CartManagerTest::testAddDuplicateOrderItemExtraField in Commerce Core 8.2

Tests that adding duplicate order items with extra fields results in merging.

File

modules/cart/tests/src/Kernel/CartManagerTest.php, line 189

Class

CartManagerTest
Tests the cart manager.

Namespace

Drupal\Tests\commerce_cart\Kernel

Code

public function testAddDuplicateOrderItemExtraField() {

  // Add an extra field to the default order item type form display.
  $form_display = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('commerce_order_item.default.add_to_cart');
  $this
    ->assertNotEmpty($form_display);
  $form_display
    ->setComponent('field_custom_text', [
    'type' => 'string_textfield',
  ]);
  $form_display
    ->save();
  $cart = $this->cartProvider
    ->createCart('default', $this->store, $this->user);
  $this
    ->assertInstanceOf(OrderInterface::class, $cart);
  $this
    ->assertEmpty($cart
    ->getItems());

  // Add order item with custom text.
  $order_item1 = $this->cartManager
    ->createOrderItem($this->variation1);
  $order_item1
    ->set('field_custom_text', 'Blue');
  $order_item1
    ->save();
  $order_item1 = $this->cartManager
    ->addOrderItem($cart, $order_item1);
  $order_item1 = $this
    ->reloadEntity($order_item1);
  $this
    ->assertNotEmpty($cart
    ->hasItem($order_item1));
  $this
    ->assertEquals(1, $order_item1
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item1
    ->getOrderId());
  $this
    ->assertEquals(new Price('1.00', 'USD'), $cart
    ->getTotalPrice());

  // Second item for same variation, different text should not be combined.
  $order_item2 = $this->cartManager
    ->createOrderItem($this->variation1, 3);
  $order_item2
    ->set('field_custom_text', 'Red');
  $order_item2
    ->save();
  $order_item2 = $this->cartManager
    ->addOrderItem($cart, $order_item2);
  $order_item2 = $this
    ->reloadEntity($order_item2);
  $this
    ->assertEquals(1, $order_item1
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item1
    ->getOrderId());
  $this
    ->assertNotEmpty($cart
    ->hasItem($order_item2));
  $this
    ->assertEquals(3, $order_item2
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item2
    ->getOrderId());
  $this
    ->assertEquals(new Price('4.00', 'USD'), $cart
    ->getTotalPrice());

  // Third item should be combined with first.
  $order_item3 = $this->cartManager
    ->createOrderItem($this->variation1, 3);
  $order_item3
    ->set('field_custom_text', 'Blue');
  $order_item3
    ->save();
  $order_item3 = $this->cartManager
    ->addOrderItem($cart, $order_item3);
  $order_item3 = $this
    ->reloadEntity($order_item3);
  $this
    ->assertNotEmpty($cart
    ->hasItem($order_item2));
  $this
    ->assertEquals(3, $order_item2
    ->getQuantity());
  $this
    ->assertNotEmpty($cart
    ->hasItem($order_item3));
  $this
    ->assertEquals(4, $order_item3
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item3
    ->getOrderId());
  $this
    ->assertEquals(new Price('7.00', 'USD'), $cart
    ->getTotalPrice());
}