|
| 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\Api\CustomerRepositoryInterface; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\Controller\Result\Raw; |
| 13 | +use Magento\Framework\Controller\Result\Redirect; |
| 14 | +use Magento\Framework\Controller\ResultFactory; |
| 15 | +use Magento\Framework\Message\Collection; |
| 16 | +use Magento\Framework\Message\ManagerInterface; |
| 17 | +use Magento\Customer\Model\Session; |
| 18 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 19 | +use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface; |
| 20 | +use Magento\Framework\Exception\LocalizedException; |
| 21 | +use Magento\Framework\View\Result\Page; |
| 22 | +use Magento\Framework\View\Page\Config; |
| 23 | +use Magento\Framework\View\Page\Title; |
| 24 | +use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | +use Psr\Log\LoggerInterface; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | +use Magento\LoginAsCustomerFrontendUi\Controller\Login\Index; |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for Magento\LoginAsCustomerFrontendUi\Controller\Login\Index |
| 32 | + */ |
| 33 | +class IndexTest extends TestCase |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var Index |
| 37 | + */ |
| 38 | + private $controller; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var ResultFactory|MockObject |
| 42 | + */ |
| 43 | + private $resultFactoryMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var RequestInterface|MockObject |
| 47 | + */ |
| 48 | + private $requestMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var AuthenticateCustomerBySecretInterface|MockObject |
| 52 | + */ |
| 53 | + private $authenticateCustomerBySecretMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var Session|MockObject |
| 57 | + */ |
| 58 | + private $customerSessionMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var ManagerInterface|MockObject |
| 62 | + */ |
| 63 | + private $messageManagerMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var LoggerInterface|MockObject |
| 67 | + */ |
| 68 | + private $loggerMock; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var CustomerInterface|MockObject |
| 72 | + */ |
| 73 | + private $customerMock; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var Redirect|MockObject |
| 77 | + */ |
| 78 | + private $resultRedirectMock; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var Raw|MockObject |
| 82 | + */ |
| 83 | + private $resultPageMock; |
| 84 | + |
| 85 | + /** |
| 86 | + * @var Config|MockObject |
| 87 | + */ |
| 88 | + private $pageConfigMock; |
| 89 | + |
| 90 | + /** |
| 91 | + * @var Title|MockObject |
| 92 | + */ |
| 93 | + private $pageTitleMock; |
| 94 | + |
| 95 | + /** |
| 96 | + * Set up the test environment |
| 97 | + */ |
| 98 | + protected function setUp(): void |
| 99 | + { |
| 100 | + $this->resultFactoryMock = $this->createMock(ResultFactory::class); |
| 101 | + $this->requestMock = $this->createMock(RequestInterface::class); |
| 102 | + $this->authenticateCustomerBySecretMock = |
| 103 | + $this->getMockForAbstractClass(AuthenticateCustomerBySecretInterface::class); |
| 104 | + $getAuthenticationDataBySecretMock = |
| 105 | + $this->getMockForAbstractClass(GetAuthenticationDataBySecretInterface::class); |
| 106 | + $customerRepositoryMock = $this->getMockForAbstractClass(CustomerRepositoryInterface::class); |
| 107 | + $this->customerSessionMock = $this->createMock(Session::class); |
| 108 | + $this->messageManagerMock = $this->getMockForAbstractClass(ManagerInterface::class); |
| 109 | + $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); |
| 110 | + $this->customerMock = $this->getMockForAbstractClass(CustomerInterface::class); |
| 111 | + $this->pageConfigMock = $this->createMock(Config::class); |
| 112 | + $this->resultRedirectMock = $this->createMock(Redirect::class); |
| 113 | + $this->resultPageMock = $this->createMock(Page::class); |
| 114 | + $this->pageConfigMock = $this->createMock(Config::class); |
| 115 | + $this->pageTitleMock = $this->createMock(Title::class); |
| 116 | + $this->controller = new Index( |
| 117 | + $this->resultFactoryMock, |
| 118 | + $this->requestMock, |
| 119 | + $customerRepositoryMock, |
| 120 | + $getAuthenticationDataBySecretMock, |
| 121 | + $this->authenticateCustomerBySecretMock, |
| 122 | + $this->messageManagerMock, |
| 123 | + $this->loggerMock, |
| 124 | + $this->customerSessionMock |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test the success case where authentication succeeds |
| 130 | + */ |
| 131 | + public function testExecuteSuccess(): void |
| 132 | + { |
| 133 | + $secret = 'valid_secret'; |
| 134 | + $this->requestMock->expects($this->once())->method('getParam')->willReturn($secret); |
| 135 | + $collectionMock = $this->createMock(Collection::class); |
| 136 | + $this->messageManagerMock->expects($this->once())->method('getMessages')->willReturn($collectionMock); |
| 137 | + $this->authenticateCustomerBySecretMock->expects($this->once())->method('execute')->with($secret); |
| 138 | + $this->customerSessionMock->expects($this->once()) |
| 139 | + ->method('getCustomer') |
| 140 | + ->willReturn($this->customerMock); |
| 141 | + $this->customerMock->expects($this->once())->method('getFirstname')->willReturn('Test'); |
| 142 | + $this->customerMock->expects($this->once())->method('getLastname')->willReturn('Testing'); |
| 143 | + $this->messageManagerMock->expects($this->once()) |
| 144 | + ->method('addSuccessMessage') |
| 145 | + ->with('You are logged in as customer: Test Testing'); |
| 146 | + $this->resultFactoryMock->expects($this->exactly(3)) |
| 147 | + ->method('create') |
| 148 | + ->willReturnMap([ |
| 149 | + [ResultFactory::TYPE_REDIRECT, [], $this->resultRedirectMock], |
| 150 | + [ResultFactory::TYPE_PAGE, [], $this->resultPageMock], |
| 151 | + [ResultFactory::TYPE_PAGE, [], $this->resultPageMock] |
| 152 | + ]); |
| 153 | + $this->resultPageMock->expects($this->once())->method('getConfig')->willReturn($this->pageConfigMock); |
| 154 | + $this->pageConfigMock->expects($this->once())->method('getTitle')->willReturn($this->pageTitleMock); |
| 155 | + $this->pageTitleMock->expects($this->once())->method('set')->with('You are logged in'); |
| 156 | + $this->controller->execute(); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Test the failure case where a LocalizedException is thrown |
| 161 | + */ |
| 162 | + public function testExecuteLocalizedException(): void |
| 163 | + { |
| 164 | + $secret = 'invalid_secret'; |
| 165 | + $this->requestMock->expects($this->once())->method('getParam')->willReturn($secret); |
| 166 | + $this->messageManagerMock->expects($this->never())->method('getMessages'); |
| 167 | + $exceptionMessage = 'Invalid secret provided'; |
| 168 | + $this->authenticateCustomerBySecretMock->expects($this->once()) |
| 169 | + ->method('execute') |
| 170 | + ->with($secret) |
| 171 | + ->willThrowException(new LocalizedException(__($exceptionMessage))); |
| 172 | + $this->messageManagerMock->expects($this->once())->method('addErrorMessage')->with($exceptionMessage); |
| 173 | + $this->resultFactoryMock->expects($this->once()) |
| 174 | + ->method('create') |
| 175 | + ->with(ResultFactory::TYPE_REDIRECT) |
| 176 | + ->willReturn($this->resultRedirectMock); |
| 177 | + $this->resultRedirectMock->expects($this->once()) |
| 178 | + ->method('setPath') |
| 179 | + ->with('/') |
| 180 | + ->willReturnSelf(); |
| 181 | + $this->controller->execute(); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Test the failure case where a general Exception is thrown |
| 186 | + */ |
| 187 | + public function testExecuteGeneralException(): void |
| 188 | + { |
| 189 | + $secret = 'invalid_secret'; |
| 190 | + $this->requestMock->expects($this->once()) |
| 191 | + ->method('getParam') |
| 192 | + ->with('secret') |
| 193 | + ->willReturn($secret); |
| 194 | + $this->messageManagerMock->expects($this->never())->method('getMessages'); |
| 195 | + $exceptionMessage = 'Unexpected error'; |
| 196 | + $this->authenticateCustomerBySecretMock->expects($this->once()) |
| 197 | + ->method('execute') |
| 198 | + ->with($secret) |
| 199 | + ->willThrowException(new \Exception($exceptionMessage)); |
| 200 | + $this->loggerMock->expects($this->once())->method('error')->with($exceptionMessage); |
| 201 | + $this->messageManagerMock->expects($this->once()) |
| 202 | + ->method('addErrorMessage') |
| 203 | + ->with('Cannot login to account.'); |
| 204 | + $this->resultFactoryMock->expects($this->once()) |
| 205 | + ->method('create') |
| 206 | + ->with(ResultFactory::TYPE_REDIRECT) |
| 207 | + ->willReturn($this->resultRedirectMock); |
| 208 | + $this->resultRedirectMock->expects($this->once()) |
| 209 | + ->method('setPath') |
| 210 | + ->with('/') |
| 211 | + ->willReturnSelf(); |
| 212 | + $this->controller->execute(); |
| 213 | + } |
| 214 | +} |
0 commit comments