3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Captcha \Test \Unit \Model \Customer \Plugin ;
7
8
8
9
class AjaxLoginTest extends \PHPUnit \Framework \TestCase
9
10
{
10
11
/**
11
- * @var \PHPUnit_Framework_MockObject_MockObject
12
+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Checkout\Model\Session
12
13
*/
13
14
protected $ sessionManagerMock ;
14
15
15
16
/**
16
- * @var \PHPUnit_Framework_MockObject_MockObject
17
+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Captcha\Helper\Data
17
18
*/
18
19
protected $ captchaHelperMock ;
19
20
20
21
/**
21
- * @var \PHPUnit_Framework_MockObject_MockObject
22
+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Controller\Result\JsonFactory
22
23
*/
23
24
protected $ jsonFactoryMock ;
24
25
@@ -38,12 +39,12 @@ class AjaxLoginTest extends \PHPUnit\Framework\TestCase
38
39
protected $ requestMock ;
39
40
40
41
/**
41
- * @var \PHPUnit_Framework_MockObject_MockObject
42
+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Controller\Ajax\Login
42
43
*/
43
44
protected $ loginControllerMock ;
44
45
45
46
/**
46
- * @var \PHPUnit_Framework_MockObject_MockObject
47
+ * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Serialize\Serializer\Json
47
48
*/
48
49
protected $ serializerMock ;
49
50
@@ -72,8 +73,12 @@ protected function setUp()
72
73
73
74
$ this ->loginControllerMock ->expects ($ this ->any ())->method ('getRequest ' )
74
75
->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
+
77
82
$ this ->formIds = ['user_login ' ];
78
83
$ this ->serializerMock = $ this ->createMock (\Magento \Framework \Serialize \Serializer \Json::class);
79
84
@@ -103,11 +108,18 @@ public function testAroundExecute()
103
108
$ this ->captchaMock ->expects ($ this ->once ())->method ('logAttempt ' )->with ($ username );
104
109
$ this ->captchaMock ->expects ($ this ->once ())->method ('isCorrect ' )->with ($ captchaString )
105
110
->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 ));
107
112
108
113
$ closure = function () {
109
114
return 'result ' ;
110
115
};
116
+
117
+ $ this ->captchaHelperMock
118
+ ->expects ($ this ->exactly (1 ))
119
+ ->method ('getCaptcha ' )
120
+ ->with ('user_login ' )
121
+ ->will ($ this ->returnValue ($ this ->captchaMock ));
122
+
111
123
$ this ->assertEquals ('result ' , $ this ->model ->aroundExecute ($ this ->loginControllerMock , $ closure ));
112
124
}
113
125
@@ -128,18 +140,21 @@ public function testAroundExecuteIncorrectCaptcha()
128
140
$ this ->captchaMock ->expects ($ this ->once ())->method ('logAttempt ' )->with ($ username );
129
141
$ this ->captchaMock ->expects ($ this ->once ())->method ('isCorrect ' )
130
142
->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 ));
132
144
133
145
$ this ->sessionManagerMock ->expects ($ this ->once ())->method ('setUsername ' )->with ($ username );
134
146
$ this ->jsonFactoryMock ->expects ($ this ->once ())->method ('create ' )
135
147
->will ($ this ->returnValue ($ this ->resultJsonMock ));
136
148
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 ());
139
154
140
155
$ closure = function () {
141
156
};
142
- $ this ->assertEquals (' response ' , $ this ->model ->aroundExecute ($ this ->loginControllerMock , $ closure ));
157
+ $ this ->assertEquals ($ this -> resultJsonMock , $ this ->model ->aroundExecute ($ this ->loginControllerMock , $ closure ));
143
158
}
144
159
145
160
/**
@@ -151,7 +166,7 @@ public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent
151
166
{
152
167
$ this ->requestMock ->expects ($ this ->once ())->method ('getContent ' )
153
168
->will ($ this ->returnValue (json_encode ($ requestContent )));
154
- $ this ->serializerMock ->expects (( $ this ->once () ))->method ('unserialize ' )
169
+ $ this ->serializerMock ->expects ($ this ->once ())->method ('unserialize ' )
155
170
->will ($ this ->returnValue ($ requestContent ));
156
171
157
172
$ this ->captchaMock ->expects ($ this ->once ())->method ('isRequired ' )->with ($ username )
@@ -168,16 +183,39 @@ public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent
168
183
/**
169
184
* @return array
170
185
*/
171
- public function aroundExecuteCaptchaIsNotRequired ()
186
+ public function aroundExecuteCaptchaIsNotRequired (): array
172
187
{
173
188
return [
174
189
[
175
190
'username ' => 'name ' ,
176
191
'requestData ' => ['username ' => 'name ' , 'captcha_string ' => 'string ' ],
177
192
],
193
+ [
194
+ 'username ' => 'name ' ,
195
+ 'requestData ' =>
196
+ [
197
+ 'username ' => 'name ' ,
198
+ 'captcha_string ' => 'string ' ,
199
+ 'captcha_form_id ' => $ this ->formIds [0 ]
200
+ ],
201
+ ],
178
202
[
179
203
'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
+ ],
181
219
],
182
220
];
183
221
}
0 commit comments