|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\commerce_promotion\Kernel; |
| 4 | + |
| 5 | +use Drupal\commerce_order\Entity\Order; |
| 6 | +use Drupal\commerce_order\Entity\OrderItemType; |
| 7 | +use Drupal\commerce_order\Entity\OrderType; |
| 8 | +use Drupal\commerce_price\Price; |
| 9 | +use Drupal\commerce_promotion\Entity\Promotion; |
| 10 | +use Drupal\commerce_promotion\Entity\PromotionInterface; |
| 11 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests promotion compatibility options. |
| 15 | + * |
| 16 | + * @group commerce |
| 17 | + * @group commerce_promotion |
| 18 | + */ |
| 19 | +class PromotionCompatibilityTest extends CommerceKernelTestBase { |
| 20 | + |
| 21 | + /** |
| 22 | + * The test order. |
| 23 | + * |
| 24 | + * @var \Drupal\commerce_order\Entity\OrderInterface |
| 25 | + */ |
| 26 | + protected $order; |
| 27 | + |
| 28 | + /** |
| 29 | + * Modules to enable. |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + public static $modules = [ |
| 34 | + 'entity_reference_revisions', |
| 35 | + 'profile', |
| 36 | + 'state_machine', |
| 37 | + 'commerce_order', |
| 38 | + 'commerce_promotion', |
| 39 | + ]; |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + protected function setUp() { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->installEntitySchema('profile'); |
| 48 | + $this->installEntitySchema('commerce_order'); |
| 49 | + $this->installEntitySchema('commerce_order_type'); |
| 50 | + $this->installEntitySchema('commerce_promotion'); |
| 51 | + $this->installEntitySchema('commerce_promotion_coupon'); |
| 52 | + $this->installConfig([ |
| 53 | + 'profile', |
| 54 | + 'commerce_order', |
| 55 | + 'commerce_promotion', |
| 56 | + ]); |
| 57 | + |
| 58 | + // An order item type that doesn't need a purchasable entity, for simplicity. |
| 59 | + OrderItemType::create([ |
| 60 | + 'id' => 'test', |
| 61 | + 'label' => 'Test', |
| 62 | + 'orderType' => 'default', |
| 63 | + ])->save(); |
| 64 | + |
| 65 | + $this->order = Order::create([ |
| 66 | + 'type' => 'default', |
| 67 | + 'state' => 'completed', |
| 68 | + 'mail' => 'test@example.com', |
| 69 | + 'ip_address' => '127.0.0.1', |
| 70 | + 'order_number' => '6', |
| 71 | + 'store_id' => $this->store, |
| 72 | + 'order_items' => [], |
| 73 | + 'total_price' => new Price('100.00', 'USD'), |
| 74 | + 'uid' => $this->createUser()->id(), |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Tests the compatibility setting. |
| 80 | + */ |
| 81 | + public function testCompatibility() { |
| 82 | + $order_type = OrderType::load('default'); |
| 83 | + |
| 84 | + // Starts now, enabled. No end time. |
| 85 | + $promotion1 = Promotion::create([ |
| 86 | + 'name' => 'Promotion 1', |
| 87 | + 'order_types' => [$order_type], |
| 88 | + 'stores' => [$this->store->id()], |
| 89 | + 'status' => TRUE, |
| 90 | + 'offer' => [ |
| 91 | + 'target_plugin_id' => 'commerce_promotion_order_percentage_off', |
| 92 | + 'target_plugin_configuration' => [ |
| 93 | + 'amount' => '0.10', |
| 94 | + ], |
| 95 | + ], |
| 96 | + ]); |
| 97 | + $this->assertEquals(SAVED_NEW, $promotion1->save()); |
| 98 | + |
| 99 | + $promotion2 = Promotion::create([ |
| 100 | + 'name' => 'Promotion 2', |
| 101 | + 'order_types' => [$order_type], |
| 102 | + 'stores' => [$this->store->id()], |
| 103 | + 'status' => TRUE, |
| 104 | + 'offer' => [ |
| 105 | + 'target_plugin_id' => 'commerce_promotion_order_percentage_off', |
| 106 | + 'target_plugin_configuration' => [ |
| 107 | + 'amount' => '0.10', |
| 108 | + ], |
| 109 | + ], |
| 110 | + ]); |
| 111 | + $this->assertEquals(SAVED_NEW, $promotion2->save()); |
| 112 | + |
| 113 | + $this->assertTrue($promotion1->applies($this->order)); |
| 114 | + $this->assertTrue($promotion2->applies($this->order)); |
| 115 | + |
| 116 | + $promotion1->setWeight(-10); |
| 117 | + $promotion1->save(); |
| 118 | + |
| 119 | + $promotion2->setWeight(10); |
| 120 | + $promotion2->setCompatibility(PromotionInterface::COMPATIBLE_NONE); |
| 121 | + $promotion2->save(); |
| 122 | + |
| 123 | + $promotion1->apply($this->order); |
| 124 | + $this->assertFalse($promotion2->applies($this->order)); |
| 125 | + |
| 126 | + $this->container->get('commerce_order.order_refresh')->refresh($this->order); |
| 127 | + $this->assertEquals(1, count($this->order->collectAdjustments())); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments