You are here

public function OrderItemFixedAmountOffTest::testDisplayInclusive in Commerce Core 8.2

Tests the display-inclusive offer.

@covers ::apply

File

modules/promotion/tests/src/Kernel/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOffTest.php, line 90

Class

OrderItemFixedAmountOffTest
Tests the percentage off offer for order items.

Namespace

Drupal\Tests\commerce_promotion\Kernel\Plugin\Commerce\PromotionOffer

Code

public function testDisplayInclusive() {

  // Starts now, enabled. No end time.
  $promotion = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_item_fixed_amount_off',
      'target_plugin_configuration' => [
        'display_inclusive' => TRUE,
        'amount' => [
          'number' => '15.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $promotion
    ->save();
  $order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => '2',
    'unit_price' => $this->variation
      ->getPrice(),
    'purchased_entity' => $this->variation
      ->id(),
  ]);
  $order_item
    ->save();
  $this->order
    ->addItem($order_item);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  $this->order = $this
    ->reloadEntity($this->order);

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = $this
    ->reloadEntity($order_item);

  // Offer amount larger than the order item unit price.
  $this
    ->assertEquals(new Price('0.00', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('0.00', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('0.00', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(1, count($order_item
    ->getAdjustments()));
  $adjustment = $order_item
    ->getAdjustments()[0];
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-20.00', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertTrue($adjustment
    ->isIncluded());

  // Offer amount smaller than the order item unit price.
  $this->variation
    ->setPrice(new Price('20', 'USD'));
  $this->variation
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  $this->order = $this
    ->reloadEntity($this->order);
  $order_item = $this
    ->reloadEntity($order_item);
  $this
    ->assertEquals(new Price('5.00', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('10.00', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('10.00', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(1, count($order_item
    ->getAdjustments()));
  $adjustment = $order_item
    ->getAdjustments()[0];
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-30.00', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertTrue($adjustment
    ->isIncluded());

  // Test with multiple promotions.
  // Starts now, enabled. No end time.
  $another_promotion = Promotion::create([
    'name' => 'Promotion 2',
    'display_name' => '$20 off',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'status' => TRUE,
    'offer' => [
      'target_plugin_id' => 'order_item_fixed_amount_off',
      'target_plugin_configuration' => [
        'display_inclusive' => TRUE,
        'amount' => [
          'number' => '20.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
  ]);
  $another_promotion
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  $this->order = $this
    ->reloadEntity($this->order);
  $order_item = $this
    ->reloadEntity($order_item);
  $adjustments = $order_item
    ->getAdjustments();
  $this
    ->assertEquals(new Price('0.00', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('0.00', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('0.00', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(2, count($adjustments));
  $this
    ->assertEquals('Discount', $adjustments[0]
    ->getLabel());
  $this
    ->assertEquals(new Price('-30.00', 'USD'), $adjustments[0]
    ->getAmount());
  $this
    ->assertTrue($adjustments[0]
    ->isIncluded());
  $this
    ->assertEquals('$20 off', $adjustments[1]
    ->getLabel());
  $this
    ->assertEquals(new Price('-10.00', 'USD'), $adjustments[1]
    ->getAmount());
  $this
    ->assertTrue($adjustments[1]
    ->isIncluded());
}