You are here

public function OrderTest::testHandlingLegacyOrderItemAdjustments in Commerce Core 8.2

Tests the handling of legacy order item adjustments on adjustment clear.

@covers ::clearAdjustments @covers ::collectAdjustments

File

modules/order/tests/src/Kernel/Entity/OrderTest.php, line 401

Class

OrderTest
Tests the Order entity.

Namespace

Drupal\Tests\commerce_order\Kernel\Entity

Code

public function testHandlingLegacyOrderItemAdjustments() {

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => '2',
    'unit_price' => new Price('10.00', 'USD'),
    'adjustments' => [
      new Adjustment([
        'type' => 'custom',
        'label' => '10% off',
        'amount' => new Price('-1.00', 'USD'),
        'percentage' => '0.1',
      ]),
      new Adjustment([
        'type' => 'fee',
        'label' => 'Random fee',
        'amount' => new Price('2.00', 'USD'),
      ]),
    ],
    'uses_legacy_adjustments' => TRUE,
  ]);
  $order_item
    ->save();
  $order = Order::create([
    'type' => 'default',
    'order_items' => [
      $order_item,
    ],
    'state' => 'draft',
  ]);

  // Confirm that legacy adjustments are multiplied by quantity.
  $adjustments = $order
    ->collectAdjustments();
  $this
    ->assertCount(2, $adjustments);
  $this
    ->assertEquals('-2.00', $adjustments[0]
    ->getAmount()
    ->getNumber());
  $this
    ->assertEquals('4.00', $adjustments[1]
    ->getAmount()
    ->getNumber());

  // Confirm that the legacy order item adjustments are converted on clear.
  $order
    ->clearAdjustments();
  $order_items = $order
    ->getItems();
  $order_item = reset($order_items);
  $adjustments = $order_item
    ->getAdjustments();
  $this
    ->assertFalse($order_item
    ->usesLegacyAdjustments());
  $this
    ->assertCount(1, $adjustments);
  $this
    ->assertEquals('-2.00', $adjustments[0]
    ->getAmount()
    ->getNumber());

  // The order item adjustments are no longer multiplied by quantity.
  $this
    ->assertEquals($adjustments, $order
    ->collectAdjustments());
}