diff --git a/app/code/Magento/CurrencySymbol/Controller/Adminhtml/System/Currency/FetchRates.php b/app/code/Magento/CurrencySymbol/Controller/Adminhtml/System/Currency/FetchRates.php index a99c5d696096d3acb8c36937e2e78b4553a1ec8c..f6463d67084a9a95a2f7ff27c39385f16e9f937a 100644 --- a/app/code/Magento/CurrencySymbol/Controller/Adminhtml/System/Currency/FetchRates.php +++ b/app/code/Magento/CurrencySymbol/Controller/Adminhtml/System/Currency/FetchRates.php @@ -22,7 +22,7 @@ class FetchRates extends \Magento\CurrencySymbol\Controller\Adminhtml\System\Cur $service = $this->getRequest()->getParam('rate_services'); $this->_getSession()->setCurrencyRateService($service); if (!$service) { - throw new \Exception(__('Please specify a correct Import Service.')); + throw new \Magento\Framework\Exception\LocalizedException(__('Please specify a correct Import Service.')); } try { /** @var \Magento\Directory\Model\Currency\Import\ImportInterface $importModel */ diff --git a/app/code/Magento/Integration/Setup/InstallSchema.php b/app/code/Magento/Integration/Setup/InstallSchema.php index 9588da6905210f53b0c47aaecc05c1f483d30107..28d24cab8f3a26047403ded521eba906af078d02 100644 --- a/app/code/Magento/Integration/Setup/InstallSchema.php +++ b/app/code/Magento/Integration/Setup/InstallSchema.php @@ -268,9 +268,6 @@ class InstallSchema implements InstallSchemaInterface \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE )->setComment( 'OAuth Nonce' - )->setOption( - 'type', - 'MyISAM' ); $installer->getConnection()->createTable($table); diff --git a/app/code/Magento/Quote/Api/CartItemRepositoryInterface.php b/app/code/Magento/Quote/Api/CartItemRepositoryInterface.php index 992a93583b33679287c2e3513f31980e937ea072..96787607500b7216d5826ec63141a12741a564bf 100644 --- a/app/code/Magento/Quote/Api/CartItemRepositoryInterface.php +++ b/app/code/Magento/Quote/Api/CartItemRepositoryInterface.php @@ -28,22 +28,45 @@ interface CartItemRepositoryInterface public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem); /** - * Remove bundle option + * Removes the specified item from the specified cart. + * + * @param int $cartId The cart ID. + * @param int $itemId The item ID of the item to be removed. + * @return bool + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified item or cart does not exist. + * @throws \Magento\Framework\Exception\CouldNotSaveException The item could not be removed. + */ + public function deleteById($cartId, $itemId); + + /** + * Lists items that are assigned to a specified cart. + * + * @param int $customerId Customer ID. + * @return \Magento\Quote\Api\Data\CartItemInterface[] Array of items. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function getListForCustomer($customerId); + + /** + * Adds the specified item to the specified cart. * - * @param \Magento\Quote\Api\Data\CartItemInterface $cartItem - * @return void - * @throws \Magento\Framework\Exception\CouldNotSaveException + * @param int $customerId Customer ID. + * @param \Magento\Quote\Api\Data\CartItemInterface $cartItem The item. + * @return \Magento\Quote\Api\Data\CartItemInterface Item. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + * @throws \Magento\Framework\Exception\CouldNotSaveException The specified item could not be saved to the cart. + * @throws \Magento\Framework\Exception\InputException The specified item or cart is not valid. */ - public function delete(\Magento\Quote\Api\Data\CartItemInterface $cartItem); + public function saveForCustomer($customerId, \Magento\Quote\Api\Data\CartItemInterface $cartItem); /** * Removes the specified item from the specified cart. * - * @param int $cartId The cart ID. + * @param int $customerId Customer ID. * @param int $itemId The item ID of the item to be removed. * @return bool * @throws \Magento\Framework\Exception\NoSuchEntityException The specified item or cart does not exist. * @throws \Magento\Framework\Exception\CouldNotSaveException The item could not be removed. */ - public function deleteById($cartId, $itemId); + public function deleteByIdForCustomer($customerId, $itemId); } diff --git a/app/code/Magento/Quote/Api/CartManagementInterface.php b/app/code/Magento/Quote/Api/CartManagementInterface.php index 4bf49813ac4313319350c774ee038d1f312453da..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 administrative 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 $storeId + * @return int Cart ID. * @throws \Magento\Framework\Exception\CouldNotSaveException The empty cart and quote could not be created. + */ + 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 createEmptyCart($storeId); + public function createEmptyCartForCustomer($customerId); /** * Returns information for the cart for a specified customer. diff --git a/app/code/Magento/Quote/Api/Data/CartItemInterface.php b/app/code/Magento/Quote/Api/Data/CartItemInterface.php index 4951fe1698006419285934bc95c4645537394e1c..7fd33b17342861f61f868535d4baecfcfec46750 100644 --- a/app/code/Magento/Quote/Api/Data/CartItemInterface.php +++ b/app/code/Magento/Quote/Api/Data/CartItemInterface.php @@ -119,14 +119,14 @@ interface CartItemInterface extends \Magento\Framework\Api\ExtensibleDataInterfa /** * Returns Quote id. * - * @return int + * @return string */ public function getQuoteId(); /** * Sets Quote id. * - * @param int $quoteId + * @param string $quoteId * @return $this */ public function setQuoteId($quoteId); diff --git a/app/code/Magento/Quote/Api/GuestBillingAddressManagementInterface.php b/app/code/Magento/Quote/Api/GuestBillingAddressManagementInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..0400edba25ea1ec38f32cc2e4ed20c145485bd4d --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestBillingAddressManagementInterface.php @@ -0,0 +1,32 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +/** + * Billing address management interface for guest carts. + */ +interface GuestBillingAddressManagementInterface +{ + /** + * Assigns a specified billing address to a specified cart. + * + * @param string $cartId The cart ID. + * @param \Magento\Quote\Api\Data\AddressInterface $address Billing address data. + * @return int Address ID. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + * @throws \Magento\Framework\Exception\InputException The specified cart ID or address data is not valid. + */ + public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address); + + /** + * Returns the billing address for a specified quote. + * + * @param string $cartId The cart ID. + * @return \Magento\Quote\Api\Data\AddressInterface Quote billing address object. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function get($cartId); +} diff --git a/app/code/Magento/Quote/Api/GuestCartItemRepositoryInterface.php b/app/code/Magento/Quote/Api/GuestCartItemRepositoryInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..40138b5e0cfdcc741a462fd19262df68fbc9c027 --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestCartItemRepositoryInterface.php @@ -0,0 +1,43 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +/** + * Cart Item repository interface for guest carts. + */ +interface GuestCartItemRepositoryInterface +{ + /** + * Lists items that are assigned to a specified cart. + * + * @param string $cartId The cart ID. + * @return \Magento\Quote\Api\Data\CartItemInterface[] Array of items. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function getList($cartId); + + /** + * Adds the specified item to the specified cart. + * + * @param \Magento\Quote\Api\Data\CartItemInterface $cartItem The item. + * @return \Magento\Quote\Api\Data\CartItemInterface Item. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + * @throws \Magento\Framework\Exception\CouldNotSaveException The specified item could not be saved to the cart. + * @throws \Magento\Framework\Exception\InputException The specified item or cart is not valid. + */ + public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem); + + /** + * Removes the specified item from the specified cart. + * + * @param string $cartId The cart ID. + * @param int $itemId The item ID of the item to be removed. + * @return bool + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified item or cart does not exist. + * @throws \Magento\Framework\Exception\CouldNotSaveException The item could not be removed. + */ + public function deleteById($cartId, $itemId); +} diff --git a/app/code/Magento/Quote/Api/GuestCartManagementInterface.php b/app/code/Magento/Quote/Api/GuestCartManagementInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..7fa298c2d719d7586a81f41ce777502d7d49b60e --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestCartManagementInterface.php @@ -0,0 +1,38 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +/** + * Cart Management interface for guest carts. + */ +interface GuestCartManagementInterface +{ + /** + * Enables an customer or guest user to create an empty cart and quote for an anonymous customer. + * + * @return string Cart ID. + * @throws \Magento\Framework\Exception\CouldNotSaveException The empty cart and quote could not be created. + */ + public function createEmptyCart(); + + /** + * Assigns a specified customer to a specified shopping cart. + * + * @param string $cartId The cart ID. + * @param int $customerId The customer ID. + * @param int $storeId + * @return boolean + */ + public function assignCustomer($cartId, $customerId, $storeId); + + /** + * Places an order for a specified cart. + * + * @param string $cartId The cart ID. + * @return int Order ID. + */ + public function placeOrder($cartId); +} diff --git a/app/code/Magento/Quote/Api/GuestCartRepositoryInterface.php b/app/code/Magento/Quote/Api/GuestCartRepositoryInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..eb7293e15ee908aa31e5154aa5b86cf630a5cd97 --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestCartRepositoryInterface.php @@ -0,0 +1,21 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +/** + * Cart Repository interface for guest carts. + */ +interface GuestCartRepositoryInterface +{ + /** + * Enables a guest user to return information for a specified cart. + * + * @param string $cartId + * @return \Magento\Quote\Api\Data\CartInterface + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + public function get($cartId); +} diff --git a/app/code/Magento/Quote/Api/GuestCartTotalRepositoryInterface.php b/app/code/Magento/Quote/Api/GuestCartTotalRepositoryInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..6384407793a7a5f736751d304292a82a5b212c42 --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestCartTotalRepositoryInterface.php @@ -0,0 +1,22 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Api; + +/** + * Cart totals repository interface for guest carts. + */ +interface GuestCartTotalRepositoryInterface +{ + /** + * Returns quote totals data for a specified cart. + * + * @param string $cartId The cart ID. + * @return \Magento\Quote\Api\Data\TotalsInterface Quote totals data. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function get($cartId); +} diff --git a/app/code/Magento/Quote/Api/GuestCouponManagementInterface.php b/app/code/Magento/Quote/Api/GuestCouponManagementInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..14d0a1d5def59cb543f8fc60024ba8f43f155af5 --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestCouponManagementInterface.php @@ -0,0 +1,44 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Api; + +/** + * Coupon management interface for guest carts. + */ +interface GuestCouponManagementInterface +{ + /** + * Returns information for a coupon in a specified cart. + * + * @param string $cartId The cart ID. + * @return string The coupon code data. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function get($cartId); + + /** + * Adds a coupon by code to a specified cart. + * + * @param string $cartId The cart ID. + * @param string $couponCode The coupon code data. + * @return bool + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + * @throws \Magento\Framework\Exception\CouldNotSaveException The specified coupon could not be added. + */ + public function set($cartId, $couponCode); + + /** + * Deletes a coupon from a specified cart. + * + * @param string $cartId The cart ID. + * @return bool + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + * @throws \Magento\Framework\Exception\CouldNotDeleteException The specified coupon could not be deleted. + */ + public function remove($cartId); +} diff --git a/app/code/Magento/Quote/Api/GuestPaymentMethodManagementInterface.php b/app/code/Magento/Quote/Api/GuestPaymentMethodManagementInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..e40142b3e2cb2052603955128fad18bfb4aee765 --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestPaymentMethodManagementInterface.php @@ -0,0 +1,42 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +/** + * Payment method management interface for guest carts. + */ +interface GuestPaymentMethodManagementInterface +{ + /** + * Adds a specified payment method to a specified shopping cart. + * + * @param string $cartId The cart ID. + * @param \Magento\Quote\Api\Data\PaymentInterface $method The payment method. + * @return int Payment method ID. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + * @throws \Magento\Framework\Exception\State\InvalidTransitionException The billing or shipping address + * is not set, or the specified payment method is not available. + */ + public function set($cartId, \Magento\Quote\Api\Data\PaymentInterface $method); + + /** + * Returns the payment method for a specified shopping cart. + * + * @param string $cartId The cart ID. + * @return \Magento\Quote\Api\Data\PaymentInterface Payment method object. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function get($cartId); + + /** + * Lists available payment methods for a specified shopping cart. + * + * @param string $cartId The cart ID. + * @return \Magento\Quote\Api\Data\PaymentMethodInterface[] Array of payment methods. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function getList($cartId); +} diff --git a/app/code/Magento/Quote/Api/GuestShippingAddressManagementInterface.php b/app/code/Magento/Quote/Api/GuestShippingAddressManagementInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..c208b9a0fe19d91fc60b678eb4e12ab2a95d5b64 --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestShippingAddressManagementInterface.php @@ -0,0 +1,32 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +/** + * Shipping address management interface for guest carts. + */ +interface GuestShippingAddressManagementInterface +{ + /** + * Assigns a specified shipping address to a specified cart. + * + * @param string $cartId The cart ID. + * @param \Magento\Quote\Api\Data\AddressInterface $address The shipping address data. + * @return int Address ID. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + * @throws \Magento\Framework\Exception\InputException The specified cart ID or address data is not valid. + */ + public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address); + + /** + * Returns the shipping address for a specified quote. + * + * @param string $cartId The cart ID. + * @return \Magento\Quote\Api\Data\AddressInterface Shipping address object. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist. + */ + public function get($cartId); +} diff --git a/app/code/Magento/Quote/Api/GuestShippingMethodManagementInterface.php b/app/code/Magento/Quote/Api/GuestShippingMethodManagementInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..0e881009d98567359254ceb57397a0fbb403155b --- /dev/null +++ b/app/code/Magento/Quote/Api/GuestShippingMethodManagementInterface.php @@ -0,0 +1,47 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +/** + * Shipping method management interface for guest carts. + */ +interface GuestShippingMethodManagementInterface +{ + /** + * Sets the carrier and shipping methods codes for a specified cart. + * + * @param string $cartId The shopping cart ID. + * @param string $carrierCode The carrier code. + * @param string $methodCode The shipping method code. + * @return bool + * @throws \Magento\Framework\Exception\InputException The shipping method is not valid for an empty cart. + * @throws \Magento\Framework\Exception\CouldNotSaveException The shipping method could not be saved. + * @throws \Magento\Framework\Exception\StateException The billing or shipping address is not set. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart contains only virtual products + * so the shipping method does not apply. + */ + public function set($cartId, $carrierCode, $methodCode); + + /** + * Returns selected shipping method for a specified quote. + * + * @param string $cartId The shopping cart ID. + * @return \Magento\Quote\Api\Data\ShippingMethodInterface Shipping method. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified shopping cart does not exist. + * @throws \Magento\Framework\Exception\StateException The shipping address is not set. + */ + public function get($cartId); + + /** + * Lists applicable shipping methods for a specified quote. + * + * @param string $cartId The shopping cart ID. + * @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods. + * @throws \Magento\Framework\Exception\NoSuchEntityException The specified quote does not exist. + * @throws \Magento\Framework\Exception\StateException The shipping address is not set. + */ + public function getList($cartId); +} diff --git a/app/code/Magento/Quote/Model/Cart/Access/CartManagementPlugin.php b/app/code/Magento/Quote/Model/Cart/Access/CartManagementPlugin.php deleted file mode 100644 index f7ab6e07b0ecd9b135f3b800cd9f05e7c29e3791..0000000000000000000000000000000000000000 --- a/app/code/Magento/Quote/Model/Cart/Access/CartManagementPlugin.php +++ /dev/null @@ -1,57 +0,0 @@ -<?php -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ - -namespace Magento\Quote\Model\Cart\Access; - -use Magento\Framework\Exception\AuthorizationException; -use Magento\Authorization\Model\UserContextInterface; - -class CartManagementPlugin -{ - /** - * @var UserContextInterface - */ - protected $userContext; - - /** - * @var int[] - */ - protected $allowedUserTypes = [ - UserContextInterface::USER_TYPE_ADMIN, - UserContextInterface::USER_TYPE_INTEGRATION, - ]; - - /** - * @param UserContextInterface $userContext - */ - public function __construct(UserContextInterface $userContext) - { - $this->userContext = $userContext; - } - - /** - * Check whether access is allowed for create cart resource - * - * @param \Magento\Quote\Api\CartManagementInterface $subject - * @param int $cartId - * @param int $customerId - * @param int $storeId - * - * @return void - * @throws AuthorizationException if access denied - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function beforeAssignCustomer( - \Magento\Quote\Api\CartManagementInterface $subject, - $cartId, - $customerId, - $storeId - ) { - if (!in_array($this->userContext->getUserType(), $this->allowedUserTypes)) { - throw new AuthorizationException(__('Access denied')); - } - } -} diff --git a/app/code/Magento/Quote/Model/Cart/Access/CartRepositoryPlugin.php b/app/code/Magento/Quote/Model/Cart/Access/CartRepositoryPlugin.php deleted file mode 100644 index 0410af82ca10c736cda6c151f8bd34e81e30d60d..0000000000000000000000000000000000000000 --- a/app/code/Magento/Quote/Model/Cart/Access/CartRepositoryPlugin.php +++ /dev/null @@ -1,73 +0,0 @@ -<?php -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ - -namespace Magento\Quote\Model\Cart\Access; - -use Magento\Framework\Api\SearchCriteria; -use Magento\Framework\Exception\AuthorizationException; -use Magento\Authorization\Model\UserContextInterface; - -class CartRepositoryPlugin -{ - /** - * @var UserContextInterface - */ - protected $userContext; - - /** - * @var int[] - */ - protected $allowedUserTypes = [ - UserContextInterface::USER_TYPE_ADMIN, - UserContextInterface::USER_TYPE_INTEGRATION, - ]; - - /** - * @param UserContextInterface $userContext - */ - public function __construct(UserContextInterface $userContext) - { - $this->userContext = $userContext; - } - - /** - * Check whether access is allowed for cart resource - * - * @param \Magento\Quote\Api\CartRepositoryInterface $subject - * @param int $cartId - * - * @return void - * @throws AuthorizationException if access denied - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function beforeGet( - \Magento\Quote\Api\CartRepositoryInterface $subject, - $cartId - ) { - if (!in_array($this->userContext->getUserType(), $this->allowedUserTypes)) { - throw new AuthorizationException(__('Access denied')); - } - } - - /** - * Check whether access is allowed for cart list resource - * - * @param \Magento\Quote\Api\CartRepositoryInterface $subject - * @param SearchCriteria $searchCriteria - * - * @return void - * @throws AuthorizationException if access denied - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function beforeGetList( - \Magento\Quote\Api\CartRepositoryInterface $subject, - SearchCriteria $searchCriteria - ) { - if (!in_array($this->userContext->getUserType(), $this->allowedUserTypes)) { - throw new AuthorizationException(__('Access denied')); - } - } -} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestBillingAddressManagement.php b/app/code/Magento/Quote/Model/GuestCart/GuestBillingAddressManagement.php new file mode 100644 index 0000000000000000000000000000000000000000..61788f4920def75997a0a12225990c5ba1be89d8 --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestBillingAddressManagement.php @@ -0,0 +1,61 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\GuestBillingAddressManagementInterface; +use Magento\Quote\Api\BillingAddressManagementInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Billing address management service for guest carts. + */ +class GuestBillingAddressManagement implements GuestBillingAddressManagementInterface +{ + /** + * @var QuoteIdMaskFactory + */ + private $quoteIdMaskFactory; + + /** + * @var BillingAddressManagementInterface + */ + private $billingAddressManagement; + + /** + * Constructs a quote billing address service object. + * + * @param BillingAddressManagementInterface $billingAddressManagement + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + BillingAddressManagementInterface $billingAddressManagement, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + $this->billingAddressManagement = $billingAddressManagement; + } + + /** + * {@inheritDoc} + */ + public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->billingAddressManagement->assign($quoteIdMask->getId(), $address); + } + + /** + * {@inheritDoc} + */ + public function get($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->billingAddressManagement->get($quoteIdMask->getId()); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestCartItemRepository.php b/app/code/Magento/Quote/Model/GuestCart/GuestCartItemRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..a3750601070b89099292981361a99667a5ec39b7 --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestCartItemRepository.php @@ -0,0 +1,78 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\Data\CartItemInterface; +use Magento\Quote\Model\Quote\Item\Repository; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Cart Item repository class for guest carts. + */ +class GuestCartItemRepository implements \Magento\Quote\Api\GuestCartItemRepositoryInterface +{ + /** + * @var Repository + */ + protected $repository; + + /** + * @var QuoteIdMaskFactory + */ + protected $quoteIdMaskFactory; + + /** + * Constructs a read service object. + * + * @param \Magento\Quote\Model\Quote\Item\Repository $repository + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + \Magento\Quote\Model\Quote\Item\Repository $repository, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + $this->repository = $repository; + } + + /** + * {@inheritdoc} + */ + public function getList($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + $cartItemList = $this->repository->getList($quoteIdMask->getId()); + /** @var $item CartItemInterface */ + foreach ($cartItemList as $item) { + $item->setQuoteId($quoteIdMask->getMaskedId()); + } + return $cartItemList; + } + + /** + * {@inheritdoc} + */ + public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartItem->getQuoteId(), 'masked_id'); + $cartItem->setQuoteId($quoteIdMask->getId()); + return $this->repository->save($cartItem); + } + + /** + * {@inheritdoc} + */ + public function deleteById($cartId, $itemId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->repository->deleteById($quoteIdMask->getId(), $itemId); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php b/app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php new file mode 100644 index 0000000000000000000000000000000000000000..c62fab57f1bc5bf9328c7bc02cc58745b10377d1 --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php @@ -0,0 +1,77 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\GuestCartManagementInterface; +use Magento\Quote\Api\CartManagementInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Cart Management class for guest carts. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class GuestCartManagement implements GuestCartManagementInterface +{ + /** + * @var CartManagementInterface + */ + protected $quoteManagement; + + /** + * @var QuoteIdMaskFactory + */ + protected $quoteIdMaskFactory; + + /** + * Initialize dependencies. + * + * @param CartManagementInterface $quoteManagement + * @param QuoteIdMaskFactory $quoteIdMaskFactory + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + */ + public function __construct( + CartManagementInterface $quoteManagement, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->quoteManagement = $quoteManagement; + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + } + + /** + * {@inheritdoc} + */ + public function createEmptyCart() + { + /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create(); + $cartId = $this->quoteManagement->createEmptyCart(); + $quoteIdMask->setId($cartId)->save(); + return $quoteIdMask->getMaskedId(); + } + + /** + * {@inheritdoc} + */ + public function assignCustomer($cartId, $customerId, $storeId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->quoteManagement->assignCustomer($quoteIdMask->getId(), $customerId, $storeId); + } + + /** + * {@inheritdoc} + */ + public function placeOrder($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->quoteManagement->placeOrder($quoteIdMask->getId()); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestCartRepository.php b/app/code/Magento/Quote/Model/GuestCart/GuestCartRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..577f2b2a76b039477036bfd932f68963b64c217a --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestCartRepository.php @@ -0,0 +1,51 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\GuestCartRepositoryInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Api\CartRepositoryInterface; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Cart Repository class for guest carts. + */ +class GuestCartRepository implements GuestCartRepositoryInterface +{ + /** + * @var QuoteIdMaskFactory + */ + protected $quoteIdMaskFactory; + + /** + * @var CartRepositoryInterface + */ + protected $quoteRepository; + + /** + * Initialize dependencies. + * + * @param CartRepositoryInterface $quoteRepository + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + CartRepositoryInterface $quoteRepository, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + $this->quoteRepository = $quoteRepository; + } + + /** + * {@inheritdoc} + */ + public function get($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->quoteRepository->get($quoteIdMask->getId()); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestCartTotalRepository.php b/app/code/Magento/Quote/Model/GuestCart/GuestCartTotalRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..1ab5f5eeb8c43a88c14705240f17878335b71fce --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestCartTotalRepository.php @@ -0,0 +1,52 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\CartTotalRepositoryInterface; +use Magento\Quote\Api\GuestCartTotalRepositoryInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Cart totals repository class for guest carts. + */ +class GuestCartTotalRepository implements GuestCartTotalRepositoryInterface +{ + /** + * @var QuoteIdMaskFactory + */ + private $quoteIdMaskFactory; + + /** + * @var CartTotalRepositoryInterface + */ + private $cartTotalRepository; + + /** + * Constructs a cart totals data object. + * + * @param CartTotalRepositoryInterface $cartTotalRepository + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + CartTotalRepositoryInterface $cartTotalRepository, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->cartTotalRepository = $cartTotalRepository; + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + } + + /** + * {@inheritDoc} + */ + public function get($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->cartTotalRepository->get($quoteIdMask->getId()); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestCouponManagement.php b/app/code/Magento/Quote/Model/GuestCart/GuestCouponManagement.php new file mode 100644 index 0000000000000000000000000000000000000000..90b1047ee3c867a5b3a414f42da47469b0b79387 --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestCouponManagement.php @@ -0,0 +1,72 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\GuestCouponManagementInterface; +use Magento\Quote\Api\CouponManagementInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Coupon management class for guest carts. + */ +class GuestCouponManagement implements GuestCouponManagementInterface +{ + /** + * @var QuoteIdMaskFactory + */ + private $quoteIdMaskFactory; + + /** + * @var CouponManagementInterface + */ + private $couponManagement; + + /** + * Constructs a coupon read service object. + * + * @param CouponManagementInterface $couponManagement + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + CouponManagementInterface $couponManagement, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + $this->couponManagement = $couponManagement; + } + + /** + * {@inheritdoc} + */ + public function get($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->couponManagement->get($quoteIdMask->getId()); + } + + /** + * {@inheritdoc} + */ + public function set($cartId, $couponCode) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->couponManagement->set($quoteIdMask->getId(), $couponCode); + } + + /** + * {@inheritdoc} + */ + public function remove($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->couponManagement->remove($quoteIdMask->getId()); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestPaymentMethodManagement.php b/app/code/Magento/Quote/Model/GuestCart/GuestPaymentMethodManagement.php new file mode 100644 index 0000000000000000000000000000000000000000..b0b99599ef14d631512360cd1e5f9ff1446a1d2b --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestPaymentMethodManagement.php @@ -0,0 +1,71 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\PaymentMethodManagementInterface; +use Magento\Quote\Api\GuestPaymentMethodManagementInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Payment method management class for guest carts. + */ +class GuestPaymentMethodManagement implements GuestPaymentMethodManagementInterface +{ + /** + * @var QuoteIdMaskFactory + */ + protected $quoteIdMaskFactory; + + /** + * @var PaymentMethodManagementInterface + */ + protected $paymentMethodManagement; + + /** + * Initialize dependencies. + * + * @param PaymentMethodManagementInterface $paymentMethodManagement + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + PaymentMethodManagementInterface $paymentMethodManagement, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + $this->paymentMethodManagement = $paymentMethodManagement; + } + + /** + * {@inheritdoc} + */ + public function set($cartId, \Magento\Quote\Api\Data\PaymentInterface $method) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->paymentMethodManagement->set($quoteIdMask->getId(), $method); + } + + /** + * {@inheritdoc} + */ + public function get($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->paymentMethodManagement->get($quoteIdMask->getId()); + } + + /** + * {@inheritdoc} + */ + public function getList($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->paymentMethodManagement->getList($quoteIdMask->getId()); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestShippingAddressManagement.php b/app/code/Magento/Quote/Model/GuestCart/GuestShippingAddressManagement.php new file mode 100644 index 0000000000000000000000000000000000000000..f2ce3fb46fcf9834d1af86ae5a9e2b311e478eed --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestShippingAddressManagement.php @@ -0,0 +1,61 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\GuestShippingAddressManagementInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; +use Magento\Quote\Api\ShippingAddressManagementInterface; + +/** + * Shipping address management class for guest carts. + */ +class GuestShippingAddressManagement implements GuestShippingAddressManagementInterface +{ + /** + * @var QuoteIdMaskFactory + */ + protected $quoteIdMaskFactory; + + /** + * @var ShippingAddressManagementInterface + */ + protected $shippingAddressManagement; + + /** + * Constructs a quote shipping address write service object. + * + * @param ShippingAddressManagementInterface $shippingAddressManagement + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + ShippingAddressManagementInterface $shippingAddressManagement, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->shippingAddressManagement = $shippingAddressManagement; + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + } + + /** + * {@inheritDoc} + */ + public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->shippingAddressManagement->assign($quoteIdMask->getId(), $address); + } + + /** + * {@inheritDoc} + */ + public function get($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->shippingAddressManagement->get($quoteIdMask->getId()); + } +} diff --git a/app/code/Magento/Quote/Model/GuestCart/GuestShippingMethodManagement.php b/app/code/Magento/Quote/Model/GuestCart/GuestShippingMethodManagement.php new file mode 100644 index 0000000000000000000000000000000000000000..e847e1bd36051c8da0845f0857d53994ce387ec9 --- /dev/null +++ b/app/code/Magento/Quote/Model/GuestCart/GuestShippingMethodManagement.php @@ -0,0 +1,72 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Model\GuestCart; + +use Magento\Quote\Api\GuestShippingMethodManagementInterface; +use Magento\Quote\Api\ShippingMethodManagementInterface; +use Magento\Quote\Model\QuoteIdMask; +use Magento\Quote\Model\QuoteIdMaskFactory; + +/** + * Shipping method management class for guest carts. + */ +class GuestShippingMethodManagement implements GuestShippingMethodManagementInterface +{ + /** + * @var ShippingMethodManagementInterface + */ + private $shippingMethodManagement; + + /** + * @var QuoteIdMaskFactory + */ + private $quoteIdMaskFactory; + + /** + * Constructs a shipping method read service object. + * + * @param ShippingMethodManagementInterface $shippingMethodManagement + * @param QuoteIdMaskFactory $quoteIdMaskFactory + */ + public function __construct( + ShippingMethodManagementInterface $shippingMethodManagement, + QuoteIdMaskFactory $quoteIdMaskFactory + ) { + $this->shippingMethodManagement = $shippingMethodManagement; + $this->quoteIdMaskFactory = $quoteIdMaskFactory; + } + + /** + * {@inheritDoc} + */ + public function get($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->shippingMethodManagement->get($quoteIdMask->getId()); + } + + /** + * {@inheritDoc} + */ + public function getList($cartId) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->shippingMethodManagement->getList($quoteIdMask->getId()); + } + + /** + * {@inheritDoc} + */ + public function set($cartId, $carrierCode, $methodCode) + { + /** @var $quoteIdMask QuoteIdMask */ + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); + return $this->shippingMethodManagement->set($quoteIdMask->getId(), $carrierCode, $methodCode); + } +} diff --git a/app/code/Magento/Quote/Model/Quote/Item/Repository.php b/app/code/Magento/Quote/Model/Quote/Item/Repository.php index fc4a0f7db7ef47c3b71ea4bbab5be324167a7425..35b928ecb37a05f51135bf34ece53d0ecbd3a07b 100644 --- a/app/code/Magento/Quote/Model/Quote/Item/Repository.php +++ b/app/code/Magento/Quote/Model/Quote/Item/Repository.php @@ -107,10 +107,8 @@ class Repository implements \Magento\Quote\Api\CartItemRepositoryInterface /** * {@inheritdoc} */ - public function delete(\Magento\Quote\Api\Data\CartItemInterface $cartItem) + public function deleteById($cartId, $itemId) { - $cartId = $cartItem->getQuoteId(); - $itemId = $cartItem->getItemId(); /** * Quote. * @@ -129,18 +127,35 @@ class Repository implements \Magento\Quote\Api\CartItemRepositoryInterface } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not remove item from quote')); } + + return true; } /** * {@inheritdoc} */ - public function deleteById($cartId, $itemId) + public function getListForCustomer($customerId) { - $item = $this->itemDataFactory->create() - ->setQuoteId($cartId) - ->setItemId($itemId); + $cart = $this->quoteRepository->getActiveForCustomer($customerId); + return $this->getList($cart->getId()); + } - $this->delete($item); - return true; + /** + * {@inheritdoc} + */ + public function saveForCustomer($customerId, \Magento\Quote\Api\Data\CartItemInterface $cartItem) + { + $cart = $this->quoteRepository->getActiveForCustomer($customerId); + $cartItem->setQuoteId($cart->getId()); + return $this->save($cartItem); + } + + /** + * {@inheritdoc} + */ + public function deleteByIdForCustomer($customerId, $itemId) + { + $cart = $this->quoteRepository->getActiveForCustomer($customerId); + return $this->deleteById($cart->getId(), $itemId); } } diff --git a/app/code/Magento/Quote/Model/QuoteIdMask.php b/app/code/Magento/Quote/Model/QuoteIdMask.php new file mode 100644 index 0000000000000000000000000000000000000000..953fe1d7af87e232fca36fd79b9d79d0e69d367d --- /dev/null +++ b/app/code/Magento/Quote/Model/QuoteIdMask.php @@ -0,0 +1,63 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Model; + +/** + * QuoteIdMask model + * + * @method string getMaskedId() + * @method QuoteIdMask setMaskedId() + */ +class QuoteIdMask extends \Magento\Framework\Model\AbstractModel +{ + /** + * @var \Magento\Framework\Math\Random + */ + protected $randomDataGenerator; + + /** + * @param \Magento\Framework\Model\Context $context + * @param \Magento\Framework\Registry $registry + * @param \Magento\Framework\Math\Random $randomDataGenerator + * @param \Magento\Framework\Model\Resource\AbstractResource $resource + * @param \Magento\Framework\Data\Collection\Db $resourceCollection + * @param array $data + */ + public function __construct( + \Magento\Framework\Model\Context $context, + \Magento\Framework\Registry $registry, + \Magento\Framework\Math\Random $randomDataGenerator, + \Magento\Framework\Model\Resource\AbstractResource $resource = null, + \Magento\Framework\Data\Collection\Db $resourceCollection = null, + array $data = [] + ) { + $this->randomDataGenerator = $randomDataGenerator; + parent::__construct($context, $registry, $resource, $resourceCollection, $data); + } + + /** + * Initialize resource + * + * @return void + */ + protected function _construct() + { + $this->_init('Magento\Quote\Model\Resource\Quote\QuoteIdMask'); + } + + /** + * Initialize quote identifier before save + * + * @return $this + */ + public function beforeSave() + { + parent::beforeSave(); + $this->setMaskedId($this->randomDataGenerator->getUniqueHash()); + return $this; + } +} diff --git a/app/code/Magento/Quote/Model/QuoteManagement.php b/app/code/Magento/Quote/Model/QuoteManagement.php index 241f7d4c5784fee1ee0c48dbb53f94f4c42169a2..c09a32964aa3f3cf9c7c797a7629d55a10e19b21 100644 --- a/app/code/Magento/Quote/Model/QuoteManagement.php +++ b/app/code/Magento/Quote/Model/QuoteManagement.php @@ -6,17 +6,18 @@ namespace Magento\Quote\Model; -use Magento\Quote\Model\Quote as QuoteEntity; +use Magento\Authorization\Model\UserContextInterface; use Magento\Framework\Event\ManagerInterface as EventManager; -use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory; -use Magento\Sales\Api\OrderManagementInterface as OrderManagement; +use Magento\Framework\Exception\CouldNotSaveException; +use Magento\Framework\Exception\StateException; +use Magento\Quote\Model\Quote as QuoteEntity; use Magento\Quote\Model\Quote\Address\ToOrder as ToOrderConverter; use Magento\Quote\Model\Quote\Address\ToOrderAddress as ToOrderAddressConverter; use Magento\Quote\Model\Quote\Item\ToOrderItem as ToOrderItemConverter; use Magento\Quote\Model\Quote\Payment\ToOrderPayment as ToOrderPaymentConverter; -use Magento\Authorization\Model\UserContextInterface; -use Magento\Framework\Exception\CouldNotSaveException; -use Magento\Framework\Exception\StateException; +use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory; +use Magento\Sales\Api\OrderManagementInterface as OrderManagement; +use Magento\Store\Model\StoreManagerInterface; /** * Class QuoteManagement @@ -95,6 +96,11 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface */ protected $dataObjectHelper; + /** + * @var StoreManagerInterface + */ + protected $storeManager; + /** * @param EventManager $eventManager * @param QuoteValidator $quoteValidator @@ -110,6 +116,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository * @param \Magento\Customer\Model\CustomerFactory $customerModelFactory * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper + * @param StoreManagerInterface $storeManager * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -126,7 +133,8 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface QuoteRepository $quoteRepository, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Model\CustomerFactory $customerModelFactory, - \Magento\Framework\Api\DataObjectHelper $dataObjectHelper + \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, + StoreManagerInterface $storeManager ) { $this->eventManager = $eventManager; $this->quoteValidator = $quoteValidator; @@ -142,16 +150,32 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface $this->customerRepository = $customerRepository; $this->customerModelFactory = $customerModelFactory; $this->dataObjectHelper = $dataObjectHelper; + $this->storeManager = $storeManager; } /** * {@inheritdoc} */ - public function createEmptyCart($storeId) + public function createEmptyCart() { - $quote = $this->userContext->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER - ? $this->createCustomerCart($storeId) - : $this->createAnonymousCart($storeId); + $storeId = $this->storeManager->getStore()->getStoreId(); + $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); @@ -213,16 +237,17 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface /** * Creates a cart for the currently logged-in customer. * + * @param int $customerId * @param int $storeId * @return \Magento\Quote\Model\Quote Cart object. * @throws CouldNotSaveException The cart could not be created. */ - protected function createCustomerCart($storeId) + protected function createCustomerCart($customerId, $storeId) { - $customer = $this->customerRepository->getById($this->userContext->getUserId()); + $customer = $this->customerRepository->getById($customerId); try { - $this->quoteRepository->getActiveForCustomer($this->userContext->getUserId()); + $this->quoteRepository->getActiveForCustomer($customerId); throw new CouldNotSaveException(__('Cannot create quote')); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { diff --git a/app/code/Magento/Quote/Model/Resource/Quote/QuoteIdMask.php b/app/code/Magento/Quote/Model/Resource/Quote/QuoteIdMask.php new file mode 100644 index 0000000000000000000000000000000000000000..8fa2507073a2e4e9b86d23bc2c953363ecd5703b --- /dev/null +++ b/app/code/Magento/Quote/Model/Resource/Quote/QuoteIdMask.php @@ -0,0 +1,26 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Model\Resource\Quote; + +use Magento\Framework\Model\Resource\Db\AbstractDb; + +/** + * QuoteIdMask Resource model + * @codeCoverageIgnore + */ +class QuoteIdMask extends AbstractDb +{ + /** + * Main table and field initialization + * + * @return void + */ + protected function _construct() + { + $this->_init('quote_id_mask', 'quote_id'); + $this->_isPkAutoIncrement = false; + } +} 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/Setup/InstallSchema.php b/app/code/Magento/Quote/Setup/InstallSchema.php index 7e9947028060c0ec596393146748c4e5907b7789..905cfba56782664a89b0d33b722e9e43828e7319 100644 --- a/app/code/Magento/Quote/Setup/InstallSchema.php +++ b/app/code/Magento/Quote/Setup/InstallSchema.php @@ -1535,6 +1535,35 @@ class InstallSchema implements InstallSchemaInterface ); $installer->getConnection()->createTable($table); + /** + * Create table to store cartId and obscured UUID based cartId mapping + */ + $table = $installer->getConnection()->newTable( + $installer->getTable('quote_id_mask') + )->addColumn( + 'quote_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false], + 'Quote ID' + )->addForeignKey( + 'quote_id', + 'quote_id', + $installer->getTable('quote'), + 'entity_id', + \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE + )->addColumn( + 'masked_id', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 32, + ['nullable' => 'false'], + 'Masked ID' + )->setComment( + 'Quote ID and masked ID mapping' + ); + + $installer->getConnection()->createTable($table); + $installer->endSetup(); } diff --git a/app/code/Magento/Quote/Test/Unit/Model/Cart/Access/CartManagementPluginTest.php b/app/code/Magento/Quote/Test/Unit/Model/Cart/Access/CartManagementPluginTest.php deleted file mode 100644 index 4a900ce7550ec771285259f09d35f2055e6eaeeb..0000000000000000000000000000000000000000 --- a/app/code/Magento/Quote/Test/Unit/Model/Cart/Access/CartManagementPluginTest.php +++ /dev/null @@ -1,63 +0,0 @@ -<?php -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ - -namespace Magento\Quote\Test\Unit\Model\Cart\Access; - -use \Magento\Quote\Model\Cart\Access\CartManagementPlugin; - -class CartManagementPluginTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var \Magento\Quote\Model\Cart\Access\CartManagementPlugin - */ - protected $model; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $userContextMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $subjectMock; - - protected function setUp() - { - $this->userContextMock = $this->getMock('Magento\Authorization\Model\UserContextInterface'); - $this->subjectMock = $this->getMock('\Magento\Quote\Api\CartManagementInterface'); - $this->model = new CartManagementPlugin($this->userContextMock); - } - - /** - * @param int $userType - * @dataProvider successTypeDataProvider - */ - public function testBeforeCreateSuccess($userType) - { - $this->userContextMock->expects($this->once())->method('getUserType')->will($this->returnValue($userType)); - $this->model->beforeAssignCustomer($this->subjectMock, 1, 1, 1); - } - - public function successTypeDataProvider() - { - return [ - 'admin' => [\Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN], - 'integration' => [\Magento\Authorization\Model\UserContextInterface::USER_TYPE_INTEGRATION], - ]; - } - - /** - * @expectedException \Magento\Framework\Exception\AuthorizationException - * @expectedExceptionMessage Access denied - */ - public function testBeforeCreateDenied() - { - $this->userContextMock->expects($this->once())->method('getUserType') - ->will($this->returnValue(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER)); - $this->model->beforeAssignCustomer($this->subjectMock, 1, 1, 1); - } -} diff --git a/app/code/Magento/Quote/Test/Unit/Model/Cart/Access/CartRepositoryPluginTest.php b/app/code/Magento/Quote/Test/Unit/Model/Cart/Access/CartRepositoryPluginTest.php deleted file mode 100644 index d4e5a472c390440024e37095090d777f8b7cf0ba..0000000000000000000000000000000000000000 --- a/app/code/Magento/Quote/Test/Unit/Model/Cart/Access/CartRepositoryPluginTest.php +++ /dev/null @@ -1,90 +0,0 @@ -<?php -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ - -namespace Magento\Quote\Test\Unit\Model\Cart\Access; - -use \Magento\Quote\Model\Cart\Access\CartRepositoryPlugin; - -class CartRepositoryPluginTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var \Magento\Quote\Model\Cart\Access\CartRepositoryPlugin - */ - protected $model; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $userContextMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $subjectMock; - - protected function setUp() - { - $this->userContextMock = $this->getMock('Magento\Authorization\Model\UserContextInterface'); - $this->subjectMock = $this->getMock('\Magento\Quote\Api\CartRepositoryInterface'); - $this->model = new CartRepositoryPlugin($this->userContextMock); - } - - /** - * @param int $userType - * @dataProvider successTypeDataProvider - */ - public function testBeforeGetSuccess($userType) - { - $this->userContextMock->expects($this->once())->method('getUserType')->will($this->returnValue($userType)); - $this->model->beforeGet($this->subjectMock, 1); - } - - /** - * @expectedException \Magento\Framework\Exception\AuthorizationException - * @expectedExceptionMessage Access denied - */ - public function testBeforeGetCartDenied() - { - $this->userContextMock->expects($this->once())->method('getUserType') - ->will($this->returnValue(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER)); - $this->model->beforeGet($this->subjectMock, 1); - } - - public function successTypeDataProvider() - { - return [ - 'admin' => [\Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN], - 'integration' => [\Magento\Authorization\Model\UserContextInterface::USER_TYPE_INTEGRATION], - ]; - } - - /** - * @param int $userType - * @dataProvider successTypeDataProvider - */ - public function testBeforeGetCartSuccess($userType) - { - $this->userContextMock->expects($this->once())->method('getUserType')->will($this->returnValue($userType)); - $this->model->beforeGetList( - $this->subjectMock, - $this->getMock('\Magento\Framework\Api\SearchCriteria', [], [], '', false) - ); - } - - /** - * @expectedException \Magento\Framework\Exception\AuthorizationException - * @expectedExceptionMessage Access denied - */ - public function testBeforeGetListDenied() - { - $this->userContextMock->expects($this->once())->method('getUserType') - ->will($this->returnValue(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER)); - $this->model->beforeGetList( - $this->subjectMock, - $this->getMock('\Magento\Framework\Api\SearchCriteria', [], [], '', false) - ); - } -} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestBillingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestBillingAddressManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4f6a3e63a632df58afc6eadeb44871820866100c --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestBillingAddressManagementTest.php @@ -0,0 +1,97 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestBillingAddressManagementTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Model\GuestCart\GuestBillingAddressManagement + */ + protected $model; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $billingAddressManagementMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $addressMock; + + /** + * @var string + */ + protected $maskedCartId; + + /** + * @var int + */ + protected $cartId; + + /** + * @return void + */ + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->addressMock = $this->getMock('\Magento\Quote\Model\Quote\Address', [], [], '', false); + $this->billingAddressManagementMock = $this->getMock( + 'Magento\Quote\Api\BillingAddressManagementInterface', + [], + [], + '', + false + ); + + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 123; + + $guestCartTestHelper = new GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->model = $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestBillingAddressManagement', + [ + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock, + 'billingAddressManagement' => $this->billingAddressManagementMock + ] + ); + } + + /** + * @return void + */ + public function testGet() + { + $this->billingAddressManagementMock->expects($this->once())->method('get')->willReturn($this->addressMock); + $this->assertEquals($this->addressMock, $this->model->get($this->maskedCartId)); + } + + /** + * @return void + */ + public function testAssign() + { + $addressId = 1; + $this->billingAddressManagementMock->expects($this->once())->method('assign')->willReturn($addressId); + $this->assertEquals($addressId, $this->model->assign($this->maskedCartId, $this->addressMock)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartItemRepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartItemRepositoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9535e3c1c7455e063d9fa7770e48e15616ede6a0 --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartItemRepositoryTest.php @@ -0,0 +1,133 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestCartItemRepositoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Model\GuestCart\GuestCartItemRepository + */ + protected $guestCartItemRepository; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $cartItemRepositoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteItemMock; + + /** + * @var string + */ + protected $maskedCartId; + + /** + * @var string + */ + protected $cartId; + + /** + * @return void + */ + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 33; + + /** + * @var \Magento\Quote\Test\Unit\Model\GuestCart\GuestCartTestHelper + */ + $guestCartTestHelper = new \Magento\Quote\Test\Unit\Model\GuestCart\GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = + $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->quoteIdMaskMock->expects($this->any()) + ->method('getMaskedId') + ->willReturn($this->maskedCartId); + + $this->quoteItemMock = $this->getMock('\Magento\Quote\Model\Quote\Item', [], [], '', false); + $this->quoteItemMock->expects($this->any()) + ->method('getItemId') + ->willReturn($this->maskedCartId); + $this->quoteItemMock->expects($this->any()) + ->method('getQuoteId') + ->willReturn($this->maskedCartId); + $this->quoteItemMock->expects($this->any()) + ->method('setQuoteId') + ->with($this->cartId); + + $this->cartItemRepositoryMock = $this->getMock('\Magento\Quote\Model\Quote\Item\Repository', [], [], '', false); + $this->guestCartItemRepository = + $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestCartItemRepository', + [ + 'repository' => $this->cartItemRepositoryMock, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock, + ] + ); + } + + /** + * @return void + */ + public function testSave() + { + $expectedValue = 'expected value'; + $this->cartItemRepositoryMock->expects($this->once()) + ->method('save') + ->willReturn($expectedValue); + $this->assertEquals($expectedValue, $this->guestCartItemRepository->save($this->quoteItemMock)); + } + + /** + * @return void + */ + public function testGetList() + { + $itemMock = $this->getMock('\Magento\Quote\Model\Quote\Item', [], [], '', false); + $itemMock->expects($this->any()) + ->method('setQuoteId') + ->with($this->maskedCartId); + $this->cartItemRepositoryMock->expects($this->once()) + ->method('getList') + ->with($this->cartId) + ->will($this->returnValue([$itemMock])); + $this->assertEquals([$itemMock], $this->guestCartItemRepository->getList($this->maskedCartId)); + } + + /** + * @return void + */ + public function testDeleteById() + { + $itemId = 5; + $this->cartItemRepositoryMock->expects($this->once()) + ->method('deleteById') + ->with($this->cartId, $itemId) + ->willReturn(true); + $this->assertTrue($this->guestCartItemRepository->deleteById($this->maskedCartId, $itemId)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..444aa62322a04a9a039b274755c0e8ab863a2ddc --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartManagementTest.php @@ -0,0 +1,116 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestCartManagementTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteManagementMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteRepositoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var \Magento\Quote\Model\GuestCart\GuestCartManagement + */ + protected $guestCartManagement; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->quoteManagementMock = $this->getMockForAbstractClass( + 'Magento\Quote\Api\CartManagementInterface', + [], + '', + false, + true, + true, + [] + ); + $this->quoteIdMaskFactoryMock = $this->getMock( + 'Magento\Quote\Model\QuoteIdMaskFactory', + ['create'], + [], + '', + false + ); + $this->quoteIdMaskMock = $this->getMock( + 'Magento\Quote\Model\QuoteIdMask', + ['getId', 'getMaskedId', 'load', 'save', 'setId'], + [], + '', + false + ); + + $this->guestCartManagement = $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestCartManagement', + [ + 'quoteManagement' => $this->quoteManagementMock, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock + ] + ); + } + + public function testCreateEmptyCart() + { + $maskedCartId = 'masked1cart2id3'; + $cartId = 1; + + $this->quoteIdMaskMock->expects($this->once())->method('setId')->with($cartId)->willReturnSelf(); + $this->quoteIdMaskMock->expects($this->once())->method('save')->willReturnSelf(); + $this->quoteIdMaskMock->expects($this->once())->method('getMaskedId')->willreturn($maskedCartId); + $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock); + $this->quoteManagementMock->expects($this->once())->method('createEmptyCart')->willReturn($cartId); + + $this->assertEquals($maskedCartId, $this->guestCartManagement->createEmptyCart()); + } + + public function testAssignCustomer() + { + $maskedCartId = 'masked1cart2id3'; + $cartId = 1; + $customerId = 1; + $storeId = 1; + + $this->quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf(); + $this->quoteIdMaskMock->expects($this->once())->method('getId')->willReturn($maskedCartId); + $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock); + $this->quoteManagementMock->expects($this->once())->method('assignCustomer')->willReturn(true); + + $this->assertEquals(true, $this->guestCartManagement->assignCustomer($cartId, $customerId, $storeId)); + } + + public function testPlaceOrder() + { + $maskedCartId = 'masked1cart2id3'; + $cartId = 1; + $orderId = 1; + + $this->quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf(); + $this->quoteIdMaskMock->expects($this->once())->method('getId')->willReturn($maskedCartId); + $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock); + $this->quoteManagementMock->expects($this->once())->method('placeOrder')->willReturn($orderId); + + $this->assertEquals($orderId, $this->guestCartManagement->placeOrder($cartId)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartRepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartRepositoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3a7e2fac3ae37a9fb1d30bdceb39a88616c40ac6 --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartRepositoryTest.php @@ -0,0 +1,75 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestCartRepositoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Model\GuestCart\GuestCartRepository + */ + protected $model; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteRepositoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var string + */ + protected $maskedCartId; + + /** + * @var int + */ + protected $cartId; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->quoteRepositoryMock = $this->getMock('Magento\Quote\Api\CartRepositoryInterface', [], [], '', false); + $this->quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false); + + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 123; + + $guestCartTestHelper = new GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->model = $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestCartRepository', + [ + 'quoteRepository' => $this->quoteRepositoryMock, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock + ] + ); + } + + public function testGet() + { + $this->quoteRepositoryMock->expects($this->once())->method('get')->willReturn($this->quoteMock); + $this->assertEquals($this->quoteMock, $this->model->get($this->maskedCartId)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartTestHelper.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartTestHelper.php new file mode 100644 index 0000000000000000000000000000000000000000..25649077b5fd6c9bbe8a478b4987947d59eda4cc --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartTestHelper.php @@ -0,0 +1,54 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +/** + * Class GuestCartTestHelper + * + */ +class GuestCartTestHelper +{ + /** + * @var \PHPUnit_Framework_TestCase + */ + protected $testCase; + + /** + * Initialize helper + * + * @param \PHPUnit_Framework_TestCase $testCase + */ + public function __construct(\PHPUnit_Framework_TestCase $testCase) + { + $this->testCase = $testCase; + } + + /** + * Return mocks with expected invokes + * + * First element is quoteIdMaskFactoryMock, second one is quoteIdMaskMock + * + * @param $maskedCartId + * @param $cartId + * @return array + */ + public function mockQuoteIdMask($maskedCartId, $cartId) + { + $quoteIdMaskMock = $this->testCase->getMock( + 'Magento\Quote\Model\QuoteIdMask', + ['load', 'getId', 'getMaskedId'], + [], + '', + false + ); + $quoteIdMaskFactoryMock = $this->testCase->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false); + $quoteIdMaskFactoryMock->expects($this->testCase->once())->method('create')->willReturn($quoteIdMaskMock); + $quoteIdMaskMock->expects($this->testCase->once())->method('load')->with($maskedCartId)->willReturnSelf(); + $quoteIdMaskMock->expects($this->testCase->once())->method('getId')->willReturn($cartId); + return [$quoteIdMaskFactoryMock, $quoteIdMaskMock]; + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartTotalRepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartTotalRepositoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a8ef8d94e9fa2f92b324d81931ed87c7cd647e0b --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartTotalRepositoryTest.php @@ -0,0 +1,82 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestCartTotalRepositoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Model\GuestCart\GuestCartTotalRepository + */ + protected $model; + + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + protected $objectManager; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $cartTotalRepository; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var string + */ + protected $maskedCartId; + + /** + * @var int + */ + protected $cartId; + + public function setUp() + { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->cartTotalRepository = $this->getMockBuilder('Magento\Quote\Api\CartTotalRepositoryInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 123; + + $guestCartTestHelper = new GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->model = $this->objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestCartTotalRepository', + [ + 'cartTotalRepository' => $this->cartTotalRepository, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock, + ] + ); + } + + public function testGetTotals() + { + $retValue = 'retValue'; + + $this->cartTotalRepository->expects($this->once()) + ->method('get') + ->with($this->cartId) + ->will($this->returnValue($retValue)); + $this->assertSame($retValue, $this->model->get($this->maskedCartId)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCouponManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCouponManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..74b00ed942f4a1ba2d8e05e4ab678a9ad05c7d3d --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCouponManagementTest.php @@ -0,0 +1,87 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestCouponManagementTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Model\GuestCart\GuestCouponManagement + */ + protected $model; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $couponManagementMock; + + /** + * @var string + */ + protected $maskedCartId; + + /** + * @var int + */ + protected $cartId; + + /** + * @var string + */ + protected $couponCode; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->couponManagementMock = $this->getMock('Magento\Quote\Api\CouponManagementInterface', [], [], '', false); + + $this->couponCode = 'test_coupon_code'; + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 123; + + $guestCartTestHelper = new GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->model = $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestCouponManagement', + [ + 'couponManagement' => $this->couponManagementMock, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock + ] + ); + } + + public function testGet() + { + $this->couponManagementMock->expects($this->once())->method('get')->willReturn($this->couponCode); + $this->assertEquals($this->couponCode, $this->model->get($this->maskedCartId)); + } + + public function testSet() + { + $this->couponManagementMock->expects($this->once())->method('set')->willReturn(true); + $this->assertTrue($this->model->set($this->maskedCartId, $this->couponCode)); + } + + public function testRemove() + { + $this->couponManagementMock->expects($this->once())->method('remove')->willReturn(true); + $this->assertTrue($this->model->remove($this->maskedCartId)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestPaymentMethodManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestPaymentMethodManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..ebde097f89bd4c551b920d69fac12fc3b4543497 --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestPaymentMethodManagementTest.php @@ -0,0 +1,94 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestPaymentMethodManagementTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Model\GuestCart\GuestPaymentMethodManagement + */ + protected $model; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $paymentMethodManagementMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $paymentMock; + + /** + * @var string + */ + protected $maskedCartId; + + /** + * @var int + */ + protected $cartId; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->paymentMethodManagementMock = $this->getMock( + 'Magento\Quote\Api\PaymentMethodManagementInterface', + [], + [], + '', + false + ); + $this->paymentMock = $this->getMock('Magento\Quote\Model\Quote\Payment', [], [], '', false); + + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 11; + + $guestCartTestHelper = new GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->model = $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestPaymentMethodManagement', + [ + 'paymentMethodManagement' => $this->paymentMethodManagementMock, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock + ] + ); + } + + public function testGet() + { + $this->paymentMethodManagementMock->expects($this->once())->method('get')->willReturn($this->paymentMock); + $this->assertEquals($this->paymentMock, $this->model->get($this->maskedCartId)); + } + + public function testGetList() + { + $paymentMethod = $this->getMock('Magento\Quote\Api\Data\PaymentMethodInterface', [], [], '', false); + $this->paymentMethodManagementMock->expects($this->once())->method('getList')->willReturn([$paymentMethod]); + $this->assertEquals([$paymentMethod], $this->model->getList($this->maskedCartId)); + } + + public function testSetSimpleProduct() + { + $paymentId = 20; + $this->paymentMethodManagementMock->expects($this->once())->method('set')->willReturn($paymentId); + $this->assertEquals($paymentId, $this->model->set($this->maskedCartId, $this->paymentMock)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestShippingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestShippingAddressManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..67e35a93bf73c07409993caf9926d5fc77232301 --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestShippingAddressManagementTest.php @@ -0,0 +1,91 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestShippingAddressManagementTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Api\GuestShippingAddressManagementInterface + */ + protected $model; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteAddressMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $quoteIdMaskMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $shippingAddressManagementMock; + + /** + * @var string + */ + protected $maskedCartId; + + /** + * @var int + */ + protected $cartId; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->shippingAddressManagementMock = $this->getMock( + 'Magento\Quote\Api\ShippingAddressManagementInterface', + [], + [], + '', + false + ); + $this->quoteAddressMock = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false); + + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 123; + + $guestCartTestHelper = new GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->model = $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestShippingAddressManagement', + [ + 'shippingAddressManagement' => $this->shippingAddressManagementMock, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock + ] + ); + } + + public function testAssign() + { + $addressId = 1; + $this->shippingAddressManagementMock->expects($this->once())->method('assign')->willReturn($addressId); + $this->assertEquals($addressId, $this->model->assign($this->maskedCartId, $this->quoteAddressMock)); + } + + public function testGet() + { + $this->shippingAddressManagementMock->expects($this->once())->method('get')->willReturn( + $this->quoteAddressMock + ); + $this->assertEquals($this->quoteAddressMock, $this->model->get($this->maskedCartId)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestShippingMethodManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestShippingMethodManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c91510a165fac2c1ecbacf3400cfba4b6f6d5be1 --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestShippingMethodManagementTest.php @@ -0,0 +1,103 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Test\Unit\Model\GuestCart; + +class GuestShippingMethodManagementTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Api\GuestShippingMethodManagementInterface + */ + private $model; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $shippingMethodManagementMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $quoteIdMaskFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $quoteIdMaskMock; + + /** + * @var string + */ + private $maskedCartId; + + /** + * @var string + */ + private $cartId; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->shippingMethodManagementMock = + $this->getMockBuilder('Magento\Quote\Api\ShippingMethodManagementInterface') + ->getMockForAbstractClass(); + + $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73'; + $this->cartId = 867; + + $guestCartTestHelper = new GuestCartTestHelper($this); + list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask( + $this->maskedCartId, + $this->cartId + ); + + $this->model = $objectManager->getObject( + 'Magento\Quote\Model\GuestCart\GuestShippingMethodManagement', + [ + 'shippingMethodManagement' => $this->shippingMethodManagementMock, + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock, + ] + ); + } + + public function testSet() + { + $carrierCode = 'carrierCode'; + $methodCode = 'methodCode'; + + $retValue = 'retValue'; + $this->shippingMethodManagementMock->expects($this->once()) + ->method('set') + ->with($this->cartId, $carrierCode, $methodCode) + ->will($this->returnValue($retValue)); + + $this->assertEquals($retValue, $this->model->set($this->maskedCartId, $carrierCode, $methodCode)); + } + + public function testGetList() + { + $retValue = 'retValue'; + $this->shippingMethodManagementMock->expects($this->once()) + ->method('getList') + ->with($this->cartId) + ->will($this->returnValue($retValue)); + + $this->assertEquals($retValue, $this->model->getList($this->maskedCartId)); + } + + public function testGet() + { + $retValue = 'retValue'; + $this->shippingMethodManagementMock->expects($this->once()) + ->method('get') + ->with($this->cartId) + ->will($this->returnValue($retValue)); + + $this->assertEquals($retValue, $this->model->get($this->maskedCartId)); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php index b90d93933827cbe676a413b79cbc4ed2e0d615b4..9081537f2790e8389100a5e3c4549e3578cc0222 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php @@ -7,7 +7,7 @@ namespace Magento\Quote\Test\Unit\Model\Quote\Item; -use \Magento\Quote\Model\Quote\Item\Repository; +use Magento\Quote\Model\Quote\Item\Repository; class RepositoryTest extends \PHPUnit_Framework_TestCase { @@ -160,6 +160,38 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $this->assertEquals($this->quoteItemMock, $this->repository->save($this->dataMock)); } + /** + * @return void + */ + public function testSaveForCustomer() + { + $customerId = 1; + $cartId = 13; + $this->quoteRepositoryMock->expects($this->once())->method('getActiveForCustomer') + ->with($customerId) + ->will($this->returnValue($this->quoteMock)); + $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId); + $this->dataMock->expects($this->once())->method('getQty')->will($this->returnValue(12)); + $this->dataMock->expects($this->once())->method('getQuoteId')->willReturn($cartId); + $this->quoteRepositoryMock->expects($this->once()) + ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); + $this->productRepositoryMock->expects($this->once()) + ->method('get') + ->will($this->returnValue($this->productMock)); + $this->dataMock->expects($this->once())->method('getSku'); + $this->quoteMock->expects($this->once())->method('addProduct')->with($this->productMock, 12); + $this->quoteMock->expects($this->never())->method('getItemById'); + $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); + $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); + $this->quoteMock + ->expects($this->once()) + ->method('getItemByProduct') + ->with($this->productMock) + ->will($this->returnValue($this->quoteItemMock)); + $this->dataMock->expects($this->once())->method('getItemId')->will($this->returnValue(null)); + $this->assertEquals($this->quoteItemMock, $this->repository->saveForCustomer($customerId, $this->dataMock)); + } + /** * @return void * @expectedException \Magento\Framework\Exception\NoSuchEntityException @@ -260,15 +292,13 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase { $cartId = 11; $itemId = 5; - $this->dataMock->expects($this->once())->method('getQuoteId')->willReturn($cartId); - $this->dataMock->expects($this->once())->method('getItemId')->willReturn($itemId); $this->quoteRepositoryMock->expects($this->once()) ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); $this->quoteMock->expects($this->once()) ->method('getItemById')->with($itemId)->will($this->returnValue(false)); $this->quoteMock->expects($this->never())->method('removeItem'); - $this->repository->delete($this->dataMock); + $this->repository->deleteById($cartId, $itemId); } /** @@ -280,8 +310,6 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase { $cartId = 11; $itemId = 5; - $this->dataMock->expects($this->once())->method('getQuoteId')->willReturn($cartId); - $this->dataMock->expects($this->once())->method('getItemId')->willReturn($itemId); $this->quoteRepositoryMock->expects($this->once()) ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); $this->quoteMock->expects($this->once()) @@ -296,18 +324,21 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase ->with($this->quoteMock) ->willThrowException($exception); - $this->repository->delete($this->dataMock); + $this->repository->deleteById($cartId, $itemId); } /** * @return void */ - public function testDelete() + public function testDeleteByIdForCustomer() { + $customerId = 1; $cartId = 11; $itemId = 5; - $this->dataMock->expects($this->once())->method('getQuoteId')->willReturn($cartId); - $this->dataMock->expects($this->once())->method('getItemId')->willReturn($itemId); + $this->quoteRepositoryMock->expects($this->once())->method('getActiveForCustomer') + ->with($customerId) + ->will($this->returnValue($this->quoteMock)); + $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId); $this->quoteRepositoryMock->expects($this->once()) ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); $this->quoteMock->expects($this->once()) @@ -316,7 +347,7 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); - $this->repository->delete($this->dataMock); + $this->assertTrue($this->repository->deleteByIdForCustomer($customerId, $itemId)); } /** @@ -334,6 +365,27 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $this->assertEquals([$itemMock], $this->repository->getList(33)); } + /** + * @return void + */ + public function testGetListForCustomer() + { + $cartId = 1; + $customerId = 33; + $quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false); + $this->quoteRepositoryMock->expects($this->once())->method('getActiveForCustomer') + ->with($customerId) + ->will($this->returnValue($quoteMock)); + $quoteMock->expects($this->once())->method('getId')->willReturn($cartId); + $this->quoteRepositoryMock->expects($this->once())->method('getActive') + ->with($cartId) + ->will($this->returnValue($quoteMock)); + $itemMock = $this->getMock('\Magento\Quote\Model\Quote\Item', [], [], '', false); + $quoteMock->expects($this->any())->method('getAllItems')->will($this->returnValue([$itemMock])); + + $this->assertEquals([$itemMock], $this->repository->getListForCustomer($customerId)); + } + /** * @return void */ @@ -341,13 +393,6 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase { $cartId = 11; $itemId = 5; - $this->itemDataFactoryMock->expects($this->once())->method('create')->willReturn($this->dataMock); - $this->dataMock->expects($this->once())->method('setQuoteId') - ->with($cartId)->willReturn($this->dataMock); - $this->dataMock->expects($this->once())->method('setItemId') - ->with($itemId)->willReturn($this->dataMock); - $this->dataMock->expects($this->once())->method('getQuoteId')->willReturn($cartId); - $this->dataMock->expects($this->once())->method('getItemId')->willReturn($itemId); $this->quoteRepositoryMock->expects($this->once()) ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); $this->quoteMock->expects($this->once()) diff --git a/app/code/Magento/Quote/Test/Unit/Model/QuoteIdMaskTest.php b/app/code/Magento/Quote/Test/Unit/Model/QuoteIdMaskTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9bd03b6bea45e5fc97ad1eac52d1285c458bd2b0 --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/QuoteIdMaskTest.php @@ -0,0 +1,35 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Test\Unit\Model; + +use Magento\Framework\Math\Random; + +/** + * Unit test for \Magento\Quote\Model\QuoteIdMask + */ +class QuoteIdMaskTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Quote\Model\QuoteIdMask + */ + protected $quoteIdMask; + + protected function setUp() + { + $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->quoteIdMask = $helper->getObject( + 'Magento\Quote\Model\QuoteIdMask', + ['randomDataGenerator' => new Random()] + ); + } + + public function testBeforeSave() + { + $this->quoteIdMask->beforeSave(); + $this->assertNotNull($this->quoteIdMask->getMaskedId(), 'Masked identifier is not generated.'); + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php index 522734a324ad98bc080654f4ceaee3e3a6da295e..12c4248d82c81175ac6d23d31f714611e1f08495 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php @@ -86,6 +86,11 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase */ protected $customerFactoryMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $storeManagerMock; + protected function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); @@ -140,6 +145,15 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase '', false ); + $this->storeManagerMock = $this->getMockForAbstractClass( + 'Magento\Store\Model\StoreManagerInterface', + [], + '', + false, + true, + true, + ['getStore', 'getStoreId'] + ); $dataObjectHelper = $this->getMock('\Magento\Framework\Api\DataObjectHelper', [], [], '', false); @@ -160,6 +174,7 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase 'customerRepository' => $this->customerRepositoryMock, 'customerModelFactory' => $this->customerFactoryMock, 'dataObjectHelper' => $dataObjectHelper, + 'storeManager' => $this->storeManagerMock ] ); } @@ -171,20 +186,19 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false); - $this->userContextMock->expects($this->once())->method('getUserType') - ->willReturn(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_GUEST); - $this->quoteRepositoryMock->expects($this->once())->method('create')->willReturn($quoteMock); $quoteMock->expects($this->any())->method('setStoreId')->with($storeId); - $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock); $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId); - $this->assertEquals($quoteId, $this->model->createEmptyCart($storeId)); + $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf(); + $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $this->assertEquals($quoteId, $this->model->createEmptyCart()); } - public function testCreateEmptyCartLoggedInUser() + public function testCreateEmptyCartForCustomer() { $storeId = 345; $quoteId = 2311; @@ -192,18 +206,6 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false); - $this->userContextMock->expects($this->once())->method('getUserType') - ->willReturn(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER); - - $this->userContextMock->expects($this->atLeastOnce())->method('getUserId')->willReturn($userId); - - $customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false); - $this->customerRepositoryMock - ->expects($this->once()) - ->method('getById') - ->with($userId) - ->willReturn($customerMock); - $this->quoteRepositoryMock ->expects($this->once()) ->method('getActiveForCustomer') @@ -212,48 +214,39 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase $this->quoteRepositoryMock->expects($this->once())->method('create')->willReturn($quoteMock); $quoteMock->expects($this->any())->method('setStoreId')->with($storeId); - $quoteMock->expects($this->any())->method('setCustomer')->with($customerMock); $quoteMock->expects($this->any())->method('setCustomerIsGuest')->with(0); - $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock); $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId); - $this->assertEquals($quoteId, $this->model->createEmptyCart($storeId)); + $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf(); + $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $this->assertEquals($quoteId, $this->model->createEmptyCartForCustomer($userId)); } /** * @expectedException \Magento\Framework\Exception\CouldNotSaveException */ - public function testCreateEmptyCartLoggedInUserException() + public function testCreateEmptyCartForCustomerException() { $storeId = 345; $userId = 567; $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false); - $this->userContextMock->expects($this->once())->method('getUserType') - ->willReturn(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER); - - $this->userContextMock->expects($this->atLeastOnce())->method('getUserId')->willReturn($userId); - - $customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false); - $this->customerRepositoryMock - ->expects($this->once()) - ->method('getById') - ->with($userId) - ->willReturn($customerMock); - $this->quoteRepositoryMock ->expects($this->once()) ->method('getActiveForCustomer') ->with($userId); $this->quoteRepositoryMock->expects($this->never())->method('create')->willReturn($quoteMock); - $this->quoteRepositoryMock->expects($this->never())->method('save')->with($quoteMock); - $this->model->createEmptyCart($storeId); + $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf(); + $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $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/acl.xml b/app/code/Magento/Quote/etc/acl.xml new file mode 100644 index 0000000000000000000000000000000000000000..14792adfa7170110bfc1da6dd6c6c6c347686f87 --- /dev/null +++ b/app/code/Magento/Quote/etc/acl.xml @@ -0,0 +1,18 @@ +<?xml version="1.0"?> +<!-- +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +--> +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Acl/etc/acl.xsd"> + <acl> + <resources> + <resource id="Magento_Backend::admin"> + <resource id="Magento_Cart::cart" title="Carts" sortOrder="40"> + <resource id="Magento_Cart::manage" title="Manage carts" sortOrder="10" /> + </resource> + </resource> + </resources> + </acl> +</config> diff --git a/app/code/Magento/Quote/etc/di.xml b/app/code/Magento/Quote/etc/di.xml index 214e414e3207d0c9a2438f9f68d3ac35456f581e..4eeb1ff2b61a7e865d24c28af7411c599b7c3f05 100644 --- a/app/code/Magento/Quote/etc/di.xml +++ b/app/code/Magento/Quote/etc/di.xml @@ -25,4 +25,22 @@ <preference for="Magento\Quote\Api\Data\TotalsInterface" type="\Magento\Quote\Model\Cart\Totals" /> <preference for="Magento\Quote\Api\Data\TotalsItemInterface" type="\Magento\Quote\Model\Quote\Cart\Totals\Item" /> <preference for="Magento\Quote\Api\Data\CurrencyInterface" type="\Magento\Quote\Model\Cart\Currency" /> + + <preference for="Magento\Quote\Api\GuestCartManagementInterface" type="Magento\Quote\Model\GuestCart\GuestCartManagement" /> + <preference for="Magento\Quote\Api\GuestCartRepositoryInterface" type="Magento\Quote\Model\GuestCart\GuestCartRepository" /> + <preference for="Magento\Quote\Api\GuestCartItemRepositoryInterface" type="Magento\Quote\Model\GuestCart\GuestCartItemRepository" /> + <preference for="Magento\Quote\Api\GuestCouponManagementInterface" type="Magento\Quote\Model\GuestCart\GuestCouponManagement" /> + <preference for="Magento\Quote\Api\GuestPaymentMethodManagementInterface" type="Magento\Quote\Model\GuestCart\GuestPaymentMethodManagement" /> + <preference for="Magento\Quote\Api\GuestCartTotalRepositoryInterface" type="Magento\Quote\Model\GuestCart\GuestCartTotalRepository" /> + <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 412dab6361b1f7ef5e24168ebcb9b1b980a32c48..9540a2d3b88aa9d72923608322fec5834eb2d382 100644 --- a/app/code/Magento/Quote/etc/webapi.xml +++ b/app/code/Magento/Quote/etc/webapi.xml @@ -7,34 +7,101 @@ --> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd"> + + <!-- Managing Cart --> <route url="/V1/carts/:cartId" method="GET"> <service class="Magento\Quote\Api\CartRepositoryInterface" method="get"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts" method="GET"> <service class="Magento\Quote\Api\CartRepositoryInterface" method="getList"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <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"> <service class="Magento\Quote\Api\CartManagementInterface" method="assignCustomer"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> + + <!-- Managing my Cart --> + <route url="/V1/carts/mine" method="POST"> + <service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCartForCustomer"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="customerId" force="true">%customer_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine" method="GET"> + <service class="Magento\Quote\Api\CartManagementInterface" method="getCartForCustomer"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <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 --> + + <route url="/V1/guest-carts/:cartId" method="GET"> + <service class="Magento\Quote\Api\GuestCartRepositoryInterface" method="get"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + <!-- No getList for anonymous guest cart users --> + <route url="/V1/guest-carts" method="POST"> + <service class="Magento\Quote\Api\GuestCartManagementInterface" method="createEmptyCart"/> <resources> <resource ref="anonymous" /> </resources> </route> + <route url="/V1/guest-carts/:cartId" method="PUT"> + <service class="Magento\Quote\Api\GuestCartManagementInterface" method="assignCustomer"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + <route url="/V1/guest-carts/:cartId/order" method="PUT"> + <service class="Magento\Quote\Api\GuestCartManagementInterface" method="placeOrder"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + + <!-- Managing Cart Shipment Method --> <route url="/V1/carts/:cartId/selected-shipping-method" method="PUT"> <service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="set"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/:cartId/selected-shipping-method" method="GET"> @@ -45,14 +112,65 @@ </route> <route url="/V1/carts/:cartId/shipping-methods" method="GET"> <service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="getList"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </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"/> <resources> <resource ref="anonymous" /> </resources> </route> + <route url="/V1/guest-carts/:cartId/selected-shipping-method" method="GET"> + <service class="Magento\Quote\Api\GuestShippingMethodManagementInterface" method="get"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + <route url="/V1/guest-carts/:cartId/shipping-methods" method="GET"> + <service class="Magento\Quote\Api\GuestShippingMethodManagementInterface" method="getList"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + + <!-- Managing Cart Items --> <route url="/V1/carts/:cartId/items" method="GET"> <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="getList"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/items" method="POST"> @@ -64,91 +182,346 @@ <route url="/V1/carts/items/:itemId" method="PUT"> <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/:cartId/items/:itemId" method="DELETE"> <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="deleteById"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> + + <!-- Managing Guest Cart Items --> + <route url="/V1/guest-carts/:cartId/items" method="GET"> + <service class="Magento\Quote\Api\GuestCartItemRepositoryInterface" method="getList"/> <resources> <resource ref="anonymous" /> </resources> </route> - <route url="/V1/carts/:cartId/selected-payment-methods" method="GET"> - <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="get"/> + <route url="/V1/guest-carts/items" method="POST"> + <service class="Magento\Quote\Api\GuestCartItemRepositoryInterface" method="save"/> <resources> <resource ref="anonymous" /> </resources> </route> - <route url="/V1/carts/:cartId/selected-payment-methods" method="PUT"> - <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="set"/> + <route url="/V1/guest-carts/items/:itemId" method="PUT"> + <service class="Magento\Quote\Api\GuestCartItemRepositoryInterface" method="save"/> <resources> <resource ref="anonymous" /> </resources> </route> + <route url="/V1/guest-carts/:cartId/items/:itemId" method="DELETE"> + <service class="Magento\Quote\Api\GuestCartItemRepositoryInterface" method="deleteById"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + + <!-- Managing my Cart Items --> + <route url="/V1/carts/mine/items" method="GET"> + <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="getList"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="customerId" force="true">%customer_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/items" method="POST"> + <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="customerId" force="true">%customer_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/items/:itemId" method="PUT"> + <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="customerId" force="true">%customer_id%</parameter> + </data> + </route> + <route url="/V1/carts/mine/items/:itemId" method="DELETE"> + <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="deleteById"/> + <resources> + <resource ref="self" /> + </resources> + <data> + <parameter name="customerId" force="true">%customer_id%</parameter> + </data> + </route> + + <!-- Managing Cart Payment --> + <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-method" method="PUT"> + <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="set"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> <route url="/V1/carts/:cartId/payment-methods" method="GET"> <service class="Magento\Quote\Api\PaymentMethodManagementInterface" method="getList"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> + + <!-- Managing Guest Cart Payment --> + <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-method" method="PUT"> + <service class="Magento\Quote\Api\GuestPaymentMethodManagementInterface" method="set"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + <route url="/V1/guest-carts/:cartId/payment-methods" method="GET"> + <service class="Magento\Quote\Api\GuestPaymentMethodManagementInterface" method="getList"/> <resources> <resource ref="anonymous" /> </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"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/:cartId/billing-address" method="POST"> <service class="Magento\Quote\Api\BillingAddressManagementInterface" method="assign"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> + + <!-- Managing Guest Cart Billing address --> + <route url="/V1/guest-carts/:cartId/billing-address" method="GET"> + <service class="Magento\Quote\Api\GuestBillingAddressManagementInterface" method="get"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + <route url="/V1/guest-carts/:cartId/billing-address" method="POST"> + <service class="Magento\Quote\Api\GuestBillingAddressManagementInterface" method="assign"/> <resources> <resource ref="anonymous" /> </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"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/:cartId/coupons/:couponCode" method="PUT"> <service class="Magento\Quote\Api\CouponManagementInterface" method="set"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/:cartId/coupons" method="DELETE"> <service class="Magento\Quote\Api\CouponManagementInterface" method="remove"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> + + <!-- Managing Guest Cart Coupons --> + <route url="/V1/guest-carts/:cartId/coupons" method="GET"> + <service class="Magento\Quote\Api\GuestCouponManagementInterface" method="get"/> <resources> <resource ref="anonymous" /> </resources> </route> + <route url="/V1/guest-carts/:cartId/coupons/:couponCode" method="PUT"> + <service class="Magento\Quote\Api\GuestCouponManagementInterface" method="set"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + <route url="/V1/guest-carts/:cartId/coupons" method="DELETE"> + <service class="Magento\Quote\Api\GuestCouponManagementInterface" method="remove"/> + <resources> + <resource ref="anonymous" /> + </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"/> <resources> - <resource ref="anonymous" /> + <resource ref="Magento_Cart::manage" /> </resources> </route> <route url="/V1/carts/:cartId/shipping-address" method="POST"> <service class="Magento\Quote\Api\ShippingAddressManagementInterface" method="assign"/> + <resources> + <resource ref="Magento_Cart::manage" /> + </resources> + </route> + + <!-- Managing Guest Cart Shipping address --> + <route url="/V1/guest-carts/:cartId/shipping-address" method="GET"> + <service class="Magento\Quote\Api\GuestShippingAddressManagementInterface" method="get"/> + <resources> + <resource ref="anonymous" /> + </resources> + </route> + <route url="/V1/guest-carts/:cartId/shipping-address" method="POST"> + <service class="Magento\Quote\Api\GuestShippingAddressManagementInterface" method="assign"/> <resources> <resource ref="anonymous" /> </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"/> <resources> - <resource ref="anonymous" /> + <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> + <resource ref="Magento_Cart::manage" /> + </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> - <route url="/V1/customer/:customerId/cart" method="GET"> - <service class="Magento\Quote\Api\CartManagementInterface" method="getCartForCustomer"/> + + <!-- 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/Quote/etc/webapi_rest/di.xml b/app/code/Magento/Quote/etc/webapi_rest/di.xml deleted file mode 100644 index 67774a740401df953616e1fedae05e822e81d32e..0000000000000000000000000000000000000000 --- a/app/code/Magento/Quote/etc/webapi_rest/di.xml +++ /dev/null @@ -1,15 +0,0 @@ -<?xml version="1.0"?> -<!-- -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ ---> -<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> - <type name="Magento\Quote\Api\CartRepositoryInterface"> - <plugin name="admin_access" type="\Magento\Quote\Model\Cart\Access\CartRepositoryPlugin" /> - </type> - <type name="Magento\Quote\Api\CartManagementInterface"> - <plugin name="admin_access" type="\Magento\Quote\Model\Cart\Access\CartManagementPlugin" /> - </type> -</config> diff --git a/app/code/Magento/Quote/etc/webapi_soap/di.xml b/app/code/Magento/Quote/etc/webapi_soap/di.xml deleted file mode 100644 index 67774a740401df953616e1fedae05e822e81d32e..0000000000000000000000000000000000000000 --- a/app/code/Magento/Quote/etc/webapi_soap/di.xml +++ /dev/null @@ -1,15 +0,0 @@ -<?xml version="1.0"?> -<!-- -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ ---> -<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> - <type name="Magento\Quote\Api\CartRepositoryInterface"> - <plugin name="admin_access" type="\Magento\Quote\Model\Cart\Access\CartRepositoryPlugin" /> - </type> - <type name="Magento\Quote\Api\CartManagementInterface"> - <plugin name="admin_access" type="\Magento\Quote\Model\Cart\Access\CartManagementPlugin" /> - </type> -</config> 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 1f977421884bb9bb8d300244fd3995986c30a6f5..b8ec4629f35812fd01c7e8a6b1cf03577d4e9529 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartManagementTest.php @@ -13,6 +13,7 @@ class CartManagementTest extends WebapiAbstract const SERVICE_VERSION = 'V1'; const SERVICE_NAME = 'quoteCartManagementV1'; const RESOURCE_PATH = '/V1/carts/'; + const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token"; protected $createdQuotes = []; @@ -26,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' => [ @@ -46,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']); } /** @@ -298,26 +366,67 @@ class CartManagementTest extends WebapiAbstract $items = $order->getAllItems(); $this->assertCount(1, $items); $this->assertEquals('Simple Product', $items[0]->getName()); - $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'); + + $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()); + } + + /** + * Test to get my cart based on customer authentication token or session + * * @magentoApiDataFixture Magento/Sales/_files/quote_with_customer.php */ public function testGetCartForCustomer() { + // 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'); + $cart = $this->getCart('test01'); $customerId = $cart->getCustomer()->getId(); $serviceInfo = [ 'rest' => [ - 'resourcePath' => '/V1/customer/' . $customerId . '/cart', + 'resourcePath' => '/V1/carts/mine', 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + 'token' => $token ], 'soap' => [ 'service' => 'quoteCartManagementV1', 'serviceVersion' => 'V1', 'operation' => 'quoteCartManagementV1GetCartForCustomer', + 'token' => $token ], ]; diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php index add493db96a143faf32a0d26e183122067aaa9e6..9169ee6ca6ab690c9d367e7adc33dedaa3f217fe 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php @@ -163,8 +163,8 @@ class CartRepositoryTest extends WebapiAbstract ->setValue($cart->getSubtotal()) ->create(); - $yesterdayDate = (new \DateTime())->sub(new \DateInterval('P1D'))->format('Y-m-d'); - $tomorrowDate = (new \DateTime())->add(new \DateInterval('P1D'))->format('Y-m-d'); + $yesterdayDate = (new \DateTime($cart->getCreatedAt()))->sub(new \DateInterval('P1D'))->format('Y-m-d'); + $tomorrowDate = (new \DateTime($cart->getCreatedAt()))->add(new \DateInterval('P1D'))->format('Y-m-d'); $minCreatedAtFilter = $this->filterBuilder->setField('created_at') ->setConditionType('gteq') ->setValue($yesterdayDate) 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/GuestBillingAddressManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestBillingAddressManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..97838fe9f26393312c5a24c53f5ad85ab0f061e1 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestBillingAddressManagementTest.php @@ -0,0 +1,145 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Api; + +use Magento\Quote\Api\Data\AddressInterface; +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestBillingAddressManagementTest extends WebapiAbstract +{ + const SERVICE_VERSION = 'V1'; + const SERVICE_NAME = 'quoteGuestBillingAddressManagementV1'; + const RESOURCE_PATH = '/V1/guest-carts/'; + + /** + * @var \Magento\TestFramework\ObjectManager + */ + protected $objectManager; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + protected function getQuoteMaskedId($quoteId) + { + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMaskFactory')->create(); + $quoteIdMask->load($quoteId); + return $quoteIdMask->getMaskedId(); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetAddress() + { + $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() + ]; + + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/billing-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Get', + ], + ]; + + $requestData = ["cartId" => $cartId]; + $this->assertEquals($data, $this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetAddress() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/billing-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Assign', + ], + ]; + + $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 = [ + "cartId" => $cartId, + '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/GuestCartItemRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartItemRepositoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..7287cd302008205adfafc6b6b96a7a65064e28c4 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartItemRepositoryTest.php @@ -0,0 +1,226 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestCartItemRepositoryTest extends WebapiAbstract +{ + const SERVICE_VERSION = 'V1'; + const SERVICE_NAME = 'quoteGuestCartItemRepositoryV1'; + const RESOURCE_PATH = '/V1/guest-carts/'; + + /** + * @var \Magento\TestFramework\ObjectManager + */ + protected $objectManager; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php + */ + public function testGetList() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_item_with_items', 'reserved_order_id'); + $cartId = $quote->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $output = []; + /** @var \Magento\Quote\Api\Data\CartItemInterface $item */ + foreach ($quote->getAllItems() as $item) { + //Set masked Cart ID + $item->setQuoteId($cartId); + $data = [ + 'item_id' => $item->getItemId(), + 'sku' => $item->getSku(), + 'name' => $item->getName(), + 'price' => $item->getPrice(), + 'qty' => $item->getQty(), + 'product_type' => $item->getProductType(), + 'quote_id' => $item->getQuoteId() + ]; + + $output[] = $data; + } + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'GetList', + ], + ]; + + $requestData = ["cartId" => $cartId]; + $this->assertEquals($output, $this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + */ + public function testAddItem() + { + /** @var \Magento\Catalog\Model\Product $product */ + $product = $this->objectManager->create('Magento\Catalog\Model\Product')->load(2); + $productSku = $product->getSku(); + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $quote->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'items', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Save', + ], + ]; + + $requestData = [ + "cartItem" => [ + "sku" => $productSku, + "qty" => 7, + "quote_id" => $cartId, + ], + ]; + $this->_webApiCall($serviceInfo, $requestData); + $this->assertTrue($quote->hasProductId(2)); + $this->assertEquals(7, $quote->getItemByProduct($product)->getQty()); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php + */ + public function testRemoveItem() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_item_with_items', 'reserved_order_id'); + $cartId = $quote->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $product = $this->objectManager->create('Magento\Catalog\Model\Product'); + $productId = $product->getIdBySku('simple_one'); + $product->load($productId); + $itemId = $quote->getItemByProduct($product)->getId(); + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'DeleteById', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "itemId" => $itemId, + ]; + $this->assertTrue($this->_webApiCall($serviceInfo, $requestData)); + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_item_with_items', 'reserved_order_id'); + $this->assertFalse($quote->hasProductId($productId)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php + */ + public function testUpdateItem() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_item_with_items', 'reserved_order_id'); + $cartId = $quote->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $product = $this->objectManager->create('Magento\Catalog\Model\Product'); + $productId = $product->getIdBySku('simple_one'); + $product->load($productId); + $itemId = $quote->getItemByProduct($product)->getId(); + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . 'items/' . $itemId, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Save', + ], + ]; + + if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { + $requestData = [ + "cartItem" => [ + "qty" => 5, + "quote_id" => $cartId, + "itemId" => $itemId, + ], + ]; + } else { + $requestData = [ + "cartItem" => [ + "qty" => 5, + "quote_id" => $cartId, + ], + ]; + } + $this->_webApiCall($serviceInfo, $requestData); + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_item_with_items', 'reserved_order_id'); + $this->assertTrue($quote->hasProductId(1)); + $item = $quote->getItemByProduct($product); + $this->assertEquals(5, $item->getQty()); + $this->assertEquals($itemId, $item->getItemId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9058a930b196d518058f1db9693fe4a5091a0b45 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartManagementTest.php @@ -0,0 +1,345 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Api; + +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestCartManagementTest extends WebapiAbstract +{ + const SERVICE_VERSION = 'V1'; + const SERVICE_NAME = 'quoteGuestCartManagementV1'; + const RESOURCE_PATH = '/V1/guest-carts/'; + + protected $createdQuotes = []; + + /** + * @var \Magento\TestFramework\ObjectManager + */ + protected $objectManager; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + public function testCreate() + { + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'CreateEmptyCart', + ], + ]; + + $requestData = ['storeId' => 1]; + $quoteId = $this->_webApiCall($serviceInfo, $requestData); + $this->assertTrue(strlen($quoteId) >= 32); + $this->createdQuotes[] = $quoteId; + } + + 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(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMask'); + $quoteIdMask->delete($quote->getId()); + } + } + + /** + * @magentoApiDataFixture Magento/Sales/_files/quote.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ + public function testAssignCustomer() + { + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test01', 'reserved_order_id'); + $cartId = $quote->getId(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + /** @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/guest-carts/' . $cartId, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => 'V1', + 'operation' => self::SERVICE_NAME . 'AssignCustomer', + ], + ]; + + $requestData = [ + 'cartId' => $cartId, + 'customerId' => $customerId, + 'storeId' => 1, + ]; + // Cart must be anonymous (see fixture) + $this->assertEmpty($quote->getCustomerId()); + + $this->assertTrue($this->_webApiCall($serviceInfo, $requestData)); + // Reload target quote + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test01', 'reserved_order_id'); + $this->assertEquals(0, $quote->getCustomerIsGuest()); + $this->assertEquals($customer->getId(), $quote->getCustomerId()); + $this->assertEquals($customer->getFirstname(), $quote->getCustomerFirstname()); + $this->assertEquals($customer->getLastname(), $quote->getCustomerLastname()); + } + + /** + * @magentoApiDataFixture Magento/Sales/_files/quote.php + * @expectedException \Exception + */ + public function testAssignCustomerThrowsExceptionIfThereIsNoCustomerWithGivenId() + { + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test01', 'reserved_order_id'); + $cartId = $quote->getId(); + $customerId = 9999; + $serviceInfo = [ + 'soap' => [ + 'serviceVersion' => 'V1', + 'service' => self::SERVICE_NAME, + 'operation' => self::SERVICE_NAME . 'AssignCustomer', + ], + 'rest' => [ + 'resourcePath' => '/V1/guest-carts/' . $cartId, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + ]; + $requestData = [ + 'cartId' => $cartId, + 'customerId' => $customerId, + 'storeId' => 1, + ]; + + $this->_webApiCall($serviceInfo, $requestData); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @expectedException \Exception + */ + public function testAssignCustomerThrowsExceptionIfThereIsNoCartWithGivenId() + { + $cartId = 9999; + $customerId = 1; + $serviceInfo = [ + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => 'V1', + 'operation' => self::SERVICE_NAME . 'AssignCustomer', + ], + 'rest' => [ + 'resourcePath' => '/V1/guest-carts/' . $cartId, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + ]; + $requestData = [ + 'cartId' => $cartId, + 'customerId' => $customerId, + 'storeId' => 1, + ]; + + $this->_webApiCall($serviceInfo, $requestData); + } + + /** + * @magentoApiDataFixture Magento/Sales/_files/quote_with_customer.php + * @expectedException \Exception + * @expectedExceptionMessage Cannot assign customer to the given cart. The cart is not anonymous. + */ + public function testAssignCustomerThrowsExceptionIfTargetCartIsNotAnonymous() + { + /** @var $customer \Magento\Customer\Model\Customer */ + $customer = $this->objectManager->create('Magento\Customer\Model\Customer')->load(1); + $customerId = $customer->getId(); + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test01', 'reserved_order_id'); + $cartId = $quote->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $serviceInfo = [ + 'rest' => [ + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + 'resourcePath' => '/V1/guest-carts/' . $cartId, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => 'V1', + 'operation' => self::SERVICE_NAME . 'AssignCustomer', + ], + ]; + + $requestData = [ + 'cartId' => $cartId, + 'customerId' => $customerId, + 'storeId' => 1, + ]; + $this->_webApiCall($serviceInfo, $requestData); + } + + /** + * @magentoApiDataFixture Magento/Sales/_files/quote.php + * @magentoApiDataFixture Magento/Customer/_files/customer_non_default_website_id.php + * @expectedException \Exception + * @expectedExceptionMessage Cannot assign customer to the given cart. The cart belongs to different store. + */ + public function testAssignCustomerThrowsExceptionIfCartIsAssignedToDifferentStore() + { + $repository = $this->objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface'); + /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */ + $customer = $repository->getById(1); + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test01', 'reserved_order_id'); + + $customerId = $customer->getId(); + $cartId = $quote->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $serviceInfo = [ + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => 'V1', + 'operation' => self::SERVICE_NAME . 'AssignCustomer', + ], + 'rest' => [ + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + 'resourcePath' => '/V1/guest-carts/' . $cartId, + ], + ]; + + $requestData = [ + 'cartId' => $cartId, + 'customerId' => $customerId, + 'storeId' => 1, + ]; + $this->_webApiCall($serviceInfo, $requestData); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @magentoApiDataFixture Magento/Sales/_files/quote.php + * @expectedException \Exception + * @expectedExceptionMessage Cannot assign customer to the given cart. Customer already has active cart. + */ + public function testAssignCustomerThrowsExceptionIfCustomerAlreadyHasActiveCart() + { + /** @var $customer \Magento\Customer\Model\Customer */ + $customer = $this->objectManager->create('Magento\Customer\Model\Customer')->load(1); + // Customer has a quote with reserved order ID test_order_1 (see fixture) + /** @var $customerQuote \Magento\Quote\Model\Quote */ + $customerQuote = $this->objectManager->create('Magento\Quote\Model\Quote') + ->load('test_order_1', 'reserved_order_id'); + $customerQuote->setIsActive(1)->save(); + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test01', 'reserved_order_id'); + + $cartId = $quote->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $customerId = $customer->getId(); + + $serviceInfo = [ + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'operation' => self::SERVICE_NAME . 'AssignCustomer', + 'serviceVersion' => 'V1', + ], + 'rest' => [ + 'resourcePath' => '/V1/guest-carts/' . $cartId, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + ]; + + $requestData = [ + 'cartId' => $cartId, + 'customerId' => $customerId, + 'storeId' => 1, + ]; + $this->_webApiCall($serviceInfo, $requestData); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php + */ + public function testPlaceOrder() + { + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test_order_1', 'reserved_order_id'); + $cartId = $quote->getId(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $serviceInfo = [ + 'soap' => [ + 'service' => 'quoteGuestCartManagementV1', + 'operation' => 'quoteGuestCartManagementV1PlaceOrder', + 'serviceVersion' => 'V1', + ], + 'rest' => [ + 'resourcePath' => '/V1/guest-carts/' . $cartId . '/order', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + ]; + + $orderId = $this->_webApiCall($serviceInfo, ['cartId' => $cartId]); + + /** @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()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartRepositoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b3c8e3f024e70c12332a800999e68298bad7473c --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartRepositoryTest.php @@ -0,0 +1,129 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +use Magento\TestFramework\ObjectManager; +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestCartRepositoryTest extends WebapiAbstract +{ + /** + * @var ObjectManager + */ + private $objectManager; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + protected function tearDown() + { + try { + $cart = $this->getCart('test01'); + $cart->delete(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMask'); + $quoteIdMask->delete($cart->getId()); + } catch (\InvalidArgumentException $e) { + // Do nothing if cart fixture was not used + } + parent::tearDown(); + } + + /** + * Retrieve quote by given reserved order ID + * + * @param string $reservedOrderId + * @return \Magento\Quote\Model\Quote + * @throws \InvalidArgumentException + */ + protected function getCart($reservedOrderId) + { + /** @var $cart \Magento\Quote\Model\Quote */ + $cart = $this->objectManager->get('Magento\Quote\Model\Quote'); + $cart->load($reservedOrderId, 'reserved_order_id'); + if (!$cart->getId()) { + throw new \InvalidArgumentException('There is no quote with provided reserved order ID.'); + } + return $cart; + } + + /** + * @magentoApiDataFixture Magento/Sales/_files/quote.php + */ + public function testGetCart() + { + $cart = $this->getCart('test01'); + $cartId = $cart->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/guest-carts/' . $quoteIdMask->getMaskedId(), + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => 'quoteGuestCartRepositoryV1', + 'serviceVersion' => 'V1', + 'operation' => 'quoteGuestCartRepositoryV1Get', + ], + ]; + + $requestData = ['cartId' => $quoteIdMask->getMaskedId()]; + $cartData = $this->_webApiCall($serviceInfo, $requestData); + $this->assertEquals($cart->getId(), $cartData['id']); + $this->assertEquals($cart->getCreatedAt(), $cartData['created_at']); + $this->assertEquals($cart->getUpdatedAt(), $cartData['updated_at']); + $this->assertEquals($cart->getIsActive(), $cartData['is_active']); + $this->assertEquals($cart->getIsVirtual(), $cartData['is_virtual']); + $this->assertEquals($cart->getOrigOrderId(), $cartData['orig_order_id']); + $this->assertEquals($cart->getItemsCount(), $cartData['items_count']); + $this->assertEquals($cart->getItemsQty(), $cartData['items_qty']); + //following checks will be uncommented when all cart related services are ready + $this->assertContains('customer', $cartData); + $this->assertEquals(true, $cartData['customer_is_guest']); + $this->assertContains('currency', $cartData); + $this->assertEquals($cart->getGlobalCurrencyCode(), $cartData['currency']['global_currency_code']); + $this->assertEquals($cart->getBaseCurrencyCode(), $cartData['currency']['base_currency_code']); + $this->assertEquals($cart->getQuoteCurrencyCode(), $cartData['currency']['quote_currency_code']); + $this->assertEquals($cart->getStoreCurrencyCode(), $cartData['currency']['store_currency_code']); + $this->assertEquals($cart->getBaseToGlobalRate(), $cartData['currency']['base_to_global_rate']); + $this->assertEquals($cart->getBaseToQuoteRate(), $cartData['currency']['base_to_quote_rate']); + $this->assertEquals($cart->getStoreToBaseRate(), $cartData['currency']['store_to_base_rate']); + $this->assertEquals($cart->getStoreToQuoteRate(), $cartData['currency']['store_to_quote_rate']); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No such entity with + */ + public function testGetCartThrowsExceptionIfThereIsNoCartWithProvidedId() + { + $cartId = 9999; + + $serviceInfo = [ + 'soap' => [ + 'service' => 'quoteGuestCartRepositoryV1', + 'serviceVersion' => 'V1', + 'operation' => 'quoteGuestCartRepositoryV1Get', + ], + 'rest' => [ + 'resourcePath' => '/V1/guest-carts/' . $cartId, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + ]; + + $requestData = ['cartId' => $cartId]; + $this->_webApiCall($serviceInfo, $requestData); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartTotalRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartTotalRepositoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..87cbbbdaaf40ec9f52f36a41bc5945c6fb69ddbe --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartTotalRepositoryTest.php @@ -0,0 +1,182 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +use Magento\Quote\Model\Cart\Totals; +use Magento\Quote\Model\Cart\Totals\Item as ItemTotals; +use Magento\Framework\Api\FilterBuilder; +use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\TestFramework\ObjectManager; +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestCartTotalRepositoryTest extends WebapiAbstract +{ + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var SearchCriteriaBuilder + */ + private $searchBuilder; + + /** + * @var FilterBuilder + */ + private $filterBuilder; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->searchBuilder = $this->objectManager->create( + 'Magento\Framework\Api\SearchCriteriaBuilder' + ); + $this->filterBuilder = $this->objectManager->create( + 'Magento\Framework\Api\FilterBuilder' + ); + } + + protected function getQuoteMaskedId($quoteId) + { + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMaskFactory')->create(); + $quoteIdMask->load($quoteId); + return $quoteIdMask->getMaskedId(); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testGetTotals() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $this->getQuoteMaskedId($quote->getId()); + + /** @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)], + ]; + + $requestData = ['cartId' => $cartId]; + + $data = $this->formatTotalsData($data); + + $this->assertEquals($data, $this->_webApiCall($this->getServiceInfoForTotalsService($cartId), $requestData)); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No such entity + */ + public function testGetTotalsWithAbsentQuote() + { + $cartId = 'unknownCart'; + $requestData = ['cartId' => $cartId]; + $this->_webApiCall($this->getServiceInfoForTotalsService($cartId), $requestData); + } + + /** + * Get service info for totals service + * + * @param string $cartId + * @return array + */ + protected function getServiceInfoForTotalsService($cartId) + { + return [ + 'soap' => [ + 'service' => 'quoteGuestCartTotalRepositoryV1', + 'serviceVersion' => 'V1', + 'operation' => 'quoteGuestCartTotalRepositoryV1get', + ], + 'rest' => [ + 'resourcePath' => '/V1/guest-carts/' . $cartId . '/totals', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + ]; + } + + /** + * Adjust response details for SOAP protocol + * + * @param array $data + * @return array + */ + protected function formatTotalsData($data) + { + foreach ($data as $key => $field) { + if (is_numeric($field)) { + $data[$key] = round($field, 1); + if ($data[$key] === null) { + $data[$key] = 0.0; + } + } + } + + unset($data[Totals::KEY_BASE_SUBTOTAL_INCL_TAX]); + + return $data; + } + + /** + * Fetch quote item totals data from quote + * + * @param \Magento\Quote\Model\Quote $quote + * @return array + */ + protected function getQuoteItemTotalsData(\Magento\Quote\Model\Quote $quote) + { + $items = $quote->getAllItems(); + $item = array_shift($items); + + return [ + ItemTotals::KEY_PRICE => $item->getPrice(), + ItemTotals::KEY_BASE_PRICE => $item->getBasePrice(), + ItemTotals::KEY_QTY => $item->getQty(), + ItemTotals::KEY_ROW_TOTAL => $item->getRowTotal(), + ItemTotals::KEY_BASE_ROW_TOTAL => $item->getBaseRowTotal(), + ItemTotals::KEY_ROW_TOTAL_WITH_DISCOUNT => $item->getRowTotalWithDiscount(), + ItemTotals::KEY_TAX_AMOUNT => $item->getTaxAmount(), + ItemTotals::KEY_BASE_TAX_AMOUNT => $item->getBaseTaxAmount(), + ItemTotals::KEY_TAX_PERCENT => $item->getTaxPercent(), + ItemTotals::KEY_DISCOUNT_AMOUNT => $item->getDiscountAmount(), + ItemTotals::KEY_BASE_DISCOUNT_AMOUNT => $item->getBaseDiscountAmount(), + ItemTotals::KEY_DISCOUNT_PERCENT => $item->getDiscountPercent(), + ItemTotals::KEY_PRICE_INCL_TAX => $item->getPriceInclTax(), + ItemTotals::KEY_BASE_PRICE_INCL_TAX => $item->getBasePriceInclTax(), + ItemTotals::KEY_ROW_TOTAL_INCL_TAX => $item->getRowTotalInclTax(), + ItemTotals::KEY_BASE_ROW_TOTAL_INCL_TAX => $item->getBaseRowTotalInclTax(), + ]; + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCouponManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCouponManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..296d5d60767fb9f07477c7651c8a583a4d15939e --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCouponManagementTest.php @@ -0,0 +1,175 @@ +<?php +/** + * + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Api; + +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestCouponManagementTest extends WebapiAbstract +{ + const SERVICE_VERSION = 'V1'; + const SERVICE_NAME = 'quoteGuestCouponManagementV1'; + const RESOURCE_PATH = '/V1/guest-carts/'; + + /** + * @var \Magento\TestFramework\ObjectManager + */ + protected $objectManager; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + public function tearDown() + { + $createdQuotes = ['test_order_1', 'test01']; + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + foreach ($createdQuotes as $quoteId) { + $quote->load($quoteId, 'reserved_order_id'); + $quote->delete(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMask'); + $quoteIdMask->delete($quote->getId()); + } + } + + protected function getQuoteMaskedId($quoteId) + { + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMaskFactory')->create(); + $quoteIdMask->load($quoteId); + return $quoteIdMask->getMaskedId(); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php + */ + public function testGet() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $couponCode = $quote->getCouponCode(); + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' , + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Get', + ], + ]; + + $requestData = ["cartId" => $cartId]; + $this->assertEquals($couponCode, $this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php + */ + public function testDelete() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $this->getQuoteMaskedId($quote->getId()); + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Remove', + ], + ]; + $requestData = ["cartId" => $cartId]; + $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 testSetCouponThrowsExceptionIfCouponDoesNotExist() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $couponCode = 'invalid_coupon_code'; + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . $couponCode, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Set', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "couponCode" => $couponCode, + ]; + + $this->_webApiCall($serviceInfo, $requestData); + } + + /** + * @magentoApiDataFixture Magento/Sales/_files/quote.php + * @magentoApiDataFixture Magento/Checkout/_files/discount_10percent.php + */ + public function testSetCouponSuccess() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test01', 'reserved_order_id'); + $cartId = $this->getQuoteMaskedId($quote->getId()); + $salesRule = $this->objectManager->create('Magento\SalesRule\Model\Rule'); + $salesRule->load('Test Coupon', 'name'); + $couponCode = $salesRule->getCouponCode(); + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . $couponCode, + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Set', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "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 new file mode 100644 index 0000000000000000000000000000000000000000..40579922f1896f761ef85e891a1e5f59daae39dd --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestPaymentMethodManagementTest.php @@ -0,0 +1,333 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +class GuestPaymentMethodManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract +{ + const SERVICE_VERSION = 'V1'; + const SERVICE_NAME = 'quoteGuestPaymentMethodManagementV1'; + const RESOURCE_PATH = '/V1/guest-carts/'; + + /** + * @var \Magento\TestFramework\ObjectManager + */ + protected $objectManager; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + protected function tearDown() + { + $this->deleteCart('test_order_1'); + $this->deleteCart('test_order_1_with_payment'); + $this->deleteCart('test_order_with_virtual_product'); + $this->deleteCart('test_order_with_virtual_product_without_address'); + parent::tearDown(); + } + + /** + * Delete quote by given reserved order ID + * + * @param string $reservedOrderId + * @return void + */ + protected function deleteCart($reservedOrderId) + { + try { + /** @var $cart \Magento\Quote\Model\Quote */ + $cart = $this->objectManager->get('Magento\Quote\Model\Quote'); + $cart->load($reservedOrderId, 'reserved_order_id'); + if (!$cart->getId()) { + throw new \InvalidArgumentException('There is no quote with provided reserved order ID.'); + } + $cart->delete(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMask'); + $quoteIdMask->delete($cart->getId()); + } catch (\InvalidArgumentException $e) { + // Do nothing if cart fixture was not used + } + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php + */ + public function testReSetPayment() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('\Magento\Quote\Model\Quote'); + $quote->load('test_order_1_with_payment', 'reserved_order_id'); + $cartId = $this->getMaskedCartId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'set', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "method" => [ + 'method' => 'checkmo', + 'po_number' => null, + 'cc_owner' => 'John', + 'cc_type' => null, + 'cc_exp_year' => null, + 'cc_exp_month' => null, + ], + ]; + + $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + */ + public function testSetPaymentWithVirtualProduct() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('\Magento\Quote\Model\Quote'); + $quote->load('test_order_with_virtual_product', 'reserved_order_id'); + $cartId = $this->getMaskedCartId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'set', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "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)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentWithSimpleProduct() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('\Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $this->getMaskedCartId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'set', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "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)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php + * @expectedException \Exception + * @expectedExceptionMessage Billing address is not set + */ + public function testSetPaymentWithVirtualProductWithoutAddress() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('\Magento\Quote\Model\Quote'); + $quote->load('test_order_with_virtual_product_without_address', 'reserved_order_id'); + $cartId = $this->getMaskedCartId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'set', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "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)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @expectedException \Exception + * @expectedExceptionMessage Shipping address is not set + */ + public function testSetPaymentWithSimpleProductWithoutAddress() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('\Magento\Quote\Model\Quote'); + $quote->load('test_order_with_simple_product_without_address', 'reserved_order_id'); + $cartId = $this->getMaskedCartId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'set', + ], + ]; + + $requestData = [ + "cartId" => $cartId, + "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)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetList() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $this->getMaskedCartId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/payment-methods', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'getList', + ], + ]; + + $requestData = ["cartId" => $cartId]; + $requestResponse = $this->_webApiCall($serviceInfo, $requestData); + + $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 testGet() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1_with_payment', 'reserved_order_id'); + $cartId = $this->getMaskedCartId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'get', + ], + ]; + + $requestData = ["cartId" => $cartId]; + $requestResponse = $this->_webApiCall($serviceInfo, $requestData); + + 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. + * + * @param string $cartId + * @return string + */ + protected function getMaskedCartId($cartId) + { + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + return $quoteIdMask->getMaskedId(); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestShippingAddressManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestShippingAddressManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a1c22bf0ee869fda4e3fec3d588101ed748dfb36 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestShippingAddressManagementTest.php @@ -0,0 +1,235 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Quote\Api; + +use Magento\Quote\Api\Data\AddressInterface; +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestShippingAddressManagementTest extends WebapiAbstract +{ + const SERVICE_VERSION = 'V1'; + const SERVICE_NAME = 'quoteGuestShippingAddressManagementV1'; + const RESOURCE_PATH = '/V1/guest-carts/'; + + /** + * @var \Magento\TestFramework\ObjectManager + */ + protected $objectManager; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + public function tearDown() + { + $createdQuotes = ['test_order_1', 'test_order_with_virtual_product']; + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + foreach ($createdQuotes as $quoteId) { + $quote->load($quoteId, 'reserved_order_id'); + $quote->delete(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMask'); + $quoteIdMask->delete($quote->getId()); + } + } + + protected function getQuoteMaskedId($quoteId) + { + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = $this->objectManager->create('Magento\Quote\Model\QuoteIdMaskFactory')->create(); + $quoteIdMask->load($quoteId); + return $quoteIdMask->getMaskedId(); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetAddress() + { + $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(); + + $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() + ]; + + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/shipping-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Get', + ], + ]; + + $requestData = ["cartId" => $cartId]; + $this->assertEquals($data, $this->_webApiCall($serviceInfo, $requestData)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + * + * @expectedException \Exception + * @expectedExceptionMessage Cart contains virtual product(s) only. Shipping address is not applicable + */ + public function testGetAddressOfQuoteWithVirtualProduct() + { + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_with_virtual_product', 'reserved_order_id'); + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/shipping-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Get', + ], + ]; + + $this->_webApiCall($serviceInfo, ["cartId" => $cartId]); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetAddress() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/shipping-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Assign', + ], + ]; + + $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 = [ + "cartId" => $cartId, + '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); + } + } + + /** + * Set address to quote with virtual products only + * + * @expectedException \Exception + * @expectedExceptionMessage Cart contains virtual product(s) only. Shipping address is not applicable + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + */ + public function testSetAddressForVirtualQuote() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_with_virtual_product', 'reserved_order_id'); + $cartId = $this->getQuoteMaskedId($quote->getId()); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/shipping-address', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Assign', + ], + ]; + + $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 = [ + "cartId" => $cartId, + 'address' => $addressData, + ]; + + $this->_webApiCall($serviceInfo, $requestData); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestShippingMethodManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestShippingMethodManagementTest.php new file mode 100644 index 0000000000000000000000000000000000000000..54b89387651643a72a5c7aac9a789898942dd28e --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestShippingMethodManagementTest.php @@ -0,0 +1,334 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Quote\Api; + +use Magento\Quote\Api\Data\ShippingMethodInterface; +use Magento\TestFramework\ObjectManager; +use Magento\TestFramework\TestCase\WebapiAbstract; + +class GuestShippingMethodManagementTest extends WebapiAbstract +{ + const SERVICE_VERSION = 'V1'; + const SERVICE_NAME = 'quoteGuestShippingMethodManagementV1'; + const RESOURCE_PATH = '/V1/guest-carts/'; + + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var \Magento\Quote\Model\Quote + */ + protected $quote; + + protected function setUp() + { + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + } + + protected function getServiceInfo() + { + return [ + 'rest' => [ + 'resourcePath' => '/V1/carts/' . $this->quote->getId() . '/selected-shipping-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Set', + ], + ]; + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetMethod() + { + $this->quote->load('test_order_1', 'reserved_order_id'); + $serviceInfo = $this->getServiceInfo(); + + $cartId = $this->quote->getId(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $requestData = [ + 'cartId' => $cartId, + 'carrierCode' => 'flatrate', + 'methodCode' => 'flatrate', + ]; + $result = $this->_webApiCall($serviceInfo, $requestData); + $this->assertEquals(true, $result); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetMethodWrongMethod() + { + $expectedMessage = 'Carrier with such method not found: %1, %2'; + $this->quote->load('test_order_1', 'reserved_order_id'); + $serviceInfo = $this->getServiceInfo(); + $carrierCode = 'flatrate'; + $methodCode = 'wrongMethod'; + + $cartId = $this->quote->getId(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $requestData = [ + 'cartId' => $cartId, + 'carrierCode' => $carrierCode, + 'methodCode' => $methodCode, + ]; + try { + $this->_webApiCall($serviceInfo, $requestData); + } catch (\SoapFault $e) { + $this->assertContains( + $expectedMessage, + $e->getMessage(), + 'SoapFault does not contain expected message.' + ); + } catch (\Exception $e) { + $errorObj = $this->processRestExceptionResult($e); + $this->assertEquals($expectedMessage, $errorObj['message']); + $this->assertEquals([$carrierCode, $methodCode], $errorObj['parameters']); + } + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testSetMethodWithoutShippingAddress() + { + $this->quote->load('test_order_with_simple_product_without_address', 'reserved_order_id'); + $serviceInfo = $this->getServiceInfo(); + + $cartId = $this->quote->getId(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $requestData = [ + 'cartId' => $cartId, + 'carrierCode' => 'flatrate', + 'methodCode' => 'flatrate', + ]; + try { + $this->_webApiCall($serviceInfo, $requestData); + } catch (\SoapFault $e) { + $message = $e->getMessage(); + } catch (\Exception $e) { + $message = json_decode($e->getMessage())->message; + } + $this->assertEquals('Shipping address is not set', $message); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testGetMethod() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + + $cartId = $quote->getId(); + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $shippingAddress = $quote->getShippingAddress(); + list($carrierCode, $methodCode) = explode('_', $shippingAddress->getShippingMethod()); + list($carrierTitle, $methodTitle) = explode(' - ', $shippingAddress->getShippingDescription()); + $data = [ + ShippingMethodInterface::KEY_CARRIER_CODE => $carrierCode, + ShippingMethodInterface::KEY_METHOD_CODE => $methodCode, + ShippingMethodInterface::KEY_CARRIER_TITLE => $carrierTitle, + ShippingMethodInterface::KEY_METHOD_TITLE => $methodTitle, + ShippingMethodInterface::KEY_SHIPPING_AMOUNT => $shippingAddress->getShippingAmount(), + ShippingMethodInterface::KEY_BASE_SHIPPING_AMOUNT => $shippingAddress->getBaseShippingAmount(), + ShippingMethodInterface::KEY_AVAILABLE => true, + ]; + + $requestData = ["cartId" => $cartId]; + $this->assertEquals($data, $this->_webApiCall($this->getSelectedMethodServiceInfo($cartId), $requestData)); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + */ + public function testGetMethodOfVirtualCart() + { + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $cartId = $quote->load('test_order_with_virtual_product', 'reserved_order_id')->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $result = $this->_webApiCall($this->getSelectedMethodServiceInfo($cartId), ["cartId" => $cartId]); + $this->assertEquals([], $result); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetMethodOfCartWithNoShippingMethod() + { + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $cartId = $quote->load('test_order_1', 'reserved_order_id')->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $result = $this->_webApiCall($this->getSelectedMethodServiceInfo($cartId), ["cartId" => $cartId]); + $this->assertEquals([], $result); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + * + */ + public function testGetListForVirtualCart() + { + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $cartId = $quote->load('test_order_with_virtual_product', 'reserved_order_id')->getId(); + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $this->assertEquals([], $this->_webApiCall($this->getListServiceInfo($cartId), ["cartId" => $cartId])); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetList() + { + /** @var \Magento\Quote\Model\Quote $quote */ + $quote = $this->objectManager->create('Magento\Quote\Model\Quote'); + $quote->load('test_order_1', 'reserved_order_id'); + $cartId = $quote->getId(); + if (!$cartId) { + $this->fail('quote fixture failed'); + } + + /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ + $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); + $quoteIdMask->load($cartId); + //Use masked cart Id + $cartId = $quoteIdMask->getMaskedId(); + + $quote->getShippingAddress()->collectShippingRates(); + $expectedRates = $quote->getShippingAddress()->getGroupedAllShippingRates(); + + $expectedData = $this->convertRates($expectedRates, $quote->getQuoteCurrencyCode()); + + $requestData = ["cartId" => $cartId]; + + $returnedRates = $this->_webApiCall($this->getListServiceInfo($cartId), $requestData); + $this->assertEquals($expectedData, $returnedRates); + } + + /** + * @param string $cartId + * @return array + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + protected function getSelectedMethodServiceInfo($cartId) + { + return $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-shipping-method', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Get', + ], + ]; + } + + /** + * Service info + * + * @param int $cartId + * @return array + */ + protected function getListServiceInfo($cartId) + { + return [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/shipping-methods', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'GetList', + ], + ]; + } + + /** + * Convert rate models array to data array + * + * @param string $currencyCode + * @param \Magento\Quote\Model\Quote\Address\Rate[] $groupedRates + * @return array + */ + protected function convertRates($groupedRates, $currencyCode) + { + $result = []; + /** @var \Magento\Quote\Model\Cart\ShippingMethodConverter $converter */ + $converter = $this->objectManager->create('\Magento\Quote\Model\Cart\ShippingMethodConverter'); + foreach ($groupedRates as $carrierRates) { + foreach ($carrierRates as $rate) { + $result[] = $converter->modelToDataObject($rate, $currencyCode)->__toArray(); + } + } + return $result; + } +} 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/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved.php index 65cdb896f134ccdc968d833ae6816ebf8a15ab70..c06216ed07d89f96f5e144815c68a97964e6300b 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved.php @@ -12,3 +12,11 @@ require 'quote_with_address.php'; $quote->collectTotals()->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved_rollback.php index 9bc73ec6d964fc69c7bdc78fa6a8fa15b83a0583..098b8e746cd5b099605ed37fb437cd2ba8e82781 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_saved_rollback.php @@ -10,3 +10,7 @@ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $quote = $objectManager->create('Magento\Quote\Model\Quote'); $quote->load('test_order_1', 'reserved_order_id')->delete(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create('Magento\Quote\Model\QuoteIdMask'); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment.php index 355706e20824a14eea670c5cc3678c2cdf2ee9d0..1f0a0a45197c70eec8b032eea8555dba723c18e9 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment.php @@ -19,3 +19,11 @@ $quote->getPayment()->setMethod('checkmo'); $quote->collectTotals(); $quote->save(); $quote->getPayment()->setMethod('checkmo'); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment_rollback.php index 7d04f85c78907dae2949522fef4d0001086cdac0..e999e00ff14336961e2581ba9983960cd1e0e9fe 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_check_payment_rollback.php @@ -5,7 +5,10 @@ * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ +require __DIR__ . '/../../Sales/_files/default_rollback.php'; /** @var $objectManager \Magento\TestFramework\ObjectManager */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $objectManager->get('Magento\Framework\Registry')->unregister('quote'); +$quote = $objectManager->create('Magento\Quote\Model\Quote'); +$quote->load('test_order_1', 'reserved_order_id')->delete(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_items_saved.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_items_saved.php index d017de00edb5e8a0a476d4804a84903e70160489..dcf8ab45afc9802ac729f00210a68f59bfee159d 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_items_saved.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_items_saved.php @@ -42,3 +42,11 @@ $quoteProduct = $product->load($product->getIdBySku('simple_one')); $quote->setReservedOrderId('test_order_item_with_items') ->addProduct($product->load($product->getIdBySku('simple_one')), 1); $quote->collectTotals()->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved.php index d39da02e43d07343fa5d566c23e1016fa6f2bde2..e15c08b356f56ffc6677a1018b6613a1f4223a74 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved.php @@ -25,3 +25,11 @@ $quote->getPayment() ->setAdditionalData(serialize($paymentDetails)); $quote->collectTotals()->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved_rollback.php index c828e4acf0c793a28d33237f0c31a815ca3b8095..92d03e750c176358c6927bd9724022b80e40a565 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_payment_saved_rollback.php @@ -10,3 +10,7 @@ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $quote = $objectManager->create('Magento\Quote\Model\Quote'); $quote->load('test_order_1_with_payment', 'reserved_order_id')->delete(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create('Magento\Quote\Model\QuoteIdMask'); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved.php index 3d410ba47f558b83bcc8d91d93bed5221a22f37c..e917e5c33e8ddfcb09eafeb08420a2df028ef886 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved.php @@ -26,3 +26,11 @@ $quote->setStoreId( ); $quote->collectTotals()->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php index 60b8a06b2dcfe58c829b67db2fcde71798e88e1f..5639fc74d602ef9f26a9ebcaa5e50a23e03052e4 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php @@ -10,3 +10,7 @@ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $quote = $objectManager->create('Magento\Quote\Model\Quote'); $quote->load('test_order_with_simple_product_without_address', 'reserved_order_id')->delete(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create('Magento\Quote\Model\QuoteIdMask'); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address.php index f892d8e7abade648085ddb2cb5b6f1cc44d79a48..56ae6b1a7643bd074ebd807ea00a77d60da41769 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address.php @@ -52,3 +52,11 @@ $quote->setStoreId( ); $quote->collectTotals()->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php index abd211dba8b7844b3644c2ae0e406b867da88c0b..c87f4d43a8ac58aab785cb180a22c241c9f6aba7 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php @@ -9,3 +9,7 @@ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $quote = $objectManager->create('Magento\Quote\Model\Quote'); $quote->load('test_order_with_virtual_product', 'reserved_order_id')->delete(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create('Magento\Quote\Model\QuoteIdMask'); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved.php index d68124215662e455600af9d538dc7f2db59f7c51..1d19150c3a3a52f274c6301d4559e17245293717 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved.php @@ -19,3 +19,11 @@ $quote->setStoreId(1) ); $quote->collectTotals()->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php index 4447d3cf57862f35cded4b095ccc5d0285bd06e0..914ec199cfd3308429bb94bd660ca8f5c04da531 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php @@ -10,3 +10,7 @@ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $quote = $objectManager->create('Magento\Quote\Model\Quote'); $quote->load('test_order_with_virtual_product_without_address', 'reserved_order_id')->delete(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create('Magento\Quote\Model\QuoteIdMask'); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php b/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php index 8bdafe16f41aa3263b3a75e208e2a8545efbaa5d..2fb7467a1c15bc58a2a6391dfb9eb8854c3c456e 100644 --- a/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php @@ -65,3 +65,11 @@ $quote->getPayment()->setMethod('checkmo'); $quote->setIsMultiShipping('1'); $quote->collectTotals(); $quote->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/quote_with_customer.php b/dev/tests/integration/testsuite/Magento/Sales/_files/quote_with_customer.php index 8d5992519026fd63f5669052fc465a5f932accff..40e14157ff895104bac231809bc61c6b81f98bfc 100644 --- a/dev/tests/integration/testsuite/Magento/Sales/_files/quote_with_customer.php +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/quote_with_customer.php @@ -15,3 +15,11 @@ $customerRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager( ->create('Magento\Customer\Api\CustomerRepositoryInterface'); $customer = $customerRepository->getById(1); $quote->setCustomer($customer)->setCustomerIsGuest(false)->save(); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create('Magento\Quote\Model\QuoteIdMaskFactory') + ->create(); +$quoteIdMask->setId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); diff --git a/lib/internal/Magento/Framework/Phrase.php b/lib/internal/Magento/Framework/Phrase.php index 02c4e0c063c63a36ae430ad59ac233d43b9417ac..992834a4816e1c14f8a56c488a739ed97c3d70f4 100644 --- a/lib/internal/Magento/Framework/Phrase.php +++ b/lib/internal/Magento/Framework/Phrase.php @@ -9,6 +9,7 @@ namespace Magento\Framework; use Zend\Stdlib\JsonSerializable; use Magento\Framework\Phrase\RendererInterface; +use Magento\Framework\Phrase\Renderer\Placeholder; class Phrase implements JsonSerializable { @@ -51,7 +52,11 @@ class Phrase implements JsonSerializable */ public static function getRenderer() { - return self::$renderer; + if (self::$renderer) { + return self::$renderer; + } else { + self::$renderer = new \Magento\Framework\Phrase\Renderer\Placeholder(); + } } /** @@ -93,7 +98,7 @@ class Phrase implements JsonSerializable */ public function render() { - return self::$renderer ? self::$renderer->render([$this->text], $this->arguments) : $this->text; + return self::getRenderer() ? self::getRenderer()->render([$this->text], $this->arguments) : $this->text; } /** diff --git a/lib/internal/Magento/Framework/Phrase/README.md b/lib/internal/Magento/Framework/Phrase/README.md index 9024d6c5bb29d3806a979904d50363e4b52d1433..779979edbb489f8369cc192f1e19ecb19b578699 100644 --- a/lib/internal/Magento/Framework/Phrase/README.md +++ b/lib/internal/Magento/Framework/Phrase/README.md @@ -1,8 +1,8 @@ -# Phase +# Phrase -Class *\Magento\Framework\Phrase* calls renderer to make the translation of the text. **Phase** provides *RedererInterface* and a few renderers to support different kinds of needs of translation of the text. Here are list of renderers in this library: +Class *\Magento\Framework\Phrase* calls renderer to make the translation of the text. **Phrase** provides *RendererInterface* and a few renderers to support different kinds of needs of translation of the text. Here are list of renderers in this library: + * Placeholder render - it replaces placeholders with parameters for substitution. It is the default render if none is set for the Phrase. * Translate render - it is a base renderer that implements text translations. * Inline render - it adds inline translate part to text translation and returns the strings by a template. - * Placeholder render - it replaces placeholders with parameters for substitution. * Composite render - it can have several renderers, calls each renderer for processing the text. Array of renderer class names pass into composite render constructor as a parameter. \ No newline at end of file diff --git a/lib/internal/Magento/Framework/Reflection/TypeProcessor.php b/lib/internal/Magento/Framework/Reflection/TypeProcessor.php index f458d7bd8ece62465704e1d8ef22fd8980ec18a5..580be037ad2062b87fa247b6e5e23f89e75b9241 100644 --- a/lib/internal/Magento/Framework/Reflection/TypeProcessor.php +++ b/lib/internal/Magento/Framework/Reflection/TypeProcessor.php @@ -6,12 +6,14 @@ namespace Magento\Framework\Reflection; use Magento\Framework\Exception\SerializationException; +use Magento\Framework\Phrase; use Zend\Code\Reflection\ClassReflection; use Zend\Code\Reflection\ParameterReflection; -use Magento\Framework\Phrase; /** * Type processor of config reader properties + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class TypeProcessor { @@ -108,6 +110,9 @@ class TypeProcessor public function register($type) { $typeName = $this->normalizeType($type); + if (null === $typeName) { + return null; + } if (!$this->isTypeSimple($typeName) && !$this->isTypeAny($typeName)) { $typeSimple = $this->getArrayItemType($type); if (!(class_exists($typeSimple) || interface_exists($typeSimple))) { @@ -288,6 +293,9 @@ class TypeProcessor */ public function normalizeType($type) { + if ($type == 'null') { + return null; + } $normalizationMap = [ self::STRING_TYPE => self::NORMALIZED_STRING_TYPE, self::INT_TYPE => self::NORMALIZED_INT_TYPE, diff --git a/lib/internal/Magento/Framework/Test/Unit/PhraseTest.php b/lib/internal/Magento/Framework/Test/Unit/PhraseTest.php index 79a257b4ff77ceb505138f1cc486499c9ee06ca3..f3e8c8d9e0195e9fd64d659d6a020d71c8f93f88 100755 --- a/lib/internal/Magento/Framework/Test/Unit/PhraseTest.php +++ b/lib/internal/Magento/Framework/Test/Unit/PhraseTest.php @@ -19,6 +19,11 @@ class PhraseTest extends \PHPUnit_Framework_TestCase */ protected $rendererMock; + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + protected $objectManager; + /** * SetUp method * @@ -29,6 +34,7 @@ class PhraseTest extends \PHPUnit_Framework_TestCase $this->defaultRenderer = Phrase::getRenderer(); $this->rendererMock = $this->getMockBuilder('Magento\Framework\Phrase\RendererInterface') ->getMock(); + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); } /** @@ -51,7 +57,10 @@ class PhraseTest extends \PHPUnit_Framework_TestCase $text = 'some text'; $arguments = ['arg1', 'arg2']; $result = 'rendered text'; - $phrase = new Phrase($text, $arguments); + $phrase = $this->objectManager->getObject('Magento\Framework\Phrase', [ + 'text' => $text, + 'arguments' => $arguments, + ]); Phrase::setRenderer($this->rendererMock); $this->rendererMock->expects($this->once()) @@ -72,7 +81,9 @@ class PhraseTest extends \PHPUnit_Framework_TestCase $this->rendererMock->expects($this->never()) ->method('render'); - new Phrase('some text'); + $this->objectManager->getObject('Magento\Framework\Phrase', [ + 'text' => 'some text', + ]); } /** @@ -85,7 +96,10 @@ class PhraseTest extends \PHPUnit_Framework_TestCase $text = 'some text'; $arguments = ['arg1', 'arg2']; $result = 'rendered text'; - $phrase = new Phrase($text, $arguments); + $phrase = $this->objectManager->getObject('Magento\Framework\Phrase', [ + 'text' => $text, + 'arguments' => $arguments, + ]); Phrase::setRenderer($this->rendererMock); $this->rendererMock->expects($this->once()) @@ -105,6 +119,9 @@ class PhraseTest extends \PHPUnit_Framework_TestCase { $text = 'some text'; $phrase = new Phrase($text); + $phrase = $this->objectManager->getObject('Magento\Framework\Phrase', [ + 'text' => $text, + ]); $this->assertEquals($text, $phrase->getText()); } @@ -118,10 +135,36 @@ class PhraseTest extends \PHPUnit_Framework_TestCase { $text = 'some text'; $arguments = ['arg1', 'arg2']; - $phrase1 = new Phrase($text); - $phrase2 = new Phrase($text, $arguments); + $phrase1 = $this->objectManager->getObject('Magento\Framework\Phrase', [ + 'text' => $text, + ]); + $phrase2 = $this->objectManager->getObject('Magento\Framework\Phrase', [ + 'text' => $text, + 'arguments' => $arguments, + ]); $this->assertEquals([], $phrase1->getArguments()); $this->assertEquals($arguments, $phrase2->getArguments()); } + + /** + * Test default rendering + * + * @return void + */ + public function testDefaultRendering() + { + $text = 'parameter1 is replaced by %1 parameter2 is replaced by %2'; + $arguments = ['arg1', 'arg2']; + $result = 'parameter1 is replaced by arg1 parameter2 is replaced by arg2'; + $phrase = $this->objectManager->getObject('Magento\Framework\Phrase', [ + 'text' => $text, + 'arguments' => $arguments, + ]); + + $this->assertEquals($text, $phrase->getText()); + $this->assertEquals($arguments, $phrase->getArguments()); + $this->assertTrue($phrase->getRenderer() instanceof \Magento\Framework\Phrase\Renderer\Placeholder); + $this->assertEquals($result, $phrase->render()); + } } 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(); +}