You are here

public function BuyXGetYTest::testPercentageOff in Commerce Core 8.2

Tests the percentage off offer type.

@covers ::apply

File

modules/promotion/tests/src/Kernel/Plugin/Commerce/PromotionOffer/BuyXGetYTest.php, line 269

Class

BuyXGetYTest
Tests the "Buy X Get Y" offer.

Namespace

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

Code

public function testPercentageOff() {
  $offer = $this->promotion
    ->getOffer();
  $offer_configuration = $offer
    ->getConfiguration();
  $offer_configuration['offer_type'] = 'percentage';
  $offer_configuration['offer_percentage'] = '0.1';
  $offer_configuration['offer_amount'] = NULL;
  $offer
    ->setConfiguration($offer_configuration);
  $this->promotion
    ->setOffer($offer);
  $this->promotion
    ->setDisplayName('Buy X Get Y!');

  /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
  $order_item_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order_item');
  $first_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[0], [
    // Double the buy_quantity -> double the get_quantity.
    'quantity' => '13',
  ]);
  $first_order_item
    ->save();
  $second_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[1], [
    'quantity' => '2',
  ]);

  // Test having a single offer order item, quantity < get_quantity.
  $third_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[2], [
    'quantity' => '6',
  ]);
  $second_order_item
    ->save();
  $this->order
    ->setItems([
    $first_order_item,
    $second_order_item,
    $third_order_item,
  ]);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  list($first_order_item, $second_order_item, $third_order_item) = $this->order
    ->getItems();
  $this
    ->assertCount(0, $first_order_item
    ->getAdjustments());
  $this
    ->assertCount(0, $second_order_item
    ->getAdjustments());
  $this
    ->assertCount(1, $third_order_item
    ->getAdjustments());
  $adjustments = $third_order_item
    ->getAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Buy X Get Y!', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-18', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());

  // Test having two offer order items, one ($third_order_item) reduced
  // completely, the other ($fourth_order_item) reduced partially.
  $fourth_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[2], [
    'quantity' => '3',
  ]);
  $this->order
    ->addItem($fourth_order_item);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  list($first_order_item, $second_order_item, $third_order_item, $fourth_order_item) = $this->order
    ->getItems();
  $this
    ->assertCount(0, $first_order_item
    ->getAdjustments());
  $this
    ->assertCount(0, $second_order_item
    ->getAdjustments());
  $this
    ->assertCount(1, $third_order_item
    ->getAdjustments());
  $this
    ->assertCount(1, $fourth_order_item
    ->getAdjustments());
  $adjustments = $third_order_item
    ->getAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Buy X Get Y!', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-18', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
  $adjustments = $fourth_order_item
    ->getAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Buy X Get Y!', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-6', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
}