2020use PHPUnit \Framework \MockObject \MockObject ;
2121
2222/**
23- * Test for \Magento\ReCaptchaContact\Observer\ContactFormObserver class.
24- *
2523 * @magentoAppArea frontend
26- * @magentoDbIsolation enabled
2724 * @magentoAppIsolation enabled
2825 */
2926class ContactFormTest extends AbstractController
@@ -60,6 +57,11 @@ protected function setUp()
6057 $ this ->_objectManager ->addSharedInstance ($ captchaValidatorMock , Validator::class);
6158 }
6259
60+ /**
61+ * @magentoConfigFixture base_website customer/captcha/enable 0
62+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
63+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
64+ */
6365 public function testGetRequestIfReCaptchaIsDisabled ()
6466 {
6567 $ this ->initConfig (0 , 'test_public_key ' , 'test_private_key ' );
@@ -68,7 +70,8 @@ public function testGetRequestIfReCaptchaIsDisabled()
6870 }
6971
7072 /**
71- * @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
73+ * @magentoConfigFixture base_website customer/captcha/enable 0
74+ * @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
7275 */
7376 public function testGetRequestIfReCaptchaKeysAreNotConfigured ()
7477 {
@@ -78,7 +81,10 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured()
7881 }
7982
8083 /**
81- * @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
84+ * @magentoConfigFixture base_website customer/captcha/enable 0
85+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
86+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
87+ * @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
8288 */
8389 public function testGetRequestIfReCaptchaIsEnabled ()
8490 {
@@ -87,51 +93,78 @@ public function testGetRequestIfReCaptchaIsEnabled()
8793 $ this ->checkSuccessfulGetResponse (true );
8894 }
8995
90- public function testPostRequestIfReCaptchaKeysAreNotConfigured ()
96+ /**
97+ * @magentoConfigFixture base_website customer/captcha/enable 0
98+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
99+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
100+ */
101+ public function testPostRequestIfReCaptchaIsDisabled ()
91102 {
92- $ this ->initConfig (1 , null , null );
103+ $ this ->initConfig (0 , ' test_public_key ' , ' test_private_key ' );
93104
94- $ this ->checkSuccessfulPostResponse (true );
105+ $ this ->checkPostResponse (true );
95106 }
96107
97- public function testPostRequestIfReCaptchaIsDisabled ()
108+ /**
109+ * @magentoConfigFixture base_website customer/captcha/enable 0
110+ * @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
111+ */
112+ public function testPostRequestIfReCaptchaKeysAreNotConfigured ()
98113 {
99- $ this ->initConfig (0 , ' test_public_key ' , ' test_private_key ' );
114+ $ this ->initConfig (1 , null , null );
100115
101- $ this ->checkSuccessfulPostResponse (true );
116+ $ this ->checkPostResponse (true );
102117 }
103118
119+ /**
120+ * @magentoConfigFixture base_website customer/captcha/enable 0
121+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
122+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
123+ * @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
124+ */
104125 public function testPostRequestWithSuccessfulReCaptchaValidation ()
105126 {
106127 $ this ->initConfig (1 , 'test_public_key ' , 'test_private_key ' );
107128 $ this ->captchaValidationResultMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (true );
108129
109- $ this ->checkSuccessfulPostResponse (
130+ $ this ->checkPostResponse (
110131 true ,
111- [CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test_response ' ]
132+ [
133+ CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test ' ,
134+ ]
112135 );
113136 }
114137
115- public function testPostRequestWithFailedReCaptchaValidation ()
138+ /**
139+ * @magentoConfigFixture base_website customer/captcha/enable 0
140+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
141+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
142+ * @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
143+ */
144+ public function testPostRequestIfReCaptchaParameterIsMissed ()
116145 {
117146 $ this ->initConfig (1 , 'test_public_key ' , 'test_private_key ' );
118- $ this ->captchaValidationResultMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
119147
120- $ this ->checkSuccessfulPostResponse (
121- false ,
122- [CaptchaResponseResolverInterface:: PARAM_RECAPTCHA => ' test_response ' ]
123- );
148+ $ this ->expectException (InputException::class);
149+ $ this -> expectExceptionMessage ( ' Can not resolve reCAPTCHA parameter. ' );
150+
151+ $ this -> checkPostResponse ( false );
124152 }
125153
126- public function testPostRequestIfReCaptchaParameterIsMissed ()
154+ /**
155+ * @magentoConfigFixture base_website customer/captcha/enable 0
156+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
157+ * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
158+ * @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
159+ */
160+ public function testPostRequestWithFailedReCaptchaValidation ()
127161 {
128162 $ this ->initConfig (1 , 'test_public_key ' , 'test_private_key ' );
129- $ this ->captchaValidationResultMock ->expects ($ this ->never ())->method ('isValid ' );
130- $ this ->expectException (InputException::class);
131- $ this ->expectExceptionMessage ('Can not resolve reCAPTCHA parameter. ' );
163+ $ this ->captchaValidationResultMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
132164
133- $ this ->checkSuccessfulPostResponse (
134- false
165+ $ this ->checkPostResponse (
166+ false ,
167+ [CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test ' ]
135168 );
136169 }
137170
@@ -153,28 +186,28 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
153186 }
154187
155188 /**
156- * @param bool $result
189+ * @param bool $isSuccessfulRequest
157190 * @param array $postValues
158- * @throws LocalizedException
159- * @throws \Magento\Framework\Exception\NoSuchEntityException
160191 */
161- private function checkSuccessfulPostResponse (bool $ result , array $ postValues = [])
192+ private function checkPostResponse (bool $ isSuccessfulRequest , array $ postValues = [])
162193 {
163- $ this ->getRequest ()->setPostValue (array_replace_recursive (
164- [
165- 'form_key ' => $ this ->formKey ->getFormKey (),
166- 'name ' => 'customer name ' ,
167- 'comment ' => 'comment ' ,
168- 169- ],
170- $ postValues
171- ))->setMethod (HttpRequest::METHOD_POST );
194+ $ this ->getRequest ()
195+ ->setMethod (HttpRequest::METHOD_POST )
196+ ->setPostValue (array_replace_recursive (
197+ [
198+ 'form_key ' => $ this ->formKey ->getFormKey (),
199+ 'name ' => 'customer name ' ,
200+ 'comment ' => 'comment ' ,
201+ 202+ ],
203+ $ postValues
204+ ));
172205
173206 $ this ->dispatch ('contact/index/post ' );
174207
175208 $ this ->assertRedirect ($ this ->stringContains ('contact/index ' ));
176209
177- if ($ result ) {
210+ if ($ isSuccessfulRequest ) {
178211 $ this ->assertSessionMessages (
179212 $ this ->contains (
180213 "Thanks for contacting us with your comments and questions. We'll respond to you very soon. "
@@ -197,9 +230,20 @@ private function checkSuccessfulPostResponse(bool $result, array $postValues = [
197230 */
198231 private function initConfig (?int $ enabled , ?string $ public , ?string $ private ): void
199232 {
200- $ this ->mutableScopeConfig ->setValue ('recaptcha_frontend/type_for/newsletter ' , null , ScopeInterface::SCOPE_WEBSITE );
201- $ this ->mutableScopeConfig ->setValue ('recaptcha_frontend/type_for/contact ' , $ enabled ? 'invisible ' : null , ScopeInterface::SCOPE_WEBSITE );
202- $ this ->mutableScopeConfig ->setValue ('recaptcha_frontend/type_invisible/public_key ' , $ public , ScopeInterface::SCOPE_WEBSITE );
203- $ this ->mutableScopeConfig ->setValue ('recaptcha_frontend/type_invisible/private_key ' , $ private , ScopeInterface::SCOPE_WEBSITE );
233+ $ this ->mutableScopeConfig ->setValue (
234+ 'recaptcha_frontend/type_for/contact ' ,
235+ $ enabled ? 'invisible ' : null ,
236+ ScopeInterface::SCOPE_WEBSITE
237+ );
238+ $ this ->mutableScopeConfig ->setValue (
239+ 'recaptcha_frontend/type_invisible/public_key ' ,
240+ $ public ,
241+ ScopeInterface::SCOPE_WEBSITE
242+ );
243+ $ this ->mutableScopeConfig ->setValue (
244+ 'recaptcha_frontend/type_invisible/private_key ' ,
245+ $ private ,
246+ ScopeInterface::SCOPE_WEBSITE
247+ );
204248 }
205249}
0 commit comments