Skip to content

Commit 6ee558e

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-3994' into PR_2025_07_08_muntianu
2 parents 5c48592 + 1b62c85 commit 6ee558e

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed

app/code/Magento/LoginAsCustomerQuote/Plugin/LoginAsCustomerApi/ProcessShoppingCartPlugin.php

Lines changed: 3 additions & 3 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 2020 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -64,7 +64,7 @@ public function beforeExecute(
6464
AuthenticateCustomerBySecretInterface $subject,
6565
string $secret
6666
) {
67-
if (!$this->customerSession->getId()) {
67+
if (!$this->customerSession->getId() && $this->checkoutSession->getQuote()->getId()) {
6868
$quote = $this->checkoutSession->getQuote();
6969
/* Remove items from guest cart */
7070
$quote->removeAllItems();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomerQuote\Test\Unit\Plugin\LoginAsCustomerApi;
9+
10+
use Magento\Checkout\Model\Session as CheckoutSession;
11+
use Magento\Customer\Model\Session as CustomerSession;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface;
14+
use Magento\LoginAsCustomerQuote\Plugin\LoginAsCustomerApi\ProcessShoppingCartPlugin;
15+
use Magento\Quote\Api\CartRepositoryInterface;
16+
use Magento\Quote\Model\Quote;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Unit tests for ProcessShoppingCartPlugin
22+
*/
23+
class ProcessShoppingCartPluginTest extends TestCase
24+
{
25+
/**
26+
* @var CustomerSession|MockObject
27+
*/
28+
private $customerSession;
29+
30+
/**
31+
* @var CheckoutSession|MockObject
32+
*/
33+
private $checkoutSession;
34+
35+
/**
36+
* @var CartRepositoryInterface|MockObject
37+
*/
38+
private $quoteRepository;
39+
40+
/**
41+
* @var Quote|MockObject
42+
*/
43+
private $quote;
44+
45+
/**
46+
* @var ProcessShoppingCartPlugin
47+
*/
48+
private $plugin;
49+
50+
/**
51+
* Set up test environment
52+
*/
53+
protected function setUp(): void
54+
{
55+
$this->customerSession = $this->getMockBuilder(CustomerSession::class)->disableOriginalConstructor()->getMock();
56+
$this->checkoutSession = $this->getMockBuilder(CheckoutSession::class)->disableOriginalConstructor()->getMock();
57+
$this->quoteRepository = $this->getMockBuilder(CartRepositoryInterface::class)
58+
->disableOriginalConstructor()
59+
->getMockForAbstractClass();
60+
$this->quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
61+
$this->plugin = new ProcessShoppingCartPlugin(
62+
$this->customerSession,
63+
$this->checkoutSession,
64+
$this->quoteRepository
65+
);
66+
}
67+
68+
/**
69+
* Test beforeExecute when if condition is true
70+
*/
71+
public function testBeforeExecuteWithNoCustomerIdAndQuoteId(): void
72+
{
73+
$this->customerSession->expects($this->once())->method('getId')->willReturn(null);
74+
$this->checkoutSession->expects($this->exactly(2))->method('getQuote')->willReturn($this->quote);
75+
$this->quote->expects($this->once())->method('getId')->willReturn(123);
76+
$this->quote->expects($this->once())->method('removeAllItems')->willReturnSelf();
77+
$this->quote->expects($this->once())->method('setCustomerIsGuest')->with(0)->willReturnSelf();
78+
$this->quoteRepository->expects($this->once())->method('save')->with($this->quote);
79+
$subject = $this->createMock(AuthenticateCustomerBySecretInterface::class);
80+
$secret = 'test-secret';
81+
$result = $this->plugin->beforeExecute($subject, $secret);
82+
$this->assertNull($result);
83+
}
84+
85+
/**
86+
* Test beforeExecute when if condition is false
87+
*
88+
* @throws LocalizedException
89+
*/
90+
public function testBeforeExecuteWithCustomerId(): void
91+
{
92+
$this->customerSession->expects($this->once())->method('getId')->willReturn(456);
93+
$this->checkoutSession->expects($this->never())->method('getQuote');
94+
$this->quote->expects($this->never())->method('getId');
95+
$this->noGuestCart();
96+
}
97+
98+
/**
99+
* Test beforeExecute when if condition is false (no quote ID)
100+
*
101+
* @throws LocalizedException
102+
*/
103+
public function testBeforeExecuteWithNoQuoteId(): void
104+
{
105+
$this->customerSession->expects($this->once())->method('getId')->willReturn(null);
106+
$this->checkoutSession->expects($this->once())->method('getQuote')->willReturn($this->quote);
107+
$this->quote->expects($this->once())->method('getId')->willReturn(null);
108+
$this->noGuestCart();
109+
}
110+
111+
/**
112+
* Expected that no guest cart exist
113+
*
114+
* @return void
115+
* @throws LocalizedException
116+
*/
117+
private function noGuestCart(): void
118+
{
119+
$this->quote->expects($this->never())->method('removeAllItems');
120+
$this->quote->expects($this->never())->method('setCustomerIsGuest');
121+
$this->quoteRepository->expects($this->never())->method('save');
122+
$subject = $this->getMockBuilder(AuthenticateCustomerBySecretInterface::class)
123+
->disableOriginalConstructor()
124+
->getMockForAbstractClass();
125+
$secret = 'test-secret';
126+
$result = $this->plugin->beforeExecute($subject, $secret);
127+
$this->assertNull($result);
128+
}
129+
}

0 commit comments

Comments
 (0)