Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LoginTest.php
Go to the documentation of this file.
1 <?php
11 
13 
17 class LoginTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $object;
23 
27  protected $request;
28 
32  protected $response;
33 
37  protected $customerSession;
38 
42  protected $objectManager;
43 
48 
52  protected $jsonHelperMock;
53 
57  protected $resultJson;
58 
62  protected $resultJsonFactory;
63 
67  protected $resultRaw;
68 
72  protected $redirectMock;
73 
77  private $cookieManager;
78 
82  private $cookieMetadataFactory;
83 
87  private $cookieMetadata;
88 
89  protected function setUp()
90  {
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']
96  );
97  $this->customerSession = $this->createPartialMock(
98  \Magento\Customer\Model\Session::class,
99  [
100  'isLoggedIn',
101  'getLastCustomerId',
102  'getBeforeAuthUrl',
103  'setBeforeAuthUrl',
104  'setCustomerDataAsLoggedIn',
105  'regenerateId'
106  ]
107  );
108  $this->objectManager = $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, ['get']);
109  $this->customerAccountManagementMock =
110  $this->createPartialMock(\Magento\Customer\Model\AccountManagement::class, ['authenticate']);
111 
112  $this->jsonHelperMock = $this->createPartialMock(\Magento\Framework\Json\Helper\Data::class, ['jsonDecode']);
113 
114  $this->resultJson = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
115  ->disableOriginalConstructor()
116  ->getMock();
117  $this->resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
118  ->disableOriginalConstructor()
119  ->setMethods(['create'])
120  ->getMock();
121 
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()
130  ->getMock();
131 
132  $this->resultRaw = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
133  ->disableOriginalConstructor()
134  ->getMock();
135  $resultRawFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
136  ->disableOriginalConstructor()
137  ->setMethods(['create'])
138  ->getMock();
139  $resultRawFactory->expects($this->atLeastOnce())
140  ->method('create')
141  ->willReturn($this->resultRaw);
142 
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);
147 
148  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
149  $this->object = $objectManager->getObject(
150  \Magento\Customer\Controller\Ajax\Login::class,
151  [
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
162  ]
163  );
164  }
165 
166  public function testLogin()
167  {
168  $jsonRequest = '{"username":"[email protected]", "password":"password"}';
169  $loginSuccessResponse = '{"errors": false, "message":"Login successful."}';
170 
171  $this->request
172  ->expects($this->any())
173  ->method('getContent')
174  ->willReturn($jsonRequest);
175 
176  $this->request
177  ->expects($this->any())
178  ->method('getMethod')
179  ->willReturn('POST');
180 
181  $this->request
182  ->expects($this->any())
183  ->method('isXmlHttpRequest')
184  ->willReturn(true);
185 
186  $this->resultJsonFactory->expects($this->atLeastOnce())
187  ->method('create')
188  ->willReturn($this->resultJson);
189 
190  $this->jsonHelperMock
191  ->expects($this->any())
192  ->method('jsonDecode')
193  ->with($jsonRequest)
194  ->willReturn(['username' => '[email protected]', 'password' => 'password']);
195 
196  $customerMock = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
197  $this->customerAccountManagementMock
198  ->expects($this->any())
199  ->method('authenticate')
200  ->with('[email protected]', 'password')
201  ->willReturn($customerMock);
202 
203  $this->customerSession->expects($this->once())
204  ->method('setCustomerDataAsLoggedIn')
205  ->with($customerMock);
206 
207  $this->customerSession->expects($this->once())->method('regenerateId');
208 
209  $redirectMock = $this->createMock(\Magento\Customer\Model\Account\Redirect::class);
210  $this->object->setAccountRedirect($redirectMock);
211  $redirectMock->expects($this->once())->method('getRedirectCookie')->willReturn('some_url1');
212 
213  $this->cookieManager->expects($this->once())
214  ->method('getCookie')
215  ->with('mage-cache-sessid')
216  ->willReturn(true);
217  $this->cookieMetadataFactory->expects($this->once())
218  ->method('createCookieMetadata')
219  ->willReturn($this->cookieMetadata);
220  $this->cookieMetadata->expects($this->once())
221  ->method('setPath')
222  ->with('/')
223  ->willReturnSelf();
224  $this->cookieManager->expects($this->once())
225  ->method('deleteCookie')
226  ->with('mage-cache-sessid', $this->cookieMetadata)
227  ->willReturnSelf();
228 
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')
233  ->willReturn(0);
234 
235  $this->redirectMock->expects($this->once())->method('success')->willReturn('some_url2');
236  $this->resultRaw->expects($this->never())->method('setHttpResponseCode');
237 
238  $result = [
239  'errors' => false,
240  'message' => __('Login successful.'),
241  'redirectUrl' => 'some_url2',
242  ];
243 
244  $this->resultJson
245  ->expects($this->once())
246  ->method('setData')
247  ->with($result)
248  ->willReturn($loginSuccessResponse);
249  $this->assertEquals($loginSuccessResponse, $this->object->execute());
250  }
251 
252  public function testLoginFailure()
253  {
254  $jsonRequest = '{"username":"[email protected]", "password":"invalid"}';
255  $loginFailureResponse = '{"message":"Invalid login or password."}';
256 
257  $this->request
258  ->expects($this->any())
259  ->method('getContent')
260  ->willReturn($jsonRequest);
261 
262  $this->request
263  ->expects($this->any())
264  ->method('getMethod')
265  ->willReturn('POST');
266 
267  $this->request
268  ->expects($this->any())
269  ->method('isXmlHttpRequest')
270  ->willReturn(true);
271 
272  $this->resultJsonFactory->expects($this->once())
273  ->method('create')
274  ->willReturn($this->resultJson);
275 
276  $this->jsonHelperMock
277  ->expects($this->any())
278  ->method('jsonDecode')
279  ->with($jsonRequest)
280  ->willReturn(['username' => '[email protected]', 'password' => 'invalid']);
281 
282  $customerMock = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
283  $this->customerAccountManagementMock
284  ->expects($this->any())
285  ->method('authenticate')
286  ->with('[email protected]', 'invalid')
287  ->willThrowException(new InvalidEmailOrPasswordException(__('Invalid login or password.')));
288 
289  $this->customerSession->expects($this->never())
290  ->method('setCustomerDataAsLoggedIn')
291  ->with($customerMock);
292 
293  $this->customerSession->expects($this->never())->method('regenerateId');
294 
295  $result = [
296  'errors' => true,
297  'message' => __('Invalid login or password.')
298  ];
299  $this->resultJson
300  ->expects($this->once())
301  ->method('setData')
302  ->with($result)
303  ->willReturn($loginFailureResponse);
304 
305  $this->assertEquals($loginFailureResponse, $this->object->execute());
306  }
307 }
__()
Definition: __.php:13