You are here

public function CombinationOfferTest::testApply in Commerce Core 8.2

Tests the combination offer.

@covers ::apply

File

modules/promotion/tests/src/Kernel/Plugin/Commerce/PromotionOffer/CombinationOfferTest.php, line 99

Class

CombinationOfferTest
Tests the combination offer plugin.

Namespace

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

Code

public function testApply() {

  // Starts now, enabled. No end time.
  $promotion = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'combination_offer',
      'target_plugin_configuration' => [
        'offers' => [
          [
            'target_plugin_id' => 'order_item_percentage_off',
            'target_plugin_configuration' => [
              'display_inclusive' => TRUE,
              'percentage' => '0.50',
              'conditions' => [
                [
                  'plugin' => 'order_item_product_type',
                  'configuration' => [
                    'product_types' => [
                      'default',
                    ],
                  ],
                ],
              ],
            ],
          ],
          [
            'target_plugin_id' => 'order_buy_x_get_y',
            'target_plugin_configuration' => [
              'buy_quantity' => 1,
              'get_quantity' => 1,
              'get_conditions' => [
                [
                  'plugin' => 'order_item_product_type',
                  'configuration' => [
                    'product_types' => [
                      'test',
                    ],
                  ],
                ],
              ],
              'offer_type' => 'fixed_amount',
              'offer_amount' => [
                'number' => '2.00',
                'currency_code' => 'USD',
              ],
            ],
          ],
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $promotion
    ->save();
  $this
    ->assertInstanceOf(CombinationOfferInterface::class, $promotion
    ->getOffer());
  $this
    ->assertCount(2, $promotion
    ->getOffer()
    ->getOffers());
  $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->order
    ->getItems()[0];
  $this
    ->assertEquals(1, count($order_item
    ->getAdjustments()));
  $adjustment = $order_item
    ->getAdjustments()[0];
  $this
    ->assertEquals(new Price('5', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('10', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('10', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-10.00', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertTrue($adjustment
    ->isIncluded());

  // Add another product to test that the "buy x get y" offer applies.
  $variation = ProductVariation::create([
    'type' => 'default',
    'sku' => $this
      ->randomMachineName(),
    'price' => [
      'number' => '10',
      'currency_code' => 'USD',
    ],
  ]);
  $test_product = Product::create([
    'type' => 'test',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $variation,
    ],
  ]);
  $test_product
    ->save();
  $order_item_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order_item');
  $test_order_item = $order_item_storage
    ->createFromPurchasableEntity($variation, [
    'quantity' => '1',
  ]);
  $test_order_item
    ->save();
  $this->order
    ->addItem($test_order_item);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  $order_item = $this->order
    ->getItems()[0];
  $this
    ->assertEquals(1, count($order_item
    ->getAdjustments()));
  $adjustment = $order_item
    ->getAdjustments()[0];
  $this
    ->assertEquals(new Price('5', 'USD'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('10', 'USD'), $order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('10', 'USD'), $order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-10.00', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertTrue($adjustment
    ->isIncluded());
  $test_order_item = $this->order
    ->getItems()[1];
  $this
    ->assertEquals(1, count($test_order_item
    ->getAdjustments()));
  $adjustment = $test_order_item
    ->getAdjustments()[0];
  $this
    ->assertEquals(new Price('10', 'USD'), $test_order_item
    ->getUnitPrice());
  $this
    ->assertEquals(new Price('10', 'USD'), $test_order_item
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('8', 'USD'), $test_order_item
    ->getAdjustedTotalPrice());
  $this
    ->assertFalse($adjustment
    ->isIncluded());
}