diff --git a/app/code/Magento/Quote/Api/CartManagementInterface.php b/app/code/Magento/Quote/Api/CartManagementInterface.php index 5dff50e9b3a5694699762d1151ad589ba35af086..fdc0d18085cf0a4b0acf0adcd9dfa8366b78190e 100644 --- a/app/code/Magento/Quote/Api/CartManagementInterface.php +++ b/app/code/Magento/Quote/Api/CartManagementInterface.php @@ -8,13 +8,21 @@ namespace Magento\Quote\Api; interface CartManagementInterface { /** - * Enables an customer or guest user to create an empty cart and quote for an anonymous customer. + * Creates an empty cart and quote for a guest. * - * @param int|null $customerId The customer ID. * @return int Cart ID. * @throws \Magento\Framework\Exception\CouldNotSaveException The empty cart and quote could not be created. */ - public function createEmptyCart($customerId = null); + public function createEmptyCart(); + + /** + * Creates an empty cart and quote for a specified customer. + * + * @param int $customerId The customer ID. + * @return int Cart ID. + * @throws \Magento\Framework\Exception\CouldNotSaveException The empty cart and quote could not be created. + */ + public function createEmptyCartForCustomer($customerId); /** * Returns information for the cart for a specified customer. diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php b/app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php index 243c70f95e13eaa690646d1fc97aba1c2c9876fb..c62fab57f1bc5bf9328c7bc02cc58745b10377d1 100644 --- a/app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php +++ b/app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php @@ -46,11 +46,11 @@ class GuestCartManagement implements GuestCartManagementInterface /** * {@inheritdoc} */ - public function createEmptyCart($customerId = null) + public function createEmptyCart() { /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */ $quoteIdMask = $this->quoteIdMaskFactory->create(); - $cartId = $this->quoteManagement->createEmptyCart($customerId); + $cartId = $this->quoteManagement->createEmptyCart(); $quoteIdMask->setId($cartId)->save(); return $quoteIdMask->getMaskedId(); } diff --git a/app/code/Magento/Quote/Model/QuoteManagement.php b/app/code/Magento/Quote/Model/QuoteManagement.php index 4cfc07b3a318e1a4013032e7309c7fc804f83d35..c09a32964aa3f3cf9c7c797a7629d55a10e19b21 100644 --- a/app/code/Magento/Quote/Model/QuoteManagement.php +++ b/app/code/Magento/Quote/Model/QuoteManagement.php @@ -156,12 +156,26 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface /** * {@inheritdoc} */ - public function createEmptyCart($customerId = null) + public function createEmptyCart() { $storeId = $this->storeManager->getStore()->getStoreId(); - $quote = $customerId - ? $this->createCustomerCart($customerId, $storeId) - : $this->createAnonymousCart($storeId); + $quote = $this->createAnonymousCart($storeId); + + try { + $this->quoteRepository->save($quote); + } catch (\Exception $e) { + throw new CouldNotSaveException(__('Cannot create quote')); + } + return $quote->getId(); + } + + /** + * {@inheritdoc} + */ + public function createEmptyCartForCustomer($customerId) + { + $storeId = $this->storeManager->getStore()->getStoreId(); + $quote = $this->createCustomerCart($customerId, $storeId); try { $this->quoteRepository->save($quote); diff --git a/app/code/Magento/Quote/Model/Webapi/ParamOverriderCartId.php b/app/code/Magento/Quote/Model/Webapi/ParamOverriderCartId.php new file mode 100644 index 0000000000000000000000000000000000000000..3f5b280d505d2762e48b42c4715f1cdf74aa04a0 --- /dev/null +++ b/app/code/Magento/Quote/Model/Webapi/ParamOverriderCartId.php @@ -0,0 +1,63 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Model\Webapi; + +use Magento\Authorization\Model\UserContextInterface; +use Magento\Framework\Webapi\Rest\Request\ParamOverriderInterface; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Quote\Api\CartManagementInterface; + +/** + * Replaces a "%cart_id%" value with the current authenticated customer's cart + */ +class ParamOverriderCartId implements ParamOverriderInterface +{ + /** + * @var UserContextInterface + */ + private $userContext; + + /** + * @var CartManagementInterface + */ + private $cartManagement; + + /** + * Constructs an object to override the cart ID parameter on a request. + * + * @param UserContextInterface $userContext + * @param CartManagementInterface $cartManagement + */ + public function __construct( + UserContextInterface $userContext, + CartManagementInterface $cartManagement + ) { + $this->userContext = $userContext; + $this->cartManagement = $cartManagement; + } + + /** + * {@inheritDoc} + */ + public function getOverriddenValue() + { + try { + if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) { + $customerId = $this->userContext->getUserId(); + + /** @var \Magento\Quote\Api\Data\CartInterface */ + $cart = $this->cartManagement->getCartForCustomer($customerId); + if ($cart) { + return $cart->getId(); + } + } + } catch (NoSuchEntityException $e) { + /* do nothing and just return null */ + } + return null; + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php index d58adf62d675cd8a0cb462586e7b48fdeb27d8b8..12c4248d82c81175ac6d23d31f714611e1f08495 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php @@ -198,7 +198,7 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase $this->assertEquals($quoteId, $this->model->createEmptyCart()); } - public function testCreateEmptyCartLoggedInUser() + public function testCreateEmptyCartForCustomer() { $storeId = 345; $quoteId = 2311; @@ -222,13 +222,13 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf(); $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); - $this->assertEquals($quoteId, $this->model->createEmptyCart($userId)); + $this->assertEquals($quoteId, $this->model->createEmptyCartForCustomer($userId)); } /** * @expectedException \Magento\Framework\Exception\CouldNotSaveException */ - public function testCreateEmptyCartLoggedInUserException() + public function testCreateEmptyCartForCustomerException() { $storeId = 345; $userId = 567; @@ -246,7 +246,7 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf(); $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); - $this->model->createEmptyCart($userId); + $this->model->createEmptyCartForCustomer($userId); } /** diff --git a/app/code/Magento/Quote/Test/Unit/Model/Webapi/ParamOverriderCartIdTest.php b/app/code/Magento/Quote/Test/Unit/Model/Webapi/ParamOverriderCartIdTest.php new file mode 100644 index 0000000000000000000000000000000000000000..cb83bf6112341df2a9f9ccadcd2824b57ebed0fc --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/Webapi/ParamOverriderCartIdTest.php @@ -0,0 +1,116 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Test\Unit\Model\Webapi; + +use Magento\Authorization\Model\UserContextInterface; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Quote\Api\CartManagementInterface; +use Magento\Quote\Model\Webapi\ParamOverriderCartId; + +/** + * Test for \Magento\Quote\Model\Webapi\ParamOverriderCartId + */ +class ParamOverriderCartIdTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ParamOverriderCartId + */ + private $model; + + /** + * @var UserContextInterface + */ + private $userContext; + + public function setUp() + { + $this->userContext = $this->getMockBuilder('Magento\Authorization\Model\UserContextInterface') + ->getMockForAbstractClass(); + $this->cartManagement = $this->getMockBuilder('Magento\Quote\Api\CartManagementInterface') + ->getMockForAbstractClass(); + $this->model = (new ObjectManager($this))->getObject( + 'Magento\Quote\Model\Webapi\ParamOverriderCartId', + [ + 'userContext' => $this->userContext, + 'cartManagement' => $this->cartManagement, + ] + ); + } + + public function testGetOverriddenValueIsCustomerAndCartExists() + { + $retValue = 'retValue'; + $customerId = 1; + + $this->userContext->expects($this->once()) + ->method('getUserType') + ->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER)); + $this->userContext->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($customerId)); + + $cart = $this->getMockBuilder('Magento\Quote\Api\Data\CartInterface') + ->getMockForAbstractClass(); + $this->cartManagement->expects($this->once()) + ->method('getCartForCustomer') + ->with($customerId) + ->will($this->returnValue($cart)); + $cart->expects($this->once()) + ->method('getId') + ->will($this->returnValue($retValue)); + + $this->assertSame($retValue, $this->model->getOverriddenValue()); + } + + public function testGetOverriddenValueIsCustomerAndCartDoesNotExist() + { + $customerId = 1; + + $this->userContext->expects($this->once()) + ->method('getUserType') + ->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER)); + $this->userContext->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($customerId)); + + $this->cartManagement->expects($this->once()) + ->method('getCartForCustomer') + ->with($customerId) + ->will($this->throwException(new NoSuchEntityException())); + + $this->assertNull($this->model->getOverriddenValue()); + } + + public function testGetOverriddenValueIsCustomerAndCartIsNull() + { + $customerId = 1; + + $this->userContext->expects($this->once()) + ->method('getUserType') + ->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER)); + $this->userContext->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($customerId)); + + $this->cartManagement->expects($this->once()) + ->method('getCartForCustomer') + ->with($customerId) + ->will($this->returnValue(null)); + + $this->assertNull($this->model->getOverriddenValue()); + } + + public function testGetOverriddenValueIsNotCustomer() + { + $this->userContext->expects($this->once()) + ->method('getUserType') + ->will($this->returnValue(UserContextInterface::USER_TYPE_ADMIN)); + + $this->assertNull($this->model->getOverriddenValue()); + } +} diff --git a/app/code/Magento/Quote/etc/di.xml b/app/code/Magento/Quote/etc/di.xml index a305a4a63284a2f5dff9998a31a1fedb54eff704..4eeb1ff2b61a7e865d24c28af7411c599b7c3f05 100644 --- a/app/code/Magento/Quote/etc/di.xml +++ b/app/code/Magento/Quote/etc/di.xml @@ -35,4 +35,12 @@ <preference for="Magento\Quote\Api\GuestShippingAddressManagementInterface" type="Magento\Quote\Model\GuestCart\GuestShippingAddressManagement" /> <preference for="Magento\Quote\Api\GuestShippingMethodManagementInterface" type="Magento\Quote\Model\GuestCart\GuestShippingMethodManagement" /> <preference for="Magento\Quote\Api\GuestBillingAddressManagementInterface" type="Magento\Quote\Model\GuestCart\GuestBillingAddressManagement" /> + + <type name="Magento\Webapi\Controller\Rest\ParamsOverrider"> + <arguments> + <argument name="paramOverriders" xsi:type="array"> + <item name="%cart_id%" xsi:type="object">Magento\Quote\Model\Webapi\ParamOverriderCartId</item> + </argument> + </arguments> + </type> </config> diff --git a/app/code/Magento/Quote/etc/webapi.xml b/app/code/Magento/Quote/etc/webapi.xml index 708de5e88af332934a2126d9d131af3b19749540..9540a2d3b88aa9d72923608322fec5834eb2d382 100644 --- a/app/code/Magento/Quote/etc/webapi.xml +++ b/app/code/Magento/Quote/etc/webapi.xml @@ -24,7 +24,13 @@ <route url="/V1/carts/" method="POST"> <service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCart"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> + <route url="/V1/customers/:customerId/carts" method="POST"> + <service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCartForCustomer"/> + <resources> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/:cartId" method="PUT"> @@ -36,7 +42,7 @@ <!-- Managing my Cart --> <route url="/V1/carts/mine" method="POST"> - <service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCart"/> + <service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCartForCustomer"/> <resources> <resource ref="self" /> </resources> @@ -53,6 +59,15 @@ <parameter name="customerId" force="true">%customer_id%</parameter> </data> </route> + <route url="/V1/carts/mine/order" method="PUT"> + <service class="Magento\Quote\Api\CartManagementInterface" method="placeOrder"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> <!-- Managing guest carts --> @@ -102,6 +117,35 @@ </resources> </route> + <!-- Managing My Cart Shipment Method --> + <route url="/V1/carts/mine/selected-shipping-method" method="PUT"> + <service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="set"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/selected-shipping-method" method="GET"> + <service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="get"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/shipping-methods" method="GET"> + <service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="getList"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <!-- Managing Guest Cart Shipment Method --> <route url="/V1/guest-carts/:cartId/selected-shipping-method" method="PUT"> <service class="Magento\Quote\Api\GuestShippingMethodManagementInterface" method="set"/> @@ -213,13 +257,13 @@ </route> <!-- Managing Cart Payment --> - <route url="/V1/carts/:cartId/selected-payment-methods" method="GET"> + <route url="/V1/carts/:cartId/selected-payment-method" method="GET"> <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="get"/> <resources> <resource ref="Magento_Cart::manage" /> </resources> </route> - <route url="/V1/carts/:cartId/selected-payment-methods" method="PUT"> + <route url="/V1/carts/:cartId/selected-payment-method" method="PUT"> <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="set"/> <resources> <resource ref="Magento_Cart::manage" /> @@ -233,13 +277,13 @@ </route> <!-- Managing Guest Cart Payment --> - <route url="/V1/guest-carts/:cartId/selected-payment-methods" method="GET"> + <route url="/V1/guest-carts/:cartId/selected-payment-method" method="GET"> <service class="Magento\Quote\Api\GuestPaymentMethodManagementInterface" method="get"/> <resources> <resource ref="anonymous" /> </resources> </route> - <route url="/V1/guest-carts/:cartId/selected-payment-methods" method="PUT"> + <route url="/V1/guest-carts/:cartId/selected-payment-method" method="PUT"> <service class="Magento\Quote\Api\GuestPaymentMethodManagementInterface" method="set"/> <resources> <resource ref="anonymous" /> @@ -252,6 +296,35 @@ </resources> </route> + <!-- Managing my Cart Payment --> + <route url="/V1/carts/mine/selected-payment-method" method="GET"> + <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="get"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/selected-payment-method" method="PUT"> + <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="set"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/payment-methods" method="GET"> + <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="getList"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <!-- Managing Cart Billing address --> <route url="/V1/carts/:cartId/billing-address" method="GET"> <service class="Magento\Quote\Api\BillingAddressManagementInterface" method="get"/> @@ -266,7 +339,7 @@ </resources> </route> - <!-- Guest Managing Cart Billing address --> + <!-- Managing Guest Cart Billing address --> <route url="/V1/guest-carts/:cartId/billing-address" method="GET"> <service class="Magento\Quote\Api\GuestBillingAddressManagementInterface" method="get"/> <resources> @@ -280,6 +353,26 @@ </resources> </route> + <!-- Managing My Cart Billing address --> + <route url="/V1/carts/mine/billing-address" method="GET"> + <service class="Magento\Quote\Api\BillingAddressManagementInterface" method="get"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/billing-address" method="POST"> + <service class="Magento\Quote\Api\BillingAddressManagementInterface" method="assign"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <!-- Managing Cart Coupons --> <route url="/V1/carts/:cartId/coupons" method="GET"> <service class="Magento\Quote\Api\CouponManagementInterface" method="get"/> @@ -320,6 +413,35 @@ </resources> </route> + <!-- Managing mine Cart Coupons --> + <route url="/V1/carts/mine/coupons" method="GET"> + <service class="Magento\Quote\Api\CouponManagementInterface" method="get"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/coupons/:couponCode" method="PUT"> + <service class="Magento\Quote\Api\CouponManagementInterface" method="set"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/coupons" method="DELETE"> + <service class="Magento\Quote\Api\CouponManagementInterface" method="remove"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <!-- Managing Cart Shipping address --> <route url="/V1/carts/:cartId/shipping-address" method="GET"> <service class="Magento\Quote\Api\ShippingAddressManagementInterface" method="get"/> @@ -348,6 +470,26 @@ </resources> </route> + <!-- Managing My Cart Shipping address --> + <route url="/V1/carts/mine/shipping-address" method="GET"> + <service class="Magento\Quote\Api\ShippingAddressManagementInterface" method="get"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/shipping-address" method="POST"> + <service class="Magento\Quote\Api\ShippingAddressManagementInterface" method="assign"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> + <!-- Managing Cart Order --> <route url="/V1/carts/:cartId/order" method="PUT"> <service class="Magento\Quote\Api\CartManagementInterface" method="placeOrder"/> @@ -355,6 +497,8 @@ <resource ref="Magento_Cart::manage" /> </resources> </route> + + <!-- Managing Cart Total --> <route url="/V1/carts/:cartId/totals" method="GET"> <service class="Magento\Quote\Api\CartTotalRepositoryInterface" method="get"/> <resources> @@ -362,10 +506,22 @@ </resources> </route> + <!-- Managing Guest Cart Total --> <route url="/V1/guest-carts/:cartId/totals" method="GET"> <service class="Magento\Quote\Api\GuestCartTotalRepositoryInterface" method="get"/> <resources> <resource ref="anonymous" /> </resources> </route> + + <!-- Managing My Cart Total --> + <route url="/V1/carts/mine/totals" method="GET"> + <service class="Magento\Quote\Api\CartTotalRepositoryInterface" method="get"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="cartId" force="true">%cart_id%</parameter> + </data> + </route> </routes> diff --git a/app/code/Magento/Webapi/Controller/Rest/ParamOverriderCustomerId.php b/app/code/Magento/Webapi/Controller/Rest/ParamOverriderCustomerId.php new file mode 100644 index 0000000000000000000000000000000000000000..09a27569293993b92a68d8cdde5dab7d67e450e8 --- /dev/null +++ b/app/code/Magento/Webapi/Controller/Rest/ParamOverriderCustomerId.php @@ -0,0 +1,42 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Webapi\Controller\Rest; + +use Magento\Authorization\Model\UserContextInterface; +use Magento\Framework\Webapi\Rest\Request\ParamOverriderInterface; + +/** + * Replaces a "%customer_id%" value with the real customer id + */ +class ParamOverriderCustomerId implements ParamOverriderInterface +{ + /** + * @var UserContextInterface + */ + private $userContext; + + /** + * Constructs an object to override the customer ID parameter on a request. + * + * @param UserContextInterface $userContext + */ + public function __construct(UserContextInterface $userContext) + { + $this->userContext = $userContext; + } + + /** + * {@inheritDoc} + */ + public function getOverriddenValue() + { + if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) { + return $this->userContext->getUserId(); + } + return null; + } +} diff --git a/app/code/Magento/Webapi/Controller/Rest/ParamsOverrider.php b/app/code/Magento/Webapi/Controller/Rest/ParamsOverrider.php index 140f7dcd6a3b37eb1ec921ec6c3dd0172a283738..f7b4f4605a7a62cdf3246fd6ad5fc1e8de279061 100644 --- a/app/code/Magento/Webapi/Controller/Rest/ParamsOverrider.php +++ b/app/code/Magento/Webapi/Controller/Rest/ParamsOverrider.php @@ -6,7 +6,7 @@ namespace Magento\Webapi\Controller\Rest; -use Magento\Authorization\Model\UserContextInterface; +use Magento\Framework\Webapi\Rest\Request\ParamOverriderInterface; use Magento\Webapi\Model\Config\Converter; /** @@ -15,18 +15,19 @@ use Magento\Webapi\Model\Config\Converter; class ParamsOverrider { /** - * @var \Magento\Authorization\Model\UserContextInterface + * @var ParamOverriderInterface[] */ - protected $userContext; + private $paramOverriders; /** * Initialize dependencies * - * @param UserContextInterface $userContext + * @param ParamOverriderInterface[] $paramOverriders */ - public function __construct(UserContextInterface $userContext) - { - $this->userContext = $userContext; + public function __construct( + array $paramOverriders = [] + ) { + $this->paramOverriders = $paramOverriders; } /** @@ -41,10 +42,9 @@ class ParamsOverrider foreach ($parameters as $name => $paramData) { $arrayKeys = explode('.', $name); if ($paramData[Converter::KEY_FORCE] || !$this->isNestedArrayValueSet($inputData, $arrayKeys)) { - if ($paramData[Converter::KEY_VALUE] == '%customer_id%' - && $this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER - ) { - $value = $this->userContext->getUserId(); + $paramValue = $paramData[Converter::KEY_VALUE]; + if (isset($this->paramOverriders[$paramValue])) { + $value = $this->paramOverriders[$paramValue]->getOverriddenValue(); } else { $value = $paramData[Converter::KEY_VALUE]; } diff --git a/app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamOverriderCustomerIdTest.php b/app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamOverriderCustomerIdTest.php new file mode 100644 index 0000000000000000000000000000000000000000..bf0f152eddf5f123e36faa3207712a71f7c9b369 --- /dev/null +++ b/app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamOverriderCustomerIdTest.php @@ -0,0 +1,59 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Webapi\Test\Unit\Controller\Rest; + +use Magento\Authorization\Model\UserContextInterface; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Webapi\Controller\Rest\ParamOverriderCustomerId; + +class ParamOverriderCustomerIdTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ParamOverriderCustomerId + */ + private $model; + + /** + * @var UserContextInterface + */ + private $userContext; + + public function setUp() + { + $this->userContext = $this->getMockBuilder('Magento\Authorization\Model\UserContextInterface') + ->getMockForAbstractClass(); + $this->model = (new ObjectManager($this))->getObject( + 'Magento\Webapi\Controller\Rest\ParamOverriderCustomerId', + [ + 'userContext' => $this->userContext + ] + ); + } + + public function testGetOverriddenValueIsCustomer() + { + $retValue = 'retValue'; + + $this->userContext->expects($this->once()) + ->method('getUserType') + ->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER)); + $this->userContext->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($retValue)); + + $this->assertSame($retValue, $this->model->getOverriddenValue()); + } + + public function testGetOverriddenValueIsNotCustomer() + { + $this->userContext->expects($this->once()) + ->method('getUserType') + ->will($this->returnValue(UserContextInterface::USER_TYPE_ADMIN)); + + $this->assertNull($this->model->getOverriddenValue()); + } +} diff --git a/app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamsOverriderTest.php b/app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamsOverriderTest.php index 2cfe94f7d07ceafa3929116b46a7d79923243270..f5aa363b8b9c2bdeb549d4ee87cc53da2db59bcd 100644 --- a/app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamsOverriderTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamsOverriderTest.php @@ -31,10 +31,15 @@ class ParamsOverriderTest extends \PHPUnit_Framework_TestCase $userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId)); $userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType)); + $paramOverriderCustomerId = $objectManager->getObject( + 'Magento\Webapi\Controller\Rest\ParamOverriderCustomerId', + ['userContext' => $userContextMock] + ); + /** @var \Magento\Webapi\Controller\Rest\ParamsOverrider $paramsOverrider */ $paramsOverrider = $objectManager->getObject( 'Magento\Webapi\Controller\Rest\ParamsOverrider', - ['userContext' => $userContextMock] + ['paramOverriders' => ['%customer_id%' => $paramOverriderCustomerId ]] ); $this->assertEquals($expectedOverriddenParams, $paramsOverrider->override($requestData, $parameters)); @@ -84,7 +89,7 @@ class ParamsOverriderTest extends \PHPUnit_Framework_TestCase 'force true, value present, override value is %customer_id%, not a customer' => [ ['Name1' => 'valueIn'], ['Name1' => ['force' => true, 'value' => '%customer_id%']], - ['Name1' => '%customer_id%'], + ['Name1' => null], 1234, UserContextInterface::USER_TYPE_INTEGRATION, ], diff --git a/app/code/Magento/Webapi/etc/di.xml b/app/code/Magento/Webapi/etc/di.xml index d9f47dfd67a79a139cd1f69f957c5f508d98c8a4..12b661f639c428c681b98b1df7c0f5a8131da632 100644 --- a/app/code/Magento/Webapi/etc/di.xml +++ b/app/code/Magento/Webapi/etc/di.xml @@ -30,4 +30,11 @@ <type name="Magento\Integration\Model\ConfigBasedIntegrationManager"> <plugin name="webapiSetup" type="Magento\Webapi\Model\Plugin\Manager" /> </type> + <type name="Magento\Webapi\Controller\Rest\ParamsOverrider"> + <arguments> + <argument name="paramOverriders" xsi:type="array"> + <item name="%customer_id%" xsi:type="object">Magento\Webapi\Controller\Rest\ParamOverriderCustomerId</item> + </argument> + </arguments> + </type> </config> diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/BillingAddressManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/BillingAddressManagementTest.php index f78abc4a896adf0c4d64d5697b634b1031a3e17d..8d6c238db91f51b1afec55af59fc90d72899c6e9 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/BillingAddressManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/BillingAddressManagementTest.php @@ -132,4 +132,118 @@ class BillingAddressManagementTest extends WebapiAbstract $this->assertEquals($value, $savedData[$key]); } } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetMyAddress() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + /** @var \Magento\Quote\Model\Quote\Address $address */ + $address = $quote->getBillingAddress(); + + $data = [ + AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(), + AddressInterface::KEY_ID => (int)$address->getId(), + AddressInterface::KEY_CUSTOMER_ID => $address->getCustomerId(), + AddressInterface::KEY_REGION => $address->getRegion(), + AddressInterface::KEY_REGION_ID => $address->getRegionId(), + AddressInterface::KEY_REGION_CODE => $address->getRegionCode(), + AddressInterface::KEY_STREET => $address->getStreet(), + AddressInterface::KEY_COMPANY => $address->getCompany(), + AddressInterface::KEY_TELEPHONE => $address->getTelephone(), + AddressInterface::KEY_POSTCODE => $address->getPostcode(), + AddressInterface::KEY_CITY => $address->getCity(), + AddressInterface::KEY_FIRSTNAME => $address->getFirstname(), + AddressInterface::KEY_LASTNAME => $address->getLastname(), + AddressInterface::KEY_EMAIL => $address->getEmail() + ]; + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/billing-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token + ], + ]; + + $this->assertEquals($data, $this->_webApiCall($serviceInfo)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetMyAddress() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/billing-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + 'token' => $token + ], + ]; + + $addressData = [ + 'firstname' => 'John', + 'lastname' => 'Smith', + 'email' => 'cat@dog.com', + 'company' => 'eBay Inc', + 'street' => ['Typical Street', 'Tiny House 18'], + 'city' => 'Big City', + 'region_id' => 12, + 'region' => 'California', + 'region_code' => 'CA', + 'postcode' => '0985432', + 'country_id' => 'US', + 'telephone' => '88776655', + 'fax' => '44332255', + ]; + $requestData = [ + 'address' => $addressData, + ]; + + $addressId = $this->_webApiCall($serviceInfo, $requestData); + + //reset $quote to reload data + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $address = $quote->getBillingAddress(); + $address->getRegionCode(); + $savedData = $address->getData(); + $this->assertEquals($addressId, $savedData['address_id']); + //custom checks for street, region and address_type + foreach ($addressData['street'] as $streetLine) { + $this->assertContains($streetLine, $quote->getBillingAddress()->getStreet()); + } + unset($addressData['street']); + $this->assertEquals('billing', $savedData['address_type']); + //check the rest of fields + foreach ($addressData as $key => $value) { + $this->assertEquals($value, $savedData[$key]); + } + } } diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartManagementTest.php index 970e4998f011c9add35b69840cbbe4a6520eff3a..3393c28e0a57947f37beee2c56af3d3954315ca4 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartManagementTest.php @@ -27,7 +27,17 @@ class CartManagementTest extends WebapiAbstract $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); } - public function testCreate() + public function tearDown() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + foreach ($this->createdQuotes as $quoteId) { + $quote->load($quoteId); + $quote->delete(); + } + } + + public function testCreateEmptyCartForGuest() { $serviceInfo = [ 'rest' => [ @@ -47,14 +57,71 @@ class CartManagementTest extends WebapiAbstract $this->createdQuotes[] = $quoteId; } - public function tearDown() + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ + public function testCreateEmptyCartForCustomer() { - /** @var \Magento\Quote\Model\Quote $quote */ - $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); - foreach ($this->createdQuotes as $quoteId) { - $quote->load($quoteId); - $quote->delete(); - } + /** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */ + $repository = $this->objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface'); + /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */ + $customer = $repository->getById(1); + $customerId = $customer->getId(); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/customers/' . $customerId . '/carts', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'CreateEmptyCartForCustomer', + ], + ]; + + $quoteId = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]); + $this->assertGreaterThan(0, $quoteId); + $this->createdQuotes[] = $quoteId; + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ + public function testCreateEmptyCartAndGetCartForCustomer() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/carts/mine', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + 'token' => $token + ] + ]; + + $quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden + $this->assertGreaterThan(0, $quoteId); + $this->createdQuotes[] = $quoteId; + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/carts/mine', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token + ] + ]; + + /** @var \Magento\Quote\Api\Data\CartInterface $cart */ + $cart = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden + $this->assertEquals($quoteId, $cart['id']); } /** @@ -302,6 +369,41 @@ class CartManagementTest extends WebapiAbstract $quote->delete(); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php + */ + public function testPlaceOrderForMyCart() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test_order_1', 'reserved_order_id'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/carts/mine/order', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + 'token' => $token + ], + ]; + + $orderId = $this->_webApiCall($serviceInfo, []); + + /** @var \Magento\Sales\Model\Order $order */ + $order = $this->objectManager->create('Magento\Sales\Model\Order')->load($orderId); + $items = $order->getAllItems(); + $this->assertCount(1, $items); + $this->assertEquals('Simple Product', $items[0]->getName()); + $quote->delete(); + } + /** * Test to get my cart based on customer authentication token or session * diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartTotalRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartTotalRepositoryTest.php index 257cd01237d947ab653b0069b176da3b83036618..406b755d77dd133e839ab27c4396ef3ef2ddd8de 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartTotalRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartTotalRepositoryTest.php @@ -171,4 +171,64 @@ class CartTotalRepositoryTest extends WebapiAbstract ItemTotals::KEY_BASE_ROW_TOTAL_INCL_TAX => $item->getBaseRowTotalInclTax(), ]; } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testGetMyTotals() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/carts/mine/totals', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token + ], + ]; + + /** @var \Magento\Quote\Model\Quote\Address $shippingAddress */ + $shippingAddress = $quote->getShippingAddress(); + + $data = [ + Totals::KEY_BASE_GRAND_TOTAL => $quote->getBaseGrandTotal(), + Totals::KEY_GRAND_TOTAL => $quote->getGrandTotal(), + Totals::KEY_BASE_SUBTOTAL => $quote->getBaseSubtotal(), + Totals::KEY_SUBTOTAL => $quote->getSubtotal(), + Totals::KEY_BASE_SUBTOTAL_WITH_DISCOUNT => $quote->getBaseSubtotalWithDiscount(), + Totals::KEY_SUBTOTAL_WITH_DISCOUNT => $quote->getSubtotalWithDiscount(), + Totals::KEY_DISCOUNT_AMOUNT => $shippingAddress->getDiscountAmount(), + Totals::KEY_BASE_DISCOUNT_AMOUNT => $shippingAddress->getBaseDiscountAmount(), + Totals::KEY_SHIPPING_AMOUNT => $shippingAddress->getShippingAmount(), + Totals::KEY_BASE_SHIPPING_AMOUNT => $shippingAddress->getBaseShippingAmount(), + Totals::KEY_SHIPPING_DISCOUNT_AMOUNT => $shippingAddress->getShippingDiscountAmount(), + Totals::KEY_BASE_SHIPPING_DISCOUNT_AMOUNT => $shippingAddress->getBaseShippingDiscountAmount(), + Totals::KEY_TAX_AMOUNT => $shippingAddress->getTaxAmount(), + Totals::KEY_BASE_TAX_AMOUNT => $shippingAddress->getBaseTaxAmount(), + Totals::KEY_SHIPPING_TAX_AMOUNT => $shippingAddress->getShippingTaxAmount(), + Totals::KEY_BASE_SHIPPING_TAX_AMOUNT => $shippingAddress->getBaseShippingTaxAmount(), + Totals::KEY_SUBTOTAL_INCL_TAX => $shippingAddress->getSubtotalInclTax(), + Totals::KEY_BASE_SUBTOTAL_INCL_TAX => $shippingAddress->getBaseSubtotalTotalInclTax(), + Totals::KEY_SHIPPING_INCL_TAX => $shippingAddress->getShippingInclTax(), + Totals::KEY_BASE_SHIPPING_INCL_TAX => $shippingAddress->getBaseShippingInclTax(), + Totals::KEY_BASE_CURRENCY_CODE => $quote->getBaseCurrencyCode(), + Totals::KEY_QUOTE_CURRENCY_CODE => $quote->getQuoteCurrencyCode(), + Totals::KEY_ITEMS => [$this->getQuoteItemTotalsData($quote)], + ]; + + $data = $this->formatTotalsData($data); + + $this->assertEquals($data, $this->_webApiCall($serviceInfo)); + } } diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CouponManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CouponManagementTest.php index f6f2a4655c0bc4c94258d4a377b142a4b5ac1fbd..e6c8a97b918c488762656b2850a251cc4fec215d 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CouponManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CouponManagementTest.php @@ -148,4 +148,147 @@ class CouponManagementTest extends WebapiAbstract $this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode); } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php + */ + public function testGetMyCoupon() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $couponCode = $quote->getCouponCode(); + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons' , + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token, + ], + ]; + + $requestData = []; + $this->assertEquals($couponCode, $this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php + */ + public function testDeleteMyCoupon() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE, + 'token' => $token, + ], + ]; + $requestData = []; + $this->assertTrue($this->_webApiCall($serviceInfo, $requestData)); + $quote->load('test_order_1', 'reserved_order_id'); + $this->assertEquals('', $quote->getCouponCode()); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @expectedException \Exception + * @expectedExceptionMessage Coupon code is not valid + */ + public function testSetMyCouponThrowsExceptionIfCouponDoesNotExist() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + $couponCode = 'invalid_coupon_code'; + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + 'token' => $token, + ], + ]; + + $requestData = [ + "couponCode" => $couponCode, + ]; + + $this->_webApiCall($serviceInfo, $requestData); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Sales/_files/quote.php + * @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php + */ + public function testSetMyCouponSuccess() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test01', 'reserved_order_id'); + $cartId = $quote->getId(); + $salesRule = $this->objectManager->create('Magento\SalesRule\Model\Rule'); + $salesRule->load('Test Coupon for General', 'name'); + $couponCode = $salesRule->getCouponCode(); + + /* Since this isn't a full quote fixture, need to assign it to the right customer */ + $cartManagementService = $this->objectManager->create( + 'Magento\Quote\Api\CartManagementInterface' + ); + $cartManagementService->assignCustomer($cartId, 1, 1); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + 'token' => $token, + ], + ]; + + $requestData = [ + "couponCode" => $couponCode, + ]; + + $this->assertTrue($this->_webApiCall($serviceInfo, $requestData)); + + $quoteWithCoupon = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quoteWithCoupon->load('test01', 'reserved_order_id'); + + $this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode); + } } diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestPaymentMethodManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestPaymentMethodManagementTest.php index a1eba522ea594f5b36a9b00bcbc41a46c2d798c4..40579922f1896f761ef85e891a1e5f59daae39dd 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestPaymentMethodManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestPaymentMethodManagementTest.php @@ -66,7 +66,7 @@ class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\W $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -103,7 +103,7 @@ class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\W $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -139,7 +139,7 @@ class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\W $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -178,7 +178,7 @@ class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\W $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -216,7 +216,7 @@ class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\W $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -286,7 +286,7 @@ class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\W $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, ], 'soap' => [ @@ -299,25 +299,22 @@ class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\W $requestData = ["cartId" => $cartId]; $requestResponse = $this->_webApiCall($serviceInfo, $requestData); - $this->assertArrayHasKey('method', $requestResponse); - $this->assertArrayHasKey('po_number', $requestResponse); - $this->assertArrayHasKey('cc_owner', $requestResponse); - $this->assertArrayHasKey('cc_type', $requestResponse); - $this->assertArrayHasKey('cc_exp_year', $requestResponse); - $this->assertArrayHasKey('cc_exp_month', $requestResponse); - $this->assertArrayHasKey('additional_data', $requestResponse); - - $this->assertNotNull($requestResponse['method']); - $this->assertNotNull($requestResponse['po_number']); - $this->assertNotNull($requestResponse['cc_owner']); - $this->assertNotNull($requestResponse['cc_type']); - $this->assertNotNull($requestResponse['cc_exp_year']); - $this->assertNotNull($requestResponse['cc_exp_month']); - $this->assertNotNull($requestResponse['additional_data']); + foreach ($this->getPaymentMethodFieldsForAssert() as $field) { + $this->assertArrayHasKey($field, $requestResponse); + $this->assertNotNull($requestResponse[$field]); + } $this->assertEquals('checkmo', $requestResponse['method']); } + /** + * @return array + */ + protected function getPaymentMethodFieldsForAssert() + { + return ['method', 'po_number', 'cc_owner', 'cc_type', 'cc_exp_year', 'cc_exp_month', 'additional_data']; + } + /** * Retrieve masked cart ID for guest cart. * diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/PaymentMethodManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/PaymentMethodManagementTest.php index 8288f1f9b5d874e3ca80bd323ee1777c8de751f3..b4aa67ab9fa7d3061541f02879d878e84a1e110f 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/PaymentMethodManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/PaymentMethodManagementTest.php @@ -33,7 +33,7 @@ class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\Webapi $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -70,7 +70,7 @@ class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\Webapi $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -106,7 +106,7 @@ class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\Webapi $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -145,7 +145,7 @@ class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\Webapi $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -183,7 +183,7 @@ class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\Webapi $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, ], 'soap' => [ @@ -253,7 +253,7 @@ class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\Webapi $serviceInfo = [ 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-methods', + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, ], 'soap' => [ @@ -266,22 +266,126 @@ class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\Webapi $requestData = ["cartId" => $cartId]; $requestResponse = $this->_webApiCall($serviceInfo, $requestData); - $this->assertArrayHasKey('method', $requestResponse); - $this->assertArrayHasKey('po_number', $requestResponse); - $this->assertArrayHasKey('cc_owner', $requestResponse); - $this->assertArrayHasKey('cc_type', $requestResponse); - $this->assertArrayHasKey('cc_exp_year', $requestResponse); - $this->assertArrayHasKey('cc_exp_month', $requestResponse); - $this->assertArrayHasKey('additional_data', $requestResponse); - - $this->assertNotNull($requestResponse['method']); - $this->assertNotNull($requestResponse['po_number']); - $this->assertNotNull($requestResponse['cc_owner']); - $this->assertNotNull($requestResponse['cc_type']); - $this->assertNotNull($requestResponse['cc_exp_year']); - $this->assertNotNull($requestResponse['cc_exp_month']); - $this->assertNotNull($requestResponse['additional_data']); + foreach ($this->getPaymentMethodFieldsForAssert() as $field) { + $this->assertArrayHasKey($field, $requestResponse); + $this->assertNotNull($requestResponse[$field]); + } $this->assertEquals('checkmo', $requestResponse['method']); } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetListMine() + { + $this->_markTestAsRestOnly(); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/payment-methods', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $this->getCustomerToken() + ] + ]; + + $requestResponse = $this->_webApiCall($serviceInfo); + + $expectedResponse = [ + 'code' => 'checkmo', + 'title' => 'Check / Money order', + ]; + + $this->assertGreaterThan(0, count($requestResponse)); + $this->assertContains($expectedResponse, $requestResponse); + } + + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php + */ + public function testGetMine() + { + $this->_markTestAsRestOnly(); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1_with_payment', 'reserved_order_id'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $this->getCustomerToken() + ] + ]; + + $requestResponse = $this->_webApiCall($serviceInfo); + + foreach ($this->getPaymentMethodFieldsForAssert() as $field) { + $this->assertArrayHasKey($field, $requestResponse); + $this->assertNotNull($requestResponse[$field]); + } + $this->assertEquals('checkmo', $requestResponse['method']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentWithSimpleProductMine() + { + $this->_markTestAsRestOnly(); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('\Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + 'token' => $this->getCustomerToken() + ] + ]; + + $requestData = [ + "method" => [ + 'method' => 'checkmo', + 'po_number' => '200', + 'cc_owner' => 'tester', + 'cc_type' => 'test', + 'cc_exp_year' => '2014', + 'cc_exp_month' => '1', + ], + ]; + + $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * @return array + */ + protected function getPaymentMethodFieldsForAssert() + { + return ['method', 'po_number', 'cc_owner', 'cc_type', 'cc_exp_year', 'cc_exp_month', 'additional_data']; + } + + /** + * Get customer ID token + * + * @return string + */ + protected function getCustomerToken() + { + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + return $token; + } } diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingAddressManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingAddressManagementTest.php index ff9c032601f880b6bac99622c94f2e5f6ba7915a..9a8db29b15a7abfe050ccf675c24b701eab51538 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingAddressManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingAddressManagementTest.php @@ -207,4 +207,124 @@ class ShippingAddressManagementTest extends WebapiAbstract $this->_webApiCall($serviceInfo, $requestData); } + + /** + * Test getting shipping address based on the customer's authentication token. + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetMyAddress() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + /** @var \Magento\Quote\Model\Quote\Address $address */ + $address = $quote->getShippingAddress(); + + $addressData = [ + AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(), + AddressInterface::KEY_ID => (int)$address->getId(), + AddressInterface::KEY_CUSTOMER_ID => $address->getCustomerId(), + AddressInterface::KEY_REGION => $address->getRegion(), + AddressInterface::KEY_REGION_ID => $address->getRegionId(), + AddressInterface::KEY_REGION_CODE => $address->getRegionCode(), + AddressInterface::KEY_STREET => $address->getStreet(), + AddressInterface::KEY_COMPANY => $address->getCompany(), + AddressInterface::KEY_TELEPHONE => $address->getTelephone(), + AddressInterface::KEY_POSTCODE => $address->getPostcode(), + AddressInterface::KEY_CITY => $address->getCity(), + AddressInterface::KEY_FIRSTNAME => $address->getFirstname(), + AddressInterface::KEY_LASTNAME => $address->getLastname(), + AddressInterface::KEY_EMAIL => $address->getEmail() + ]; + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/shipping-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token + ], + ]; + + $requestData = []; + $this->assertEquals($addressData, $this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * Test setting shipping address based on the customer's authentication token. + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetMyAddress() + { + $this->_markTestAsRestOnly(); + + // get customer ID token + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'mine/shipping-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + 'token' => $token + ], + ]; + + $addressData = [ + 'firstname' => 'John', + 'lastname' => 'Smith', + 'email' => 'cat@dog.com', + 'company' => 'eBay Inc', + 'street' => ['Typical Street', 'Tiny House 18'], + 'city' => 'Big City', + 'region_id' => 12, + 'region' => 'California', + 'region_code' => 'CA', + 'postcode' => '0985432', + 'country_id' => 'US', + 'telephone' => '88776655', + 'fax' => '44332255', + ]; + $requestData = [ + 'address' => $addressData, + ]; + + $addressId = $this->_webApiCall($serviceInfo, $requestData); + + //reset $quote to reload data + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $address = $quote->getShippingAddress(); + $address->getRegionCode(); + $savedData = $address->getData(); + $this->assertEquals($addressId, $savedData['address_id'], 'Invalid address ID'); + $this->assertEquals(0, $savedData['same_as_billing']); + //custom checks for street, region and address_type + $this->assertEquals($addressData['street'], $quote->getShippingAddress()->getStreet()); + unset($addressData['street']); + + $this->assertEquals('shipping', $savedData['address_type']); + //check the rest of fields + foreach ($addressData as $key => $value) { + $this->assertEquals($value, $savedData[$key], 'Invalid value for ' . $key); + } + } } diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingMethodManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingMethodManagementTest.php index 6217cc99b088f063669901fdc5e1d492794e1e2c..60865f081c99f6e7fdbdc93b6a901e95189e0509 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingMethodManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/ShippingMethodManagementTest.php @@ -117,6 +117,50 @@ class ShippingMethodManagementTest extends WebapiAbstract $this->assertEquals('Shipping address is not set', $message); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetMethodForMyCart() + { + $this->_markTestAsRestOnly(); + + $this->quote->load('test_order_1', 'reserved_order_id'); + + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/carts/mine/selected-shipping-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + 'token' => $token + ] + ]; + + $requestData = [ + 'cartId' => 999, + 'carrierCode' => 'flatrate', + 'methodCode' => 'flatrate', + ]; // cartId 999 will be overridden + + $result = $this->_webApiCall($serviceInfo, $requestData); + $this->assertEquals(true, $result); + + /** @var \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManagementService */ + $shippingMethodManagementService = $this->objectManager->create( + 'Magento\Quote\Api\ShippingMethodManagementInterface' + ); + $shippingMethod = $shippingMethodManagementService->get($this->quote->getId()); + + $this->assertNotNull($shippingMethod); + $this->assertEquals('flatrate', $shippingMethod->getCarrierCode()); + $this->assertEquals('flatrate', $shippingMethod->getMethodCode()); + } + /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php */ @@ -169,6 +213,41 @@ class ShippingMethodManagementTest extends WebapiAbstract $this->assertEquals([], $result); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetMethodForMyCart() + { + $this->_markTestAsRestOnly(); + + $this->quote->load('test_order_1', 'reserved_order_id'); + + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManagementService */ + $shippingMethodManagementService = $this->objectManager->create( + 'Magento\Quote\Api\ShippingMethodManagementInterface' + ); + $shippingMethodManagementService->set($this->quote->getId(), 'flatrate', 'flatrate'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/carts/mine/selected-shipping-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token + ] + ]; + + $result = $this->_webApiCall($serviceInfo, []); + $this->assertEquals('flatrate', $result[ShippingMethodInterface::KEY_CARRIER_CODE]); + $this->assertEquals('flatrate', $result[ShippingMethodInterface::KEY_METHOD_CODE]); + } + /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php * @@ -204,6 +283,54 @@ class ShippingMethodManagementTest extends WebapiAbstract $this->assertEquals($expectedData, $returnedRates); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetListForMyCart() + { + $this->_markTestAsRestOnly(); + + $this->quote->load('test_order_1', 'reserved_order_id'); + + /** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = $this->objectManager->create( + 'Magento\Integration\Service\V1\CustomerTokenServiceInterface' + ); + $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + + /** @var \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManagementService */ + $shippingMethodManagementService = $this->objectManager->create( + 'Magento\Quote\Api\ShippingMethodManagementInterface' + ); + $shippingMethodManagementService->set($this->quote->getId(), 'flatrate', 'flatrate'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/carts/mine/shipping-methods', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token + ] + ]; + + $result = $this->_webApiCall($serviceInfo, []); + $this->assertNotEmpty($result); + $this->assertCount(1, $result); + + $shippingMethod = $shippingMethodManagementService->get($this->quote->getId()); + $expectedData = [ + ShippingMethodInterface::KEY_CARRIER_CODE => $shippingMethod->getCarrierCode(), + ShippingMethodInterface::KEY_METHOD_CODE => $shippingMethod->getMethodCode(), + ShippingMethodInterface::KEY_CARRIER_TITLE => $shippingMethod->getCarrierTitle(), + ShippingMethodInterface::KEY_METHOD_TITLE => $shippingMethod->getMethodTitle(), + ShippingMethodInterface::KEY_SHIPPING_AMOUNT => $shippingMethod->getAmount(), + ShippingMethodInterface::KEY_BASE_SHIPPING_AMOUNT => $shippingMethod->getBaseAmount(), + ShippingMethodInterface::KEY_AVAILABLE => $shippingMethod->getAvailable(), + ]; + + $this->assertEquals($expectedData, $result[0]); + } + /** * @param string $cartId * @return array diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_generalusers.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_generalusers.php new file mode 100644 index 0000000000000000000000000000000000000000..c66670aa8719af2cc1a5575596f7b8c65661bf58 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_generalusers.php @@ -0,0 +1,28 @@ +<?php +/** + * SalesRule 10% discount coupon + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +/** @var \Magento\SalesRule\Model\Rule $salesRule */ +$salesRule = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\SalesRule\Model\Rule'); + +$data = [ + 'name' => 'Test Coupon for General', + 'is_active' => true, + 'website_ids' => [ + \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + 'Magento\Store\Model\StoreManagerInterface' + )->getStore()->getWebsiteId() + ], + 'customer_group_ids' => [1], + 'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC, + 'coupon_code' => uniqid(), + 'simple_action' => \Magento\SalesRule\Model\Rule::BY_PERCENT_ACTION, + 'discount_amount' => 10, + 'discount_step' => 1 +]; + +$salesRule->loadPost($data)->setUseAutoGeneration(false)->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_generalusers_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_generalusers_rollback.php new file mode 100644 index 0000000000000000000000000000000000000000..a6f462f767443532421f66b9e8d144335469233e --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_generalusers_rollback.php @@ -0,0 +1,12 @@ +<?php +/** + * SalesRule 10% discount coupon + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +/** @var \Magento\SalesRule\Model\Rule $salesRule */ +$salesRule = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\SalesRule\Model\Rule'); +$salesRule->load('Test Coupon for General', 'name'); +$salesRule->delete(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_rollback.php new file mode 100644 index 0000000000000000000000000000000000000000..53c90ee1778f1eb0c67b16c1af81e4a87f9cb47f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/discount_10percent_rollback.php @@ -0,0 +1,12 @@ +<?php +/** + * SalesRule 10% discount coupon + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +/** @var \Magento\SalesRule\Model\Rule $salesRule */ +$salesRule = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\SalesRule\Model\Rule'); +$salesRule->load('Test Coupon', 'name'); +$salesRule->delete(); diff --git a/lib/internal/Magento/Framework/Webapi/Rest/Request/ParamOverriderInterface.php b/lib/internal/Magento/Framework/Webapi/Rest/Request/ParamOverriderInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..92d748e923eb148cadd2b8a2af7a8628116f2630 --- /dev/null +++ b/lib/internal/Magento/Framework/Webapi/Rest/Request/ParamOverriderInterface.php @@ -0,0 +1,37 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Framework\Webapi\Rest\Request; + +/** + * Override parameter values + * + * Parameters in the webapi.xml can be forced. This ensures that on specific routes, a specific value is always used. + * For instance, if there is a ".../me/..." route, the route should use only user information specific to the + * currently logged in user. More specifically, if there was a "/customers/me/addresses" route, the service method + * invoked could have a signature of "getAddresses($customerId)", but in the webapi.xml, the $customerId parameter + * would be forced to be the customer id of the current authenticated user. + * + * The forced override parameter configuration is in the webapi.xml. + * + * <data> + * <parameter name="customer.id" force="true">%customer_id%</parameter> + * </data> + * + * Classes which implement ParamOverriderInterface would return the real value for the parameter, so a + * ParamOverriderCustomerId would return the current authenticated user's customer id. If you + * create new ParamOverriderInterface implementations, you can register new implementations by + * adding to the parameter list for ParamsOverrider's dependency injection configuration. + */ +interface ParamOverriderInterface +{ + /** + * Returns the overridden value to use. + * + * @return string|int|null + */ + public function getOverriddenValue(); +}