Skip to content

Commit

Permalink
14086: Guest cart API ignoring cartId in url for some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed Mar 5, 2020
1 parent 5a9fcab commit 2bdcc11
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 25 deletions.
49 changes: 49 additions & 0 deletions app/code/Magento/Quote/Plugin/UpdateCartId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Quote\Plugin;

use Magento\Framework\Webapi\Rest\Request as RestRequest;
use Magento\Quote\Api\Data\CartItemInterface;
use Magento\Quote\Api\GuestCartItemRepositoryInterface;

/**
* Update cart id from request param
*/
class UpdateCartId
{
/**
* @var RestRequest $request
*/
private $request;

/**
* @param RestRequest $request
*/
public function __construct(RestRequest $request)
{
$this->request = $request;
}

/**
* Update id from request if param cartId exist
*
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
* @param CartItemInterface $cartItem
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeSave(
GuestCartItemRepositoryInterface $guestCartItemRepository,
CartItemInterface $cartItem
): void {
if ($cartId = $this->request->getParam('cartId')) {
$cartItem->setQuoteId($cartId);
}
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Quote/etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
<plugin name="accessControl" type="Magento\Quote\Model\QuoteRepository\Plugin\AccessChangeQuoteControl" />
<plugin name="authorization" type="Magento\Quote\Model\QuoteRepository\Plugin\Authorization" />
</type>
<type name="Magento\Quote\Api\GuestCartItemRepositoryInterface">
<plugin name="updateCartIdFromRequest" type="Magento\Quote\Plugin\UpdateCartId" />
</type>
</config>
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Api;

use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\CatalogInventory\Model\Stock;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\WebapiAbstract;

/**
* Test for Magento\Quote\Api\GuestCartItemRepositoryInterface.
*/
class GuestCartItemRepositoryTest extends WebapiAbstract
{
const SERVICE_VERSION = 'V1';
const SERVICE_NAME = 'quoteGuestCartItemRepositoryV1';
const RESOURCE_PATH = '/V1/guest-carts/';
public const SERVICE_NAME = 'quoteGuestCartItemRepositoryV1';
private const SERVICE_VERSION = 'V1';
private const RESOURCE_PATH = '/V1/guest-carts/';

/**
* @var \Magento\TestFramework\ObjectManager
* @var ObjectManager
*/
protected $objectManager;
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->objectManager = Bootstrap::getObjectManager();
}

/**
* Test quote items
*
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
*/
public function testGetList()
Expand Down Expand Up @@ -112,12 +121,16 @@ public function testAddItem()
];

$requestData = [
"cartItem" => [
"sku" => $productSku,
"qty" => 7,
"quote_id" => $cartId,
'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());
Expand Down Expand Up @@ -205,20 +218,11 @@ public function testUpdateItem(array $stockData, string $errorMessage = null)
],
];

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,
],
$requestData['cartItem']['qty'] = 5;
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
$requestData['cartItem'] += [
'quote_id' => $cartId,
'itemId' => $itemId,
];
}
if ($errorMessage) {
Expand Down

0 comments on commit 2bdcc11

Please sign in to comment.