Skip to content

Commit d7443da

Browse files
committed
ACP2E-4137: [Cloud] No such entity with cartId
1 parent 0ebbcb4 commit d7443da

File tree

3 files changed

+245
-23
lines changed

3 files changed

+245
-23
lines changed

app/code/Magento/LoginAsCustomerFrontendUi/Controller/Login/Index.php

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

88
namespace Magento\LoginAsCustomerFrontendUi\Controller\Login;
99

10-
use Magento\Customer\Api\CustomerRepositoryInterface;
1110
use Magento\Customer\Model\Session;
1211
use Magento\Framework\App\ObjectManager;
1312
use Magento\Framework\App\RequestInterface;
@@ -17,7 +16,6 @@
1716
use Magento\Framework\Exception\LocalizedException;
1817
use Magento\Framework\App\Action\HttpGetActionInterface;
1918
use Magento\Framework\Message\ManagerInterface;
20-
use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface;
2119
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface;
2220
use Psr\Log\LoggerInterface;
2321
use Magento\Checkout\Model\Session as CheckoutSession;
@@ -37,18 +35,6 @@ class Index implements HttpGetActionInterface
3735
*/
3836
private $request;
3937

40-
/**
41-
* @var CustomerRepositoryInterface
42-
* @deprecated
43-
*/
44-
private $customerRepository;
45-
46-
/**
47-
* @var GetAuthenticationDataBySecretInterface
48-
* @deprecated
49-
*/
50-
private $getAuthenticationDataBySecret;
51-
5238
/**
5339
* @var AuthenticateCustomerBySecretInterface
5440
*/
@@ -77,8 +63,6 @@ class Index implements HttpGetActionInterface
7763
/**
7864
* @param ResultFactory $resultFactory
7965
* @param RequestInterface $request
80-
* @param CustomerRepositoryInterface $customerRepository
81-
* @param GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret
8266
* @param AuthenticateCustomerBySecretInterface $authenticateCustomerBySecret
8367
* @param ManagerInterface $messageManager
8468
* @param LoggerInterface $logger
@@ -88,8 +72,6 @@ class Index implements HttpGetActionInterface
8872
public function __construct(
8973
ResultFactory $resultFactory,
9074
RequestInterface $request,
91-
CustomerRepositoryInterface $customerRepository,
92-
GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret,
9375
AuthenticateCustomerBySecretInterface $authenticateCustomerBySecret,
9476
ManagerInterface $messageManager,
9577
LoggerInterface $logger,
@@ -98,8 +80,6 @@ public function __construct(
9880
) {
9981
$this->resultFactory = $resultFactory;
10082
$this->request = $request;
101-
$this->customerRepository = $customerRepository;
102-
$this->getAuthenticationDataBySecret = $getAuthenticationDataBySecret;
10383
$this->authenticateCustomerBySecret = $authenticateCustomerBySecret;
10484
$this->messageManager = $messageManager;
10585
$this->logger = $logger;
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomerFrontendUi\Test\Unit\Controller\Login;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\Controller\Result\Redirect;
14+
use Magento\Framework\Controller\Result\RedirectFactory;
15+
use Magento\Framework\Controller\ResultFactory;
16+
use Magento\Framework\Controller\ResultInterface;
17+
use Magento\Framework\Exception\LocalizedException;
18+
use Magento\Framework\Message\ManagerInterface;
19+
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface;
20+
use Magento\LoginAsCustomerFrontendUi\Controller\Login\Index;
21+
use Magento\Checkout\Model\Session as CheckoutSession;
22+
use PHPUnit\Framework\MockObject\MockObject;
23+
use PHPUnit\Framework\TestCase;
24+
use Psr\Log\LoggerInterface;
25+
26+
/**
27+
* Test class for \Magento\LoginAsCustomerFrontendUi\Controller\Login\Index
28+
*
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30+
*/
31+
class IndexTest extends TestCase
32+
{
33+
/**
34+
* @var Index
35+
*/
36+
private $controller;
37+
38+
/**
39+
* @var RequestInterface|MockObject
40+
*/
41+
private $requestMock;
42+
43+
/**
44+
* @var ResultFactory|MockObject
45+
*/
46+
private $resultFactoryMock;
47+
48+
/**
49+
* @var AuthenticateCustomerBySecretInterface|MockObject
50+
*/
51+
private $authenticateCustomerBySecretMock;
52+
53+
/**
54+
* @var ManagerInterface|MockObject
55+
*/
56+
private $messageManagerMock;
57+
58+
/**
59+
* @var LoggerInterface|MockObject
60+
*/
61+
private $loggerMock;
62+
63+
/**
64+
* @var Session|MockObject
65+
*/
66+
private $customerSessionMock;
67+
68+
/**
69+
* @var CheckoutSession|MockObject
70+
*/
71+
private $checkoutSessionMock;
72+
73+
/**
74+
* @var Customer|MockObject
75+
*/
76+
private $customerMock;
77+
78+
/**
79+
* @var Redirect|MockObject
80+
*/
81+
private $redirectMock;
82+
83+
/**
84+
* @var ResultInterface|MockObject
85+
*/
86+
private $resultPageMock;
87+
88+
/**
89+
* @var RedirectFactory|MockObject
90+
*/
91+
private $redirectFactoryMock;
92+
93+
protected function setUp(): void
94+
{
95+
$this->requestMock = $this->getMockForAbstractClass(RequestInterface::class);
96+
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
97+
$this->authenticateCustomerBySecretMock = $this->getMockForAbstractClass(
98+
AuthenticateCustomerBySecretInterface::class
99+
);
100+
$this->messageManagerMock = $this->getMockForAbstractClass(ManagerInterface::class);
101+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
102+
$this->customerSessionMock = $this->createMock(Session::class);
103+
$this->checkoutSessionMock = $this->createMock(CheckoutSession::class);
104+
$this->customerMock = $this->getMockBuilder(Customer::class)
105+
->disableOriginalConstructor()
106+
->addMethods(['getFirstname', 'getLastname'])
107+
->getMock();
108+
$this->redirectMock = $this->createMock(Redirect::class);
109+
$this->resultPageMock = $this->getMockBuilder(ResultInterface::class)
110+
->disableOriginalConstructor()
111+
->addMethods(['getConfig', 'getTitle', 'set'])
112+
->getMockForAbstractClass();
113+
$this->redirectFactoryMock = $this->createMock(RedirectFactory::class);
114+
$this->controller = new Index(
115+
$this->resultFactoryMock,
116+
$this->requestMock,
117+
$this->authenticateCustomerBySecretMock,
118+
$this->messageManagerMock,
119+
$this->loggerMock,
120+
$this->customerSessionMock,
121+
$this->checkoutSessionMock
122+
);
123+
}
124+
125+
/**
126+
* Test execute method with existing session
127+
*/
128+
public function testExecuteWithQuoteId()
129+
{
130+
$secret = 'test-secret';
131+
$firstName = 'John';
132+
$lastName = 'Doe';
133+
$this->requestMock->expects($this->once())
134+
->method('getParam')
135+
->with('secret')
136+
->willReturn($secret);
137+
$this->resultFactoryMock->expects($this->exactly(3))
138+
->method('create')
139+
->willReturn($this->resultPageMock);
140+
$this->checkoutSessionMock->expects($this->once())->method('getQuoteId')->willReturn(123);
141+
$this->checkoutSessionMock->expects($this->once())->method('clearQuote');
142+
$this->checkoutSessionMock->expects($this->once())->method('clearStorage');
143+
$this->mockHelper($secret, $firstName, $lastName);
144+
}
145+
146+
/**
147+
* Test execute method without existing session
148+
*/
149+
public function testExecuteWithoutQuoteId()
150+
{
151+
$secret = 'test-secret';
152+
$firstName = 'John';
153+
$lastName = 'Doe';
154+
$this->requestMock->expects($this->once())
155+
->method('getParam')
156+
->with('secret')
157+
->willReturn($secret);
158+
$this->resultFactoryMock->expects($this->exactly(3))
159+
->method('create')
160+
->willReturn($this->resultPageMock);
161+
$this->checkoutSessionMock->expects($this->once())->method('getQuoteId')->willReturn(false);
162+
$this->checkoutSessionMock->expects($this->never())->method('clearQuote');
163+
$this->checkoutSessionMock->expects($this->never())->method('clearStorage');
164+
$this->mockHelper($secret, $firstName, $lastName);
165+
}
166+
167+
/**
168+
* Test execute method with LocalizedException
169+
*/
170+
public function testExecuteWithLocalizedException()
171+
{
172+
$secret = 'test-secret';
173+
$exceptionMessage = 'Authentication failed';
174+
$this->requestMock->expects($this->once())
175+
->method('getParam')
176+
->with('secret')
177+
->willReturn($secret);
178+
$this->checkoutSessionMock->expects($this->once())->method('getQuoteId')->willReturn(false);
179+
$this->authenticateCustomerBySecretMock->expects($this->once())
180+
->method('execute')
181+
->with($secret)
182+
->willThrowException(new LocalizedException(__($exceptionMessage)));
183+
$this->messageManagerMock->expects($this->once())->method('addErrorMessage')->with($exceptionMessage);
184+
$this->resultFactoryMock->expects($this->once())->method('create')->willReturn($this->redirectMock);
185+
$this->redirectMock->expects($this->once())
186+
->method('setPath')
187+
->with('/');
188+
$result = $this->controller->execute();
189+
$this->assertInstanceOf(ResultInterface::class, $result);
190+
}
191+
192+
/**
193+
* Test execute method with Exception
194+
*/
195+
public function testExecuteWithGenericException()
196+
{
197+
$secret = 'test-secret';
198+
$exceptionMessage = 'Error';
199+
$this->requestMock->expects($this->once())
200+
->method('getParam')
201+
->with('secret')
202+
->willReturn($secret);
203+
$this->checkoutSessionMock->expects($this->once())->method('getQuoteId')->willReturn(null);
204+
$this->authenticateCustomerBySecretMock->expects($this->once())
205+
->method('execute')
206+
->with($secret)
207+
->willThrowException(new \Exception($exceptionMessage));
208+
$this->loggerMock->expects($this->once())->method('error')->with($exceptionMessage);
209+
$this->messageManagerMock->expects($this->once())
210+
->method('addErrorMessage')
211+
->with(__('Cannot login to account.'));
212+
$this->resultFactoryMock->expects($this->once())->method('create')->willReturn($this->redirectMock);
213+
$this->redirectMock->expects($this->once())->method('setPath')->with('/');
214+
$result = $this->controller->execute();
215+
$this->assertInstanceOf(ResultInterface::class, $result);
216+
}
217+
218+
/**
219+
* @param string $secret
220+
* @param string $firstName
221+
* @param string $lastName
222+
* @return void
223+
*/
224+
private function mockHelper(string $secret, string $firstName, string $lastName): void
225+
{
226+
$this->authenticateCustomerBySecretMock->expects($this->once())->method('execute')->with($secret);
227+
$this->customerSessionMock->expects($this->once())
228+
->method('getCustomer')
229+
->willReturn($this->customerMock);
230+
$this->customerMock->expects($this->once())->method('getFirstname')->willReturn($firstName);
231+
$this->customerMock->expects($this->once())->method('getLastname')->willReturn($lastName);
232+
$this->messageManagerMock->expects($this->once())
233+
->method('addSuccessMessage')
234+
->with(__('You are logged in as customer: %1', $firstName . ' ' . $lastName));
235+
$this->resultPageMock->expects($this->once())->method('getConfig')->willReturnSelf();
236+
$this->resultPageMock->expects($this->once())->method('getTitle')->willReturnSelf();
237+
$this->resultPageMock->expects($this->once())->method('set')->with(__('You are logged in'));
238+
$result = $this->controller->execute();
239+
$this->assertInstanceOf(ResultInterface::class, $result);
240+
}
241+
}

app/code/Magento/LoginAsCustomerFrontendUi/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"magento/framework": "*",
77
"magento/module-login-as-customer-api": "*",
88
"magento/module-customer": "*",
9-
"magento/module-store": "*"
9+
"magento/module-store": "*",
10+
"magento/module-checkout": "*"
1011
},
1112
"type": "magento2-module",
1213
"license": [

0 commit comments

Comments
 (0)