Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magento/magento2#14086: Guest cart API ignoring cartId in url for some methods #27172

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/code/Magento/Quote/Plugin/UpdateCartId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 {
$cartId = $this->request->getParam('cartId');

if ($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