diff --git a/recaptcha.module b/recaptcha.module index 1cfc1a18c5960baf0996771719bf341ed6607b50..0efe633a9039f88acb7f881becdf13234fd6318e 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' => '
', - '#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' => '', + '#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 7980d499d1535ad0d20cf9cb634a932c7e5e33e5..a238777ec35398ba7912fe023f2157cc713104eb 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" . '