Skip to content

Commit 9e4d9e6

Browse files
author
Bryant Luk
committed
MAGETWO-36271: Create /mine API for CouponManagement
- Add mine URLs for CouponManagement
1 parent f167858 commit 9e4d9e6

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

app/code/Magento/Quote/etc/webapi.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,35 @@
413413
</resources>
414414
</route>
415415

416+
<!-- Managing mine Cart Coupons -->
417+
<route url="/V1/carts/mine/coupons" method="GET">
418+
<service class="Magento\Quote\Api\CouponManagementInterface" method="get"/>
419+
<resources>
420+
<resource ref="self" />
421+
</resources>
422+
<data>
423+
<parameter name="cartId" force="true">%cart_id%</parameter>
424+
</data>
425+
</route>
426+
<route url="/V1/carts/mine/coupons/:couponCode" method="PUT">
427+
<service class="Magento\Quote\Api\CouponManagementInterface" method="set"/>
428+
<resources>
429+
<resource ref="self" />
430+
</resources>
431+
<data>
432+
<parameter name="cartId" force="true">%cart_id%</parameter>
433+
</data>
434+
</route>
435+
<route url="/V1/carts/mine/coupons" method="DELETE">
436+
<service class="Magento\Quote\Api\CouponManagementInterface" method="remove"/>
437+
<resources>
438+
<resource ref="self" />
439+
</resources>
440+
<data>
441+
<parameter name="cartId" force="true">%cart_id%</parameter>
442+
</data>
443+
</route>
444+
416445
<!-- Managing Cart Shipping address -->
417446
<route url="/V1/carts/:cartId/shipping-address" method="GET">
418447
<service class="Magento\Quote\Api\ShippingAddressManagementInterface" method="get"/>

dev/tests/api-functional/testsuite/Magento/Quote/Api/CouponManagementTest.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,154 @@ public function testSetCouponSuccess()
148148

