public function PromotionCartTest::testPromotionCartEmpty in Commerce Core 8.2
Tests promotion adjustments are removed when a cart is emptied.
File
- modules/
promotion/ tests/ src/ Kernel/ PromotionCartTest.php, line 93
Class
- PromotionCartTest
- Tests the integration between promotions and carts.
Namespace
Drupal\Tests\commerce_promotion\KernelCode
public function testPromotionCartEmpty() {
$this
->installEntitySchema('commerce_promotion_coupon');
$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'price' => [
'number' => '10.00',
'currency_code' => 'USD',
],
]);
$variation
->save();
$product = Product::create([
'type' => 'default',
'title' => 'My product',
'variations' => [
$variation,
],
]);
$product
->save();
$promotion = Promotion::create([
'name' => 'Promotion test',
'order_types' => [
'default',
],
'stores' => [
$this->store
->id(),
],
'offer' => [
'target_plugin_id' => 'order_percentage_off',
'target_plugin_configuration' => [
'percentage' => '0.10',
],
],
'status' => TRUE,
]);
$promotion
->save();
$coupon = Coupon::create([
'promotion_id' => $promotion
->id(),
'code' => 'COUPON',
'usage_limit' => 1,
'status' => 1,
]);
$coupon
->save();
$user = $this
->createUser();
/** @var \Drupal\commerce_order\Entity\OrderInterface $cart_order */
$cart = $this->cartProvider
->createCart('default', $this->store, $user);
$this->cartManager
->addEntity($cart, $variation);
$this
->assertEquals(0, count($cart
->collectAdjustments()));
$this
->assertEquals(new Price('10.00', 'USD'), $cart
->getTotalPrice());
// Add the coupon to the cart.
$cart
->get('coupons')
->appendItem($coupon);
$cart
->save();
$this
->assertEquals(1, count($cart
->collectAdjustments()));
$this
->assertEquals(new Price('9.00', 'USD'), $cart
->getTotalPrice());
// Emptying the cart should remove the promotion.
$this->container
->get('commerce_cart.cart_manager')
->emptyCart($cart);
$this->cartManager
->addEntity($cart, $variation);
$this
->assertEquals(0, count($cart
->collectAdjustments()));
$this
->assertEquals(new Price('10.00', 'USD'), $cart
->getTotalPrice());
// Add the coupon back to the cart.
$cart
->get('coupons')
->appendItem($coupon);
$cart
->save();
$this
->assertEquals(1, count($cart
->collectAdjustments()));
$this
->assertEquals(new Price('9.00', 'USD'), $cart
->getTotalPrice());
}