You are here

public function PromotionConditionTest::testOfferConditions in Commerce Core 8.2

Tests offer conditions.

File

modules/promotion/tests/src/Kernel/PromotionConditionTest.php, line 137

Class

PromotionConditionTest
Tests promotion conditions.

Namespace

Drupal\Tests\commerce_promotion\Kernel

Code

public function testOfferConditions() {

  // 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' => [
        'conditions' => [
          [
            'plugin' => 'order_item_product_type',
            'configuration' => [
              'product_types' => [
                'default',
              ],
            ],
          ],
        ],
        'percentage' => '0.10',
      ],
    ],
    'conditions' => [
      [
        'target_plugin_id' => 'order_total_price',
        'target_plugin_configuration' => [
          'operator' => '>',
          'amount' => [
            'number' => '30.00',
            'currency_code' => 'USD',
          ],
        ],
      ],
    ],
    'condition_operator' => 'AND',
  ]);
  $promotion
    ->save();
  $product_type = ProductType::create([
    'id' => 'test',
    'label' => 'Test',
    'variationType' => 'default',
  ]);
  $product_type
    ->save();
  $first_variation = ProductVariation::create([
    'type' => 'default',
    'sku' => $this
      ->randomMachineName(),
    'price' => [
      'number' => '20',
      'currency_code' => 'USD',
    ],
  ]);
  $first_variation
    ->save();
  $second_variation = ProductVariation::create([
    'type' => 'default',
    'sku' => $this
      ->randomMachineName(),
    'price' => [
      'number' => '30',
      'currency_code' => 'USD',
    ],
  ]);
  $second_variation
    ->save();
  $first_product = Product::create([
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $first_variation,
    ],
  ]);
  $first_product
    ->save();
  $second_product = Product::create([
    'type' => 'test',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $second_variation,
    ],
  ]);
  $second_product
    ->save();

  /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
  $order_item_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order_item');
  $first_order_item = $order_item_storage
    ->createFromPurchasableEntity($first_variation);
  $first_order_item
    ->save();
  $second_order_item = $order_item_storage
    ->createFromPurchasableEntity($second_variation);
  $second_order_item
    ->save();
  $this->order
    ->setItems([
    $first_order_item,
    $second_order_item,
  ]);
  $this->order->state = 'draft';
  $this->order
    ->save();
  $this->order = $this
    ->reloadEntity($this->order);
  $first_order_item = $this
    ->reloadEntity($first_order_item);
  $second_order_item = $this
    ->reloadEntity($second_order_item);
  $this
    ->assertCount(1, $first_order_item
    ->getAdjustments());
  $this
    ->assertCount(0, $second_order_item
    ->getAdjustments());
}