@@ -13,91 +13,98 @@ class CheckContactUsFormObserverTest extends \PHPUnit_Framework_TestCase
13
13
protected $ checkContactUsFormObserver ;
14
14
15
15
/**
16
- * @var \PHPUnit_Framework_MockObject_MockObject
16
+ * @var \Magento\Captcha\Helper\Data|\ PHPUnit_Framework_MockObject_MockObject
17
17
*/
18
- protected $ _helper ;
18
+ protected $ helperMock ;
19
19
20
20
/**
21
- * @var \PHPUnit_Framework_MockObject_MockObject
21
+ * @var \Magento\Framework\App\ActionFlag|\ PHPUnit_Framework_MockObject_MockObject
22
22
*/
23
- protected $ _actionFlag ;
23
+ protected $ actionFlagMock ;
24
24
25
25
/*
26
- * @var \PHPUnit_Framework_MockObject_MockObject
26
+ * @var \Magento\Framework\Message\ManagerInterface|\ PHPUnit_Framework_MockObject_MockObject
27
27
*/
28
- protected $ _messageManager ;
28
+ protected $ messageManagerMock ;
29
29
30
30
/**
31
- * @var \PHPUnit_Framework_MockObject_MockObject
31
+ * @var \Magento\Framework\App\Response\RedirectInterface|\ PHPUnit_Framework_MockObject_MockObject
32
32
*/
33
- protected $ redirect ;
33
+ protected $ redirectMock ;
34
34
35
35
/**
36
36
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
37
37
*/
38
- protected $ _objectManager ;
38
+ protected $ objectManagerHelper ;
39
39
40
40
/**
41
- * @var \Magento\Captcha\Observer\CaptchaStringResolver
41
+ * @var \Magento\Captcha\Observer\CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject
42
42
*/
43
- protected $ captchaStringResolver ;
43
+ protected $ captchaStringResolverMock ;
44
44
45
45
/**
46
- * @var \PHPUnit_Framework_MockObject_MockObject
46
+ * @var \Magento\Framework\Session\SessionManager|\ PHPUnit_Framework_MockObject_MockObject
47
47
*/
48
- protected $ _session ;
48
+ protected $ sessionMock ;
49
49
50
50
/**
51
- * @var \PHPUnit_Framework_MockObject_MockObject
51
+ * @var \Magento\Captcha\Model\DefaultModel|\ PHPUnit_Framework_MockObject_MockObject
52
52
*/
53
- protected $ _captcha ;
53
+ protected $ captchaMock ;
54
+
55
+ /**
56
+ * @var \Magento\Framework\App\Request\DataPersistorInterface|\PHPUnit_Framework_MockObject_MockObject
57
+ */
58
+ protected $ dataPersistorMock ;
54
59
55
60
protected function setUp ()
56
61
{
57
- $ this ->_objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
62
+ $ this ->objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
58
63
59
- $ this ->_helper = $ this ->getMock ('Magento\Captcha\Helper\Data ' , [], [], '' , false );
60
-
61
- $ this ->_actionFlag = $ this ->getMock ('Magento\Framework\App\ActionFlag ' , [], [], '' , false );
62
-
63
- $ this ->_messageManager = $ this ->getMock (
64
+ $ this ->helperMock = $ this ->getMock ('Magento\Captcha\Helper\Data ' , [], [], '' , false );
65
+ $ this ->actionFlagMock = $ this ->getMock ('Magento\Framework\App\ActionFlag ' , [], [], '' , false );
66
+ $ this ->messageManagerMock = $ this ->getMock (
64
67
'\Magento\Framework\Message\ManagerInterface ' ,
65
68
[],
66
69
[],
67
70
'' ,
68
71
false
69
72
);
70
-
71
- $ this ->redirect = $ this ->getMock (
73
+ $ this ->redirectMock = $ this ->getMock (
72
74
'\Magento\Framework\App\Response\RedirectInterface ' ,
73
75
[],
74
76
[],
75
77
'' ,
76
78
false
77
79
);
78
-
79
- $ this ->captchaStringResolver = $ this ->getMock (
80
+ $ this ->captchaStringResolverMock = $ this ->getMock (
80
81
'\Magento\Captcha\Observer\CaptchaStringResolver ' ,
81
82
[],
82
83
[],
83
84
'' ,
84
85
false
85
86
);
87
+ $ this ->sessionMock = $ this ->getMock ('Magento\Framework\Session\SessionManager ' , [], [], '' , false );
88
+ $ this ->dataPersistorMock = $ this ->getMockBuilder ('Magento\Framework\App\Request\DataPersistorInterface ' )
89
+ ->getMockForAbstractClass ();
86
90
87
- $ this ->_session = $ this ->getMock ('Magento\Framework\Session\SessionManager ' , [], [], '' , false );
88
-
89
- $ this ->checkContactUsFormObserver = $ this ->_objectManager ->getObject (
91
+ $ this ->checkContactUsFormObserver = $ this ->objectManagerHelper ->getObject (
90
92
'Magento\Captcha\Observer\CheckContactUsFormObserver ' ,
91
93
[
92
- 'helper ' => $ this ->_helper ,
93
- 'actionFlag ' => $ this ->_actionFlag ,
94
- 'messageManager ' => $ this ->_messageManager ,
95
- 'redirect ' => $ this ->redirect ,
96
- 'captchaStringResolver ' => $ this ->captchaStringResolver
94
+ 'helper ' => $ this ->helperMock ,
95
+ 'actionFlag ' => $ this ->actionFlagMock ,
96
+ 'messageManager ' => $ this ->messageManagerMock ,
97
+ 'redirect ' => $ this ->redirectMock ,
98
+ 'captchaStringResolver ' => $ this ->captchaStringResolverMock
97
99
]
98
100
);
101
+ $ this ->objectManagerHelper ->setBackwardCompatibleProperty (
102
+ $ this ->checkContactUsFormObserver ,
103
+ 'dataPersistor ' ,
104
+ $ this ->dataPersistorMock
105
+ );
99
106
100
- $ this ->_captcha = $ this ->getMock ('Magento\Captcha\Model\DefaultModel ' , [], [], '' , false );
107
+ $ this ->captchaMock = $ this ->getMock ('Magento\Captcha\Model\DefaultModel ' , [], [], '' , false );
101
108
}
102
109
103
110
public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid ()
@@ -107,47 +114,24 @@ public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid()
107
114
108
115
$ controller = $ this ->getMock ('Magento\Framework\App\Action\Action ' , [], [], '' , false );
109
116
$ request = $ this ->getMock ('Magento\Framework\App\Request\Http ' , [], [], '' , false );
110
- $ request ->expects (
111
- $ this ->any ()
112
- )->method (
113
- 'getPost '
114
- )->with (
115
- \Magento \Captcha \Helper \Data::INPUT_NAME_FIELD_VALUE ,
116
- null
117
- )->will (
118
- $ this ->returnValue ([$ formId => $ captchaValue ])
119
- );
120
- $ controller ->expects ($ this ->any ())->method ('getRequest ' )->will ($ this ->returnValue ($ request ));
121
- $ this ->_captcha ->expects ($ this ->any ())->method ('isRequired ' )->will ($ this ->returnValue (true ));
122
- $ this ->_captcha ->expects (
123
- $ this ->once ()
124
- )->method (
125
- 'isCorrect '
126
- )->with (
127
- $ captchaValue
128
- )->will (
129
- $ this ->returnValue (true )
130
- );
131
- $ this ->captchaStringResolver ->expects (
132
- $ this ->once ()
133
- )->method (
134
- 'resolve '
135
- )->with (
136
- $ request ,
137
- $ formId
138
- )->will (
139
- $ this ->returnValue ($ captchaValue )
140
- );
141
- $ this ->_helper ->expects (
142
- $ this ->any ()
143
- )->method (
144
- 'getCaptcha '
145
- )->with (
146
- $ formId
147
- )->will (
148
- $ this ->returnValue ($ this ->_captcha )
149
- );
150
- $ this ->_session ->expects ($ this ->never ())->method ('addError ' );
117
+ $ request ->expects ($ this ->any ())
118
+ ->method ('getPost ' )
119
+ ->with (\Magento \Captcha \Helper \Data::INPUT_NAME_FIELD_VALUE , null )
120
+ ->willReturn ([$ formId => $ captchaValue ]);
121
+ $ controller ->expects ($ this ->any ())->method ('getRequest ' )->willReturn ($ request );
122
+ $ this ->captchaMock ->expects ($ this ->any ())->method ('isRequired ' )->willReturn (true );
123
+ $ this ->captchaMock ->expects ($ this ->once ())
124
+ ->method ('isCorrect ' )
125
+ ->with ($ captchaValue )
126
+ ->willReturn (true );
127
+ $ this ->captchaStringResolverMock ->expects ($ this ->once ())
128
+ ->method ('resolve ' )
129
+ ->with ($ request , $ formId )
130
+ ->willReturn ($ captchaValue );
131
+ $ this ->helperMock ->expects ($ this ->any ())
132
+ ->method ('getCaptcha ' )
133
+ ->with ($ formId )->willReturn ($ this ->captchaMock );
134
+ $ this ->sessionMock ->expects ($ this ->never ())->method ('addError ' );
151
135
152
136
$ this ->checkContactUsFormObserver ->execute (
153
137
new \Magento \Framework \Event \Observer (['controller_action ' => $ controller ])
@@ -161,74 +145,46 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
161
145
$ warningMessage = 'Incorrect CAPTCHA. ' ;
162
146
$ redirectRoutePath = 'contact/index/index ' ;
163
147
$ redirectUrl = 'http://magento.com/contacts/ ' ;
148
+ $ postData = ['name ' => 'Some Name ' ];
164
149
165
150
$ request = $ this ->getMock ('Magento\Framework\App\Request\Http ' , [], [], '' , false );
166
151
$ response = $ this ->getMock ('Magento\Framework\App\Response\Http ' , [], [], '' , false );
167
- $ request ->expects (
168
- $ this ->any ()
169
- )->method (
170
- 'getPost '
171
- )->with (
172
- \Magento \Captcha \Helper \Data::INPUT_NAME_FIELD_VALUE ,
173
- null
174
- )->will (
175
- $ this ->returnValue ([$ formId => $ captchaValue ])
176
- );
177
-
178
- $ this ->redirect ->expects (
179
- $ this ->once ()
180
- )->method (
181
- 'redirect '
182
- )->with (
183
- $ response ,
184
- $ redirectRoutePath ,
185
- []
186
- )->will (
187
- $ this ->returnValue ($ redirectUrl )
188
- );
152
+ $ request ->expects ($ this ->any ())
153
+ ->method ('getPost ' )
154
+ ->with (\Magento \Captcha \Helper \Data::INPUT_NAME_FIELD_VALUE , null )
155
+ ->willReturn ([$ formId => $ captchaValue ]);
156
+ $ request ->expects ($ this ->once ())
157
+ ->method ('getPostValue ' )
158
+ ->willReturn ($ postData );
159
+
160
+ $ this ->redirectMock ->expects ($ this ->once ())
161
+ ->method ('redirect ' )
162
+ ->with ($ response , $ redirectRoutePath , [])
163
+ ->willReturn ($ redirectUrl );
189
164
190
165
$ controller = $ this ->getMock ('Magento\Framework\App\Action\Action ' , [], [], '' , false );
191
- $ controller ->expects ($ this ->any ())->method ('getRequest ' )->will ($ this ->returnValue ($ request ));
192
- $ controller ->expects ($ this ->any ())->method ('getResponse ' )->will ($ this ->returnValue ($ response ));
193
- $ this ->_captcha ->expects ($ this ->any ())->method ('isRequired ' )->will ($ this ->returnValue (true ));
194
- $ this ->_captcha ->expects (
195
- $ this ->once ()
196
- )->method (
197
- 'isCorrect '
198
- )->with (
199
- $ captchaValue
200
- )->will (
201
- $ this ->returnValue (false )
202
- );
203
- $ this ->captchaStringResolver ->expects (
204
- $ this ->once ()
205
- )->method (
206
- 'resolve '
207
- )->with (
208
- $ request ,
209
- $ formId
210
- )->will (
211
- $ this ->returnValue ($ captchaValue )
212
- );
213
- $ this ->_helper ->expects (
214
- $ this ->any ()
215
- )->method (
216
- 'getCaptcha '
217
- )->with (
218
- $ formId
219
- )->will (
220
- $ this ->returnValue ($ this ->_captcha )
221
- );
222
- $ this ->_messageManager ->expects ($ this ->once ())->method ('addError ' )->with ($ warningMessage );
223
- $ this ->_actionFlag ->expects (
224
- $ this ->once ()
225
- )->method (
226
- 'set '
227
- )->with (
228
- '' ,
229
- \Magento \Framework \App \Action \Action::FLAG_NO_DISPATCH ,
230
- true
231
- );
166
+ $ controller ->expects ($ this ->any ())->method ('getRequest ' )->willReturn ($ request );
167
+ $ controller ->expects ($ this ->any ())->method ('getResponse ' )->willReturn ($ response );
168
+ $ this ->captchaMock ->expects ($ this ->any ())->method ('isRequired ' )->willReturn (true );
169
+ $ this ->captchaMock ->expects ($ this ->once ())
170
+ ->method ('isCorrect ' )
171
+ ->with ($ captchaValue )
172
+ ->willReturn (false );
173
+ $ this ->captchaStringResolverMock ->expects ($ this ->once ())
174
+ ->method ('resolve ' )
175
+ ->with ($ request , $ formId )
176
+ ->willReturn ($ captchaValue );
177
+ $ this ->helperMock ->expects ($ this ->any ())
178
+ ->method ('getCaptcha ' )
179
+ ->with ($ formId )
180
+ ->willReturn ($ this ->captchaMock );
181
+ $ this ->messageManagerMock ->expects ($ this ->once ())->method ('addError ' )->with ($ warningMessage );
182
+ $ this ->actionFlagMock ->expects ($ this ->once ())
183
+ ->method ('set ' )
184
+ ->with ('' , \Magento \Framework \App \Action \Action::FLAG_NO_DISPATCH , true );
185
+ $ this ->dataPersistorMock ->expects ($ this ->once ())
186
+ ->method ('set ' )
187
+ ->with ($ formId , $ postData );
232
188
233
189
$ this ->checkContactUsFormObserver ->execute (
234
190
new \Magento \Framework \Event \Observer (['controller_action ' => $ controller ])
@@ -237,17 +193,12 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
237
193
238
194
public function testCheckContactUsFormDoesNotCheckCaptchaWhenItIsNotRequired ()
239
195
{
240
- $ this ->_helper ->expects (
241
- $ this ->any ()
242
- )->method (
243
- 'getCaptcha '
244
- )->with (
245
- 'contact_us '
246
- )->will (
247
- $ this ->returnValue ($ this ->_captcha )
248
- );
249
- $ this ->_captcha ->expects ($ this ->any ())->method ('isRequired ' )->will ($ this ->returnValue (false ));
250
- $ this ->_captcha ->expects ($ this ->never ())->method ('isCorrect ' );
196
+ $ this ->helperMock ->expects ($ this ->any ())
197
+ ->method ('getCaptcha ' )
198
+ ->with ('contact_us ' )
199
+ ->willReturn ($ this ->captchaMock );
200
+ $ this ->captchaMock ->expects ($ this ->any ())->method ('isRequired ' )->willReturn (false );
201
+ $ this ->captchaMock ->expects ($ this ->never ())->method ('isCorrect ' );
251
202
252
203
$ this ->checkContactUsFormObserver ->execute (new \Magento \Framework \Event \Observer ());
253
204
}
0 commit comments