You are here

public function PromotionOrderProcessorTest::testCouponRemoval in Commerce Core 8.2

Tests the order refresh to remove coupons from an order when invalid.

File

modules/promotion/tests/src/Kernel/PromotionOrderProcessorTest.php, line 191

Class

PromotionOrderProcessorTest
Tests the promotion order processor.

Namespace

Drupal\Tests\commerce_promotion\Kernel

Code

public function testCouponRemoval() {
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => 1,
    'unit_price' => new Price('12.00', 'USD'),
  ]);
  $order_item
    ->save();

  /** @var \Drupal\commerce_order\Entity\Order $order */
  $order = Order::create([
    'type' => 'default',
    'state' => 'draft',
    'mail' => 'test@example.com',
    'ip_address' => '127.0.0.1',
    'order_number' => '6',
    'store_id' => $this->store,
    'uid' => $this
      ->createUser(),
    'order_items' => [
      $order_item,
    ],
  ]);
  $order
    ->setRefreshState(Order::REFRESH_SKIP);
  $order
    ->save();
  $promotion = Promotion::create([
    'name' => 'Promotion',
    'order_types' => [
      'default',
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'usage_limit' => 1,
    'start_date' => '2017-01-01',
    'status' => TRUE,
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'amount' => '0.10',
      ],
    ],
  ]);
  $promotion
    ->save();
  $coupon = Coupon::create([
    'promotion_id' => $promotion
      ->id(),
    'code' => 'coupon_code',
    'usage_limit' => 1,
    'status' => TRUE,
  ]);
  $coupon
    ->save();
  $order
    ->get('coupons')
    ->appendItem($coupon);
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($order);
  $this
    ->assertCount(1, $order
    ->get('coupons')
    ->getValue());
  $coupon
    ->setEnabled(FALSE);
  $coupon
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($order);
  $this
    ->assertCount(0, $order
    ->get('coupons')
    ->getValue());
  $coupon
    ->setEnabled(TRUE);
  $coupon
    ->save();
  $order
    ->get('coupons')
    ->appendItem($coupon);
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($order);
  $this
    ->assertCount(1, $order
    ->get('coupons')
    ->getValue());
  $coupon
    ->delete();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($order);
  $this
    ->assertCount(0, $order
    ->get('coupons')
    ->getValue());
}