Skip to content

Commit 7286543

Browse files
SarmisthaSarmistha
authored andcommitted
Merge remote-tracking branch 'adobe-commerce-tier-4/ACP2E-2871' into Tier4-Kings-PR-06-03-2024
2 parents 2e08b17 + 0742953 commit 7286543

File tree

4 files changed

+180
-9
lines changed

4 files changed

+180
-9
lines changed

app/code/Magento/Wishlist/CustomerData/Wishlist.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
99
use Magento\Customer\CustomerData\SectionSourceInterface;
1010
use Magento\Framework\App\ObjectManager;
11+
use Magento\Store\Model\StoreManagerInterface;
1112

1213
/**
1314
* Wishlist section
@@ -17,7 +18,7 @@ class Wishlist implements SectionSourceInterface
1718
/**
1819
* @var string
1920
*/
20-
const SIDEBAR_ITEMS_NUMBER = 3;
21+
public const SIDEBAR_ITEMS_NUMBER = 3;
2122

2223
/**
2324
* @var \Magento\Wishlist\Helper\Data
@@ -44,19 +45,26 @@ class Wishlist implements SectionSourceInterface
4445
*/
4546
private $itemResolver;
4647

48+
/**
49+
* @var StoreManagerInterface
50+
*/
51+
private $storeManager;
52+
4753
/**
4854
* @param \Magento\Wishlist\Helper\Data $wishlistHelper
4955
* @param \Magento\Wishlist\Block\Customer\Sidebar $block
5056
* @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
5157
* @param \Magento\Framework\App\ViewInterface $view
5258
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface|null $itemResolver
59+
* @param StoreManagerInterface|null $storeManager
5360
*/
5461
public function __construct(
5562
\Magento\Wishlist\Helper\Data $wishlistHelper,
5663
\Magento\Wishlist\Block\Customer\Sidebar $block,
5764
\Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
5865
\Magento\Framework\App\ViewInterface $view,
59-
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null
66+
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null,
67+
StoreManagerInterface $storeManager = null
6068
) {
6169
$this->wishlistHelper = $wishlistHelper;
6270
$this->imageHelperFactory = $imageHelperFactory;
@@ -65,6 +73,7 @@ public function __construct(
6573
$this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(
6674
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
6775
);
76+
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
6877
}
6978

7079
/**
@@ -76,6 +85,7 @@ public function getSectionData()
7685
return [
7786
'counter' => $counter,
7887
'items' => $counter ? $this->getItems() : [],
88+
'websiteId' => $this->storeManager->getWebsite()->getId()
7989
];
8090
}
8191

app/code/Magento/Wishlist/Test/Unit/CustomerData/WishlistTest.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Catalog\Model\Product\Type\AbstractType;
1616
use Magento\Framework\App\ViewInterface;
1717
use Magento\Framework\Pricing\Render;
18+
use Magento\Store\Model\StoreManagerInterface;
1819
use Magento\Wishlist\Block\Customer\Sidebar;
1920
use Magento\Wishlist\CustomerData\Wishlist;
2021
use Magento\Wishlist\CustomerData\Wishlist as WishlistModel;
@@ -23,6 +24,7 @@
2324
use Magento\Wishlist\Model\ResourceModel\Item\Collection;
2425
use PHPUnit\Framework\MockObject\MockObject;
2526
use PHPUnit\Framework\TestCase;
27+
use Magento\Store\Api\Data\WebsiteInterface;
2628

2729
/**
2830
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -47,6 +49,15 @@ class WishlistTest extends TestCase
4749
/** @var ImageBuilder|MockObject */
4850
private $itemResolver;
4951

52+
/** @var StoreManagerInterface|MockObject */
53+
private $storeManagerMock;
54+
55+
/** @var WebsiteInterface|MockObject */
56+
private $websiteMock;
57+
58+
/** @var ImageFactory|MockObject */
59+
private $imageHelperFactory;
60+
5061
protected function setUp(): void
5162
{
5263
$this->wishlistHelperMock = $this->getMockBuilder(Data::class)
@@ -61,24 +72,34 @@ protected function setUp(): void
6172
$this->catalogImageHelperMock = $this->getMockBuilder(Image::class)
6273
->disableOriginalConstructor()
6374
->getMock();
64-
$imageHelperFactory = $this->getMockBuilder(ImageFactory::class)
75+
$this->imageHelperFactory = $this->getMockBuilder(ImageFactory::class)
6576
->disableOriginalConstructor()
6677
->onlyMethods(['create'])
6778
->getMock();
68-
$imageHelperFactory->expects($this->any())
79+
$this->imageHelperFactory->expects($this->any())
6980
->method('create')
7081
->willReturn($this->catalogImageHelperMock);
7182

7283
$this->itemResolver = $this->createMock(
7384
ItemResolverInterface::class
7485
);
7586

87+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
88+
->disableOriginalConstructor()
89+
->getMockForAbstractClass();
90+
91+
$this->websiteMock = $this->getMockBuilder(WebsiteInterface::class)
92+
->onlyMethods(['getId',])
93+
->disableOriginalConstructor()
94+
->getMockForAbstractClass();
95+
7696
$this->model = new Wishlist(
7797
$this->wishlistHelperMock,
7898
$this->sidebarMock,
79-
$imageHelperFactory,
99+
$this->imageHelperFactory,
80100
$this->viewMock,
81-
$this->itemResolver
101+
$this->itemResolver,
102+
$this->storeManagerMock
82103
);
83104
}
84105

@@ -102,6 +123,14 @@ public function testGetSectionData()
102123
$itemAddParams = ['add_params'];
103124
$itemRemoveParams = ['remove_params'];
104125

126+
$this->storeManagerMock->expects($this->once())
127+
->method('getWebsite')
128+
->willReturn($this->websiteMock);
129+
130+
$this->websiteMock->expects($this->once())
131+
->method('getId')
132+
->willReturn(1);
133+
105134
$result = [
106135
'counter' => __('1 item'),
107136
'items' => [
@@ -124,6 +153,7 @@ public function testGetSectionData()
124153
'delete_item_params' => $itemRemoveParams,
125154
],
126155
],
156+
'websiteId' => 1
127157
];
128158

