Use submit button label of form for captcha submit button

[MAILPOET-4496]
This commit is contained in:
David Remer
2022-08-19 11:41:05 +03:00
committed by Veljko V
parent 538647946e
commit 87ced17813
2 changed files with 109 additions and 28 deletions

View File

@ -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);

View File

@ -0,0 +1,80 @@
<?php
namespace MailPoet\Test\Subscription;
use MailPoet\Entities\FormEntity;
use MailPoet\Form\FormsRepository;
use MailPoet\Subscription\CaptchaRenderer;
use MailPoet\Subscription\CaptchaSession;
class CaptchaRendererTest extends \MailPoetTest
{
public function testCaptchaSubmitTextIsConfigurable() {
$expectedLabel = 'EXPECTED_LABEL';
$formRepository = $this->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();
}
}