You are here

public function OrderItemFixedAmountOffTest::testStackingPromotions in Commerce Core 8.2

Tests applying both an inclusive and a non inclusive promotion.

@covers ::apply

File

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

Class

OrderItemFixedAmountOffTest
Tests the percentage off offer for order items.

Namespace

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

Code

public function testStackingPromotions() {

  /** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
  $promotion = Promotion::create([
    'name' => 'Promotion test',
    'display_name' => '$5 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' => FALSE,
        'amount' => [
          'number' => '5.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'weight' => -1,
  ]);
  $promotion
    ->save();
  $inclusive_promotion = $promotion
    ->createDuplicate();
  $inclusive_promotion
    ->setWeight(0);
  $inclusive_promotion
    ->setDisplayName('Inclusive $2 off');
  $offer = $inclusive_promotion
    ->getOffer();
  $offer
    ->setConfiguration([
    'display_inclusive' => TRUE,
    'amount' => [
      'number' => '2.00',
      'currency_code' => 'USD',
    ],
  ]);
  $inclusive_promotion
    ->setOffer($offer);
  $inclusive_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);
  $order_item = $this
    ->reloadEntity($order_item);
  $adjustments = $order_item
    ->getAdjustments();
  $this
    ->assertEquals(2, count($adjustments));
  $this
    ->assertEquals('$5 off', $adjustments[0]
    ->getLabel());
  $this
    ->assertEquals(new Price('16', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('3', 'USD'), $order_item
    ->getAdjustedUnitPrice());
  $this
    ->assertEquals(new Price('6', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(new Price('8', 'USD'), $order_item
    ->getUnitPrice());
  $this->order
    ->recalculateTotalPrice();
  $this
    ->assertEquals(new Price('6', 'USD'), $this->order
    ->getTotalPrice());
  $another_inclusive_promotion = $inclusive_promotion
    ->createDuplicate();
  $another_inclusive_promotion
    ->setWeight(2);
  $another_inclusive_promotion
    ->setDisplayName('Inclusive $10 off');
  $offer = $another_inclusive_promotion
    ->getOffer();
  $offer
    ->setConfiguration([
    'display_inclusive' => TRUE,
    'amount' => [
      'number' => '10.00',
      'currency_code' => 'USD',
    ],
  ]);
  $another_inclusive_promotion
    ->setOffer($offer);
  $another_inclusive_promotion
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  $order_item = $this
    ->reloadEntity($order_item);
  $adjustments = $order_item
    ->getAdjustments();
  $this
    ->assertEquals(3, count($adjustments));
  $this
    ->assertEquals('$5 off', $adjustments[0]
    ->getLabel());
  $this
    ->assertFalse($adjustments[0]
    ->isIncluded());
  $this
    ->assertEquals(new Price('10', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('0', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(new Price('5', 'USD'), $order_item
    ->getUnitPrice());
  $this->order
    ->recalculateTotalPrice();
  $this
    ->assertEquals(new Price('0', 'USD'), $this->order
    ->getTotalPrice());
}