129159
/** @var Item|MockObject $itemMock */
@@ -288,6 +318,14 @@ public function testGetSectionDataWithTwoItems()
288318
->getMock();
289319
$items = [$itemMock, $itemMock];
290320

321+
$this->storeManagerMock->expects($this->once())
322+
->method('getWebsite')
323+
->willReturn($this->websiteMock);
324+
325+
$this->websiteMock->expects($this->once())
326+
->method('getId')
327+
->willReturn(1);
328+
291329
$result = [
292330
'counter' => __('%1 items', count($items)),
293331
'items' => [
@@ -328,6 +366,7 @@ public function testGetSectionDataWithTwoItems()
328366
'delete_item_params' => $itemRemoveParams,
329367
],
330368
],
369+
'websiteId' => 1
331370
];
332371

333372
$this->wishlistHelperMock->expects($this->once())
@@ -465,9 +504,18 @@ public function testGetSectionDataWithoutItems()
465504
{
466505
$items = [];
467506

507+
$this->storeManagerMock->expects($this->once())
508+
->method('getWebsite')
509+
->willReturn($this->websiteMock);
510+
511+
$this->websiteMock->expects($this->once())
512+
->method('getId')
513+
->willReturn(null);
514+
468515
$result = [
469516
'counter' => null,
470517
'items' => [],
518+
'websiteId' =>null
471519
];
472520

473521
$this->wishlistHelperMock->expects($this->once())

app/code/Magento/Wishlist/view/frontend/web/js/view/wishlist.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,32 @@
55

66
define([
77
'uiComponent',
8-
'Magento_Customer/js/customer-data'
9-
], function (Component, customerData) {
8+
'Magento_Customer/js/customer-data',
9+
'underscore'
10+
], function (Component, customerData, _) {
1011
'use strict';
1112

13+
var wishlistReloaded = false;
14+
1215
return Component.extend({
1316
/** @inheritdoc */
1417
initialize: function () {
1518
this._super();
16-
1719
this.wishlist = customerData.get('wishlist');
20+
if (!wishlistReloaded
21+
&& !_.isEmpty(this.wishlist())
22+
// Expired section names are reloaded on page load.
23+
&& _.indexOf(customerData.getExpiredSectionNames(), 'wishlist') === -1
24+
&& window.checkout
25+
&& window.checkout.websiteId
26+
&& window.checkout.websiteId !== this.wishlist().websiteId
27+
) {
28+
//set count to 0 to prevent "wishlist" blocks and count to show with wrong count and items
29+
this.wishlist().counter = 0;
30+
customerData.invalidate(['wishlist']);
31+
customerData.reload(['wishlist'], false);
32+
wishlistReloaded = true;
33+
}
1834
}
1935
});
2036
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/************************************************************************
2+
* Copyright 2024 Adobe
3+
* All Rights Reserved.
4+
*
5+
* NOTICE: All information contained herein is, and remains
6+
* the property of Adobe and its suppliers, if any. The intellectual
7+
* and technical concepts contained herein are proprietary to Adobe
8+
* and its suppliers and are protected by all applicable intellectual
9+
* property laws, including trade secret and copyright laws.
10+
* Dissemination of this information or reproduction of this material
11+
* is strictly forbidden unless prior written permission is obtained
12+
* from Adobe.
13+
* ***********************************************************************
14+
*/
15+
define([
16+
'squire'
17+
], function (Squire) {
18+
'use strict';
19+
20+
describe('Magento_Wishlist/js/view/wishlist', function () {
21+
var wishlistComponent,
22+
mockWishlist,
23+
mockCustomerData,
24+
injector;
25+
26+
function setupInjector() {
27+
injector = new Squire();
28+
29+
mockWishlist = {
30+
counter: 1,
31+
items: [],
32+
websiteId: 1
33+
};
34+
injector.clean();
35+
36+
mockCustomerData = {
37+
get: jasmine.createSpy('customerDataGet').and.returnValue(function () {
38+
return mockWishlist;
39+
}),
40+
reload: jasmine.createSpy('customerDataReload'),
41+
invalidate: jasmine.createSpy(),
42+
getExpiredSectionNames: jasmine.createSpy('getExpiredSectionNames').and.returnValue([])
43+
};
44+
injector.mock('Magento_Customer/js/customer-data', mockCustomerData);
45+
}
46+
47+
function cleanupInjector() {
48+
try {
49+
injector.clean();
50+
injector.remove();
51+
delete window.checkout;
52+
} catch (e) {}
53+
}
54+
55+
async function loadWishlistComponent() {
56+
return new Promise(resolve => {
57+
injector.require(['Magento_Wishlist/js/view/wishlist'], async function (WishlistComponent) {
58+
wishlistComponent = new WishlistComponent();
59+
resolve();
60+
});
61+
});
62+
}
63+
64+
beforeEach(async function () {
65+
setupInjector();
66+
await loadWishlistComponent();
67+
});
68+
69+
afterEach(function () {
70+
cleanupInjector();
71+
});
72+
73+
describe('Initialization', function () {
74+
it('should call customerData.get with "wishlist"', async function () {
75+
expect(mockCustomerData.get).toHaveBeenCalledWith('wishlist');
76+
});
77+
78+
it('should invalidate wishlist if websiteIds do not match', async function () {
79+
window.checkout = { websiteId: 2 };
80+
await wishlistComponent.initialize();
81+
expect(mockCustomerData.invalidate).toHaveBeenCalledWith(['wishlist']);
82+
});
83+
84+
it('should not reload wishlist if websiteIds match', async function () {
85+
window.checkout = { websiteId: 1 };
86+
await wishlistComponent.initialize();
87+
expect(mockCustomerData.reload).not.toHaveBeenCalled();
88+
});
89+
90+
it('should reload wishlist if websiteIds do not match', async function () {
91+
window.checkout = { websiteId: 2 };
92+
await wishlistComponent.initialize();
93+
expect(mockCustomerData.reload).toHaveBeenCalledWith(['wishlist'], false);
94+
});
95+
});
96+
});
97+
});

0 commit comments

Comments
 (0)