|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\commerce_promotion\Kernel; |
| 4 | + |
| 5 | +use Drupal\commerce_price\Price; |
| 6 | +use Drupal\commerce_product\Entity\Product; |
| 7 | +use Drupal\commerce_product\Entity\ProductVariation; |
| 8 | +use Drupal\commerce_promotion\Entity\Promotion; |
| 9 | +use Drupal\Tests\commerce_cart\Kernel\CartManagerTestTrait; |
| 10 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests the integration between promotions and carts. |
| 14 | + * |
| 15 | + * @group commerce |
| 16 | + */ |
| 17 | +class PromotionCartTest extends CommerceKernelTestBase { |
| 18 | + |
| 19 | + use CartManagerTestTrait; |
| 20 | + |
| 21 | + /** |
| 22 | + * The cart manager. |
| 23 | + * |
| 24 | + * @var \Drupal\commerce_cart\CartManager |
| 25 | + */ |
| 26 | + protected $cartManager; |
| 27 | + |
| 28 | + /** |
| 29 | + * The cart provider. |
| 30 | + * |
| 31 | + * @var \Drupal\commerce_cart\CartProvider |
| 32 | + */ |
| 33 | + protected $cartProvider; |
| 34 | + |
| 35 | + /** |
| 36 | + * Modules to enable. |
| 37 | + * |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + public static $modules = [ |
| 41 | + 'entity_reference_revisions', |
| 42 | + 'profile', |
| 43 | + 'state_machine', |
| 44 | + 'commerce_order', |
| 45 | + 'path', |
| 46 | + 'commerce_product', |
| 47 | + 'commerce_promotion', |
| 48 | + ]; |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + protected function setUp() { |
| 54 | + parent::setUp(); |
| 55 | + |
| 56 | + $this->installEntitySchema('profile'); |
| 57 | + $this->installEntitySchema('commerce_order'); |
| 58 | + $this->installEntitySchema('commerce_order_item'); |
| 59 | + $this->installEntitySchema('commerce_promotion'); |
| 60 | + $this->installEntitySchema('commerce_product_variation'); |
| 61 | + $this->installEntitySchema('commerce_product'); |
| 62 | + $this->installConfig([ |
| 63 | + 'profile', |
| 64 | + 'commerce_order', |
| 65 | + 'commerce_product', |
| 66 | + 'commerce_promotion', |
| 67 | + ]); |
| 68 | + $this->installSchema('commerce_promotion', ['commerce_promotion_usage']); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Tests adding a product with a promotion to the cart. |
| 73 | + */ |
| 74 | + public function testPromotionCart() { |
| 75 | + $this->installCommerceCart(); |
| 76 | + |
| 77 | + $variation = ProductVariation::create([ |
| 78 | + 'type' => 'default', |
| 79 | + 'sku' => strtolower($this->randomMachineName()), |
| 80 | + 'price' => [ |
| 81 | + 'number' => '10.00', |
| 82 | + 'currency_code' => 'USD', |
| 83 | + ], |
| 84 | + ]); |
| 85 | + $variation->save(); |
| 86 | + $product = Product::create([ |
| 87 | + 'type' => 'default', |
| 88 | + 'title' => 'My product', |
| 89 | + 'variations' => [$variation], |
| 90 | + ]); |
| 91 | + $product->save(); |
| 92 | + |
| 93 | + $promotion = Promotion::create([ |
| 94 | + 'name' => 'Promotion test', |
| 95 | + 'order_types' => ['default'], |
| 96 | + 'stores' => [$this->store->id()], |
| 97 | + 'offer' => [ |
| 98 | + 'target_plugin_id' => 'commerce_promotion_order_percentage_off', |
| 99 | + 'target_plugin_configuration' => [ |
| 100 | + 'amount' => '0.10', |
| 101 | + ], |
| 102 | + ], |
| 103 | + 'start_date' => '2017-01-01', |
| 104 | + 'status' => TRUE, |
| 105 | + ]); |
| 106 | + $promotion->save(); |
| 107 | + |
| 108 | + $user = $this->createUser(); |
| 109 | + /** @var \Drupal\commerce_order\Entity\OrderInterface $cart_order */ |
| 110 | + $cart = $this->cartProvider->createCart('default', $this->store, $user); |
| 111 | + $this->cartManager->addEntity($cart, $variation); |
| 112 | + |
| 113 | + $this->assertEquals(1, count($cart->getAdjustments())); |
| 114 | + $this->assertEquals(new Price('9.00', 'USD'), $cart->getTotalPrice()); |
| 115 | + |
| 116 | + // Disable the promotion. |
| 117 | + $promotion->setEnabled(FALSE); |
| 118 | + $promotion->save(); |
| 119 | + $this->container->get('commerce_order.order_refresh')->refresh($cart); |
| 120 | + $this->assertEmpty($cart->getAdjustments()); |
| 121 | + $this->assertEquals(new Price('10.00', 'USD'), $cart->getTotalPrice()); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments