77 private $cookieManager;
82 private $cookieMetadataFactory;
87 private $cookieMetadata;
91 $this->request = $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
92 ->disableOriginalConstructor()->getMock();
93 $this->response = $this->createPartialMock(
94 \
Magento\Framework\
App\ResponseInterface::class,
95 [
'setRedirect',
'sendResponse',
'representJson',
'setHttpResponseCode']
97 $this->customerSession = $this->createPartialMock(
98 \
Magento\Customer\Model\Session::class,
104 'setCustomerDataAsLoggedIn',
108 $this->objectManager = $this->createPartialMock(\
Magento\Framework\ObjectManager\ObjectManager::class, [
'get']);
109 $this->customerAccountManagementMock =
110 $this->createPartialMock(\
Magento\Customer\Model\AccountManagement::class, [
'authenticate']);
112 $this->jsonHelperMock = $this->createPartialMock(\
Magento\Framework\Json\Helper\Data::class, [
'jsonDecode']);
114 $this->resultJson = $this->getMockBuilder(\
Magento\Framework\Controller\Result\Json::class)
115 ->disableOriginalConstructor()
117 $this->resultJsonFactory = $this->getMockBuilder(\
Magento\Framework\Controller\Result\JsonFactory::class)
118 ->disableOriginalConstructor()
119 ->setMethods([
'create'])
122 $this->cookieManager = $this->getMockBuilder(\
Magento\Framework\Stdlib\CookieManagerInterface::class)
123 ->setMethods([
'getCookie',
'deleteCookie'])
124 ->getMockForAbstractClass();
125 $this->cookieMetadataFactory = $this->getMockBuilder(
126 \
Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
127 )->disableOriginalConstructor()->getMock();
128 $this->cookieMetadata = $this->getMockBuilder(\
Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
129 ->disableOriginalConstructor()
132 $this->resultRaw = $this->getMockBuilder(\
Magento\Framework\Controller\Result\Raw::class)
133 ->disableOriginalConstructor()
135 $resultRawFactory = $this->getMockBuilder(\
Magento\Framework\Controller\Result\RawFactory::class)
136 ->disableOriginalConstructor()
137 ->setMethods([
'create'])
139 $resultRawFactory->expects($this->atLeastOnce())
141 ->willReturn($this->resultRaw);
143 $contextMock = $this->createMock(\
Magento\Framework\
App\Action\Context::class);
144 $this->redirectMock = $this->createMock(\
Magento\Framework\
App\
Response\RedirectInterface::class);
145 $contextMock->expects($this->atLeastOnce())->method(
'getRedirect')->willReturn($this->redirectMock);
146 $contextMock->expects($this->atLeastOnce())->method(
'getRequest')->willReturn($this->request);
148 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
150 \
Magento\Customer\Controller\Ajax\Login::class,
152 'context' => $contextMock,
153 'customerSession' => $this->customerSession,
154 'helper' => $this->jsonHelperMock,
155 'response' => $this->response,
156 'resultRawFactory' => $resultRawFactory,
157 'resultJsonFactory' => $this->resultJsonFactory,
158 'objectManager' => $this->objectManager,
159 'customerAccountManagement' => $this->customerAccountManagementMock,
160 'cookieManager' => $this->cookieManager,
161 'cookieMetadataFactory' => $this->cookieMetadataFactory
169 $loginSuccessResponse =
'{"errors": false, "message":"Login successful."}';
172 ->expects($this->any())
173 ->method(
'getContent')
174 ->willReturn($jsonRequest);
177 ->expects($this->any())
178 ->method(
'getMethod')
179 ->willReturn(
'POST');
182 ->expects($this->any())
183 ->method(
'isXmlHttpRequest')
186 $this->resultJsonFactory->expects($this->atLeastOnce())
188 ->willReturn($this->resultJson);
190 $this->jsonHelperMock
191 ->expects($this->any())
192 ->method(
'jsonDecode')
196 $customerMock = $this->getMockForAbstractClass(\
Magento\Customer\Api\Data\CustomerInterface::class);
197 $this->customerAccountManagementMock
198 ->expects($this->any())
199 ->method(
'authenticate')
201 ->willReturn($customerMock);
203 $this->customerSession->expects($this->once())
204 ->method(
'setCustomerDataAsLoggedIn')
205 ->with($customerMock);
207 $this->customerSession->expects($this->once())->method(
'regenerateId');
211 $redirectMock->expects($this->once())->method(
'getRedirectCookie')->willReturn(
'some_url1');
213 $this->cookieManager->expects($this->once())
214 ->method(
'getCookie')
215 ->with(
'mage-cache-sessid')
217 $this->cookieMetadataFactory->expects($this->once())
218 ->method(
'createCookieMetadata')
219 ->willReturn($this->cookieMetadata);
220 $this->cookieMetadata->expects($this->once())
224 $this->cookieManager->expects($this->once())
225 ->method(
'deleteCookie')
226 ->with(
'mage-cache-sessid', $this->cookieMetadata)
229 $scopeConfigMock = $this->createMock(\
Magento\Framework\
App\Config\ScopeConfigInterface::class);
230 $this->
object->setScopeConfig($scopeConfigMock);
231 $scopeConfigMock->expects($this->once())->method(
'getValue')
232 ->with(
'customer/startup/redirect_dashboard')
235 $this->redirectMock->expects($this->once())->method(
'success')->willReturn(
'some_url2');
236 $this->resultRaw->expects($this->never())->method(
'setHttpResponseCode');
240 'message' =>
__(
'Login successful.'),
241 'redirectUrl' =>
'some_url2',
245 ->expects($this->once())
248 ->willReturn($loginSuccessResponse);
249 $this->assertEquals($loginSuccessResponse, $this->object->execute());
255 $loginFailureResponse =
'{"message":"Invalid login or password."}';
258 ->expects($this->any())
259 ->method(
'getContent')
260 ->willReturn($jsonRequest);
263 ->expects($this->any())
264 ->method(
'getMethod')
265 ->willReturn(
'POST');
268 ->expects($this->any())
269 ->method(
'isXmlHttpRequest')
272 $this->resultJsonFactory->expects($this->once())
274 ->willReturn($this->resultJson);
276 $this->jsonHelperMock
277 ->expects($this->any())
278 ->method(
'jsonDecode')
282 $customerMock = $this->getMockForAbstractClass(\
Magento\Customer\Api\Data\CustomerInterface::class);
283 $this->customerAccountManagementMock
284 ->expects($this->any())
285 ->method(
'authenticate')
289 $this->customerSession->expects($this->never())
290 ->method(
'setCustomerDataAsLoggedIn')
291 ->with($customerMock);
293 $this->customerSession->expects($this->never())->method(
'regenerateId');
297 'message' =>
__(
'Invalid login or password.')
300 ->expects($this->once())
303 ->willReturn($loginFailureResponse);
305 $this->assertEquals($loginFailureResponse, $this->object->execute());
$customerAccountManagementMock