public function OrderItemPercentageOffTest::testDisplayInclusive in Commerce Core 8.2
Tests the display-inclusive offer.
@covers ::apply
File
- modules/
promotion/ tests/ src/ Kernel/ Plugin/ Commerce/ PromotionOffer/ OrderItemPercentageOffTest.php, line 90
Class
- OrderItemPercentageOffTest
- Tests the percentage off offer for order items.
Namespace
Drupal\Tests\commerce_promotion\Kernel\Plugin\Commerce\PromotionOfferCode
public function testDisplayInclusive() {
$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',
'order_types' => [
$this->order
->bundle(),
],
'stores' => [
$this->store
->id(),
],
'status' => TRUE,
'offer' => [
'target_plugin_id' => 'order_item_percentage_off',
'target_plugin_configuration' => [
'display_inclusive' => TRUE,
'percentage' => '0.50',
],
],
'weight' => -1,
]);
$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('4.99', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('9.98', 'USD'), $order_item
->getTotalPrice());
$this
->assertEquals(new Price('9.98', '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('-10.00', 'USD'), $adjustment
->getAmount());
$this
->assertEquals('0.50', $adjustment
->getPercentage());
$this
->assertTrue($adjustment
->isIncluded());
$this->order
->recalculateTotalPrice();
$this
->assertEquals(new Price('9.98', 'USD'), $this->order
->getTotalPrice());
$another_promotion = Promotion::create([
'name' => 'Promotion 2',
'order_types' => [
$this->order
->bundle(),
],
'stores' => [
$this->store
->id(),
],
'status' => TRUE,
'offer' => [
'target_plugin_id' => 'order_item_percentage_off',
'target_plugin_configuration' => [
'display_inclusive' => TRUE,
'percentage' => '0.5',
],
],
'weight' => 0,
]);
$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('2.49', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('4.98', 'USD'), $order_item
->getTotalPrice());
$this
->assertEquals(new Price('4.98', 'USD'), $order_item
->getAdjustedTotalPrice());
$adjustments = $order_item
->getAdjustments();
$this
->assertEquals(2, count($adjustments));
$this
->assertEquals('Discount', $adjustments[0]
->getLabel());
$this
->assertEquals(new Price('-10', 'USD'), $adjustments[0]
->getAmount());
$this
->assertEquals(new Price('-5', 'USD'), $adjustments[1]
->getAmount());
$this
->assertEquals('0.5', $adjustments[0]
->getPercentage());
$this
->assertEquals('0.5', $adjustments[1]
->getPercentage());
$this
->assertTrue($adjustments[0]
->isIncluded());
$this
->assertTrue($adjustments[1]
->isIncluded());
$this->order
->recalculateTotalPrice();
$this
->assertEquals(new Price('4.98', 'USD'), $this->order
->getTotalPrice());
$another_promotion = Promotion::create([
'name' => 'Promotion 3',
'order_types' => [
$this->order
->bundle(),
],
'stores' => [
$this->store
->id(),
],
'status' => TRUE,
'offer' => [
'target_plugin_id' => 'order_item_percentage_off',
'target_plugin_configuration' => [
'display_inclusive' => TRUE,
'percentage' => '1',
],
],
'weight' => 1,
]);
$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('0', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('0', 'USD'), $order_item
->getTotalPrice());
$this
->assertEquals(new Price('0', 'USD'), $order_item
->getAdjustedTotalPrice());
$adjustments = $order_item
->getAdjustments();
$this
->assertEquals(3, count($adjustments));
$this
->assertEquals('Discount', $adjustments[0]
->getLabel());
$this
->assertEquals(new Price('-10', 'USD'), $adjustments[0]
->getAmount());
$this
->assertEquals(new Price('-5', 'USD'), $adjustments[1]
->getAmount());
$this
->assertEquals(new Price('-4.98', 'USD'), $adjustments[2]
->getAmount());
$this->order
->recalculateTotalPrice();
$this
->assertEquals(new Price('0', 'USD'), $this->order
->getTotalPrice());
}