Commit ae73e6e3 authored by BryanDeNijs's avatar BryanDeNijs Committed by Jakob Perry
Browse files

Issue #2959577 by BryanDeNijs, paulocs, vakulrai: Make the incorrect answer...

Issue #2959577 by BryanDeNijs, paulocs, vakulrai: Make the incorrect answer error message configurable
parent 84f543a3
......@@ -178,6 +178,20 @@ function _captcha_get_description() {
return Xss::filter($description);
}
/**
* Gets the error message for when a user enters an incorrect CAPTCHA answer.
*
* @return string
* Error message.
*/
function _captcha_get_error_message() {
$error_message = \Drupal::config('captcha.settings')->get('wrong_captcha_response_message');
if ($error_message) {
return Xss::filter($error_message);
}
return t('The answer you entered for the CAPTCHA was not correct.');
}
/**
* Parse or interpret the given captcha_type.
*
......
......@@ -509,7 +509,7 @@ function captcha_validate($element, FormStateInterface &$form_state) {
// arrays for more advanced use cases.
if (!$captcha_validate($solution, $captcha_response, $element, $form_state)) {
// Wrong answer.
$form_state->setErrorByName('captcha_response', t('The answer you entered for the CAPTCHA was not correct.'));
$form_state->setErrorByName('captcha_response', _captcha_get_error_message());
// Update wrong response counter.
if (\Drupal::config('captcha.settings')->get('enable_stats', FALSE)) {
Drupal::state()->set('captcha.wrong_response_counter', Drupal::state()
......@@ -578,7 +578,7 @@ function captcha_validate($element, FormStateInterface &$form_state) {
->expression('attempts', 'attempts + 1')
->execute();
$form_state->setErrorByName('captcha_response', t('The answer you entered for the CAPTCHA was not correct.'));
$form_state->setErrorByName('captcha_response', _captcha_get_error_message());
// Update wrong response counter.
if (\Drupal::config('captcha.settings')->get('enable_stats', FALSE)) {
Drupal::state()->set('captcha.wrong_response_counter', Drupal::state()
......
......@@ -23,6 +23,9 @@ captcha.settings:
add_captcha_description:
type: boolean
label: 'Add a description to the CAPTCHA'
wrong_captcha_response_message:
type: label
label: 'The error message when a user has entered an incorrect CAPTCHA answer.'
default_validation:
type: integer
label: 'Default CAPTCHA validation'
......
......@@ -164,6 +164,16 @@ class CaptchaSettingsForm extends ConfigFormBase {
],
];
// Field for the wrong captcha response error message.
$form['wrong_captcha_response_message'] = [
'#type' => 'textfield',
'#title' => $this->t('Wrong CAPTCHA response error message'),
'#description' => $this->t('Configurable error message that the user gets when it enters an incorrect CAPTCHA answer.'),
'#default_value' => _captcha_get_error_message(),
'#maxlength' => 256,
'#required' => TRUE,
];
// Option for case sensitive/insensitive validation of the responses.
$form['default_validation'] = [
'#type' => 'radios',
......@@ -241,6 +251,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
// Save (or reset) the CAPTCHA descriptions.
$config->set('description', $form_state->getValue('description'));
$config->set('wrong_captcha_response_message', $form_state->getValue('wrong_captcha_response_message'));
$config->set('default_validation', $form_state->getValue('default_validation'));
$config->set('persistence', $form_state->getValue('persistence'));
$config->set('enable_stats', $form_state->getValue('enable_stats'));
......
......@@ -59,6 +59,41 @@ class CaptchaTest extends CaptchaWebTestBase {
$this->assertSession()->fieldExists('pass');
}
/**
* Testing the response error menssage.
*/
public function testCaptchaResponseErrorMenssage() {
// Customize the response error message.
$this->drupalLogin($this->adminUser);
$customized_menssage = 'The answer you entered is wrong.';
$edit = [
'wrong_captcha_response_message' => $customized_menssage,
];
$this->drupalGet("admin/config/people/captcha");
$this->submitForm($edit, $this->t('Save configuration'));
// Set a CAPTCHA on login form.
/* @var \Drupal\captcha\Entity\CaptchaPoint $captcha_point */
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point->setCaptchaType('captcha/Math');
$captcha_point->enable()->save();
// Check if the menssage is default.
$this->drupalLogout();
$this->drupalGet('user');
// Try to log in, which should fail.
$edit = [
'name' => $this->adminUser->getDisplayName(),
'pass' => $this->adminUser->pass_raw,
'captcha_response' => '?',
];
$this->submitForm($edit, $this->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this->assertSession()->pageTextContains($customized_menssage, 'CAPTCHA should block user login form', 'CAPTCHA');
}
/**
* Assert function for testing if comment posting works as it should.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment