You are here

public function OrderItemPercentageOffTest::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/OrderItemPercentageOffTest.php, line 294

Class

OrderItemPercentageOffTest
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',
    'display_name' => '100% off',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'status' => TRUE,
    'offer' => [
      'target_plugin_id' => 'order_item_percentage_off',
      'target_plugin_configuration' => [
        'display_inclusive' => FALSE,
        'percentage' => '1',
      ],
    ],
    'weight' => 0,
  ]);
  $promotion
    ->save();
  $inclusive_promotion = $promotion
    ->createDuplicate();
  $inclusive_promotion
    ->setDisplayName('Inclusive 100% off');
  $offer = $inclusive_promotion
    ->getOffer();
  $offer
    ->setConfiguration([
    'display_inclusive' => TRUE,
    'percentage' => '1',
  ]);
  $inclusive_promotion
    ->setWeight(1);
  $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();

  // The adjusted total is already reduced to 0, the inclusive promotion
  // shouldn't add an adjustment.
  $this
    ->assertEquals(1, count($adjustments));
  $this
    ->assertEquals(new Price('19.98', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('0', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(new Price('9.99', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals('100% off', $adjustments[0]
    ->getLabel());
  $this->order
    ->recalculateTotalPrice();
  $this
    ->assertEquals(new Price('0', 'USD'), $this->order
    ->getTotalPrice());
}