You are here

public function PromotionConditionTest::testPromotionConditions in Commerce Core 8.2

Tests promotion conditions.

File

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

Class

PromotionConditionTest
Tests promotion conditions.

Namespace

Drupal\Tests\commerce_promotion\Kernel

Code

public function testPromotionConditions() {

  // Starts now, enabled. No end time. Matches orders under $20 or over $100.
  $promotion = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'status' => TRUE,
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.10',
      ],
    ],
    'conditions' => [
      [
        'target_plugin_id' => 'order_total_price',
        'target_plugin_configuration' => [
          'operator' => '<',
          'amount' => [
            'number' => '20.00',
            'currency_code' => 'USD',
          ],
        ],
      ],
      [
        'target_plugin_id' => 'order_total_price',
        'target_plugin_configuration' => [
          'operator' => '>',
          'amount' => [
            'number' => '100.00',
            'currency_code' => 'USD',
          ],
        ],
      ],
    ],
    'condition_operator' => 'OR',
  ]);
  $promotion
    ->save();
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => 3,
    'unit_price' => [
      'number' => '10.00',
      'currency_code' => 'USD',
    ],
  ]);
  $order_item
    ->save();
  $this->order
    ->addItem($order_item);
  $result = $promotion
    ->applies($this->order);
  $this
    ->assertFalse($result);
  $order_item
    ->setQuantity(1);
  $order_item
    ->save();
  $this->order
    ->save();
  $result = $promotion
    ->applies($this->order);
  $this
    ->assertTrue($result);
  $order_item
    ->setQuantity(11);
  $order_item
    ->save();
  $this->order
    ->save();
  $result = $promotion
    ->applies($this->order);
  $this
    ->assertTrue($result);

  // No order total can satisfy both conditions.
  $promotion
    ->setConditionOperator('AND');
  $result = $promotion
    ->applies($this->order);
  $this
    ->assertFalse($result);
}