diff --git a/recaptcha.module b/recaptcha.module index e3dfb3e..a79658e 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -11,6 +11,9 @@ require_once dirname(__FILE__) . '/recaptcha-php/src/ReCaptcha/RequestParameters require_once dirname(__FILE__) . '/recaptcha-php/src/ReCaptcha/Response.php'; require_once dirname(__FILE__) . '/src/ReCaptcha/RequestMethod/Drupal7Post.php'; +use \ReCaptcha\ReCaptcha; +use \ReCaptcha\RequestMethod; + /** * Implements hook_help(). */ @@ -68,7 +71,7 @@ function recaptcha_permission() { /** * Implements hook_captcha(). */ -function recaptcha_captcha($op, $captcha_type = '', $captcha_sid = '') { +function recaptcha_captcha($op, $captcha_type = '', $captcha_sid = '') { global $language; switch ($op) { @@ -101,7 +104,7 @@ function recaptcha_captcha($op, $captcha_type = '', $captcha_sid = '') { } $attributes = array( - 'id'=>'g-recaptcha'.$captcha_sid, + 'id' => 'g-recaptcha' . $captcha_sid, 'class' => 'g-recaptcha', 'data-sitekey' => $recaptcha_site_key, 'data-theme' => variable_get('recaptcha_theme', 'light'), @@ -150,11 +153,13 @@ JS; '#tag' => 'script', '#value' => $onload_js, '#weight' => -1, - ); + ); drupal_add_html_head($data, 'recaptcha_onload'); - // @todo: #1664602: D7 does not yet support "async" in drupal_add_js(). - // drupal_add_js(url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), array('defer' => TRUE, 'async' => TRUE, 'type' => 'external')); + /* + @todo: #1664602: D7 does not yet support "async" in drupal_add_js(). + drupal_add_js(url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), array('defer' => TRUE, 'async' => TRUE, 'type' => 'external')); + */ $data = array( '#tag' => 'script', '#value' => '', @@ -162,13 +167,12 @@ JS; '#attributes' => array( 'src' => url('https://www.google.com/recaptcha/api.js', array( 'absolute' => TRUE, - 'query' => array( - 'hl' => $language->language, - 'onload' => 'recaptchaOnLoad', - 'render' => 'explicit', - ), - ) - ), + 'query' => array( + 'hl' => $language->language, + 'onload' => 'recaptchaOnLoad', + 'render' => 'explicit', + ), + )), 'async' => 'async', 'defer' => 'defer', ), @@ -233,5 +237,11 @@ function recaptcha_captcha_validation($solution, $response, $element, $form_stat function template_preprocess_recaptcha_widget_noscript(&$variables) { $variables['sitekey'] = check_plain($variables['widget']['sitekey']); $variables['language'] = check_plain($variables['widget']['language']); - $variables['url'] = check_url(url('https://www.google.com/recaptcha/api/fallback', array('query' => array('k' => $variables['widget']['sitekey'], 'hl' => $variables['widget']['language']), 'absolute' => TRUE))); + $variables['url'] = check_url(url('https://www.google.com/recaptcha/api/fallback', array( + 'query' => array( + 'k' => $variables['widget']['sitekey'], + 'hl' => $variables['widget']['language'] + ), + 'absolute' => TRUE + ))); } diff --git a/recaptcha.test b/recaptcha.test index 4afedf7..3e8c1fc 100644 --- a/recaptcha.test +++ b/recaptcha.test @@ -4,6 +4,10 @@ * @file * Tests for reCAPTCHA module. */ + +/** + * Helper class for ReCaptcha test cases. + */ class ReCaptchaBasicTest extends DrupalWebTestCase { /** @@ -21,7 +25,7 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { /** * {@inheritdoc} */ - function setUp() { + public function setUp() { parent::setUp('captcha', 'recaptcha'); module_load_include('inc', 'captcha'); @@ -45,7 +49,7 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { /** * Test access to the administration page. */ - function testReCaptchaAdminAccess() { + public function testReCaptchaAdminAccess() { $this->drupalLogin($this->admin_user); $this->drupalGet('admin/config/people/captcha/recaptcha'); $this->assertNoText(t('Access denied'), 'Admin users should be able to access the reCAPTCHA admin page', 'reCAPTCHA'); @@ -55,7 +59,7 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { /** * Test the reCAPTCHA settings form. */ - function testReCaptchaAdminSettingsForm() { + public function testReCaptchaAdminSettingsForm() { $this->drupalLogin($this->admin_user); $site_key = $this->randomName(40); @@ -88,7 +92,7 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { /** * Testing the protection of the user login form. */ - function testReCaptchaOnLoginForm() { + public function testReCaptchaOnLoginForm() { global $language; $site_key = $this->randomName(40);; @@ -127,13 +131,17 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { // Check if there is a reCAPTCHA on the login form. $this->drupalGet('user'); - $this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); - $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); + $captcha_sid = $this->getCaptchaSid(); + $grecaptcha = '
'; + $this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.' . $grecaptcha); + $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA API script is present.'); $this->assertNoRaw($grecaptcha . '