33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Captcha \Test \Unit \Model \Customer \Plugin ;
78
89class AjaxLoginTest extends \PHPUnit \Framework \TestCase
910{
1011 /**
11- * @var \PHPUnit_Framework_MockObject_MockObject
12+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Checkout\Model\Session
1213 */
1314 protected $ sessionManagerMock ;
1415
1516 /**
16- * @var \PHPUnit_Framework_MockObject_MockObject
17+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Captcha\Helper\Data
1718 */
1819 protected $ captchaHelperMock ;
1920
2021 /**
21- * @var \PHPUnit_Framework_MockObject_MockObject
22+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Controller\Result\JsonFactory
2223 */
2324 protected $ jsonFactoryMock ;
2425
@@ -38,12 +39,12 @@ class AjaxLoginTest extends \PHPUnit\Framework\TestCase
3839 protected $ requestMock ;
3940
4041 /**
41- * @var \PHPUnit_Framework_MockObject_MockObject
42+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Controller\Ajax\Login
4243 */
4344 protected $ loginControllerMock ;
4445
4546 /**
46- * @var \PHPUnit_Framework_MockObject_MockObject
47+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Serialize\Serializer\Json
4748 */
4849 protected $ serializerMock ;
4950
@@ -72,8 +73,12 @@ protected function setUp()
7273
7374 $ this ->loginControllerMock ->expects ($ this ->any ())->method ('getRequest ' )
7475 ->will ($ this ->returnValue ($ this ->requestMock ));
75- $ this ->captchaHelperMock ->expects ($ this ->once ())->method ('getCaptcha ' )
76- ->with ('user_login ' )->will ($ this ->returnValue ($ this ->captchaMock ));
76+
77+ $ this ->captchaHelperMock
78+ ->expects ($ this ->exactly (1 ))
79+ ->method ('getCaptcha ' )
80+ ->will ($ this ->returnValue ($ this ->captchaMock ));
81+
7782 $ this ->formIds = ['user_login ' ];
7883 $ this ->serializerMock = $ this ->createMock (\Magento \Framework \Serialize \Serializer \Json::class);
7984
@@ -103,11 +108,18 @@ public function testAroundExecute()
103108 $ this ->captchaMock ->expects ($ this ->once ())->method ('logAttempt ' )->with ($ username );
104109 $ this ->captchaMock ->expects ($ this ->once ())->method ('isCorrect ' )->with ($ captchaString )
105110 ->will ($ this ->returnValue (true ));
106- $ this ->serializerMock ->expects (( $ this ->once () ))->method ('unserialize ' )->will ($ this ->returnValue ($ requestData ));
111+ $ this ->serializerMock ->expects ($ this ->once ())->method ('unserialize ' )->will ($ this ->returnValue ($ requestData ));
107112
108113 $ closure = function () {
109114 return 'result ' ;
110115 };
116+
117+ $ this ->captchaHelperMock
118+ ->expects ($ this ->exactly (1 ))
119+ ->method ('getCaptcha ' )
120+ ->with ('user_login ' )
121+ ->will ($ this ->returnValue ($ this ->captchaMock ));
122+
111123 $ this ->assertEquals ('result ' , $ this ->model ->aroundExecute ($ this ->loginControllerMock , $ closure ));
112124 }
113125
@@ -128,18 +140,21 @@ public function testAroundExecuteIncorrectCaptcha()
128140 $ this ->captchaMock ->expects ($ this ->once ())->method ('logAttempt ' )->with ($ username );
129141 $ this ->captchaMock ->expects ($ this ->once ())->method ('isCorrect ' )
130142 ->with ($ captchaString )->will ($ this ->returnValue (false ));
131- $ this ->serializerMock ->expects (( $ this ->once () ))->method ('unserialize ' )->will ($ this ->returnValue ($ requestData ));
143+ $ this ->serializerMock ->expects ($ this ->once ())->method ('unserialize ' )->will ($ this ->returnValue ($ requestData ));
132144
133145 $ this ->sessionManagerMock ->expects ($ this ->once ())->method ('setUsername ' )->with ($ username );
134146 $ this ->jsonFactoryMock ->expects ($ this ->once ())->method ('create ' )
135147 ->will ($ this ->returnValue ($ this ->resultJsonMock ));
136148
137- $ this ->resultJsonMock ->expects ($ this ->once ())->method ('setData ' )
138- ->with (['errors ' => true , 'message ' => __ ('Incorrect CAPTCHA ' )])->will ($ this ->returnValue ('response ' ));
149+ $ this ->resultJsonMock
150+ ->expects ($ this ->once ())
151+ ->method ('setData ' )
152+ ->with (['errors ' => true , 'message ' => __ ('Incorrect CAPTCHA ' )])
153+ ->will ($ this ->returnSelf ());
139154
140155 $ closure = function () {
141156 };
142- $ this ->assertEquals (' response ' , $ this ->model ->aroundExecute ($ this ->loginControllerMock , $ closure ));
157+ $ this ->assertEquals ($ this -> resultJsonMock , $ this ->model ->aroundExecute ($ this ->loginControllerMock , $ closure ));
143158 }
144159
145160 /**
@@ -151,7 +166,7 @@ public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent
151166 {
152167 $ this ->requestMock ->expects ($ this ->once ())->method ('getContent ' )
153168 ->will ($ this ->returnValue (json_encode ($ requestContent )));
154- $ this ->serializerMock ->expects (( $ this ->once () ))->method ('unserialize ' )
169+ $ this ->serializerMock ->expects ($ this ->once ())->method ('unserialize ' )
155170 ->will ($ this ->returnValue ($ requestContent ));
156171
157172 $ this ->captchaMock ->expects ($ this ->once ())->method ('isRequired ' )->with ($ username )
@@ -168,16 +183,39 @@ public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent
168183 /**
169184 * @return array
170185 */
171- public function aroundExecuteCaptchaIsNotRequired ()
186+ public function aroundExecuteCaptchaIsNotRequired (): array
172187 {
173188 return [
174189 [
175190 'username ' => 'name ' ,
176191 'requestData ' => ['username ' => 'name ' , 'captcha_string ' => 'string ' ],
177192 ],
193+ [
194+ 'username ' => 'name ' ,
195+ 'requestData ' =>
196+ [
197+ 'username ' => 'name ' ,
198+ 'captcha_string ' => 'string ' ,
199+ 'captcha_form_id ' => $ this ->formIds [0 ]
200+ ],
201+ ],
178202 [
179203 'username ' => null ,
180- 'requestData ' => ['captcha_string ' => 'string ' ],
204+ 'requestData ' =>
205+ [
206+ 'username ' => null ,
207+ 'captcha_string ' => 'string ' ,
208+ 'captcha_form_id ' => $ this ->formIds [0 ]
209+ ],
210+ ],
211+ [
212+ 'username ' => 'name ' ,
213+ 'requestData ' =>
214+ [
215+ 'username ' => 'name ' ,
216+ 'captcha_string ' => 'string ' ,
217+ 'captcha_form_id ' => null
218+ ],
181219 ],
182220 ];
183221 }
0 commit comments