|
| 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_promotion\Entity\Coupon; |
| 8 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests coupon integration with orders. |
| 12 | + * |
| 13 | + * @group commerce |
| 14 | + */ |
| 15 | +class CouponOrderIntegrationTest extends CommerceKernelTestBase { |
| 16 | + |
| 17 | + /** |
| 18 | + * The test order. |
| 19 | + * |
| 20 | + * @var \Drupal\commerce_order\Entity\OrderInterface |
| 21 | + */ |
| 22 | + protected $order; |
| 23 | + |
| 24 | + /** |
| 25 | + * Modules to enable. |
| 26 | + * |
| 27 | + * @var array |
| 28 | + */ |
| 29 | + public static $modules = [ |
| 30 | + 'entity_reference_revisions', |
| 31 | + 'profile', |
| 32 | + 'state_machine', |
| 33 | + 'commerce_order', |
| 34 | + 'commerce_product', |
| 35 | + 'commerce_promotion', |
| 36 | + ]; |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 41 | + protected function setUp() { |
| 42 | + parent::setUp(); |
| 43 | + |
| 44 | + $this->installEntitySchema('profile'); |
| 45 | + $this->installEntitySchema('commerce_order'); |
| 46 | + $this->installEntitySchema('commerce_order_type'); |
| 47 | + $this->installEntitySchema('commerce_order_item'); |
| 48 | + $this->installEntitySchema('commerce_promotion'); |
| 49 | + $this->installEntitySchema('commerce_promotion_coupon'); |
| 50 | + $this->installConfig([ |
| 51 | + 'profile', |
| 52 | + 'commerce_order', |
| 53 | + 'commerce_promotion', |
| 54 | + ]); |
| 55 | + |
| 56 | + $this->user = $this->createUser(); |
| 57 | + |
| 58 | + OrderItemType::create([ |
| 59 | + 'id' => 'test', |
| 60 | + 'label' => 'Test', |
| 61 | + 'orderType' => 'default', |
| 62 | + ])->save(); |
| 63 | + |
| 64 | + $this->order = Order::create([ |
| 65 | + 'type' => 'default', |
| 66 | + 'state' => 'completed', |
| 67 | + 'mail' => 'test@example.com', |
| 68 | + 'ip_address' => '127.0.0.1', |
| 69 | + 'order_number' => '6', |
| 70 | + 'store_id' => $this->store, |
| 71 | + 'uid' => $this->user, |
| 72 | + 'order_items' => [], |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tests the coupons field added to orders. |
| 78 | + */ |
| 79 | + public function testOrderCouponField() { |
| 80 | + $coupon1 = Coupon::create([ |
| 81 | + 'code' => $this->randomString(), |
| 82 | + 'status' => TRUE, |
| 83 | + ]); |
| 84 | + $coupon1->save(); |
| 85 | + $coupon2 = Coupon::create([ |
| 86 | + 'code' => $this->randomString(), |
| 87 | + 'status' => TRUE, |
| 88 | + ]); |
| 89 | + $coupon2->save(); |
| 90 | + |
| 91 | + $this->order->get('coupons')->appendItem($coupon1); |
| 92 | + $this->order->get('coupons')->appendItem($coupon2); |
| 93 | + $this->order->save(); |
| 94 | + |
| 95 | + $this->assertSame($coupon2, $this->order->get('coupons')->offsetGet(1)->entity); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments