Skip to content

Commit 771fd00

Browse files
committed
Merge branch 'ACP2E-3380' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-31-10-2024
2 parents a033552 + 90c85b4 commit 771fd00

File tree

2 files changed

+123
-11
lines changed

2 files changed

+123
-11
lines changed

app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistItems.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
55
*/
66
declare (strict_types = 1);
77

@@ -13,7 +13,6 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Store\Api\Data\StoreInterface;
1515
use Magento\Store\Model\StoreManagerInterface;
16-
use Magento\Wishlist\Model\Item;
1716
use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemCollection;
1817
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory;
1918
use Magento\Wishlist\Model\Wishlist;
@@ -26,12 +25,12 @@ class WishlistItems implements ResolverInterface
2625
/**
2726
* @var WishlistItemCollectionFactory
2827
*/
29-
private $wishlistItemCollectionFactory;
28+
private WishlistItemCollectionFactory $wishlistItemCollectionFactory;
3029

3130
/**
3231
* @var StoreManagerInterface
3332
*/
34-
private $storeManager;
33+
private StoreManagerInterface $storeManager;
3534

3635
/**
3736
* @param WishlistItemCollectionFactory $wishlistItemCollectionFactory
@@ -61,6 +60,10 @@ public function resolve(
6160
/** @var Wishlist $wishlist */
6261
$wishlist = $value['model'];
6362

63+
if ($context->getExtensionAttributes()->getStore() instanceof StoreInterface) {
64+
$args['store_id'] = $context->getExtensionAttributes()->getStore()->getId();
65+
}
66+
6467
/** @var WishlistItemCollection $wishlistItemCollection */
6568
$wishlistItemsCollection = $this->getWishListItems($wishlist, $args);
6669
$wishlistItems = $wishlistItemsCollection->getItems();
@@ -100,12 +103,15 @@ private function getWishListItems(Wishlist $wishlist, array $args): WishlistItem
100103

101104
/** @var WishlistItemCollection $wishlistItemCollection */
102105
$wishlistItemCollection = $this->wishlistItemCollectionFactory->create();
103-
$wishlistItemCollection
104-
->addWishlistFilter($wishlist)
105-
->addStoreFilter(array_map(function (StoreInterface $store) {
106+
$wishlistItemCollection->addWishlistFilter($wishlist);
107+
if (isset($args['store_id'])) {
108+
$wishlistItemCollection->addStoreFilter($args['store_id']);
109+
} else {
110+
$wishlistItemCollection->addStoreFilter(array_map(function (StoreInterface $store) {
106111
return $store->getId();
107-
}, $this->storeManager->getStores()))
108-
->setVisibilityFilter();
112+
}, $this->storeManager->getStores()));
113+
}
114+
$wishlistItemCollection->setVisibilityFilter();
109115
if ($currentPage > 0) {
110116
$wishlistItemCollection->setCurPage($currentPage);
111117
}
@@ -115,4 +121,4 @@ private function getWishListItems(Wishlist $wishlist, array $args): WishlistItem
115121
}
116122
return $wishlistItemCollection;
117123
}
118-
}
124+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WishlistGraphQl\Test\Unit\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
11+
use Magento\GraphQl\Model\Query\ContextInterface;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Wishlist\Model\ResourceModel\Item;
17+
use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemCollection;
18+
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory;
19+
use Magento\Wishlist\Model\Wishlist;
20+
use Magento\WishlistGraphQl\Model\Resolver\WishlistItems;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
use PHPUnit\Framework\TestCase;
23+
24+
class WishlistItemsTest extends TestCase
25+
{
26+
/**
27+
* @var WishlistItemCollectionFactory|MockObject
28+
*/
29+
private WishlistItemCollectionFactory $wishlistItemCollectionFactory;
30+
31+
/**
32+
* @var StoreManagerInterface|MockObject
33+
*/
34+
private StoreManagerInterface $storeManager;
35+
36+
/**
37+
* @return void
38+
* @throws \PHPUnit\Framework\MockObject\Exception
39+
*/
40+
protected function setUp(): void
41+
{
42+
$this->wishlistItemCollectionFactory = $this->createMock(WishlistItemCollectionFactory::class);
43+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
44+
}
45+
46+
/**
47+
* @return void
48+
* @throws \PHPUnit\Framework\MockObject\Exception
49+
*/
50+
public function testResolve(): void
51+
{
52+
$storeId = $itemId = 1;
53+
54+
$field = $this->createMock(Field::class);
55+
$context = $this->getMockBuilder(ContextInterface::class)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$store = $this->createMock(StoreInterface::class);
59+
$store->expects($this->once())->method('getId')->willReturn($storeId);
60+
61+
$extensionAttributes = $this->getMockBuilder(ContextExtensionInterface::class)
62+
->disableOriginalConstructor()
63+
->addMethods(['getStore'])
64+
->getMock();
65+
$extensionAttributes->expects($this->exactly(2))
66+
->method('getStore')
67+
->willReturn($store);
68+
$context->expects($this->exactly(2))
69+
->method('getExtensionAttributes')
70+
->willReturn($extensionAttributes);
71+
$info = $this->createMock(ResolveInfo::class);
72+
$wishlist = $this->createMock(Wishlist::class);
73+
74+
$item = $this->getMockBuilder(Item::class)
75+
->addMethods(['getId', 'getData', 'getDescription', 'getAddedAt', 'getProduct'])
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$item->expects($this->once())->method('getId')->willReturn($itemId);
79+
$item->expects($this->once())->method('getData')->with('qty');
80+
$item->expects($this->once())->method('getDescription');
81+
$item->expects($this->once())->method('getAddedAt');
82+
$item->expects($this->once())->method('getProduct');
83+
84+
$wishlistCollection = $this->createMock(WishlistItemCollection::class);
85+
$wishlistCollection->expects($this->once())
86+
->method('addWishlistFilter')
87+
->willReturnSelf();
88+
$wishlistCollection->expects($this->once())
89+
->method('addStoreFilter')
90+
->with($storeId)
91+
->willReturnSelf();
92+
$wishlistCollection->expects($this->once())->method('setVisibilityFilter')->willReturnSelf();
93+
$wishlistCollection->expects($this->once())->method('setCurPage')->willReturnSelf();
94+
$wishlistCollection->expects($this->once())->method('setPageSize')->willReturnSelf();
95+
$wishlistCollection->expects($this->once())->method('getItems')->willReturn([$item]);
96+
$wishlistCollection->expects($this->once())->method('getCurPage');
97+
$wishlistCollection->expects($this->once())->method('getPageSize');
98+
$wishlistCollection->expects($this->once())->method('getLastPageNumber');
99+
$this->wishlistItemCollectionFactory->expects($this->once())
100+
->method('create')
101+
->willReturn($wishlistCollection);
102+
103+
$resolver = new WishlistItems($this->wishlistItemCollectionFactory, $this->storeManager);
104+
$resolver->resolve($field, $context, $info, ['model' => $wishlist]);
105+
}
106+
}

0 commit comments

Comments
 (0)