Skip to content

Commit 677aad1

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'origin/MC-37603-2.4.2' into 2.4-develop
2 parents 87fed9a + d032b19 commit 677aad1

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Wishlist\Model\Wishlist;
99

10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Wishlist\Model\Item as WishlistItem;
1112
use Magento\Wishlist\Model\ItemFactory as WishlistItemFactory;
1213
use Magento\Wishlist\Model\ResourceModel\Item as WishlistItemResource;
@@ -63,7 +64,7 @@ public function __construct(
6364
public function execute(Wishlist $wishlist, array $wishlistItemsIds): WishlistOutput
6465
{
6566
foreach ($wishlistItemsIds as $wishlistItemId) {
66-
$this->removeItemFromWishlist((int) $wishlistItemId);
67+
$this->removeItemFromWishlist((int) $wishlistItemId, $wishlist);
6768
}
6869

6970
return $this->prepareOutput($wishlist);
@@ -73,12 +74,22 @@ public function execute(Wishlist $wishlist, array $wishlistItemsIds): WishlistOu
7374
* Remove product item from wishlist
7475
*
7576
* @param int $wishlistItemId
77+
* @param Wishlist $wishlist
7678
*
7779
* @return void
7880
*/
79-
private function removeItemFromWishlist(int $wishlistItemId): void
81+
private function removeItemFromWishlist(int $wishlistItemId, Wishlist $wishlist): void
8082
{
8183
try {
84+
if ($wishlist->getItem($wishlistItemId) == null) {
85+
throw new LocalizedException(
86+
__(
87+
'The wishlist item with ID "%id" does not belong to the wishlist',
88+
['id' => $wishlistItemId]
89+
)
90+
);
91+
}
92+
$wishlist->getItemCollection()->clear();
8293
/** @var WishlistItem $wishlistItem */
8394
$wishlistItem = $this->wishlistItemFactory->create();
8495
$this->wishlistItemResource->load($wishlistItem, $wishlistItemId);
@@ -90,6 +101,8 @@ private function removeItemFromWishlist(int $wishlistItemId): void
90101
}
91102

92103
$this->wishlistItemResource->delete($wishlistItem);
104+
} catch (LocalizedException $exception) {
105+
$this->addError($exception->getMessage());
93106
} catch (\Exception $e) {
94107
$this->addError(
95108
__(

app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ public function execute(Wishlist $wishlist, array $wishlistItems): WishlistOutpu
9090
private function updateItemInWishlist(Wishlist $wishlist, WishlistItemData $wishlistItemData): void
9191
{
9292
try {
93+
if ($wishlist->getItem($wishlistItemData->getId()) == null) {
94+
throw new LocalizedException(
95+
__(
96+
'The wishlist item with ID "%id" does not belong to the wishlist',
97+
['id' => $wishlistItemData->getId()]
98+
)
99+
);
100+
}
101+
$wishlist->getItemCollection()->clear();
93102
$options = $this->buyRequestBuilder->build($wishlistItemData);
94103
/** @var WishlistItem $wishlistItem */
95104
$wishlistItem = $this->wishlistItemFactory->create();

dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/DeleteProductsFromWishlistTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ public function testDeleteWishlistItemFromWishlist(): void
5555
$this->assertEmpty($wishlistResponse['items_v2']);
5656
}
5757

58+
/**
59+
* Test deleting the wishlist item of another customer
60+
*
61+
* @magentoConfigFixture default_store wishlist/general/active 1
62+
* @magentoApiDataFixture Magento/Wishlist/_files/two_wishlists_for_two_diff_customers.php
63+
*/
64+
public function testUnauthorizedWishlistItemDelete()
65+
{
66+
$wishlist = $this->getWishlist();
67+
$wishlistItem = $wishlist['customer']['wishlist']['items_v2'][0];
68+
$wishlist2 = $this->getWishlist('[email protected]');
69+
$wishlist2Id = $wishlist2['customer']['wishlist']['id'];
70+
$query = $this->getQuery((int) $wishlist2Id, (int) $wishlistItem['id']);
71+
$response = $this->graphQlMutation(
72+
$query,
73+
[],
74+
'',
75+
$this->getHeaderMap('[email protected]')
76+
);
77+
self::assertEquals(1, $response['removeProductsFromWishlist']['wishlist']['items_count']);
78+
self::assertNotEmpty($response['removeProductsFromWishlist']['wishlist']['items_v2'], 'empty wish list items');
79+
self::assertCount(1, $response['removeProductsFromWishlist']['wishlist']['items_v2']);
80+
self::assertEquals(
81+
'The wishlist item with ID "'.$wishlistItem['id'].'" does not belong to the wishlist',
82+
$response['removeProductsFromWishlist']['user_errors'][0]['message']
83+
);
84+
}
85+
5886
/**
5987
* Authentication header map
6088
*
@@ -116,9 +144,9 @@ private function getQuery(
116144
*
117145
* @throws Exception
118146
*/
119-
public function getWishlist(): array
147+
public function getWishlist(string $username = '[email protected]'): array
120148
{
121-
return $this->graphQlQuery($this->getCustomerWishlistQuery(), [], '', $this->getHeaderMap());
149+
return $this->graphQlQuery($this->getCustomerWishlistQuery(), [], '', $this->getHeaderMap($username));
122150
}
123151

124152
/**

dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/UpdateProductsFromWishlistTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,37 @@ public function testUpdateSimpleProductFromWishlist(): void
5757
$this->assertEquals($description, $wishlistResponse['items_v2'][0]['description']);
5858
}
5959

60+
/**
61+
* Test updating the wishlist item of another customer
62+
*
63+
* @magentoConfigFixture default_store wishlist/general/active 1
64+
* @magentoApiDataFixture Magento/Customer/_files/two_customers.php
65+
* @magentoApiDataFixture Magento/Wishlist/_files/two_wishlists_for_two_diff_customers.php
66+
*/
67+
public function testUnauthorizedWishlistItemUpdate()
68+
{
69+
$wishlist = $this->getWishlist();
70+
$wishlistItem = $wishlist['customer']['wishlist']['items_v2'][0];
71+
$wishlist2 = $this->getWishlist('[email protected]');
72+
$wishlist2Id = $wishlist2['customer']['wishlist']['id'];
73+
$qty = 2;
74+
$description = 'New Description';
75+
$updateWishlistQuery = $this->getQuery((int) $wishlist2Id, (int) $wishlistItem['id'], $qty, $description);
76+
$response = $this->graphQlMutation(
77+
$updateWishlistQuery,
78+
[],
79+
'',
80+
$this->getHeaderMap('[email protected]')
81+
);
82+
self::assertEquals(1, $response['updateProductsInWishlist']['wishlist']['items_count']);
83+
self::assertNotEmpty($response['updateProductsInWishlist']['wishlist']['items_v2'], 'empty wish list items');
84+
self::assertCount(1, $response['updateProductsInWishlist']['wishlist']['items_v2']);
85+
self::assertEquals(
86+
'The wishlist item with ID "'.$wishlistItem['id'].'" does not belong to the wishlist',
87+
$response['updateProductsInWishlist']['user_errors'][0]['message']
88+
);
89+
}
90+
6091
/**
6192
* Authentication header map
6293
*
@@ -124,13 +155,14 @@ private function getQuery(
124155
/**
125156
* Get wishlist result
126157
*
158+
* @param string $username
127159
* @return array
128160
*
129161
* @throws Exception
130162
*/
131-
public function getWishlist(): array
163+
public function getWishlist(string $username = '[email protected]'): array
132164
{
133-
return $this->graphQlQuery($this->getCustomerWishlistQuery(), [], '', $this->getHeaderMap());
165+
return $this->graphQlQuery($this->getCustomerWishlistQuery(), [], '', $this->getHeaderMap($username));
134166
}
135167

136168
/**

0 commit comments

Comments
 (0)