init
This commit is contained in:
+265
@@ -0,0 +1,265 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. 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 BillingAddressManagementTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteBillingAddressManagementV1';
|
||||
const RESOURCE_PATH = '/V1/carts/';
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
|
||||
*/
|
||||
public function testGetAddress()
|
||||
{
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote\Address $address */
|
||||
$address = $quote->getBillingAddress();
|
||||
|
||||
$data = [
|
||||
AddressInterface::KEY_ID => (int)$address->getId(),
|
||||
AddressInterface::KEY_REGION => $address->getRegion(),
|
||||
AddressInterface::KEY_REGION_ID => $address->getRegionId(),
|
||||
AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
|
||||
AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
|
||||
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_CUSTOMER_ID => $address->getCustomerId(),
|
||||
AddressInterface::KEY_EMAIL => $address->getEmail(),
|
||||
AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
|
||||
AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
|
||||
AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook()
|
||||
|
||||
];
|
||||
|
||||
$cartId = $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];
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
asort($data);
|
||||
asort($response);
|
||||
$this->assertEquals($data, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $quote->getId() . '/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' => '',
|
||||
'company' => 'Magento Commerce 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" => $quote->getId(),
|
||||
'address' => $addressData,
|
||||
];
|
||||
|
||||
$addressId = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
//reset $quote to reload data
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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']);
|
||||
unset($addressData['email']);
|
||||
$this->assertEquals('billing', $savedData['address_type']);
|
||||
//check the rest of fields
|
||||
foreach ($addressData as $key => $value) {
|
||||
$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\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote\Address $address */
|
||||
$address = $quote->getBillingAddress();
|
||||
|
||||
$data = [
|
||||
AddressInterface::KEY_ID => (int)$address->getId(),
|
||||
AddressInterface::KEY_REGION => $address->getRegion(),
|
||||
AddressInterface::KEY_REGION_ID => $address->getRegionId(),
|
||||
AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
|
||||
AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
|
||||
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_CUSTOMER_ID => $address->getCustomerId(),
|
||||
AddressInterface::KEY_EMAIL => $address->getEmail(),
|
||||
AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
|
||||
AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
|
||||
AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook()
|
||||
|
||||
];
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . 'mine/billing-address',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
'token' => $token
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->_webApiCall($serviceInfo);
|
||||
|
||||
asort($data);
|
||||
asort($response);
|
||||
$this->assertEquals($data, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
|
||||
*/
|
||||
public function testSetMyAddress()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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',
|
||||
'company' => 'Magento Commerce 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::class);
|
||||
$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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
||||
use Magento\Framework\Exception\AuthenticationException;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\QuoteIdMask;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Class for payment info in quote for registered customer.
|
||||
*/
|
||||
class CartAddingItemsTest extends WebapiAbstract
|
||||
{
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
/**
|
||||
* @var ProductResource
|
||||
*/
|
||||
private $productResource;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $createdQuotes = [];
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->productResource = $this->objectManager->get(ProductResource::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
foreach ($this->createdQuotes as $quoteId) {
|
||||
$quote->load($quoteId);
|
||||
$quote->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test qty for cart after adding grouped product qty specified only for goruped product.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped_with_simple.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
|
||||
* @return void
|
||||
*/
|
||||
public function testAddToCartGroupedWithParentQuantity(): void
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// Get customer ID token
|
||||
/** @var CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
|
||||
$token = $customerTokenService->createCustomerAccessToken(
|
||||
'customer_one_address@test.com',
|
||||
'password'
|
||||
);
|
||||
|
||||
// Creating empty cart for registered customer.
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/mine',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
|
||||
$quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
|
||||
$this->assertGreaterThan(0, $quoteId);
|
||||
|
||||
/** @var CartRepositoryInterface $cartRepository */
|
||||
$cartRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$quote = $cartRepository->get($quoteId);
|
||||
|
||||
$quoteItems = $quote->getItemsCollection();
|
||||
foreach ($quoteItems as $item) {
|
||||
$quote->removeItem($item->getId())->save();
|
||||
}
|
||||
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'grouped',
|
||||
'qty' => 7
|
||||
]
|
||||
];
|
||||
$this->_webApiCall($this->getServiceInfoAddToCart($token), $requestData);
|
||||
|
||||
foreach ($quote->getAllItems() as $item) {
|
||||
$this->assertEquals(7, $item->getQty());
|
||||
}
|
||||
$this->createdQuotes[] = $quoteId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test price for cart after adding product to.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_without_options_with_stock_data.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
|
||||
* @return void
|
||||
*/
|
||||
public function testPriceForCreatingQuoteFromEmptyCart()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// Get customer ID token
|
||||
/** @var CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken(
|
||||
'customer_one_address@test.com',
|
||||
'password'
|
||||
);
|
||||
|
||||
// Creating empty cart for registered customer.
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/mine',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
|
||||
$quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
|
||||
$this->assertGreaterThan(0, $quoteId);
|
||||
|
||||
// Adding item to the cart
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'simple',
|
||||
'qty' => 1
|
||||
]
|
||||
];
|
||||
$item = $this->_webApiCall($this->getServiceInfoAddToCart($token), $requestData);
|
||||
$this->assertNotEmpty($item);
|
||||
$this->assertEquals(10, $item['price']);
|
||||
|
||||
// Get payment information
|
||||
$serviceInfoForGettingPaymentInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/mine/payment-information',
|
||||
'httpMethod' => Request::HTTP_METHOD_GET,
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
$paymentInfo = $this->_webApiCall($serviceInfoForGettingPaymentInfo);
|
||||
$this->assertEquals($paymentInfo['totals']['grand_total'], 10);
|
||||
|
||||
$this->createdQuotes[] = $quoteId;
|
||||
// /** @var \Magento\Quote\Model\Quote $quote */
|
||||
// $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
// $quote->load($quoteId);
|
||||
// $quote->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test qty for cart after adding grouped product with custom qty.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped_with_simple.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
|
||||
* @return void
|
||||
*/
|
||||
public function testAddToCartGroupedCustomQuantity(): void
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
$firstProductId = $this->productResource->getIdBySku('simple_11');
|
||||
$secondProductId = $this->productResource->getIdBySku('simple_22');
|
||||
$qtyData = [$firstProductId => 2, $secondProductId => 4];
|
||||
|
||||
// Get customer ID token
|
||||
/** @var CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
|
||||
$token = $customerTokenService->createCustomerAccessToken(
|
||||
'customer_one_address@test.com',
|
||||
'password'
|
||||
);
|
||||
|
||||
// Creating empty cart for registered customer.
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/mine',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
|
||||
$quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
|
||||
$this->assertGreaterThan(0, $quoteId);
|
||||
|
||||
// Adding item to the cart
|
||||
$productOptionData = [
|
||||
'extension_attributes' => [
|
||||
'grouped_options' => [
|
||||
['id' => $firstProductId, 'qty' => $qtyData[$firstProductId]],
|
||||
['id' => $secondProductId, 'qty' => $qtyData[$secondProductId]],
|
||||
]
|
||||
]
|
||||
];
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'grouped',
|
||||
'qty' => 1,
|
||||
'product_option' => $productOptionData
|
||||
]
|
||||
];
|
||||
$response = $this->_webApiCall($this->getServiceInfoAddToCart($token), $requestData);
|
||||
$this->assertArrayHasKey('product_option', $response);
|
||||
$this->assertEquals($response['product_option'], $productOptionData);
|
||||
|
||||
/** @var CartRepositoryInterface $cartRepository */
|
||||
$cartRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$quote = $cartRepository->get($quoteId);
|
||||
|
||||
foreach ($quote->getAllItems() as $item) {
|
||||
$this->assertEquals($qtyData[$item->getProductId()], $item->getQty());
|
||||
}
|
||||
$this->createdQuotes[] = $quoteId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding grouped product when qty for grouped_options not specified.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped_with_simple.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
|
||||
* @return void
|
||||
*/
|
||||
public function testAddToCartGroupedCustomQuantityNotAllParamsSpecified(): void
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
$productId = $this->productResource->getIdBySku('simple_11');
|
||||
|
||||
// Get customer ID token
|
||||
/** @var CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
|
||||
$token = $customerTokenService->createCustomerAccessToken(
|
||||
'customer_one_address@test.com',
|
||||
'password'
|
||||
);
|
||||
|
||||
// Creating empty cart for registered customer.
|
||||
$serviceInfo = [
|
||||
'rest' => ['resourcePath' => '/V1/carts/mine', 'httpMethod' => Request::HTTP_METHOD_POST, 'token' => $token]
|
||||
];
|
||||
|
||||
$quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
|
||||
$this->assertGreaterThan(0, $quoteId);
|
||||
|
||||
// Adding item to the cart
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'grouped',
|
||||
'qty' => 1,
|
||||
'product_option' => [
|
||||
'extension_attributes' => [
|
||||
'grouped_options' => [
|
||||
['id' => $productId],
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$this->createdQuotes[] = $quoteId;
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Please specify id and qty for grouped options.');
|
||||
|
||||
$this->_webApiCall($this->getServiceInfoAddToCart($token), $requestData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns service info add to cart
|
||||
*
|
||||
* @param string $token
|
||||
* @return array
|
||||
*/
|
||||
private function getServiceInfoAddToCart(string $token): array
|
||||
{
|
||||
return [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/mine/items',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for product name in different store view
|
||||
*
|
||||
* @magentoConfigFixture web/url/use_store 1
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_multistore.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
*
|
||||
* @param string $expectedProductName
|
||||
* @param string|null $storeCode
|
||||
*
|
||||
* @return void
|
||||
* @dataProvider dataProviderForMultiStoreView
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function testForProductNameAsPerStoreView(string $expectedProductName, ?string $storeCode = null): void
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// Get customer ID token
|
||||
/** @var CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
|
||||
$token = $customerTokenService->createCustomerAccessToken(
|
||||
'customer@example.com',
|
||||
'password'
|
||||
);
|
||||
|
||||
// Creating empty cart for registered customer.
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/mine',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
$quoteId = $this->_webApiCall($serviceInfo);
|
||||
$this->assertGreaterThan(0, $quoteId);
|
||||
|
||||
// Add product to cart
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'simple',
|
||||
'qty' => 1
|
||||
]
|
||||
];
|
||||
$this->_webApiCall($this->getServiceInfoAddToCart($token), $requestData);
|
||||
|
||||
// Fetch Cart info
|
||||
$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, [], null, $storeCode);
|
||||
$carts = $cart['items'];
|
||||
$actualProductName = $carts[0]['name'] ?? '';
|
||||
|
||||
$this->assertEquals($expectedProductName, $actualProductName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForMultiStoreView(): array
|
||||
{
|
||||
return [
|
||||
'noStoreCodeInRequestPath' => [
|
||||
'Simple Product One',
|
||||
null
|
||||
],
|
||||
'defaultStoreCodeInRequestPath' => [
|
||||
'Simple Product One',
|
||||
'default'
|
||||
],
|
||||
'secondStoreCodeInRequestPath' => [
|
||||
'StoreTitle',
|
||||
'fixturestore'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Catalog\Model\CustomOptions\CustomOptionProcessor;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
class CartItemRepositoryTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteCartItemRepositoryV1';
|
||||
const RESOURCE_PATH = '/V1/carts/';
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_and_custom_options_saved.php
|
||||
*/
|
||||
public function testGetList()
|
||||
{
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items_and_custom_options', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
$output = [];
|
||||
$customOptionProcessor = $this->objectManager->get(CustomOptionProcessor::class);
|
||||
|
||||
/** @var \Magento\Quote\Api\Data\CartItemInterface $item */
|
||||
foreach ($quote->getAllItems() as $item) {
|
||||
$customOptionProcessor->processOptions($item);
|
||||
$data = [
|
||||
'item_id' => (int)$item->getItemId(),
|
||||
'sku' => $item->getSku(),
|
||||
'name' => $item->getName(),
|
||||
'price' => (float)$item->getPrice(),
|
||||
'qty' => (float)$item->getQty(),
|
||||
'product_type' => $item->getProductType(),
|
||||
'quote_id' => $item->getQuoteId(),
|
||||
];
|
||||
|
||||
if ($item->getProductOption() !== null) {
|
||||
$customOptions = $item->getProductOption()->getExtensionAttributes()->getCustomOptions();
|
||||
foreach ($customOptions as $option) {
|
||||
$data['product_option']['extension_attributes']['custom_options'][] = $option->getData();
|
||||
}
|
||||
}
|
||||
|
||||
$output[] = $data;
|
||||
}
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
|
||||
'httpMethod' => 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_without_options.php
|
||||
*/
|
||||
public function testAddItem()
|
||||
{
|
||||
/** @var \Magento\Catalog\Model\Product $product */
|
||||
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(2);
|
||||
$productSku = $product->getSku();
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
|
||||
'httpMethod' => 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 Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
|
||||
$productId = $product->getIdBySku('simple_one');
|
||||
$product->load($productId);
|
||||
$itemId = $quote->getItemByProduct($product)->getId();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
|
||||
'httpMethod' => 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(Quote::class);
|
||||
$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 Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
|
||||
$productId = $product->getIdBySku('simple_one');
|
||||
$product->load($productId);
|
||||
$itemId = $quote->getItemByProduct($product)->getId();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
|
||||
'httpMethod' => 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(Quote::class);
|
||||
$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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,497 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Framework\App\Config;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Quote Cart Management API test
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
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 = [];
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$appConfig = $this->objectManager->get(Config::class);
|
||||
$appConfig->clean();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
foreach ($this->createdQuotes as $quoteId) {
|
||||
$quote->load($quoteId);
|
||||
$quote->delete();
|
||||
}
|
||||
}
|
||||
|
||||
public function testCreateEmptyCartForGuest()
|
||||
{
|
||||
$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->assertGreaterThan(0, $quoteId);
|
||||
$this->createdQuotes[] = $quoteId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
|
||||
*/
|
||||
public function testCreateEmptyCartForCustomer()
|
||||
{
|
||||
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
|
||||
$repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
|
||||
/** @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_one_address.php
|
||||
*/
|
||||
public function testCreateEmptyCartAndGetCartForCustomer()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken(
|
||||
'customer_one_address@test.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']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class)->load('test01', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
|
||||
$repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
|
||||
/** @var $customer \Magento\Customer\Api\Data\CustomerInterface */
|
||||
$customer = $repository->getById(1);
|
||||
$customerId = $customer->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/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::class)->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
|
||||
*/
|
||||
public function testAssignCustomerThrowsExceptionIfThereIsNoCustomerWithGivenId()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->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/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
|
||||
*/
|
||||
public function testAssignCustomerThrowsExceptionIfThereIsNoCartWithGivenId()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$cartId = 9999;
|
||||
$customerId = 1;
|
||||
$serviceInfo = [
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => self::SERVICE_NAME . 'AssignCustomer',
|
||||
],
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/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
|
||||
*/
|
||||
public function testAssignCustomerThrowsExceptionIfTargetCartIsNotAnonymous()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('The customer can\'t be assigned to the cart because the cart isn\'t anonymous.');
|
||||
|
||||
/** @var $customer \Magento\Customer\Model\Customer */
|
||||
$customer = $this->objectManager->create(\Magento\Customer\Model\Customer::class)->load(1);
|
||||
$customerId = $customer->getId();
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
'resourcePath' => '/V1/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
|
||||
*/
|
||||
public function testAssignCustomerThrowsExceptionIfCartIsAssignedToDifferentStore()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage(
|
||||
'The customer can\'t be assigned to the cart. The cart belongs to a different store.'
|
||||
);
|
||||
|
||||
$repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
|
||||
/** @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::class)->load('test01', 'reserved_order_id');
|
||||
|
||||
$customerId = $customer->getId();
|
||||
$cartId = $quote->getId();
|
||||
|
||||
$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/carts/' . $cartId,
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'cartId' => $cartId,
|
||||
'customerId' => $customerId,
|
||||
'storeId' => 1,
|
||||
];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
*/
|
||||
public function testAssignCustomerCartMerged()
|
||||
{
|
||||
/** @var $customer \Magento\Customer\Model\Customer */
|
||||
$customer = $this->objectManager->create(\Magento\Customer\Model\Customer::class)->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::class)
|
||||
->load('test_order_item_with_items', 'reserved_order_id');
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
|
||||
$expectedQuoteItemsQty = $customerQuote->getItemsQty() + $quote->getItemsQty();
|
||||
|
||||
$cartId = $quote->getId();
|
||||
$customerId = $customer->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'operation' => self::SERVICE_NAME . 'AssignCustomer',
|
||||
'serviceVersion' => 'V1',
|
||||
],
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/' . $cartId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'cartId' => $cartId,
|
||||
'customerId' => $customerId,
|
||||
'storeId' => 1,
|
||||
];
|
||||
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
|
||||
|
||||
$mergedQuote = $this->objectManager
|
||||
->create(\Magento\Quote\Model\Quote::class)
|
||||
->load('test01', 'reserved_order_id');
|
||||
|
||||
$this->assertEquals($expectedQuoteItemsQty, $mergedQuote->getItemsQty());
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class)
|
||||
->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'soap' => [
|
||||
'service' => 'quoteCartManagementV1',
|
||||
'operation' => 'quoteCartManagementV1PlaceOrder',
|
||||
'serviceVersion' => 'V1',
|
||||
],
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/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::class)->load($orderId);
|
||||
$items = $order->getAllItems();
|
||||
$this->assertCount(1, $items);
|
||||
$this->assertEquals('Simple Product', $items[0]->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
|
||||
*/
|
||||
public function testPlaceOrderForMyCart()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$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::class)->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\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
$cart = $this->getCart('test01');
|
||||
$customerId = $cart->getCustomer()->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/mine',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
'token' => $token
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'quoteCartManagementV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartManagementV1GetCartForCustomer',
|
||||
'token' => $token
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = ['customerId' => $customerId];
|
||||
$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']);
|
||||
|
||||
$this->assertArrayHasKey('customer', $cartData);
|
||||
$this->assertFalse($cartData['customer_is_guest']);
|
||||
$this->assertArrayHasKey('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']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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::class);
|
||||
$cart->load($reservedOrderId, 'reserved_order_id');
|
||||
if (!$cart->getId()) {
|
||||
throw new \InvalidArgumentException('There is no quote with provided reserved order ID.');
|
||||
}
|
||||
return $cart;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,389 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Framework\Api\FilterBuilder;
|
||||
use Magento\Framework\Api\SearchCriteriaBuilder;
|
||||
use Magento\Framework\Api\SortOrderBuilder;
|
||||
use Magento\Framework\Api\SortOrder;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class CartRepositoryTest extends WebapiAbstract
|
||||
{
|
||||
private static $mineCartUrl = '/V1/carts/mine';
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var SearchCriteriaBuilder
|
||||
*/
|
||||
private $searchCriteriaBuilder;
|
||||
|
||||
/**
|
||||
* @var SortOrderBuilder
|
||||
*/
|
||||
private $sortOrderBuilder;
|
||||
|
||||
/**
|
||||
* @var FilterBuilder
|
||||
*/
|
||||
private $filterBuilder;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->filterBuilder = $this->objectManager->create(
|
||||
\Magento\Framework\Api\FilterBuilder::class
|
||||
);
|
||||
$this->sortOrderBuilder = $this->objectManager->create(
|
||||
\Magento\Framework\Api\SortOrderBuilder::class
|
||||
);
|
||||
$this->searchCriteriaBuilder = $this->objectManager->create(
|
||||
\Magento\Framework\Api\SearchCriteriaBuilder::class
|
||||
);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
try {
|
||||
/** @var CartRepositoryInterface $quoteRepository */
|
||||
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$cart = $this->getCart('test01');
|
||||
$quoteRepository->delete($cart);
|
||||
} 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
|
||||
*/
|
||||
private function getCart($reservedOrderId)
|
||||
{
|
||||
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
|
||||
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
|
||||
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
|
||||
->create();
|
||||
|
||||
/** @var CartRepositoryInterface $quoteRepository */
|
||||
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$items = $quoteRepository->getList($searchCriteria)->getItems();
|
||||
|
||||
if (empty($items)) {
|
||||
throw new \InvalidArgumentException('There is no quote with provided reserved order ID.');
|
||||
}
|
||||
|
||||
return array_pop($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests successfull get cart web-api call.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
*/
|
||||
public function testGetCart()
|
||||
{
|
||||
$cart = $this->getCart('test01');
|
||||
$cartId = $cart->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/' . $cartId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'quoteCartRepositoryV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartRepositoryV1Get',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = ['cartId' => $cartId];
|
||||
$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->assertArrayHasKey('customer', $cartData);
|
||||
$this->assertTrue($cartData['customer_is_guest']);
|
||||
$this->assertArrayHasKey('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']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests exception when cartId is not provided.
|
||||
*
|
||||
*/
|
||||
public function testGetCartThrowsExceptionIfThereIsNoCartWithProvidedId()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('No such entity with');
|
||||
|
||||
$cartId = 9999;
|
||||
|
||||
$serviceInfo = [
|
||||
'soap' => [
|
||||
'service' => 'quoteCartRepositoryV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartRepositoryV1Get',
|
||||
],
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/' . $cartId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = ['cartId' => $cartId];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests carts search.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
*/
|
||||
public function testGetList()
|
||||
{
|
||||
$cart = $this->getCart('test01');
|
||||
|
||||
// The following two filters are used as alternatives. The target cart does not match the first one.
|
||||
$grandTotalFilter = $this->filterBuilder->setField('grand_total')
|
||||
->setConditionType('gteq')
|
||||
->setValue(15)
|
||||
->create();
|
||||
$subtotalFilter = $this->filterBuilder->setField('subtotal')
|
||||
->setConditionType('eq')
|
||||
->setValue($cart->getSubtotal())
|
||||
->create();
|
||||
|
||||
$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)
|
||||
->create();
|
||||
$maxCreatedAtFilter = $this->filterBuilder->setField('created_at')
|
||||
->setConditionType('lteq')
|
||||
->setValue($tomorrowDate)
|
||||
->create();
|
||||
|
||||
$this->searchCriteriaBuilder->addFilters([$grandTotalFilter, $subtotalFilter]);
|
||||
$this->searchCriteriaBuilder->addFilters([$minCreatedAtFilter]);
|
||||
$this->searchCriteriaBuilder->addFilters([$maxCreatedAtFilter]);
|
||||
$this->searchCriteriaBuilder->addFilter('reserved_order_id', 'test01');
|
||||
/** @var SortOrder $sortOrder */
|
||||
$sortOrder = $this->sortOrderBuilder->setField('subtotal')->setDirection(SortOrder::SORT_ASC)->create();
|
||||
$this->searchCriteriaBuilder->setSortOrders([$sortOrder]);
|
||||
$searchCriteria = $this->searchCriteriaBuilder->create()->__toArray();
|
||||
$requestData = ['searchCriteria' => $searchCriteria];
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/search' . '?' . http_build_query($requestData),
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'quoteCartRepositoryV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartRepositoryV1GetList',
|
||||
],
|
||||
];
|
||||
|
||||
$searchResult = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertArrayHasKey('total_count', $searchResult);
|
||||
$this->assertEquals(1, $searchResult['total_count']);
|
||||
$this->assertArrayHasKey('items', $searchResult);
|
||||
$this->assertCount(1, $searchResult['items']);
|
||||
|
||||
$cartData = $searchResult['items'][0];
|
||||
$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->assertArrayHasKey('customer_is_guest', $cartData);
|
||||
$this->assertEquals(1, $cartData['customer_is_guest']);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testGetListThrowsExceptionIfProvidedSearchFieldIsInvalid()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$serviceInfo = [
|
||||
'soap' => [
|
||||
'service' => 'quoteCartRepositoryV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartRepositoryV1GetList',
|
||||
],
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/carts/search',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
];
|
||||
|
||||
$invalidFilter = $this->filterBuilder->setField('invalid_field')
|
||||
->setConditionType('eq')
|
||||
->setValue(0)
|
||||
->create();
|
||||
|
||||
$this->searchCriteriaBuilder->addFilters([$invalidFilter]);
|
||||
$searchCriteria = $this->searchCriteriaBuilder->create()->__toArray();
|
||||
$requestData = ['searchCriteria' => $searchCriteria];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saving quote - negative case, attempt to change customer id in the active quote for the user with Customer role.
|
||||
*
|
||||
* @dataProvider customerIdDataProvider
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
|
||||
*/
|
||||
public function testSaveQuoteException($customerId)
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Invalid state change requested');
|
||||
|
||||
$token = $this->getToken();
|
||||
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->getCart('test_order_1');
|
||||
|
||||
$requestData = $this->getRequestData($quote->getId());
|
||||
// Replace to customer id not much with current user id..
|
||||
$requestData['quote']['customer']['id'] = $customerId;
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::$mineCartUrl,
|
||||
'httpMethod' => Request::HTTP_METHOD_PUT,
|
||||
'token' => $token
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'quoteCartRepositoryV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartRepositoryV1Save',
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saving quote - positive case: successful change correct customer data.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
|
||||
*/
|
||||
public function testSaveQuote()
|
||||
{
|
||||
$token = $this->getToken();
|
||||
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->getCart('test_order_1');
|
||||
|
||||
$requestData = $this->getRequestData($quote->getId());
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::$mineCartUrl,
|
||||
'httpMethod' => Request::HTTP_METHOD_PUT,
|
||||
'token' => $token
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'quoteCartRepositoryV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartRepositoryV1Save',
|
||||
'token' => $token
|
||||
]
|
||||
];
|
||||
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
$quote->loadActive($requestData["quote"]["id"]);
|
||||
$this->assertEquals($requestData["quote"]["customer"]["firstname"], $quote->getCustomerFirstname());
|
||||
$this->assertEquals($requestData["quote"]["customer"]["middlename"], $quote->getCustomerMiddlename());
|
||||
$this->assertEquals($requestData["quote"]["customer"]["lastname"], $quote->getCustomerLastname());
|
||||
$this->assertEquals($requestData["quote"]["customer"]["email"], $quote->getCustomerEmail());
|
||||
}
|
||||
|
||||
/**
|
||||
* Request to api for the current user token.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getToken()
|
||||
{
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
CustomerTokenServiceInterface::class
|
||||
);
|
||||
|
||||
return $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Request's data for tests.
|
||||
*
|
||||
* @param $quoteId Int
|
||||
* @return array
|
||||
*/
|
||||
private function getRequestData($quoteId)
|
||||
{
|
||||
$requestData['quote'] = [
|
||||
'id' => $quoteId,
|
||||
'store_id' => 1,
|
||||
'customer' => [
|
||||
'id' => 1,
|
||||
'middlename' => 'Middlename_Test',
|
||||
'firstname' => 'Firstname_Test',
|
||||
'lastname' => 'Lastname_Test',
|
||||
'email' => 'customer@test.com'
|
||||
]
|
||||
];
|
||||
|
||||
return $requestData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides different types of customer id.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function customerIdDataProvider()
|
||||
{
|
||||
return [[999],[null],['25']];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
|
||||
use Magento\Quote\Api\Data\AddressInterface;
|
||||
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\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
|
||||
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
|
||||
use Magento\Tax\Test\Fixture\TaxRule as TaxRule;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
use Magento\TestFramework\Fixture\Config as Config;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\Quote\Address;
|
||||
use Magento\TestModuleOverrideConfig\Inheritance\Fixtures\FixturesInterface;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class CartTotalRepositoryTest extends WebapiAbstract
|
||||
{
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var SearchCriteriaBuilder
|
||||
*/
|
||||
private $searchCriteriaBuilder;
|
||||
|
||||
/**
|
||||
* @var FilterBuilder
|
||||
*/
|
||||
private $filterBuilder;
|
||||
|
||||
/**
|
||||
* @var FixturesInterface
|
||||
*/
|
||||
private $fixtures;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->searchCriteriaBuilder = $this->objectManager->create(
|
||||
\Magento\Framework\Api\SearchCriteriaBuilder::class
|
||||
);
|
||||
$this->filterBuilder = $this->objectManager->create(
|
||||
\Magento\Framework\Api\FilterBuilder::class
|
||||
);
|
||||
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote\Address $shippingAddress */
|
||||
$shippingAddress = $quote->getShippingAddress();
|
||||
|
||||
$data = $this->getData($quote, $shippingAddress);
|
||||
$data = $this->formatTotalsData($data);
|
||||
|
||||
$requestData = ['cartId' => $cartId];
|
||||
|
||||
$actual = $this->_webApiCall($this->getServiceInfoForTotalsService($cartId), $requestData);
|
||||
unset($actual['items'][0]['options']);
|
||||
unset($actual['weee_tax_applied_amount']);
|
||||
|
||||
/** TODO: cover total segments with separate test */
|
||||
unset($actual['total_segments']);
|
||||
if (array_key_exists('extension_attributes', $actual)) {
|
||||
unset($actual['extension_attributes']);
|
||||
}
|
||||
$this->assertEquals($data, $actual);
|
||||
}
|
||||
|
||||
#[
|
||||
Config('tax/defaults/region_id', '43'),
|
||||
Config('tax/defaults/postcode', '10036'),
|
||||
Config('shipping/origin/region_id', '43'),
|
||||
Config('tax/defaults/postcode', '10011'),
|
||||
DataFixture(
|
||||
TaxRule::class,
|
||||
[
|
||||
'tax_rate_ids' => [2],
|
||||
'product_tax_class_ids' => [2],
|
||||
'customer_tax_class_ids' => [3]
|
||||
],
|
||||
'tax_rule'
|
||||
),
|
||||
DataFixture(
|
||||
ProductFixture::class,
|
||||
[
|
||||
'price' => 5
|
||||
],
|
||||
'product'
|
||||
),
|
||||
DataFixture(
|
||||
GuestCartFixture::class,
|
||||
as: 'cart'
|
||||
),
|
||||
DataFixture(
|
||||
AddProductToCartFixture::class,
|
||||
[
|
||||
'cart_id' => '$cart.id$',
|
||||
'product_id' => '$product.id$'
|
||||
]
|
||||
),
|
||||
DataFixture(
|
||||
SetBillingAddressFixture::class,
|
||||
[
|
||||
'cart_id' => '$cart.id$',
|
||||
'address' => [
|
||||
AddressInterface::KEY_POSTCODE => 10036,
|
||||
AddressInterface::KEY_CITY => 'New York',
|
||||
AddressInterface::KEY_REGION_ID => 43
|
||||
]
|
||||
]
|
||||
),
|
||||
DataFixture(
|
||||
SetShippingAddressFixture::class,
|
||||
[
|
||||
'cart_id' => '$cart.id$',
|
||||
'address' => [
|
||||
AddressInterface::KEY_POSTCODE => 10036,
|
||||
AddressInterface::KEY_CITY => 'New York',
|
||||
AddressInterface::KEY_REGION_ID => 43
|
||||
]
|
||||
]
|
||||
),
|
||||
DataFixture(
|
||||
SetGuestEmailFixture::class,
|
||||
[
|
||||
'cart_id' => '$cart.id$'
|
||||
]
|
||||
),
|
||||
DataFixture(
|
||||
SetDeliveryMethodFixture::class,
|
||||
[
|
||||
'cart_id' => '$cart.id$'
|
||||
]
|
||||
),
|
||||
DataFixture(
|
||||
SetPaymentMethodFixture::class,
|
||||
[
|
||||
'cart_id' => '$cart.id$'
|
||||
]
|
||||
),
|
||||
]
|
||||
public function testGetGrandTotalsWithIncludedTaxAndSameCurrency()
|
||||
{
|
||||
$cart = $this->fixtures->get('cart');
|
||||
$cartId = $cart->getid();
|
||||
$requestData = ['cartId' => $cartId];
|
||||
$actual = $this->_webApiCall($this->getServiceInfoForTotalsService($cartId), $requestData);
|
||||
$this->assertEquals($actual['base_grand_total'], $actual['grand_total']);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testGetTotalsWithAbsentQuote()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('No such entity');
|
||||
|
||||
$cartId = 9999999999;
|
||||
$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' => 'quoteCartTotalRepositoryV1',
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => 'quoteCartTotalRepositoryV1get',
|
||||
],
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/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((float) $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_ITEM_ID => $item->getItemId(),
|
||||
ItemTotals::KEY_PRICE => (int)$item->getPrice(),
|
||||
ItemTotals::KEY_BASE_PRICE => (int)$item->getBasePrice(),
|
||||
ItemTotals::KEY_QTY => $item->getQty(),
|
||||
ItemTotals::KEY_ROW_TOTAL => (int)$item->getRowTotal(),
|
||||
ItemTotals::KEY_BASE_ROW_TOTAL => (int)$item->getBaseRowTotal(),
|
||||
ItemTotals::KEY_ROW_TOTAL_WITH_DISCOUNT => (int)$item->getRowTotalWithDiscount(),
|
||||
ItemTotals::KEY_TAX_AMOUNT => (int)$item->getTaxAmount(),
|
||||
ItemTotals::KEY_BASE_TAX_AMOUNT => (int)$item->getBaseTaxAmount(),
|
||||
ItemTotals::KEY_TAX_PERCENT => (int)$item->getTaxPercent(),
|
||||
ItemTotals::KEY_DISCOUNT_AMOUNT => (int)$item->getDiscountAmount(),
|
||||
ItemTotals::KEY_BASE_DISCOUNT_AMOUNT => (int)$item->getBaseDiscountAmount(),
|
||||
ItemTotals::KEY_DISCOUNT_PERCENT => (int)$item->getDiscountPercent(),
|
||||
ItemTotals::KEY_PRICE_INCL_TAX => (int)$item->getPriceInclTax(),
|
||||
ItemTotals::KEY_BASE_PRICE_INCL_TAX => (int)$item->getBasePriceInclTax(),
|
||||
ItemTotals::KEY_ROW_TOTAL_INCL_TAX => (int)$item->getRowTotalInclTax(),
|
||||
ItemTotals::KEY_BASE_ROW_TOTAL_INCL_TAX => (int)$item->getBaseRowTotalInclTax(),
|
||||
ItemTotals::KEY_WEEE_TAX_APPLIED_AMOUNT => $item->getWeeeTaxAppliedAmount(),
|
||||
ItemTotals::KEY_WEEE_TAX_APPLIED => $item->getWeeeTaxApplied(),
|
||||
ItemTotals::KEY_NAME => $item->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
|
||||
*/
|
||||
public function testGetMyTotals()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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 = $this->getData($quote, $shippingAddress);
|
||||
$data = $this->formatTotalsData($data);
|
||||
|
||||
$actual = $this->_webApiCall($serviceInfo);
|
||||
unset($actual['items'][0]['options']);
|
||||
unset($actual['weee_tax_applied_amount']);
|
||||
|
||||
/** TODO: cover total segments with separate test */
|
||||
unset($actual['total_segments']);
|
||||
if (array_key_exists('extension_attributes', $actual)) {
|
||||
unset($actual['extension_attributes']);
|
||||
}
|
||||
$this->assertEquals($data, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get expected data.
|
||||
*
|
||||
* @param Quote $quote
|
||||
* @param Address $shippingAddress
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getData(Quote $quote, Address $shippingAddress) : array
|
||||
{
|
||||
return [
|
||||
Totals::KEY_GRAND_TOTAL => $quote->getGrandTotal(),
|
||||
Totals::KEY_BASE_GRAND_TOTAL => $quote->getBaseGrandTotal(),
|
||||
Totals::KEY_SUBTOTAL => $quote->getSubtotal(),
|
||||
Totals::KEY_BASE_SUBTOTAL => $quote->getBaseSubtotal(),
|
||||
Totals::KEY_DISCOUNT_AMOUNT => $shippingAddress->getDiscountAmount(),
|
||||
Totals::KEY_BASE_DISCOUNT_AMOUNT => $shippingAddress->getBaseDiscountAmount(),
|
||||
Totals::KEY_SUBTOTAL_WITH_DISCOUNT => $quote->getSubtotalWithDiscount(),
|
||||
Totals::KEY_BASE_SUBTOTAL_WITH_DISCOUNT => $quote->getBaseSubtotalWithDiscount(),
|
||||
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_QTY => $quote->getItemsQty(),
|
||||
Totals::KEY_ITEMS => [$this->getQuoteItemTotalsData($quote)],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,305 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Coupon management service tests
|
||||
*/
|
||||
class CouponManagementTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteCouponManagementV1';
|
||||
const RESOURCE_PATH = '/V1/carts/';
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $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::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $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
|
||||
*/
|
||||
public function testSetCouponThrowsExceptionIfCouponDoesNotExist()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('The coupon code isn\'t valid. Verify the code and try again.');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
$couponCode = 'invalid_coupon_code';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . urlencode($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::class);
|
||||
$quote->load('test01', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
/** @var \Magento\SalesRule\Model\Rule $salesRule */
|
||||
$salesRule = $this->objectManager->create(\Magento\SalesRule\Model\Rule::class);
|
||||
$salesRuleId = $this->objectManager->get(\Magento\Framework\Registry::class)
|
||||
->registry('Magento/Checkout/_file/discount_10percent');
|
||||
$salesRule->load($salesRuleId);
|
||||
$couponCode = $salesRule->getPrimaryCoupon()->getCode();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . urlencode($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::class);
|
||||
$quoteWithCoupon->load('test01', 'reserved_order_id');
|
||||
|
||||
$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\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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
|
||||
*/
|
||||
public function testSetMyCouponThrowsExceptionIfCouponDoesNotExist()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('The coupon code isn\'t valid. Verify the code and try again.');
|
||||
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
$couponCode = 'invalid_coupon_code';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . urlencode($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\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test01', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
/** @var \Magento\SalesRule\Model\Rule $salesRule */
|
||||
$salesRule = $this->objectManager->create(\Magento\SalesRule\Model\Rule::class);
|
||||
$salesRuleId = $this->objectManager->get(\Magento\Framework\Registry::class)
|
||||
->registry('Magento/Checkout/_file/discount_10percent_generalusers');
|
||||
$salesRule->load($salesRuleId);
|
||||
$couponCode = $salesRule->getPrimaryCoupon()->getCode();
|
||||
|
||||
/* Since this isn't a full quote fixture, need to assign it to the right customer */
|
||||
$cartManagementService = $this->objectManager->create(
|
||||
\Magento\Quote\Api\CartManagementInterface::class
|
||||
);
|
||||
$cartManagementService->assignCustomer($cartId, 1, 1);
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . urlencode($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::class);
|
||||
$quoteWithCoupon->load('test01', 'reserved_order_id');
|
||||
|
||||
$this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
|
||||
}
|
||||
}
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. 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(): void
|
||||
{
|
||||
$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::class)->create();
|
||||
$quoteIdMask->load($quoteId, 'quote_id');
|
||||
return $quoteIdMask->getMaskedId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
|
||||
*/
|
||||
public function testGetAddress()
|
||||
{
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote\Address $address */
|
||||
$address = $quote->getBillingAddress();
|
||||
|
||||
$data = [
|
||||
AddressInterface::KEY_ID => (int)$address->getId(),
|
||||
AddressInterface::KEY_REGION => $address->getRegion(),
|
||||
AddressInterface::KEY_REGION_ID => $address->getRegionId(),
|
||||
AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
|
||||
AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
|
||||
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_CUSTOMER_ID => $address->getCustomerId(),
|
||||
AddressInterface::KEY_EMAIL => $address->getEmail(),
|
||||
AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
|
||||
AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
|
||||
AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook(),
|
||||
];
|
||||
|
||||
$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];
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
asort($data);
|
||||
asort($response);
|
||||
$this->assertEquals($data, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
|
||||
* @dataProvider setAddressDataProvider
|
||||
*/
|
||||
public function testSetAddress($useForShipping)
|
||||
{
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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' => '',
|
||||
'company' => 'Magento Commerce 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,
|
||||
'useForShipping' => $useForShipping
|
||||
];
|
||||
|
||||
$addressId = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
//reset $quote to reload data
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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']);
|
||||
unset($addressData['email']);
|
||||
$this->assertEquals('billing', $savedData['address_type']);
|
||||
//check the rest of fields
|
||||
foreach ($addressData as $key => $value) {
|
||||
$this->assertEquals($value, $savedData[$key]);
|
||||
}
|
||||
$address = $quote->getShippingAddress();
|
||||
$address->getRegionCode();
|
||||
$savedData = $address->getData();
|
||||
if ($useForShipping) {
|
||||
//check that shipping address set
|
||||
$this->assertEquals('shipping', $savedData['address_type']);
|
||||
$this->assertEquals(1, $savedData['same_as_billing']);
|
||||
//check the rest of fields
|
||||
foreach ($addressData as $key => $value) {
|
||||
$this->assertEquals($value, $savedData[$key]);
|
||||
}
|
||||
} else {
|
||||
$this->assertEquals(0, $savedData['same_as_billing']);
|
||||
}
|
||||
}
|
||||
|
||||
public function setAddressDataProvider()
|
||||
{
|
||||
return [
|
||||
[true],
|
||||
[false]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Option;
|
||||
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Class for testing adding and deleting items flow.
|
||||
*/
|
||||
class GuestCartAddingItemsTest extends WebapiAbstract
|
||||
{
|
||||
private const SERVICE_VERSION = 'V1';
|
||||
private const SERVICE_NAME = 'quoteGuestCartManagementV1';
|
||||
private const RESOURCE_PATH = '/V1/guest-carts/';
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
/**
|
||||
* @var ProductResource|mixed
|
||||
*/
|
||||
private mixed $productResource;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->productResource = $this->objectManager->get(ProductResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add to product with custom option and test with updating custom options.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_custom_options.php
|
||||
* @return void
|
||||
*/
|
||||
public function testAddtoCartWithCustomOptionsForCreatingQuoteFromEmptyCart()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
|
||||
$productId = $this->productResource->getIdBySku('simple_with_custom_options');
|
||||
$product = $this->objectManager->create(Product::class)->load($productId);
|
||||
$customOptionCollection = $this->objectManager->get(Option::class)
|
||||
->getProductOptionCollection($product);
|
||||
$customOptions = [];
|
||||
foreach ($customOptionCollection as $option) {
|
||||
$customOptions [] = [
|
||||
'option_id' => $option->getId(),
|
||||
'option_value' => $option->getType() !== 'field'
|
||||
? current($option->getValues())->getOptionTypeId()
|
||||
: 'test'
|
||||
];
|
||||
}
|
||||
|
||||
// Creating empty cart
|
||||
$serviceInfoForCreatingEmptyCart = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
]
|
||||
];
|
||||
$quoteId = $this->_webApiCall($serviceInfoForCreatingEmptyCart);
|
||||
|
||||
// Adding item to the cart
|
||||
$serviceInfoForAddingProduct = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
]
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'simple_with_custom_options',
|
||||
'qty' => 1,
|
||||
'product_option' => [
|
||||
'extension_attributes' => [
|
||||
'custom_options' => $customOptions
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
$item = $this->_webApiCall($serviceInfoForAddingProduct, $requestData);
|
||||
$this->assertNotEmpty($item);
|
||||
foreach ($customOptionCollection as $option) {
|
||||
$customOptions [] = [
|
||||
'option_id' => $option->getId(),
|
||||
'option_value' => $option->getType() != 'field'
|
||||
? current(array_reverse($option->getValues()))->getOptionTypeId()
|
||||
: 'test2'
|
||||
];
|
||||
}
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'simple_with_custom_options',
|
||||
'qty' => 1,
|
||||
'product_option' => [
|
||||
'extension_attributes' => [
|
||||
'custom_options' => $customOptions
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
// Update the item for the cart
|
||||
$serviceInfoForUpdateProduct = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items/' . $item['item_id'],
|
||||
'httpMethod' => Request::HTTP_METHOD_PUT,
|
||||
]
|
||||
];
|
||||
|
||||
$item = $this->_webApiCall($serviceInfoForUpdateProduct, $requestData);
|
||||
$this->assertNotEmpty($item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test price for cart after deleting and adding product to.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_without_options_with_stock_data.php
|
||||
* @return void
|
||||
*/
|
||||
public function testPriceForCreatingQuoteFromEmptyCart()
|
||||
{
|
||||
// Creating empty cart
|
||||
$serviceInfoForCreatingEmptyCart = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'CreateEmptyCart',
|
||||
],
|
||||
];
|
||||
$quoteId = $this->_webApiCall($serviceInfoForCreatingEmptyCart);
|
||||
|
||||
// Adding item to the cart
|
||||
$serviceInfoForAddingProduct = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => GuestCartItemRepositoryTest::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => GuestCartItemRepositoryTest::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'simple',
|
||||
'qty' => 1
|
||||
]
|
||||
];
|
||||
$item = $this->_webApiCall($serviceInfoForAddingProduct, $requestData);
|
||||
$this->assertNotEmpty($item);
|
||||
|
||||
// Delete the item for the cart
|
||||
$serviceInfoForDeleteProduct = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items/' . $item['item_id'],
|
||||
'httpMethod' => Request::HTTP_METHOD_DELETE,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => GuestCartItemRepositoryTest::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => GuestCartItemRepositoryTest::SERVICE_NAME . 'deleteById',
|
||||
],
|
||||
];
|
||||
$response = (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) ?
|
||||
$this->_webApiCall($serviceInfoForDeleteProduct, ['cartId' => $quoteId, 'itemId' => $item['item_id']])
|
||||
: $this->_webApiCall($serviceInfoForDeleteProduct);
|
||||
$this->assertTrue($response);
|
||||
|
||||
// Add one more item and check price for this item
|
||||
$serviceInfoForAddingProduct = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => GuestCartItemRepositoryTest::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => GuestCartItemRepositoryTest::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
$requestData = [
|
||||
'cartItem' => [
|
||||
'quote_id' => $quoteId,
|
||||
'sku' => 'simple',
|
||||
'qty' => 1
|
||||
]
|
||||
];
|
||||
$item = $this->_webApiCall($serviceInfoForAddingProduct, $requestData);
|
||||
$this->assertNotEmpty($item);
|
||||
$this->assertEquals($item['price'], 10);
|
||||
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load($quoteId);
|
||||
$quote->delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\CatalogInventory\Api\StockRegistryInterface;
|
||||
use Magento\CatalogInventory\Model\Stock;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\QuoteIdMask;
|
||||
use Magento\Quote\Model\QuoteIdMaskFactory;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Test for Magento\Quote\Api\GuestCartItemRepositoryInterface.
|
||||
*/
|
||||
class GuestCartItemRepositoryTest extends WebapiAbstract
|
||||
{
|
||||
public const SERVICE_NAME = 'quoteGuestCartItemRepositoryV1';
|
||||
private const SERVICE_VERSION = 'V1';
|
||||
private const RESOURCE_PATH = '/V1/guest-carts/';
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test quote items
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
|
||||
*/
|
||||
public function testGetList()
|
||||
{
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
/** @var QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = Bootstrap::getObjectManager()
|
||||
->create(QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//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_without_options.php
|
||||
*/
|
||||
public function testAddItem()
|
||||
{
|
||||
/** @var Product $product */
|
||||
$product = $this->objectManager->create(Product::class)->load(2);
|
||||
$productSku = $product->getSku();
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
/** @var QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = Bootstrap::getObjectManager()
|
||||
->create(QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/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,
|
||||
],
|
||||
];
|
||||
|
||||
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
|
||||
$requestData['cartItem']['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 Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
/** @var QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = Bootstrap::getObjectManager()
|
||||
->create(QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
|
||||
$product = $this->objectManager->create(Product::class);
|
||||
$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(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$this->assertFalse($quote->hasProductId($productId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
|
||||
* @param array $stockData
|
||||
* @param string|null $errorMessage
|
||||
* @dataProvider updateItemDataProvider
|
||||
*/
|
||||
public function testUpdateItem(array $stockData, string $errorMessage = null)
|
||||
{
|
||||
$this->updateStockData('simple_one', $stockData);
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
/** @var QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = Bootstrap::getObjectManager()
|
||||
->create(QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
|
||||
$product = $this->objectManager->create(Product::class);
|
||||
$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_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData['cartItem']['qty'] = 5;
|
||||
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
|
||||
$requestData['cartItem'] += [
|
||||
'quote_id' => $cartId,
|
||||
'itemId' => $itemId,
|
||||
];
|
||||
}
|
||||
if ($errorMessage) {
|
||||
$this->expectExceptionMessage($errorMessage);
|
||||
}
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that store id for quote and quote item is being changed accordingly to the requested store code
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
|
||||
* @magentoApiDataFixture Magento/Store/_files/second_store.php
|
||||
*/
|
||||
public function testUpdateItemWithChangingStoreId()
|
||||
{
|
||||
/** @var Quote $quote */
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
|
||||
/** @var QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = Bootstrap::getObjectManager()
|
||||
->create(QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
|
||||
$product = $this->objectManager->create(Product::class);
|
||||
$productId = $product->getIdBySku('simple');
|
||||
$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_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData['cartItem']['qty'] = 5;
|
||||
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
|
||||
$requestData['cartItem'] += [
|
||||
'quote_id' => $cartId,
|
||||
'itemId' => $itemId,
|
||||
];
|
||||
}
|
||||
$this->_webApiCall($serviceInfo, $requestData, null, 'fixture_second_store');
|
||||
$quote = $this->objectManager->create(Quote::class);
|
||||
$quote->load('test_order_item_with_items', 'reserved_order_id');
|
||||
$this->assertTrue($quote->hasProductId(1));
|
||||
$item = $quote->getItemByProduct($product);
|
||||
/** @var StoreManagerInterface $storeManager */
|
||||
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
|
||||
$storeId = $storeManager->getStore('fixture_second_store')
|
||||
->getId();
|
||||
$this->assertEquals($storeId, $quote->getStoreId());
|
||||
$this->assertEquals($storeId, $item->getStoreId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function updateItemDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
[]
|
||||
],
|
||||
[
|
||||
[
|
||||
'qty' => 0,
|
||||
'is_in_stock' => 1,
|
||||
'use_config_manage_stock' => 0,
|
||||
'manage_stock' => 1,
|
||||
'use_config_backorders' => 0,
|
||||
'backorders' => Stock::BACKORDERS_YES_NOTIFY,
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
'qty' => 0,
|
||||
'is_in_stock' => 1,
|
||||
'use_config_manage_stock' => 0,
|
||||
'manage_stock' => 1,
|
||||
'use_config_backorders' => 0,
|
||||
'backorders' => Stock::BACKORDERS_NO,
|
||||
],
|
||||
'There are no source items with the in stock status'
|
||||
],
|
||||
[
|
||||
[
|
||||
'qty' => 2,
|
||||
'is_in_stock' => 1,
|
||||
'use_config_manage_stock' => 0,
|
||||
'manage_stock' => 1,
|
||||
'use_config_backorders' => 0,
|
||||
'backorders' => Stock::BACKORDERS_NO,
|
||||
],
|
||||
'The requested qty is not available'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update product stock
|
||||
*
|
||||
* @param string $sku
|
||||
* @param array $stockData
|
||||
* @return void
|
||||
*/
|
||||
private function updateStockData(string $sku, array $stockData): void
|
||||
{
|
||||
if ($stockData) {
|
||||
/** @var $stockRegistry StockRegistryInterface */
|
||||
$stockRegistry = $this->objectManager->create(StockRegistryInterface::class);
|
||||
$stockItem = $stockRegistry->getStockItemBySku($sku);
|
||||
$stockItem->addData($stockData);
|
||||
$stockRegistry->updateStockItemBySku($sku, $stockItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
use Magento\Checkout\Helper\Data;
|
||||
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture;
|
||||
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
|
||||
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
|
||||
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
|
||||
use Magento\TestFramework\Fixture\Config;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
class GuestCartManagementTest extends WebapiAbstract
|
||||
{
|
||||
private const SERVICE_VERSION = 'V1';
|
||||
private const SERVICE_NAME = 'quoteGuestCartManagementV1';
|
||||
private const RESOURCE_PATH = '/V1/guest-carts/';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $createdQuotes = [];
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
foreach ($this->createdQuotes as $quoteId) {
|
||||
$quote->load($quoteId);
|
||||
$quote->delete();
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMask::class);
|
||||
$quoteIdMask->load($quoteId, 'quote_id');
|
||||
$quoteIdMask->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class)->load('test01', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMaskFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class);
|
||||
$quoteIdMask = $quoteIdMaskFactory->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
|
||||
$repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
|
||||
/** @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,
|
||||
'token' => $token
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => self::SERVICE_NAME . 'AssignCustomer',
|
||||
'token' => $token
|
||||
],
|
||||
];
|
||||
|
||||
$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::class)->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());
|
||||
$this->assertNull($quoteIdMaskFactory->create()->load($cartId, 'masked_id')->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
*/
|
||||
public function testAssignCustomerThrowsExceptionIfThereIsNoCustomerWithGivenId()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->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
|
||||
*/
|
||||
public function testAssignCustomerThrowsExceptionIfThereIsNoCartWithGivenId()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$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
|
||||
*/
|
||||
public function testAssignCustomerThrowsExceptionIfTargetCartIsNotAnonymous()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('The customer can\'t be assigned to the cart because the cart isn\'t anonymous.');
|
||||
|
||||
/** @var $customer \Magento\Customer\Model\Customer */
|
||||
$customer = $this->objectManager->create(\Magento\Customer\Model\Customer::class)->load(1);
|
||||
$customerId = $customer->getId();
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
'resourcePath' => '/V1/guest-carts/' . $cartId,
|
||||
'token' => $token
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => self::SERVICE_NAME . 'AssignCustomer',
|
||||
'token' => $token
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'cartId' => $cartId,
|
||||
'customerId' => $customerId,
|
||||
'storeId' => 1,
|
||||
];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
*/
|
||||
public function testAssignCustomerCartMerged()
|
||||
{
|
||||
/** @var $customer \Magento\Customer\Model\Customer */
|
||||
$customer = $this->objectManager->create(\Magento\Customer\Model\Customer::class)->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::class)
|
||||
->load('test_order_item_with_items', 'reserved_order_id');
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
|
||||
$expectedQuoteItemsQty = $customerQuote->getItemsQty() + $quote->getItemsQty();
|
||||
|
||||
$cartId = $quote->getId();
|
||||
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
|
||||
$customerId = $customer->getId();
|
||||
|
||||
// get customer ID token
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
$serviceInfo = [
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'operation' => self::SERVICE_NAME . 'AssignCustomer',
|
||||
'serviceVersion' => 'V1',
|
||||
'token' => $token
|
||||
],
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/guest-carts/' . $cartId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
'token' => $token
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'cartId' => $cartId,
|
||||
'customerId' => $customerId,
|
||||
'storeId' => 1,
|
||||
];
|
||||
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
|
||||
$mergedQuote = $this->objectManager
|
||||
->create(\Magento\Quote\Model\Quote::class)
|
||||
->load('test01', 'reserved_order_id');
|
||||
|
||||
$this->assertEquals($expectedQuoteItemsQty, $mergedQuote->getItemsQty());
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class)
|
||||
->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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//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::class)->load($orderId);
|
||||
$items = $order->getAllItems();
|
||||
$this->assertCount(1, $items);
|
||||
$this->assertEquals('Simple Product', $items[0]->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testAssignCustomerByGuestUser()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Enter and try again.');
|
||||
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMaskFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class);
|
||||
$quoteIdMask = $quoteIdMaskFactory->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
$repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
|
||||
/** @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->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
|
||||
#[
|
||||
Config(Data::XML_PATH_GUEST_CHECKOUT, 0),
|
||||
DataFixture(ProductFixture::class, as: 'product'),
|
||||
DataFixture(GuestCartFixture::class, as: 'cart'),
|
||||
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
|
||||
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
|
||||
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
|
||||
DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']),
|
||||
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
|
||||
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
|
||||
]
|
||||
public function testPlaceOrderWhenGuestCheckoutIsDisabled(): void
|
||||
{
|
||||
$this->expectExceptionMessage('Sorry, guest checkout is not available.');
|
||||
$fixtures = DataFixtureStorageManager::getStorage();
|
||||
$cart = $fixtures->get('cart');
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cart->getId(), 'quote_id');
|
||||
//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,
|
||||
],
|
||||
];
|
||||
$this->_webApiCall($serviceInfo, ['cartId' => $cartId]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. 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(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
try {
|
||||
$cart = $this->getCart('test01');
|
||||
$cartId = $cart->getId();
|
||||
$cart->delete();
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMask::class);
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
$quoteIdMask->delete();
|
||||
} 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::class);
|
||||
$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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
|
||||
$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->assertArrayHasKey('customer', $cartData);
|
||||
$this->assertTrue($cartData['customer_is_guest']);
|
||||
$this->assertArrayHasKey('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']);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testGetCartThrowsExceptionIfThereIsNoCartWithProvidedId()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('No such entity with');
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
+199
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. 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 $searchCriteriaBuilder;
|
||||
|
||||
/**
|
||||
* @var FilterBuilder
|
||||
*/
|
||||
private $filterBuilder;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->searchCriteriaBuilder = $this->objectManager->create(
|
||||
\Magento\Framework\Api\SearchCriteriaBuilder::class
|
||||
);
|
||||
$this->filterBuilder = $this->objectManager->create(
|
||||
\Magento\Framework\Api\FilterBuilder::class
|
||||
);
|
||||
}
|
||||
|
||||
protected function getQuoteMaskedId($quoteId)
|
||||
{
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)->create();
|
||||
$quoteIdMask->load($quoteId, 'quote_id');
|
||||
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::class);
|
||||
$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_GRAND_TOTAL => $quote->getGrandTotal(),
|
||||
Totals::KEY_BASE_GRAND_TOTAL => $quote->getBaseGrandTotal(),
|
||||
Totals::KEY_SUBTOTAL => $quote->getSubtotal(),
|
||||
Totals::KEY_BASE_SUBTOTAL => $quote->getBaseSubtotal(),
|
||||
Totals::KEY_DISCOUNT_AMOUNT => $shippingAddress->getDiscountAmount(),
|
||||
Totals::KEY_BASE_DISCOUNT_AMOUNT => $shippingAddress->getBaseDiscountAmount(),
|
||||
Totals::KEY_SUBTOTAL_WITH_DISCOUNT => $quote->getSubtotalWithDiscount(),
|
||||
Totals::KEY_BASE_SUBTOTAL_WITH_DISCOUNT => $quote->getBaseSubtotalWithDiscount(),
|
||||
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_QTY => $quote->getItemsQty(),
|
||||
Totals::KEY_ITEMS => [$this->getQuoteItemTotalsData($quote)],
|
||||
];
|
||||
|
||||
$requestData = ['cartId' => $cartId];
|
||||
|
||||
$data = $this->formatTotalsData($data);
|
||||
|
||||
$actual = $this->_webApiCall($this->getServiceInfoForTotalsService($cartId), $requestData);
|
||||
$actual = $this->formatTotalsData($actual);
|
||||
|
||||
unset($actual['items'][0]['options']);
|
||||
unset($actual['weee_tax_applied_amount']);
|
||||
|
||||
unset($actual['total_segments']);
|
||||
if (array_key_exists('extension_attributes', $actual)) {
|
||||
unset($actual['extension_attributes']);
|
||||
}
|
||||
|
||||
$this->assertEquals($data, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testGetTotalsWithAbsentQuote()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('No such entity');
|
||||
|
||||
$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((float) $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_ITEM_ID => $item->getItemId(),
|
||||
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(),
|
||||
ItemTotals::KEY_WEEE_TAX_APPLIED_AMOUNT => $item->getWeeeTaxAppliedAmount(),
|
||||
ItemTotals::KEY_WEEE_TAX_APPLIED => $item->getWeeeTaxApplied(),
|
||||
ItemTotals::KEY_NAME => $item->getName(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\QuoteIdMaskFactory;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\Quote\Model\QuoteIdMask;
|
||||
|
||||
/**
|
||||
* Class GuestCollectRatesTest checks that totals will be recollected properly with new shipping method
|
||||
*/
|
||||
class GuestCollectRatesTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteGuestCartTotalManagementV1';
|
||||
const RESOURCE_PATH = '/V1/guest-carts/';
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var Quote
|
||||
*/
|
||||
protected $quote;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->quote = $this->objectManager->create(Quote::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that totals are properly recollected after changing shipping method
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
|
||||
*/
|
||||
public function testCollectRatesWithChangedShippingMethod()
|
||||
{
|
||||
$this->quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $this->quote->getId();
|
||||
|
||||
/** @var QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = $this->objectManager
|
||||
->create(QuoteIdMaskFactory::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
$requestData = [
|
||||
"shippingMethodCode" => "freeshipping",
|
||||
"shippingCarrierCode" => "freeshipping",
|
||||
"paymentMethod" => [
|
||||
"method" => "checkmo",
|
||||
],
|
||||
];
|
||||
|
||||
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
|
||||
$requestData['cartId'] = $cartId;
|
||||
}
|
||||
|
||||
$totals = $this->_webApiCall($this->getListServiceInfo($cartId), $requestData);
|
||||
$this->assertEquals(20, $totals['grand_total']);
|
||||
$this->assertEquals(0, $totals['shipping_amount']);
|
||||
|
||||
$requestData['shippingMethodCode'] = 'flatrate';
|
||||
$requestData['shippingCarrierCode'] = 'flatrate';
|
||||
|
||||
$totals = $this->_webApiCall($this->getListServiceInfo($cartId), $requestData);
|
||||
$this->assertEquals(30, $totals['grand_total']);
|
||||
$this->assertEquals(10, $totals['shipping_amount']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Service info
|
||||
*
|
||||
* @param int $cartId
|
||||
* @return array
|
||||
*/
|
||||
protected function getListServiceInfo($cartId)
|
||||
{
|
||||
return [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/collect-totals',
|
||||
'httpMethod' => Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'quoteGuestCartTotalManagementV1CollectTotals',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. 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(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$createdQuotes = ['test_order_1', 'test01'];
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
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::class);
|
||||
$quoteIdMask->load($quoteId, 'quote_id');
|
||||
$quoteIdMask->delete();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getQuoteMaskedId($quoteId)
|
||||
{
|
||||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
|
||||
$quoteIdMask = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)->create();
|
||||
$quoteIdMask->load($quoteId, 'quote_id');
|
||||
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::class);
|
||||
$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::class);
|
||||
$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
|
||||
*/
|
||||
public function testSetCouponThrowsExceptionIfCouponDoesNotExist()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('The coupon code isn\'t valid. Verify the code and try again.');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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::class);
|
||||
$quote->load('test01', 'reserved_order_id');
|
||||
$cartId = $this->getQuoteMaskedId($quote->getId());
|
||||
$salesRule = $this->objectManager->create(\Magento\SalesRule\Model\Rule::class);
|
||||
$salesRuleId = $this->objectManager->get(\Magento\Framework\Registry::class)
|
||||
->registry('Magento/Checkout/_file/discount_10percent');
|
||||
$salesRule->load($salesRuleId);
|
||||
$couponCode = $salesRule->getPrimaryCoupon()->getCode();
|
||||
$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::class);
|
||||
$quoteWithCoupon->load('test01', 'reserved_order_id');
|
||||
|
||||
$this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
|
||||
}
|
||||
}
|
||||
+281
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. 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(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$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::class);
|
||||
$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::class);
|
||||
$quoteIdMask->load($cart->getId(), 'quote_id');
|
||||
$quoteIdMask->delete();
|
||||
} 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::class);
|
||||
$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
|
||||
],
|
||||
];
|
||||
|
||||
$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::class);
|
||||
$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'
|
||||
],
|
||||
];
|
||||
$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::class);
|
||||
$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'
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
|
||||
*/
|
||||
public function testSetPaymentWithSimpleProductWithoutAddress()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('The shipping address is missing. Set the address and try again.');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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'
|
||||
],
|
||||
];
|
||||
$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::class);
|
||||
$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::class);
|
||||
$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', '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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
return $quoteIdMask->getMaskedId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\Quote\Api\Data\AddressInterface;
|
||||
|
||||
class GuestShipmentEstimationTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteGuestShipmentEstimationV1';
|
||||
const RESOURCE_PATH = '/V1/guest-carts/';
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/SalesRule/_files/cart_rule_free_shipping.php
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
*/
|
||||
public function testEstimateByExtendedAddress()
|
||||
{
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test01', '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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/guest-carts/' . $cartId . '/estimate-shipping-methods',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => self::SERVICE_NAME . 'EstimateByExtendedAddress',
|
||||
],
|
||||
];
|
||||
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
|
||||
/** @var \Magento\Quote\Model\Quote\Address $address */
|
||||
$address = $quote->getBillingAddress();
|
||||
|
||||
$data = [
|
||||
AddressInterface::KEY_ID => (int)$address->getId(),
|
||||
AddressInterface::KEY_REGION => $address->getRegion(),
|
||||
AddressInterface::KEY_REGION_ID => $address->getRegionId(),
|
||||
AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
|
||||
AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
|
||||
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_CUSTOMER_ID => $address->getCustomerId(),
|
||||
AddressInterface::KEY_EMAIL => $address->getEmail(),
|
||||
AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
|
||||
AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
|
||||
AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook(),
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'cartId' => $cartId,
|
||||
'address' => $data
|
||||
];
|
||||
} else {
|
||||
$requestData = [
|
||||
'address' => [
|
||||
'country_id' => "US",
|
||||
'postcode' => null,
|
||||
'region' => null,
|
||||
'region_id' => null
|
||||
],
|
||||
];
|
||||
}
|
||||
// Cart must be anonymous (see fixture)
|
||||
$this->assertEmpty($quote->getCustomerId());
|
||||
|
||||
$result = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertCount(1, $result);
|
||||
foreach ($result as $rate) {
|
||||
$this->assertEquals("flatrate", $rate['carrier_code']);
|
||||
$this->assertEquals(0, $rate['amount']);
|
||||
}
|
||||
}
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\Framework\Api\ExtensibleDataInterface;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\Quote\Api\Data\AddressInterface;
|
||||
|
||||
class GuestShipmentEstimationWithExtensionAttributesTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteGuestShipmentEstimationV1';
|
||||
const RESOURCE_PATH = '/V1/guest-carts/';
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoApiDataFixture Magento/SalesRule/_files/cart_rule_free_shipping.php
|
||||
* @magentoApiDataFixture Magento/Sales/_files/quote.php
|
||||
*/
|
||||
public function testEstimateByExtendedAddress(): void
|
||||
{
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test01', '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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//Use masked cart Id
|
||||
$cartId = $quoteIdMask->getMaskedId();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/guest-carts/' . $cartId . '/estimate-shipping-methods',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => 'V1',
|
||||
'operation' => self::SERVICE_NAME . 'EstimateByExtendedAddress',
|
||||
],
|
||||
];
|
||||
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
|
||||
/** @var \Magento\Quote\Model\Quote\Address $address */
|
||||
$address = $quote->getShippingAddress();
|
||||
|
||||
$data = [
|
||||
AddressInterface::KEY_ID => (int)$address->getId(),
|
||||
AddressInterface::KEY_REGION => $address->getRegion(),
|
||||
AddressInterface::KEY_REGION_ID => $address->getRegionId(),
|
||||
AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
|
||||
AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
|
||||
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_CUSTOMER_ID => $address->getCustomerId(),
|
||||
AddressInterface::KEY_EMAIL => $address->getEmail(),
|
||||
AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
|
||||
AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
|
||||
AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook(),
|
||||
ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => [
|
||||
'discounts' => []
|
||||
]
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'cartId' => $cartId,
|
||||
'address' => $data
|
||||
];
|
||||
} else {
|
||||
|
||||
$requestData = [
|
||||
'address' => [
|
||||
'country_id' => "US",
|
||||
'postcode' => null,
|
||||
'region' => null,
|
||||
'region_id' => null,
|
||||
'extension_attributes' => [
|
||||
'discounts' => []
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
// Cart must be anonymous (see fixture)
|
||||
$this->assertEmpty($quote->getCustomerId());
|
||||
|
||||
$result = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertEquals(1, count($result));
|
||||
foreach ($result as $rate) {
|
||||
$this->assertEquals("flatrate", $rate['carrier_code']);
|
||||
$this->assertEquals(0, $rate['amount']);
|
||||
}
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. 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;
|
||||
|
||||
/**
|
||||
* @var \Magento\Quote\Model\Quote\TotalsCollector
|
||||
*/
|
||||
protected $totalsCollector;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$this->totalsCollector = $this->objectManager->create(\Magento\Quote\Model\Quote\TotalsCollector::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
|
||||
*
|
||||
*/
|
||||
public function testGetListForVirtualCart()
|
||||
{
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//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::class);
|
||||
$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::class)
|
||||
->create();
|
||||
$quoteIdMask->load($cartId, 'quote_id');
|
||||
//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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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::class);
|
||||
foreach ($groupedRates as $carrierRates) {
|
||||
foreach ($carrierRates as $rate) {
|
||||
$result[] = $converter->modelToDataObject($rate, $currencyCode)->__toArray();
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quotePaymentMethodManagementV1';
|
||||
const RESOURCE_PATH = '/V1/carts/';
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class);
|
||||
$quote->load('test_order_1_with_payment', 'reserved_order_id');
|
||||
$cartId = $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
|
||||
],
|
||||
];
|
||||
|
||||
$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::class);
|
||||
$quote->load('test_order_with_virtual_product', 'reserved_order_id');
|
||||
$cartId = $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'
|
||||
],
|
||||
];
|
||||
$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::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $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'
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
|
||||
*/
|
||||
public function testSetPaymentWithSimpleProductWithoutAddress()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('The shipping address is missing. Set the address and try again.');
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$quote->load('test_order_with_simple_product_without_address', 'reserved_order_id');
|
||||
$cartId = $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'
|
||||
],
|
||||
];
|
||||
$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::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $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::class);
|
||||
$quote->load('test_order_1_with_payment', 'reserved_order_id');
|
||||
$cartId = $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']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::class);
|
||||
$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::class);
|
||||
$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::class);
|
||||
$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'
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getPaymentMethodFieldsForAssert()
|
||||
{
|
||||
return ['method', 'po_number', 'additional_data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer ID token
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getCustomerToken()
|
||||
{
|
||||
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Quote\Api;
|
||||
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\Quote\Api\Data\ShippingMethodInterface;
|
||||
|
||||
class ShippingMethodManagementTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteShippingMethodManagementV1';
|
||||
const RESOURCE_PATH = '/V1/carts/';
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Quote\Model\Quote
|
||||
*/
|
||||
protected $quote;
|
||||
|
||||
/**
|
||||
* @var \Magento\Quote\Model\Quote\TotalsCollector
|
||||
*/
|
||||
protected $totalsCollector;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$this->totalsCollector = $this->objectManager->create(\Magento\Quote\Model\Quote\TotalsCollector::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
|
||||
*
|
||||
*/
|
||||
public function testGetListForVirtualCart()
|
||||
{
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
|
||||
$cartId = $quote->load('test_order_with_virtual_product', 'reserved_order_id')->getId();
|
||||
|
||||
$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::class);
|
||||
$quote->load('test_order_1', 'reserved_order_id');
|
||||
$cartId = $quote->getId();
|
||||
if (!$cartId) {
|
||||
$this->fail('quote fixture failed');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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\Api\CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\CustomerTokenServiceInterface::class
|
||||
);
|
||||
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
|
||||
|
||||
/** @var \Magento\Quote\Model\ShippingMethodManagementInterface $shippingMethodManagementService */
|
||||
$shippingMethodManagementService = $this->objectManager->create(
|
||||
\Magento\Quote\Model\ShippingMethodManagementInterface::class
|
||||
);
|
||||
$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(),
|
||||
ShippingMethodInterface::KEY_ERROR_MESSAGE => null,
|
||||
ShippingMethodInterface::KEY_PRICE_EXCL_TAX => $shippingMethod->getPriceExclTax(),
|
||||
ShippingMethodInterface::KEY_PRICE_INCL_TAX => $shippingMethod->getPriceInclTax(),
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedData, $result[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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::class);
|
||||
foreach ($groupedRates as $carrierRates) {
|
||||
foreach ($carrierRates as $rate) {
|
||||
$result[] = $converter->modelToDataObject($rate, $currencyCode)->__toArray();
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user