Commit c0e8dc53 authored by bnjmnm's avatar bnjmnm Committed by elachlan

Issue #2912341 by bnjmnm, elachlan, Mschudders, hswong3i, saravanaprasanth:...

Issue #2912341 by bnjmnm, elachlan, Mschudders, hswong3i, saravanaprasanth: Form id max length - 64 characters
parent 83f4bbc2
......@@ -164,14 +164,27 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
// Visitor does not have permission to skip CAPTCHAs.
module_load_include('inc', 'captcha');
if (!$account->hasPermission('skip CAPTCHA')) {
$query = \Drupal::entityQuery('captcha_point');
$query->condition('label', $form_id);
$entity_ids = $query->execute();
// If empty, see if it is a form provided by default config.
if (empty($entity_ids)) {
$query = \Drupal::entityQuery('captcha_point');
$query->condition('formId', $form_id);
$entity_ids = $query->execute();
}
/* @var CaptchaPoint $captcha_point */
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load($form_id);
if (!empty($entity_ids) && is_array($entity_ids)) {
$captcha_point_id = array_pop($entity_ids);
/* @var CaptchaPoint $captcha_point */
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load($captcha_point_id);
}
// If there is no CaptchaPoint for the form_id, try to use the base_form_id.
if (!$captcha_point || !$captcha_point->status()) {
if (empty($captcha_point) || !$captcha_point->status()) {
$form_object = $form_state->getFormObject();
if ($form_object instanceof BaseFormIdInterface) {
$base_form_id = $form_object->getBaseFormId();
......@@ -183,7 +196,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
}
}
if (!$captcha_point && $config->get('enabled_default')) {
if (empty($captcha_point) && $config->get('enabled_default')) {
// Create fake captcha point without saving.
$captcha_point = new CaptchaPoint([
'formId' => $form_id,
......@@ -192,7 +205,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
$captcha_point->enable();
}
if ($captcha_point && $captcha_point->status()) {
if (!empty($captcha_point) && $captcha_point->status()) {
// Build CAPTCHA form element.
$captcha_element = [
'#type' => 'captcha',
......
name: captcha long form id test module
type: module
description: 'Test module for testing captchas added to forms with ids longer than 64 characters'
package: Testing
core: 8.x
hidden: true
captcha_long_form_id_test.this_formid_is_intentionally_longer_than_64_characters_to_test_captcha:
path: 'captcha/test_form/long_id'
defaults:
_form: '\Drupal\captcha_long_form_id_test\Form\LongIdForm'
_title: 'LongIdForm'
requirements:
# for testing only, can be open
_access: 'TRUE'
<?php
namespace Drupal\captcha_long_form_id_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class LongIdForm.
*/
class LongIdForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'this_formid_is_intentionally_longer_than_64_characters_to_test_captcha';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['text_field'] = [
'#type' => 'textfield',
'#title' => $this->t('Text Field'),
'#maxlength' => 64,
'#size' => 64,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
}
}
......@@ -19,7 +19,7 @@ class CaptchaTestCase extends CaptchaBaseWebTestCase {
*
* @var array
*/
public static $modules = ['block'];
public static $modules = ['block', 'captcha_long_form_id_test'];
/**
* Testing the protection of the user log in form.
......@@ -271,4 +271,35 @@ class CaptchaTestCase extends CaptchaBaseWebTestCase {
$this->assertText(t('The changes have been saved.'));
}
/**
* Test that forms with IDs exceeding 64 characters can be assigned captchas.
*/
public function testLongFormId() {
// We add the form manually so we can mimic the character
// truncation of the label field as formId.
$this->drupalLogin($this->adminUser);
$this->drupalGet(self::CAPTCHA_ADMIN_PATH);
$label = 'this_formid_is_intentionally_longer_than_64_characters_to_test_captcha';
// Truncated to 64 chars so it can be a machine name.
$formId = substr($label, 0, 64);
$form_values = [
'label' => $label,
'formId' => $formId,
'captchaType' => 'captcha/Math',
];
// Create intentionally long id Captcha Point.
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add', $form_values, t('Save'));
$this->assertRaw(t('Captcha Point for %label form was created.', ['%label' => $formId]));
// We need to log out to test the captcha.
$this->drupalLogout();
// Navigate to the form with a >64 char id and confirm there is Captcha.
$this->drupalGet('captcha/test_form/long_id');
$this->assertCaptchaPresence(TRUE);
}
}
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