Projet

Général

Profil

Paste
Télécharger (8,31 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / recaptcha / recaptcha.test @ e326068a

1 be58a50c Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Tests for reCAPTCHA module.
6
 */
7 e326068a Assos Assos
8 be58a50c Assos Assos
class ReCaptchaBasicTest extends DrupalWebTestCase {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public static function getInfo() {
14
    return array(
15
      'name' => t('reCAPTCHA basic tests'),
16
      'description' => t('Test basic functionality of reCAPTCHA module.'),
17
      'group' => t('reCAPTCHA'),
18
      'dependencies' => array('captcha'),
19
    );
20
  }
21
22
  /**
23
   * {@inheritdoc}
24
   */
25 e326068a Assos Assos
  public function setUp() {
26 be58a50c Assos Assos
    parent::setUp('captcha', 'recaptcha');
27
    module_load_include('inc', 'captcha');
28
29
    // Create a normal user.
30
    $permissions = array(
31
      'access content',
32
    );
33
    $this->normal_user = $this->drupalCreateUser($permissions);
34
35
    // Create an admin user.
36
    $permissions += array(
37
      'administer CAPTCHA settings',
38
      'skip CAPTCHA',
39
      'administer permissions',
40
      'administer content types',
41
      'administer recaptcha',
42
    );
43
    $this->admin_user = $this->drupalCreateUser($permissions);
44
  }
45
46
  /**
47
   * Test access to the administration page.
48
   */
49 e326068a Assos Assos
  public function testReCaptchaAdminAccess() {
50 be58a50c Assos Assos
    $this->drupalLogin($this->admin_user);
51
    $this->drupalGet('admin/config/people/captcha/recaptcha');
52
    $this->assertNoText(t('Access denied'), 'Admin users should be able to access the reCAPTCHA admin page', 'reCAPTCHA');
53
    $this->drupalLogout();
54
  }
55
56
  /**
57
   * Test the reCAPTCHA settings form.
58
   */
59 e326068a Assos Assos
  public function testReCaptchaAdminSettingsForm() {
60 be58a50c Assos Assos
    $this->drupalLogin($this->admin_user);
61
62
    $site_key = $this->randomName(40);
63
    $secret_key = $this->randomName(40);
64
65
    // Check form validation.
66
    $edit['recaptcha_site_key'] = '';
67
    $edit['recaptcha_secret_key'] = '';
68
    $edit['recaptcha_tabindex'] = $this->randomName(2);
69
    $this->drupalPost('admin/config/people/captcha/recaptcha', $edit, t('Save configuration'));
70
71
    $this->assertRaw(t('Site key field is required.'), '[testReCaptchaConfiguration]: Empty site key detected.');
72
    $this->assertRaw(t('Secret key field is required.'), '[testReCaptchaConfiguration]: Empty secret key detected.');
73
    $this->assertRaw(t('The tabindex must be an integer.'), '[testReCaptchaConfiguration]: Invalid value for tab index detected.');
74
75
    // Save form with valid values.
76
    $edit['recaptcha_site_key'] = $site_key;
77
    $edit['recaptcha_secret_key'] = $secret_key;
78
    $edit['recaptcha_tabindex'] = 0;
79
    $this->drupalPost('admin/config/people/captcha/recaptcha', $edit, t('Save configuration'));
80
    $this->assertRaw(t('The configuration options have been saved.'), '[testReCaptchaConfiguration]: The configuration options have been saved.');
81
82
    $this->assertNoRaw(t('Site key field is required.'), '[testReCaptchaConfiguration]: Site key was not empty.');
83
    $this->assertNoRaw(t('Secret key field is required.'), '[testReCaptchaConfiguration]: Secret key was not empty.');
84
    $this->assertNoRaw(t('The tabindex must be an integer.'), '[testReCaptchaConfiguration]: Tab index had a valid input.');
85
86
    $this->drupalLogout();
87
  }
88
89
  /**
90
   * Testing the protection of the user login form.
91
   */
92 e326068a Assos Assos
  public function testReCaptchaOnLoginForm() {
93 be58a50c Assos Assos
    global $language;
94
95
    $site_key = $this->randomName(40);;
96
    $secret_key = $this->randomName(40);;
97
    $grecaptcha = '<div class="g-recaptcha" data-sitekey="' . $site_key . '" data-theme="light" data-type="image"></div>';
98
99
    // Test if login works.
100
    $this->drupalLogin($this->normal_user);
101
    $this->drupalLogout();
102
103
    $this->drupalGet('user');
104
    $this->assertNoRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is not shown on form.');
105
106
    // Enable 'captcha/Math' CAPTCHA on login form.
107
    captcha_set_form_id_setting('user_login', 'captcha/Math');
108
109
    $this->drupalGet('user');
110
    $this->assertNoRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is not shown on form.');
111
112
    // Enable 'recaptcha/reCAPTCHA' on login form.
113
    captcha_set_form_id_setting('user_login', 'recaptcha/reCAPTCHA');
114
    $result = captcha_get_form_id_setting('user_login');
115
    $this->assertNotNull($result, 'A configuration has been found for CAPTCHA point: user_login', 'reCAPTCHA');
116
    $this->assertEqual($result->module, 'recaptcha', 'reCAPTCHA module configured for CAPTCHA point: user_login', 'reCAPTCHA');
117
    $this->assertEqual($result->captcha_type, 'reCAPTCHA', 'reCAPTCHA type has been configured for CAPTCHA point: user_login', 'reCAPTCHA');
118
119
    // Check if a Math CAPTCHA is still shown on the login form. The site key
120
    // and security key have not yet configured for reCAPTCHA. The module need
121
    // to fall back to math captcha.
122
    $this->drupalGet('user');
123
    $this->assertRaw(t('Math question'), '[testReCaptchaOnLoginForm]: Math CAPTCHA is shown on form.');
124
125
    // Configure site key and security key to show reCAPTCHA and no fall back.
126
    variable_set('recaptcha_site_key', $site_key);
127
    variable_set('recaptcha_secret_key', $secret_key);
128
129
    // Check if there is a reCAPTCHA on the login form.
130
    $this->drupalGet('user');
131
    $this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.');
132
    $this->assertRaw('<script src="https://www.google.com/recaptcha/api.js?hl=' . $language->language . '" async="async" defer="defer"></script>', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.');
133
    $this->assertNoRaw($grecaptcha . '<noscript>', '[testReCaptchaOnLoginForm]: NoScript code is not enabled for the reCAPTCHA.');
134
135
    // Test if the fall back url is properly build and noscript code added.
136
    variable_set('recaptcha_noscript', 1);
137
    $this->drupalGet('user');
138
    $this->assertRaw($grecaptcha . '<noscript>', '[testReCaptchaOnLoginForm]: NoScript for reCAPTCHA is shown on form.');
139
    $this->assertRaw('https://www.google.com/recaptcha/api/fallback?k=' . $site_key . '&amp;hl=' . $language->language, '[testReCaptchaOnLoginForm]: Fallback URL with IFRAME has been found.');
140
141 e326068a Assos Assos
    // Check if there is a reCAPTCHA with global url on the login form.
142
    variable_set('recaptcha_use_globally', 1);
143
    $this->drupalGet('user/login');
144
    $this->assertRaw('<script src="https://www.recaptcha.net/recaptcha/api.js?hl=' . $language->language . '" async="async" defer="defer"></script>', '[testReCaptchaOnLoginForm]: Global reCAPTCHA is shown on form.');
145
    $this->assertRaw('https://www.recaptcha.net/recaptcha/api/fallback?k=' . $site_key . '&amp;hl=' . $language->language, '[testReCaptchaOnLoginForm]: Global fallback URL with IFRAME has been found.');
146
147 be58a50c Assos Assos
    // Check that data-size attribute does not exists.
148
    variable_set('recaptcha_size', '');
149
    $this->drupalGet('user');
150
    $element = $this->xpath('//div[@class=:class and @data-size=:size]', array(':class' => 'g-recaptcha', ':size' => 'small'));
151
    $this->assertFalse(!empty($element), 'Tag contains no data-size attribute.');
152
153
    // Check that data-size attribute exists.
154
    variable_set('recaptcha_size', 'small');
155
    $this->drupalGet('user');
156
    $element = $this->xpath('//div[@class=:class and @data-size=:size]', array(':class' => 'g-recaptcha', ':size' => 'small'));
157
    $this->assertTrue(!empty($element), 'Tag contains data-size attribute and value.');
158
159
    // Check that data-tabindex attribute does not exists.
160
    variable_set('recaptcha_tabindex', 0);
161
    $this->drupalGet('user');
162
    $element = $this->xpath('//div[@class=:class and @data-tabindex=:index]', array(':class' => 'g-recaptcha', ':index' => 0));
163
    $this->assertFalse(!empty($element), 'Tag contains no data-tabindex attribute.');
164
165
    // Check that data-tabindex attribute exists.
166
    variable_set('recaptcha_tabindex', 5);
167
    $this->drupalGet('user');
168
    $element = $this->xpath('//div[@class=:class and @data-tabindex=:index]', array(':class' => 'g-recaptcha', ':index' => 5));
169
    $this->assertTrue(!empty($element), 'Tag contains data-tabindex attribute and value.');
170
171
    // Try to log in, which should fail.
172
    $edit['name'] = $this->normal_user->name;
173
    $edit['pass'] = $this->normal_user->pass_raw;
174
    $edit['captcha_response'] = '?';
175
176
    $this->drupalPost('user', $edit, t('Log in'));
177
    // Check for error message.
178
    $this->assertText(t('The answer you entered for the CAPTCHA was not correct.'), 'CAPTCHA should block user login form', 'reCAPTCHA');
179
180 e326068a Assos Assos
    // And make sure that user is not logged in: check for name and password
181
    // fields on ?q=user.
182 be58a50c Assos Assos
    $this->drupalGet('user');
183
    $this->assertField('name', t('Username field found.'), 'reCAPTCHA');
184
    $this->assertField('pass', t('Password field found.'), 'reCAPTCHA');
185
  }
186
187
}