Skip to content

Commit 62b18ee

Browse files
committed
Revert "AC-15054: Product Add to Cart issue in Rest API"
This reverts commit a22d447.
1 parent f7a2871 commit 62b18ee

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

app/code/Magento/Quote/Plugin/UpdateCartId.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public function beforeSave(
6363
/**
6464
* Validate product's website assignment for guest cart item
6565
*
66-
* @param CartItemInterface $cartItem
67-
* @return void
6866
* @throws LocalizedException
6967
*/
7068
private function validateProductWebsiteAssignment(CartItemInterface $cartItem): void

app/code/Magento/Quote/Test/Unit/Plugin/Webapi/ValidateProductWebsiteAssignmentTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,65 @@ public function testBeforeSaveProductWebsiteIdsNotArray()
301301
$this->plugin->beforeSave($this->cartItemRepositoryMock, $this->cartItemMock);
302302
}
303303

304+
/**
305+
* Test validation when product is not found
306+
*/
307+
public function testBeforeSaveProductNotFound()
308+
{
309+
$sku = 'test-product';
310+
$quoteId = 1;
311+
$storeId = 1;
312+
$productId = 123;
313+
314+
$this->cartItemMock->expects($this->once())
315+
->method('getSku')
316+
->willReturn($sku);
317+
318+
$this->cartItemMock->expects($this->once())
319+
->method('getQuoteId')
320+
->willReturn($quoteId);
321+
322+
$this->cartRepositoryMock->expects($this->once())
323+
->method('getActive')
324+
->with($quoteId)
325+
->willReturn($this->quoteMock);
326+
327+
$this->quoteMock->expects($this->once())
328+
->method('getAllItems')
329+
->willReturn([$this->quoteItemMock]);
330+
331+
$this->quoteItemMock->expects($this->once())
332+
->method('getSku')
333+
->willReturn($sku);
334+
335+
$this->quoteItemMock->expects($this->once())
336+
->method('getStoreId')
337+
->willReturn($storeId);
338+
339+
$this->quoteItemMock->expects($this->once())
340+
->method('getProductId')
341+
->willReturn($productId);
342+
343+
$this->storeManagerMock->expects($this->once())
344+
->method('getStore')
345+
->with($storeId)
346+
->willReturn($this->storeMock);
347+
348+
$this->storeMock->expects($this->once())
349+
->method('getWebsiteId')
350+
->willReturn(1);
351+
352+
$this->productRepositoryMock->expects($this->once())
353+
->method('getById')
354+
->with($productId, false, $storeId)
355+
->willThrowException(new NoSuchEntityException(__('Product not found')));
356+
357+
$this->expectException(LocalizedException::class);
358+
$this->expectExceptionMessage('Product that you are trying to add is not available.');
359+
360+
$this->plugin->beforeSave($this->cartItemRepositoryMock, $this->cartItemMock);
361+
}
362+
304363
/**
305364
* Test validation skips when no SKU provided
306365
*/
@@ -531,4 +590,42 @@ public function testBeforeSaveWebsiteIdZero()
531590
// If we reach this point, validation passed
532591
$this->assertTrue(true);
533592
}
593+
594+
/**
595+
* Test validation when no matching quote item found for SKU
596+
*/
597+
public function testBeforeSaveNoMatchingQuoteItem()
598+
{
599+
$sku = 'test-product';
600+
$quoteId = 1;
601+
$differentSku = 'different-product';
602+
603+
$this->cartItemMock->expects($this->once())
604+
->method('getSku')
605+
->willReturn($sku);
606+
607+
$this->cartItemMock->expects($this->once())
608+
->method('getQuoteId')
609+
->willReturn($quoteId);
610+
611+
$this->cartRepositoryMock->expects($this->once())
612+
->method('getActive')
613+
->with($quoteId)
614+
->willReturn($this->quoteMock);
615+
616+
$this->quoteMock->expects($this->once())
617+
->method('getAllItems')
618+
->willReturn([$this->quoteItemMock]);
619+
620+
$this->quoteItemMock->expects($this->once())
621+
->method('getSku')
622+
->willReturn($differentSku); // Different SKU
623+
624+
// No further method calls expected since no matching item found
625+
// Method returns void, so we just verify no exception is thrown
626+
$this->plugin->beforeSave($this->cartItemRepositoryMock, $this->cartItemMock);
627+
628+
// If we reach this point, validation passed
629+
$this->assertTrue(true);
630+
}
534631
}

dev/tests/api-functional/testsuite/Magento/Quote/Api/ValidateProductWebsiteAssignmentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Quote\Api;
8+
namespace Magento\Quote\Test\Api;
99

1010
use Magento\Catalog\Test\Fixture\Product;
1111
use Magento\Customer\Test\Fixture\Customer;

0 commit comments

Comments
 (0)