|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\commerce_promotion\Kernel\Entity; |
| 4 | + |
| 5 | +use Drupal\commerce_order\Entity\Order; |
| 6 | +use Drupal\commerce_order\Entity\OrderItem; |
| 7 | +use Drupal\commerce_order\Entity\OrderItemType; |
| 8 | +use Drupal\commerce_price\Price; |
| 9 | +use Drupal\commerce_promotion\Entity\Coupon; |
| 10 | +use Drupal\commerce_promotion\Entity\Promotion; |
| 11 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests the Coupon entity. |
| 15 | + * |
| 16 | + * @coversDefaultClass \Drupal\commerce_promotion\Entity\Coupon |
| 17 | + * |
| 18 | + * @group commerce |
| 19 | + */ |
| 20 | +class CouponTest extends CommerceKernelTestBase { |
| 21 | + |
| 22 | + /** |
| 23 | + * Modules to enable. |
| 24 | + * |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + public static $modules = [ |
| 28 | + 'entity_reference_revisions', |
| 29 | + 'profile', |
| 30 | + 'state_machine', |
| 31 | + 'commerce_order', |
| 32 | + 'commerce_product', |
| 33 | + 'commerce_promotion', |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + protected function setUp() { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->installEntitySchema('profile'); |
| 43 | + $this->installEntitySchema('commerce_order'); |
| 44 | + $this->installEntitySchema('commerce_order_item'); |
| 45 | + $this->installEntitySchema('commerce_promotion'); |
| 46 | + $this->installEntitySchema('commerce_promotion_coupon'); |
| 47 | + $this->installSchema('commerce_promotion', ['commerce_promotion_usage']); |
| 48 | + $this->installConfig([ |
| 49 | + 'profile', |
| 50 | + 'commerce_order', |
| 51 | + 'commerce_promotion', |
| 52 | + ]); |
| 53 | + |
| 54 | + OrderItemType::create([ |
| 55 | + 'id' => 'test', |
| 56 | + 'label' => 'Test', |
| 57 | + 'orderType' => 'default', |
| 58 | + ])->save(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @covers ::getPromotion |
| 63 | + * @covers ::getPromotionId |
| 64 | + * @covers ::getCode |
| 65 | + * @covers ::setCode |
| 66 | + * @covers ::getUsageLimit |
| 67 | + * @covers ::setUsageLimit |
| 68 | + * @covers ::isEnabled |
| 69 | + * @covers ::setEnabled |
| 70 | + */ |
| 71 | + public function testCoupon() { |
| 72 | + $promotion = Promotion::create([ |
| 73 | + 'status' => FALSE, |
| 74 | + ]); |
| 75 | + $promotion->save(); |
| 76 | + $promotion = $this->reloadEntity($promotion); |
| 77 | + |
| 78 | + $coupon = Coupon::create([ |
| 79 | + 'status' => FALSE, |
| 80 | + 'promotion_id' => $promotion->id(), |
| 81 | + ]); |
| 82 | + |
| 83 | + $this->assertEquals($promotion, $coupon->getPromotion()); |
| 84 | + $this->assertEquals($promotion->id(), $coupon->getPromotionId()); |
| 85 | + |
| 86 | + $coupon->setCode('test_code'); |
| 87 | + $this->assertEquals('test_code', $coupon->getCode()); |
| 88 | + |
| 89 | + $coupon->setUsageLimit(10); |
| 90 | + $this->assertEquals(10, $coupon->getUsageLimit()); |
| 91 | + |
| 92 | + $coupon->setEnabled(TRUE); |
| 93 | + $this->assertEquals(TRUE, $coupon->isEnabled()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @covers ::available |
| 98 | + */ |
| 99 | + public function testAvailability() { |
| 100 | + $order_item = OrderItem::create([ |
| 101 | + 'type' => 'test', |
| 102 | + 'quantity' => 1, |
| 103 | + 'unit_price' => new Price('12.00', 'USD'), |
| 104 | + ]); |
| 105 | + $order_item->save(); |
| 106 | + $order = Order::create([ |
| 107 | + 'type' => 'default', |
| 108 | + 'state' => 'draft', |
| 109 | + |
| 110 | + 'ip_address' => '127.0.0.1', |
| 111 | + 'order_number' => '6', |
| 112 | + 'store_id' => $this->store, |
| 113 | + 'uid' => $this->createUser(), |
| 114 | + 'order_items' => [$order_item], |
| 115 | + ]); |
| 116 | + $order->setRefreshState(Order::REFRESH_SKIP); |
| 117 | + $order->save(); |
| 118 | + |
| 119 | + $promotion = Promotion::create([ |
| 120 | + 'order_types' => ['default'], |
| 121 | + 'stores' => [$this->store->id()], |
| 122 | + 'usage_limit' => 1, |
| 123 | + 'start_date' => '2017-01-01', |
| 124 | + 'status' => TRUE, |
| 125 | + ]); |
| 126 | + $promotion->save(); |
| 127 | + |
| 128 | + $coupon = Coupon::create([ |
| 129 | + 'promotion_id' => $promotion->id(), |
| 130 | + 'code' => 'coupon_code', |
| 131 | + 'usage_limit' => 1, |
| 132 | + 'status' => TRUE, |
| 133 | + ]); |
| 134 | + $coupon->save(); |
| 135 | + $this->assertTrue($coupon->available($order)); |
| 136 | + |
| 137 | + $coupon->setEnabled(FALSE); |
| 138 | + $this->assertFalse($coupon->available($order)); |
| 139 | + $coupon->setEnabled(TRUE); |
| 140 | + |
| 141 | + \Drupal::service('commerce_promotion.usage')->addUsage($order, $promotion, $coupon); |
| 142 | + $this->assertFalse($coupon->available($order)); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments