CouponManagementTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Api;
  8. use Magento\TestFramework\TestCase\WebapiAbstract;
  9. class CouponManagementTest extends WebapiAbstract
  10. {
  11. const SERVICE_VERSION = 'V1';
  12. const SERVICE_NAME = 'quoteCouponManagementV1';
  13. const RESOURCE_PATH = '/V1/carts/';
  14. /**
  15. * @var \Magento\TestFramework\ObjectManager
  16. */
  17. protected $objectManager;
  18. protected function setUp()
  19. {
  20. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  21. }
  22. /**
  23. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
  24. */
  25. public function testGet()
  26. {
  27. /** @var \Magento\Quote\Model\Quote $quote */
  28. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  29. $quote->load('test_order_1', 'reserved_order_id');
  30. $cartId = $quote->getId();
  31. $couponCode = $quote->getCouponCode();
  32. $serviceInfo = [
  33. 'rest' => [
  34. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' ,
  35. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  36. ],
  37. 'soap' => [
  38. 'service' => self::SERVICE_NAME,
  39. 'serviceVersion' => self::SERVICE_VERSION,
  40. 'operation' => self::SERVICE_NAME . 'Get',
  41. ],
  42. ];
  43. $requestData = ["cartId" => $cartId];
  44. $this->assertEquals($couponCode, $this->_webApiCall($serviceInfo, $requestData));
  45. }
  46. /**
  47. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
  48. */
  49. public function testDelete()
  50. {
  51. /** @var \Magento\Quote\Model\Quote $quote */
  52. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  53. $quote->load('test_order_1', 'reserved_order_id');
  54. $cartId = $quote->getId();
  55. $serviceInfo = [
  56. 'rest' => [
  57. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons',
  58. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  59. ],
  60. 'soap' => [
  61. 'service' => self::SERVICE_NAME,
  62. 'serviceVersion' => self::SERVICE_VERSION,
  63. 'operation' => self::SERVICE_NAME . 'Remove',
  64. ],
  65. ];
  66. $requestData = ["cartId" => $cartId];
  67. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  68. $quote->load('test_order_1', 'reserved_order_id');
  69. $this->assertEquals('', $quote->getCouponCode());
  70. }
  71. /**
  72. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  73. * @expectedException \Exception
  74. * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
  75. */
  76. public function testSetCouponThrowsExceptionIfCouponDoesNotExist()
  77. {
  78. /** @var \Magento\Quote\Model\Quote $quote */
  79. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  80. $quote->load('test_order_1', 'reserved_order_id');
  81. $cartId = $quote->getId();
  82. $couponCode = 'invalid_coupon_code';
  83. $serviceInfo = [
  84. 'rest' => [
  85. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . $couponCode,
  86. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  87. ],
  88. 'soap' => [
  89. 'service' => self::SERVICE_NAME,
  90. 'serviceVersion' => self::SERVICE_VERSION,
  91. 'operation' => self::SERVICE_NAME . 'Set',
  92. ],
  93. ];
  94. $requestData = [
  95. "cartId" => $cartId,
  96. "couponCode" => $couponCode,
  97. ];
  98. $this->_webApiCall($serviceInfo, $requestData);
  99. }
  100. /**
  101. * @magentoApiDataFixture Magento/Sales/_files/quote.php
  102. * @magentoApiDataFixture Magento/Checkout/_files/discount_10percent.php
  103. */
  104. public function testSetCouponSuccess()
  105. {
  106. /** @var \Magento\Quote\Model\Quote $quote */
  107. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  108. $quote->load('test01', 'reserved_order_id');
  109. $cartId = $quote->getId();
  110. /** @var \Magento\SalesRule\Model\Rule $salesRule */
  111. $salesRule = $this->objectManager->create(\Magento\SalesRule\Model\Rule::class);
  112. $salesRuleId = $this->objectManager->get(\Magento\Framework\Registry::class)
  113. ->registry('Magento/Checkout/_file/discount_10percent');
  114. $salesRule->load($salesRuleId);
  115. $couponCode = $salesRule->getPrimaryCoupon()->getCode();
  116. $serviceInfo = [
  117. 'rest' => [
  118. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . $couponCode,
  119. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  120. ],
  121. 'soap' => [
  122. 'service' => self::SERVICE_NAME,
  123. 'serviceVersion' => self::SERVICE_VERSION,
  124. 'operation' => self::SERVICE_NAME . 'Set',
  125. ],
  126. ];
  127. $requestData = [
  128. "cartId" => $cartId,
  129. "couponCode" => $couponCode,
  130. ];
  131. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  132. $quoteWithCoupon = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  133. $quoteWithCoupon->load('test01', 'reserved_order_id');
  134. $this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
  135. }
  136. /**
  137. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
  138. */
  139. public function testGetMyCoupon()
  140. {
  141. $this->_markTestAsRestOnly();
  142. // get customer ID token
  143. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  144. $customerTokenService = $this->objectManager->create(
  145. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  146. );
  147. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  148. /** @var \Magento\Quote\Model\Quote $quote */
  149. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  150. $quote->load('test_order_1', 'reserved_order_id');
  151. $couponCode = $quote->getCouponCode();
  152. $serviceInfo = [
  153. 'rest' => [
  154. 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons' ,
  155. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  156. 'token' => $token,
  157. ],
  158. ];
  159. $requestData = [];
  160. $this->assertEquals($couponCode, $this->_webApiCall($serviceInfo, $requestData));
  161. }
  162. /**
  163. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
  164. */
  165. public function testDeleteMyCoupon()
  166. {
  167. $this->_markTestAsRestOnly();
  168. // get customer ID token
  169. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  170. $customerTokenService = $this->objectManager->create(
  171. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  172. );
  173. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  174. /** @var \Magento\Quote\Model\Quote $quote */
  175. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  176. $quote->load('test_order_1', 'reserved_order_id');
  177. $serviceInfo = [
  178. 'rest' => [
  179. 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons',
  180. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  181. 'token' => $token,
  182. ],
  183. ];
  184. $requestData = [];
  185. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  186. $quote->load('test_order_1', 'reserved_order_id');
  187. $this->assertEquals('', $quote->getCouponCode());
  188. }
  189. /**
  190. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  191. * @expectedException \Exception
  192. * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
  193. */
  194. public function testSetMyCouponThrowsExceptionIfCouponDoesNotExist()
  195. {
  196. $this->_markTestAsRestOnly();
  197. // get customer ID token
  198. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  199. $customerTokenService = $this->objectManager->create(
  200. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  201. );
  202. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  203. $couponCode = 'invalid_coupon_code';
  204. $serviceInfo = [
  205. 'rest' => [
  206. 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode,
  207. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  208. 'token' => $token,
  209. ],
  210. ];
  211. $requestData = [
  212. "couponCode" => $couponCode,
  213. ];
  214. $this->_webApiCall($serviceInfo, $requestData);
  215. }
  216. /**
  217. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  218. * @magentoApiDataFixture Magento/Sales/_files/quote.php
  219. * @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
  220. */
  221. public function testSetMyCouponSuccess()
  222. {
  223. $this->_markTestAsRestOnly();
  224. // get customer ID token
  225. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  226. $customerTokenService = $this->objectManager->create(
  227. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  228. );
  229. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  230. /** @var \Magento\Quote\Model\Quote $quote */
  231. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  232. $quote->load('test01', 'reserved_order_id');
  233. $cartId = $quote->getId();
  234. /** @var \Magento\SalesRule\Model\Rule $salesRule */
  235. $salesRule = $this->objectManager->create(\Magento\SalesRule\Model\Rule::class);
  236. $salesRuleId = $this->objectManager->get(\Magento\Framework\Registry::class)
  237. ->registry('Magento/Checkout/_file/discount_10percent_generalusers');
  238. $salesRule->load($salesRuleId);
  239. $couponCode = $salesRule->getPrimaryCoupon()->getCode();
  240. /* Since this isn't a full quote fixture, need to assign it to the right customer */
  241. $cartManagementService = $this->objectManager->create(
  242. \Magento\Quote\Api\CartManagementInterface::class
  243. );
  244. $cartManagementService->assignCustomer($cartId, 1, 1);
  245. $serviceInfo = [
  246. 'rest' => [
  247. 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode,
  248. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  249. 'token' => $token,
  250. ],
  251. ];
  252. $requestData = [
  253. "couponCode" => $couponCode,
  254. ];
  255. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  256. $quoteWithCoupon = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  257. $quoteWithCoupon->load('test01', 'reserved_order_id');
  258. $this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
  259. }
  260. }