diff --git a/mailpoet/lib/Subscription/CaptchaRenderer.php b/mailpoet/lib/Subscription/CaptchaRenderer.php index 870bfd42b3..fa915b9c31 100644 --- a/mailpoet/lib/Subscription/CaptchaRenderer.php +++ b/mailpoet/lib/Subscription/CaptchaRenderer.php @@ -48,38 +48,12 @@ class CaptchaRenderer { } public function getCaptchaPageContent($sessionId) { + $this->captchaSession->init($sessionId); - $fields = [ - [ - 'id' => 'captcha', - 'type' => 'text', - 'params' => [ - 'label' => __('Type in the characters you see in the picture above:', 'mailpoet'), - 'value' => '', - 'obfuscate' => false, - ], - ], - ]; - - $form = array_merge( - $fields, - [ - [ - 'id' => 'submit', - 'type' => 'submit', - 'params' => [ - 'label' => __('Subscribe', 'mailpoet'), - ], - ], - ] - ); - $captchaSessionForm = $this->captchaSession->getFormData(); - $formId = 0; - $showSuccessMessage = !empty($_GET['mailpoet_success']); $showErrorMessage = !empty($_GET['mailpoet_error']); - + $formId = 0; if (isset($captchaSessionForm['form_id'])) { $formId = (int)$captchaSessionForm['form_id']; } elseif ($showSuccessMessage) { @@ -93,6 +67,33 @@ class CaptchaRenderer { return false; } + $fields = [ + [ + 'id' => 'captcha', + 'type' => 'text', + 'params' => [ + 'label' => __('Type in the characters you see in the picture above:', 'mailpoet'), + 'value' => '', + 'obfuscate' => false, + ], + ], + ]; + + $submitBlocks = $formModel->getBlocksByTypes(['submit']); + $submitLabel = count($submitBlocks) && $submitBlocks[0]['params']['label'] ? $submitBlocks[0]['params']['label'] : __('Subscribe', 'mailpoet'); + $form = array_merge( + $fields, + [ + [ + 'id' => 'submit', + 'type' => 'submit', + 'params' => [ + 'label' => $submitLabel, + ], + ], + ] + ); + if ($showSuccessMessage) { // Display a success message in a no-JS flow return $this->renderFormMessages($formModel, $showSuccessMessage); diff --git a/mailpoet/tests/integration/Subscription/CaptchaRendererTest.php b/mailpoet/tests/integration/Subscription/CaptchaRendererTest.php new file mode 100644 index 0000000000..f84dd5b734 --- /dev/null +++ b/mailpoet/tests/integration/Subscription/CaptchaRendererTest.php @@ -0,0 +1,80 @@ +diContainer->get(FormsRepository::class); + $form = new FormEntity('captcha-render-test-form'); + $form->setBody([ + [ + 'type' => 'text', + 'id' => 'email', + ], + [ + 'type' => 'submit', + 'params' => [ + 'label' => $expectedLabel, + ], + ], + ]); + $form->setSettings([ + 'success_message' => 'tada!', + ]); + $form->setId(1); + $formRepository->persist($form); + $formRepository->flush(); + $captchaSession = $this->diContainer->get(CaptchaSession::class); + $captchaSession->init(); + $captchaSession->setFormData(['form_id' => $form->getId()]); + + $testee = $this->diContainer->get(CaptchaRenderer::class); + $result = $testee->getCaptchaPageContent($captchaSession->getId()); + $this->assertStringContainsString('value="' . $expectedLabel . '"', $result); + } + + public function testCaptchaSubmitTextHasDefault() { + + $formRepository = $this->diContainer->get(FormsRepository::class); + $form = new FormEntity('captcha-render-test-form'); + $form->setBody([ + [ + 'type' => 'text', + 'id' => 'email', + ], + [ + 'type' => 'submit', + 'params' => [ + 'label' => '', + ], + ], + ]); + $form->setSettings([ + 'success_message' => 'tada!', + ]); + $form->setId(1); + $formRepository->persist($form); + $formRepository->flush(); + $captchaSession = $this->diContainer->get(CaptchaSession::class); + $captchaSession->init(); + $captchaSession->setFormData(['form_id' => $form->getId()]); + + $testee = $this->diContainer->get(CaptchaRenderer::class); + $result = $testee->getCaptchaPageContent($captchaSession->getId()); + $this->assertStringContainsString('value="Subscribe"', $result); + } + + public function _before() { + $this->diContainer->get(FormsRepository::class)->truncate(); + parent::_before(); + } +}