You are here

public function BuyXGetYTest::testOrderItemSorting in Commerce Core 8.2

Tests order item sorting.

@covers ::apply

File

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

Class

BuyXGetYTest
Tests the "Buy X Get Y" offer.

Namespace

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

Code

public function testOrderItemSorting() {

  // First cheapest product gets 50% off.
  $offer = $this->promotion
    ->getOffer();
  $offer_configuration = $offer
    ->getConfiguration();
  $offer_configuration['get_quantity'] = '1';
  $offer_configuration['offer_type'] = 'percentage';
  $offer_configuration['offer_percentage'] = '0.5';
  $offer_configuration['offer_amount'] = NULL;
  $offer
    ->setConfiguration($offer_configuration);
  $this->promotion
    ->setOffer($offer);

  /** @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], [
    'quantity' => '6',
  ]);
  $first_order_item
    ->save();

  // Both order items match the get_conditions, $third_order_item should be
  // discounted because it is cheaper.
  $second_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[3], [
    'quantity' => '1',
  ]);
  $second_order_item
    ->save();
  $third_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[2], [
    'quantity' => '1',
  ]);
  $third_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());
}