You are here

public function OrderItemPercentageOffTest::testNonDisplayInclusive in Commerce Core 8.2

Tests the non-display-inclusive offer.

@covers ::apply

File

modules/promotion/tests/src/Kernel/Plugin/Commerce/PromotionOffer/OrderItemPercentageOffTest.php, line 206

Class

OrderItemPercentageOffTest
Tests the percentage off offer for order items.

Namespace

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

Code

public function testNonDisplayInclusive() {
  $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();

  // Starts now, enabled. No end time.
  $promotion = Promotion::create([
    'name' => 'Promotion 1',
    'display_name' => '50% 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' => '0.50',
      ],
    ],
  ]);
  $promotion
    ->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);
  $this
    ->assertEquals(new Price('9.99', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('19.98', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('9.99', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(1, count($order_item
    ->getAdjustments()));
  $adjustment = $order_item
    ->getAdjustments()[0];
  $this
    ->assertEquals('50% off', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-9.99', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals('0.50', $adjustment
    ->getPercentage());
  $this
    ->assertFalse($adjustment
    ->isIncluded());
  $this->order
    ->recalculateTotalPrice();
  $this
    ->assertEquals(new Price('9.99', 'USD'), $this->order
    ->getTotalPrice());

  // Test with multiple promotions.
  $another_promotion = Promotion::create([
    'name' => 'Promotion 2',
    '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' => 10,
  ]);
  $another_promotion
    ->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);
  $this
    ->assertEquals(new Price('9.99', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('19.98', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('0', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $adjustments = $order_item
    ->getAdjustments();
  $this
    ->assertEquals(2, count($adjustments));
  $this
    ->assertEquals('50% off', $adjustments[0]
    ->getLabel());
  $this
    ->assertEquals(new Price('-9.99', 'USD'), $adjustments[0]
    ->getAmount());
  $this
    ->assertEquals(new Price('-9.99', 'USD'), $adjustments[1]
    ->getAmount());
  $this
    ->assertEquals('0.50', $adjustments[0]
    ->getPercentage());
  $this
    ->assertEquals('1', $adjustments[1]
    ->getPercentage());
  $this
    ->assertFalse($adjustments[0]
    ->isIncluded());
  $this
    ->assertFalse($adjustments[1]
    ->isIncluded());
  $this->order
    ->recalculateTotalPrice();
  $this
    ->assertEquals(new Price('0', 'USD'), $this->order
    ->getTotalPrice());
}