149149
$this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
150150
}
151+
152+
/**
153+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
154+
*/
155+
public function testGetMyCoupon()
156+
{
157+
$this->_markTestAsRestOnly();
158+
159+
// get customer ID token
160+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
161+
$customerTokenService = $this->objectManager->create(
162+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
163+
);
164+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
165+
166+
/** @var \Magento\Quote\Model\Quote $quote */
167+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
168+
$quote->load('test_order_1', 'reserved_order_id');
169+
$cartId = $quote->getId();
170+
$couponCode = $quote->getCouponCode();
171+
$serviceInfo = [
172+
'rest' => [
173+
'resourcePath' => self::RESOURCE_PATH . 'mine/coupons' ,
174+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
175+
'token' => $token,
176+
],
177+
];
178+
179+
$requestData = [];
180+
$this->assertEquals($couponCode, $this->_webApiCall($serviceInfo, $requestData));
181+
}
182+
183+
/**
184+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
185+
*/
186+
public function testDeleteMyCoupon()
187+
{
188+
$this->_markTestAsRestOnly();
189+
190+
// get customer ID token
191+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
192+
$customerTokenService = $this->objectManager->create(
193+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
194+
);
195+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
196+
197+
/** @var \Magento\Quote\Model\Quote $quote */
198+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
199+
$quote->load('test_order_1', 'reserved_order_id');
200+
$cartId = $quote->getId();
201+
$serviceInfo = [
202+
'rest' => [
203+
'resourcePath' => self::RESOURCE_PATH . 'mine/coupons',
204+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
205+
'token' => $token,
206+
],
207+
];
208+
$requestData = [];
209+
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
210+
$quote->load('test_order_1', 'reserved_order_id');
211+
$this->assertEquals('', $quote->getCouponCode());
212+
}
213+
214+
/**
215+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
216+
* @expectedException \Exception
217+
* @expectedExceptionMessage Coupon code is not valid
218+
*/
219+
public function testSetMyCouponThrowsExceptionIfCouponDoesNotExist()
220+
{
221+
$this->_markTestAsRestOnly();
222+
223+
// get customer ID token
224+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
225+
$customerTokenService = $this->objectManager->create(
226+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
227+
);
228+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
229+
230+
/** @var \Magento\Quote\Model\Quote $quote */
231+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
232+
$quote->load('test_order_1', 'reserved_order_id');
233+
$cartId = $quote->getId();
234+
235+
$couponCode = 'invalid_coupon_code';
236+
237+
$serviceInfo = [
238+
'rest' => [
239+
'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode,
240+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
241+
'token' => $token,
242+
],
243+
];
244+
245+
$requestData = [
246+
"couponCode" => $couponCode,
247+
];
248+
249+
$this->_webApiCall($serviceInfo, $requestData);
250+
}
251+
252+
/**
253+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
254+
* @magentoApiDataFixture Magento/Sales/_files/quote.php
255+
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
256+
*/
257+
public function testSetMyCouponSuccess()
258+
{
259+
$this->_markTestAsRestOnly();
260+
261+
// get customer ID token
262+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
263+
$customerTokenService = $this->objectManager->create(
264+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
265+
);
266+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
267+
268+
/** @var \Magento\Quote\Model\Quote $quote */
269+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
270+
$quote->load('test01', 'reserved_order_id');
271+
$cartId = $quote->getId();
272+
$salesRule = $this->objectManager->create('Magento\SalesRule\Model\Rule');
273+
$salesRule->load('Test Coupon for General', 'name');
274+
$couponCode = $salesRule->getCouponCode();
275+
276+
/* Since this isn't a full quote fixture, need to assign it to the right customer */
277+
$cartManagementService = $this->objectManager->create(
278+
'Magento\Quote\Api\CartManagementInterface'
279+
);
280+
$cartManagementService->assignCustomer($cartId, 1, 1);
281+
282+
$serviceInfo = [
283+
'rest' => [
284+
'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode,
285+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
286+
'token' => $token,
287+
],
288+
];
289+
290+
$requestData = [
291+
"couponCode" => $couponCode,
292+
];
293+
294+
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
295+
296+
$quoteWithCoupon = $this->objectManager->create('Magento\Quote\Model\Quote');
297+
$quoteWithCoupon->load('test01', 'reserved_order_id');
298+
299+
$this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
300+
}
151301
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* SalesRule 10% discount coupon
4+
*
5+
* Copyright © 2015 Magento. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
9+
/** @var \Magento\SalesRule\Model\Rule $salesRule */
10+
$salesRule = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\SalesRule\Model\Rule');
11+
12+
$data = [
13+
'name' => 'Test Coupon for General',
14+
'is_active' => true,
15+
'website_ids' => [
16+
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
17+
'Magento\Store\Model\StoreManagerInterface'
18+
)->getStore()->getWebsiteId()
19+
],
20+
'customer_group_ids' => [1],
21+
'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC,
22+
'coupon_code' => uniqid(),
23+
'simple_action' => \Magento\SalesRule\Model\Rule::BY_PERCENT_ACTION,
24+
'discount_amount' => 10,
25+
'discount_step' => 1
26+
];
27+
28+
$salesRule->loadPost($data)->setUseAutoGeneration(false)->save();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* SalesRule 10% discount coupon
4+
*
5+
* Copyright © 2015 Magento. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
9+
/** @var \Magento\SalesRule\Model\Rule $salesRule */
10+
$salesRule = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\SalesRule\Model\Rule');
11+
$salesRule->load('Test Coupon for General', 'name');
12+
$salesRule->delete();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* SalesRule 10% discount coupon
4+
*
5+
* Copyright © 2015 Magento. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
9+
/** @var \Magento\SalesRule\Model\Rule $salesRule */
10+
$salesRule = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\SalesRule\Model\Rule');
11+
$salesRule->load('Test Coupon', 'name');
12+
$salesRule->delete();

0 commit comments

Comments
 (0)