|
| 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 | +} |
0 commit comments