9
9
10
10
use Magento \Framework \App \Request \Http as HttpRequest ;
11
11
use Magento \Framework \Data \Form \FormKey ;
12
- use Magento \Framework \Exception \InputException ;
13
12
use Magento \Framework \Message \MessageInterface ;
14
13
use Magento \Framework \Validation \ValidationResult ;
15
14
use Magento \ReCaptchaUi \Model \CaptchaResponseResolverInterface ;
@@ -43,7 +42,7 @@ class ContactFormTest extends AbstractController
43
42
/**
44
43
* @inheritDoc
45
44
*/
46
- protected function setUp ()
45
+ protected function setUp (): void
47
46
{
48
47
parent ::setUp ();
49
48
$ this ->formKey = $ this ->_objectManager ->get (FormKey::class);
@@ -62,7 +61,7 @@ protected function setUp()
62
61
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
63
62
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
64
63
*/
65
- public function testGetRequestIfReCaptchaIsDisabled ()
64
+ public function testGetRequestIfReCaptchaIsDisabled (): void
66
65
{
67
66
$ this ->setConfig (false , 'test_public_key ' , 'test_private_key ' );
68
67
@@ -76,7 +75,7 @@ public function testGetRequestIfReCaptchaIsDisabled()
76
75
* It's needed for proper work of "ifconfig" in layout during tests running
77
76
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
78
77
*/
79
- public function testGetRequestIfReCaptchaKeysAreNotConfigured ()
78
+ public function testGetRequestIfReCaptchaKeysAreNotConfigured (): void
80
79
{
81
80
$ this ->setConfig (true , null , null );
82
81
@@ -92,7 +91,7 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured()
92
91
* It's needed for proper work of "ifconfig" in layout during tests running
93
92
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
94
93
*/
95
- public function testGetRequestIfReCaptchaIsEnabled ()
94
+ public function testGetRequestIfReCaptchaIsEnabled (): void
96
95
{
97
96
$ this ->setConfig (true , 'test_public_key ' , 'test_private_key ' );
98
97
@@ -104,22 +103,22 @@ public function testGetRequestIfReCaptchaIsEnabled()
104
103
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
105
104
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
106
105
*/
107
- public function testPostRequestIfReCaptchaIsDisabled ()
106
+ public function testPostRequestIfReCaptchaIsDisabled (): void
108
107
{
109
108
$ this ->setConfig (false , 'test_public_key ' , 'test_private_key ' );
110
109
111
- $ this ->checkPostResponse ( true );
110
+ $ this ->checkSuccessfulPostResponse ( );
112
111
}
113
112
114
113
/**
115
114
* @magentoConfigFixture default_store customer/captcha/enable 0
116
115
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
117
116
*/
118
- public function testPostRequestIfReCaptchaKeysAreNotConfigured ()
117
+ public function testPostRequestIfReCaptchaKeysAreNotConfigured (): void
119
118
{
120
119
$ this ->setConfig (true , null , null );
121
120
122
- $ this ->checkPostResponse ( true );
121
+ $ this ->checkSuccessfulPostResponse ( );
123
122
}
124
123
125
124
/**
@@ -128,13 +127,12 @@ public function testPostRequestIfReCaptchaKeysAreNotConfigured()
128
127
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
129
128
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
130
129
*/
131
- public function testPostRequestWithSuccessfulReCaptchaValidation ()
130
+ public function testPostRequestWithSuccessfulReCaptchaValidation (): void
132
131
{
133
132
$ this ->setConfig (true , 'test_public_key ' , 'test_private_key ' );
134
133
$ this ->captchaValidationResultMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (true );
135
134
136
- $ this ->checkPostResponse (
137
- true ,
135
+ $ this ->checkSuccessfulPostResponse (
138
136
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test ' ]
139
137
);
140
138
}
@@ -145,14 +143,11 @@ public function testPostRequestWithSuccessfulReCaptchaValidation()
145
143
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
146
144
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
147
145
*/
148
- public function testPostRequestIfReCaptchaParameterIsMissed ()
146
+ public function testPostRequestIfReCaptchaParameterIsMissed (): void
149
147
{
150
148
$ this ->setConfig (true , 'test_public_key ' , 'test_private_key ' );
151
149
152
- $ this ->expectException (InputException::class);
153
- $ this ->expectExceptionMessage ('Can not resolve reCAPTCHA parameter. ' );
154
-
155
- $ this ->checkPostResponse (false );
150
+ $ this ->checkFailedPostResponse ();
156
151
}
157
152
158
153
/**
@@ -161,21 +156,21 @@ public function testPostRequestIfReCaptchaParameterIsMissed()
161
156
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
162
157
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
163
158
*/
164
- public function testPostRequestWithFailedReCaptchaValidation ()
159
+ public function testPostRequestWithFailedReCaptchaValidation (): void
165
160
{
166
161
$ this ->setConfig (true , 'test_public_key ' , 'test_private_key ' );
167
162
$ this ->captchaValidationResultMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
168
163
169
- $ this ->checkPostResponse (
170
- false ,
164
+ $ this ->checkFailedPostResponse (
171
165
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test ' ]
172
166
);
173
167
}
174
168
175
169
/**
176
170
* @param bool $shouldContainReCaptcha
171
+ * @return void
177
172
*/
178
- private function checkSuccessfulGetResponse ($ shouldContainReCaptcha = false )
173
+ private function checkSuccessfulGetResponse ($ shouldContainReCaptcha = false ): void
179
174
{
180
175
$ this ->dispatch ('contact/index ' );
181
176
$ content = $ this ->getResponse ()->getBody ();
@@ -190,10 +185,41 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
190
185
}
191
186
192
187
/**
193
- * @param bool $isSuccessfulRequest
194
188
* @param array $postValues
189
+ * @return void
190
+ */
191
+ private function checkSuccessfulPostResponse (array $ postValues = []): void
192
+ {
193
+ $ this ->makePostRequest ($ postValues );
194
+
195
+ $ this ->assertSessionMessages (
196
+ self ::contains (
197
+ "Thanks for contacting us with your comments and questions. We'll respond to you very soon. "
198
+ ),
199
+ MessageInterface::TYPE_SUCCESS
200
+ );
201
+ self ::assertEmpty ($ this ->getSessionMessages (MessageInterface::TYPE_ERROR ));
202
+ }
203
+
204
+ /**
205
+ * @param array $postValues
206
+ * @return void
207
+ */
208
+ private function checkFailedPostResponse (array $ postValues = []): void
209
+ {
210
+ $ this ->makePostRequest ($ postValues );
211
+
212
+ $ this ->assertSessionMessages (
213
+ $ this ->equalTo (['reCAPTCHA verification failed ' ]),
214
+ MessageInterface::TYPE_ERROR
215
+ );
216
+ }
217
+
218
+ /**
219
+ * @param array $postValues
220
+ * @return void
195
221
*/
196
- private function checkPostResponse ( bool $ isSuccessfulRequest , array $ postValues = [])
222
+ private function makePostRequest ( array $ postValues = []): void
197
223
{
198
224
$ this ->getRequest ()
199
225
->setMethod (HttpRequest::METHOD_POST )
@@ -208,29 +234,14 @@ private function checkPostResponse(bool $isSuccessfulRequest, array $postValues
208
234
));
209
235
210
236
$ this ->dispatch ('contact/index/post ' );
211
-
212
237
$ this ->assertRedirect (self ::stringContains ('contact/index ' ));
213
-
214
- if ($ isSuccessfulRequest ) {
215
- $ this ->assertSessionMessages (
216
- self ::contains (
217
- "Thanks for contacting us with your comments and questions. We'll respond to you very soon. "
218
- ),
219
- MessageInterface::TYPE_SUCCESS
220
- );
221
- self ::assertEmpty ($ this ->getSessionMessages (MessageInterface::TYPE_ERROR ));
222
- } else {
223
- $ this ->assertSessionMessages (
224
- $ this ->equalTo (['reCAPTCHA verification failed ' ]),
225
- MessageInterface::TYPE_ERROR
226
- );
227
- }
228
238
}
229
239
230
240
/**
231
241
* @param bool $isEnabled
232
242
* @param string|null $public
233
243
* @param string|null $private
244
+ * @return void
234
245
*/
235
246
private function setConfig (bool $ isEnabled , ?string $ public , ?string $ private ): void
236
247
{
@@ -250,4 +261,23 @@ private function setConfig(bool $isEnabled, ?string $public, ?string $private):
250
261
ScopeInterface::SCOPE_WEBSITE
251
262
);
252
263
}
264
+
265
+ public function tearDown (): void
266
+ {
267
+ $ this ->mutableScopeConfig ->setValue (
268
+ 'recaptcha_frontend/type_for/contact ' ,
269
+ null ,
270
+ ScopeInterface::SCOPE_WEBSITE
271
+ );
272
+ $ this ->mutableScopeConfig ->setValue (
273
+ 'recaptcha_frontend/type_invisible/public_key ' ,
274
+ null ,
275
+ ScopeInterface::SCOPE_WEBSITE
276
+ );
277
+ $ this ->mutableScopeConfig ->setValue (
278
+ 'recaptcha_frontend/type_invisible/private_key ' ,
279
+ null ,
280
+ ScopeInterface::SCOPE_WEBSITE
281
+ );
282
+ }
253
283
}
0 commit comments