summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhass2015-05-09 12:01:31 (GMT)
committerhass2015-05-09 12:01:31 (GMT)
commit9ea08433902d96afbe9f422095718be6deef888c (patch)
tree200112c60bbee218de73712f6f219dcfceeca60c
parent3c7f0bca1d2e767fb81145ba94f43951c09484c5 (diff)
Issue #2486041 by hass: addCacheableDependency to Math captcha
-rw-r--r--recaptcha.module101
-rw-r--r--src/Tests/ReCaptchaBasicTest.php9
2 files changed, 51 insertions, 59 deletions
diff --git a/recaptcha.module b/recaptcha.module
index 1cfc1a1..0efe633 100644
--- a/recaptcha.module
+++ b/recaptcha.module
@@ -60,64 +60,65 @@ function recaptcha_captcha($op, $captcha_type = '') {
$recaptcha_site_key = $config->get('site_key');
$recaptcha_secret_key = $config->get('secret_key');
- // Test if reCAPTCHA can be used, falling back to Math if it is not
- // configured.
- if (empty($recaptcha_site_key) || empty($recaptcha_secret_key)) {
- $args = func_get_args();
- return captcha_captcha('generate', 'Math', $args);
- }
- // Create the form. Captcha requires TRUE to be returned in solution.
- $captcha['solution'] = TRUE;
- $captcha['captcha_validate'] = 'recaptcha_captcha_validation';
- $captcha['form']['captcha_response'] = [
- '#type' => 'hidden',
- '#value' => 'Google no captcha',
- ];
-
- $noscript = '';
- if ($config->get('widget.noscript')) {
- $recaptcha_widget_noscript = [
- '#theme' => 'recaptcha_widget_noscript',
- '#widget' => [
- 'sitekey' => $recaptcha_site_key,
- 'language' => \Drupal::service('language_manager')->getCurrentLanguage()->getId(),
- ],
+ if (!empty($recaptcha_site_key) || !empty($recaptcha_secret_key)) {
+ // Build the reCAPTCHA captcha form if site_key and secret_key are
+ // configured. Captcha requires TRUE to be returned in solution.
+ $captcha['solution'] = TRUE;
+ $captcha['captcha_validate'] = 'recaptcha_captcha_validation';
+ $captcha['form']['captcha_response'] = [
+ '#type' => 'hidden',
+ '#value' => 'Google no captcha',
];
- $noscript = $renderer->render($recaptcha_widget_noscript);
- }
- $attributes = [
- 'class' => 'g-recaptcha',
- 'data-sitekey' => $recaptcha_site_key,
- 'data-theme' => $config->get('widget.theme'),
- 'data-type' => $config->get('widget.type'),
- 'data-tabindex' => $config->get('widget.tabindex'),
- ];
- // Filter out empty tabindex.
- $attributes = array_filter($attributes);
-
- $captcha['form']['recaptcha_widget'] = [
- '#markup' => '<div' . new Attribute($attributes) . '></div>',
- '#suffix' => $noscript,
- '#attached' => [
- 'html_head' => [
- [
+ $noscript = '';
+ if ($config->get('widget.noscript')) {
+ $recaptcha_widget_noscript = [
+ '#theme' => 'recaptcha_widget_noscript',
+ '#widget' => [
+ 'sitekey' => $recaptcha_site_key,
+ 'language' => \Drupal::service('language_manager')->getCurrentLanguage()->getId(),
+ ],
+ ];
+ $noscript = $renderer->render($recaptcha_widget_noscript);
+ }
+
+ $attributes = [
+ 'class' => 'g-recaptcha',
+ 'data-sitekey' => $recaptcha_site_key,
+ 'data-theme' => $config->get('widget.theme'),
+ 'data-type' => $config->get('widget.type'),
+ 'data-tabindex' => $config->get('widget.tabindex'),
+ ];
+ // Filter out empty tabindex.
+ $attributes = array_filter($attributes);
+
+ $captcha['form']['recaptcha_widget'] = [
+ '#markup' => '<div' . new Attribute($attributes) . '></div>',
+ '#suffix' => $noscript,
+ '#attached' => [
+ 'html_head' => [
[
- '#tag' => 'script',
- '#attributes' => [
- 'src' => Url::fromUri('https://www.google.com/recaptcha/api.js', ['query' => ['hl' => \Drupal::service('language_manager')->getCurrentLanguage()->getId()], 'absolute' => TRUE])->toString(),
- 'async' => TRUE,
- 'defer' => TRUE,
+ [
+ '#tag' => 'script',
+ '#attributes' => [
+ 'src' => Url::fromUri('https://www.google.com/recaptcha/api.js', ['query' => ['hl' => \Drupal::service('language_manager')->getCurrentLanguage()->getId()], 'absolute' => TRUE])->toString(),
+ 'async' => TRUE,
+ 'defer' => TRUE,
+ ],
],
+ 'recaptcha_api'
],
- 'recaptcha_api'
],
],
- ],
- ];
- // If module configuration changes the cache need to be refreshed.
- $renderer->addCacheableDependency($captcha['form'], $config);
+ ];
+ }
+ else {
+ // Fallback to Math captcha as reCAPTCHA is not configured.
+ $captcha = captcha_captcha('generate', 'Math');
+ }
+ // If module configuration changes the form cache need to be refreshed.
+ $renderer->addCacheableDependency($captcha['form'], $config);
}
return $captcha;
}
diff --git a/src/Tests/ReCaptchaBasicTest.php b/src/Tests/ReCaptchaBasicTest.php
index 7980d49..a238777 100644
--- a/src/Tests/ReCaptchaBasicTest.php
+++ b/src/Tests/ReCaptchaBasicTest.php
@@ -123,9 +123,6 @@ class ReCaptchaBasicTest extends WebTestBase {
$this->assertEqual($result->getCaptchaType(), 'recaptcha/reCAPTCHA', 'reCAPTCHA type has been configured for CAPTCHA point: user_login_form', 'reCAPTCHA');
//$this->verbose($result->getCaptchaType());
- // @fixme: This need to be removed. The form has incomplete caching logic. See #2481875.
- drupal_flush_all_caches();
-
// Check if a Math CAPTCHA is still shown on the login form. The site key
// and security key have not yet configured for reCAPTCHA. The module need
// to fall back to math captcha.
@@ -136,9 +133,6 @@ class ReCaptchaBasicTest extends WebTestBase {
$this->config('recaptcha.settings')->set('site_key', $site_key)->save();
$this->config('recaptcha.settings')->set('secret_key', $secret_key)->save();
- // @fixme: This need to be removed. The form has incomplete caching logic. See #2481875.
- drupal_flush_all_caches();
-
// Check if there is a reCAPTCHA on the login form.
$this->drupalGet('user/login');
$this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.');
@@ -148,9 +142,6 @@ class ReCaptchaBasicTest extends WebTestBase {
// Test if the fall back url is properly build and noscript code added.
$this->config('recaptcha.settings')->set('widget.noscript', 1)->save();
- // @fixme: This need to be removed. The form has incomplete caching logic. See #2481875.
- drupal_flush_all_caches();
-
$this->drupalGet('user/login');
$this->assertRaw($grecaptcha . "\n" . '<noscript>', '[testReCaptchaOnLoginForm]: NoScript for reCAPTCHA is shown on form.');
$this->assertRaw('https://www.google.com/recaptcha/api/fallback?k=' . $site_key . '&amp;hl=' . \Drupal::service('language_manager')->getCurrentLanguage()->getId(), '[testReCaptchaOnLoginForm]: Fallback URL with IFRAME has been found.');