1 |
11987c58
|
Florent Torregrosa
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Testing for Honeypot module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
ca92d645
|
Assos Assos
|
* Test the functionality of the Honeypot module for forms.
|
10 |
11987c58
|
Florent Torregrosa
|
*/
|
11 |
|
|
class HoneypotFormTestCase extends DrupalWebTestCase {
|
12 |
|
|
protected $adminUser;
|
13 |
|
|
protected $webUser;
|
14 |
|
|
protected $node;
|
15 |
|
|
|
16 |
|
|
public static function getInfo() {
|
17 |
|
|
return array(
|
18 |
|
|
'name' => 'Honeypot form protections',
|
19 |
|
|
'description' => 'Ensure that Honeypot protects site forms properly.',
|
20 |
|
|
'group' => 'Form API',
|
21 |
|
|
);
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
public function setUp() {
|
25 |
|
|
// Enable modules required for this test.
|
26 |
ca92d645
|
Assos Assos
|
parent::setUp(array('honeypot', 'comment', 'honeypot_test'));
|
27 |
11987c58
|
Florent Torregrosa
|
|
28 |
|
|
// Set up required Honeypot variables.
|
29 |
|
|
variable_set('honeypot_element_name', 'url');
|
30 |
|
|
// Disable time_limit protection.
|
31 |
|
|
variable_set('honeypot_time_limit', 0);
|
32 |
|
|
// Test protecting all forms.
|
33 |
|
|
variable_set('honeypot_protect_all_forms', TRUE);
|
34 |
|
|
variable_set('honeypot_log', FALSE);
|
35 |
|
|
|
36 |
|
|
// Set up other required variables.
|
37 |
|
|
variable_set('user_email_verification', TRUE);
|
38 |
|
|
variable_set('user_register', USER_REGISTER_VISITORS);
|
39 |
|
|
|
40 |
|
|
// Set up admin user.
|
41 |
|
|
$this->adminUser = $this->drupalCreateUser(array(
|
42 |
|
|
'administer honeypot',
|
43 |
|
|
'bypass honeypot protection',
|
44 |
|
|
'administer content types',
|
45 |
|
|
'administer users',
|
46 |
|
|
'access comments',
|
47 |
|
|
'post comments',
|
48 |
|
|
'skip comment approval',
|
49 |
|
|
'administer comments',
|
50 |
|
|
));
|
51 |
|
|
|
52 |
|
|
// Set up web user.
|
53 |
|
|
$this->webUser = $this->drupalCreateUser(array(
|
54 |
|
|
'access comments',
|
55 |
|
|
'post comments',
|
56 |
|
|
'create article content',
|
57 |
|
|
));
|
58 |
|
|
|
59 |
|
|
// Set up example node.
|
60 |
|
|
$this->node = $this->drupalCreateNode(array(
|
61 |
|
|
'type' => 'article',
|
62 |
|
|
'promote' => 1,
|
63 |
|
|
'uid' => $this->webUser->uid,
|
64 |
|
|
));
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* Test user registration (anonymous users).
|
69 |
|
|
*/
|
70 |
|
|
public function testProtectRegisterUserNormal() {
|
71 |
|
|
// Set up form and submit it.
|
72 |
|
|
$edit['name'] = $this->randomName();
|
73 |
|
|
$edit['mail'] = $edit['name'] . '@example.com';
|
74 |
|
|
$this->drupalPost('user/register', $edit, t('Create new account'));
|
75 |
|
|
|
76 |
|
|
// Form should have been submitted successfully.
|
77 |
|
|
$this->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), 'User registered successfully.');
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
public function testProtectUserRegisterHoneypotFilled() {
|
81 |
|
|
// Set up form and submit it.
|
82 |
|
|
$edit['name'] = $this->randomName();
|
83 |
|
|
$edit['mail'] = $edit['name'] . '@example.com';
|
84 |
|
|
$edit['url'] = 'http://www.example.com/';
|
85 |
|
|
$this->drupalPost('user/register', $edit, t('Create new account'));
|
86 |
|
|
|
87 |
|
|
// Form should have error message.
|
88 |
|
|
$this->assertText(t('There was a problem with your form submission. Please refresh the page and try again.'), 'Registration form protected by honeypot.');
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
public function testProtectRegisterUserTooFast() {
|
92 |
|
|
// Enable time limit for honeypot.
|
93 |
|
|
variable_set('honeypot_time_limit', 5);
|
94 |
|
|
|
95 |
|
|
// Set up form and submit it.
|
96 |
|
|
$edit['name'] = $this->randomName();
|
97 |
|
|
$edit['mail'] = $edit['name'] . '@example.com';
|
98 |
|
|
$this->drupalPost('user/register', $edit, t('Create new account'));
|
99 |
|
|
|
100 |
|
|
// Form should have error message.
|
101 |
55670b15
|
Assos Assos
|
$this->assertText(t('There was a problem with your form submission. Please wait 6 seconds and try again.'), 'Registration form protected by time limit.');
|
102 |
11987c58
|
Florent Torregrosa
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* Test comment form protection.
|
106 |
|
|
*/
|
107 |
|
|
public function testProtectCommentFormNormal() {
|
108 |
|
|
$comment = 'Test comment.';
|
109 |
|
|
|
110 |
|
|
// Disable time limit for honeypot.
|
111 |
|
|
variable_set('honeypot_time_limit', 0);
|
112 |
|
|
|
113 |
|
|
// Log in the web user.
|
114 |
|
|
$this->drupalLogin($this->webUser);
|
115 |
|
|
|
116 |
|
|
// Set up form and submit it.
|
117 |
|
|
$edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment;
|
118 |
|
|
$this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
|
119 |
|
|
$this->assertText(t('Your comment has been posted.'), 'Comment posted successfully.');
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
public function testProtectCommentFormHoneypotFilled() {
|
123 |
|
|
$comment = 'Test comment.';
|
124 |
|
|
|
125 |
|
|
// Log in the web user.
|
126 |
|
|
$this->drupalLogin($this->webUser);
|
127 |
|
|
|
128 |
|
|
// Set up form and submit it.
|
129 |
|
|
$edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment;
|
130 |
|
|
$edit['url'] = 'http://www.example.com/';
|
131 |
|
|
$this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
|
132 |
|
|
$this->assertText(t('There was a problem with your form submission. Please refresh the page and try again.'), 'Comment posted successfully.');
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
public function testProtectCommentFormHoneypotBypass() {
|
136 |
|
|
// Log in the admin user.
|
137 |
|
|
$this->drupalLogin($this->adminUser);
|
138 |
|
|
|
139 |
|
|
// Get the comment reply form and ensure there's no 'url' field.
|
140 |
|
|
$this->drupalGet('comment/reply/' . $this->node->nid);
|
141 |
|
|
$this->assertNoText('id="edit-url" name="url"', 'Honeypot home page field not shown.');
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Test node form protection.
|
146 |
|
|
*/
|
147 |
|
|
public function testProtectNodeFormTooFast() {
|
148 |
|
|
// Log in the admin user.
|
149 |
|
|
$this->drupalLogin($this->webUser);
|
150 |
|
|
|
151 |
|
|
// Reset the time limit to 5 seconds.
|
152 |
|
|
variable_set('honeypot_time_limit', 5);
|
153 |
|
|
|
154 |
|
|
// Set up the form and submit it.
|
155 |
|
|
$edit["title"] = 'Test Page';
|
156 |
|
|
$this->drupalPost('node/add/article', $edit, t('Save'));
|
157 |
|
|
$this->assertText(t('There was a problem with your form submission.'), 'Honeypot node form timestamp protection works.');
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
/**
|
161 |
|
|
* Test node form protection.
|
162 |
|
|
*/
|
163 |
|
|
public function testProtectNodeFormPreviewPassthru() {
|
164 |
|
|
// Log in the admin user.
|
165 |
|
|
$this->drupalLogin($this->webUser);
|
166 |
|
|
|
167 |
|
|
// Post a node form using the 'Preview' button and make sure it's allowed.
|
168 |
|
|
$edit["title"] = 'Test Page';
|
169 |
|
|
$this->drupalPost('node/add/article', $edit, t('Preview'));
|
170 |
|
|
$this->assertNoText(t('There was a problem with your form submission.'), 'Honeypot not blocking node form previews.');
|
171 |
|
|
}
|
172 |
ca92d645
|
Assos Assos
|
|
173 |
|
|
/**
|
174 |
|
|
* Test programmatic submission.
|
175 |
|
|
*/
|
176 |
|
|
public function testProgrammaticSubmission() {
|
177 |
|
|
// Enable time limit protection.
|
178 |
|
|
variable_set('honeypot_time_limit', 5);
|
179 |
|
|
|
180 |
|
|
// Create a user for which we are going to trigger the password reset.
|
181 |
|
|
$edit = array();
|
182 |
|
|
$edit['name'] = 'robo-user';
|
183 |
|
|
$edit['mail'] = $edit['name'] . '@example.com';
|
184 |
|
|
$edit['status'] = 1;
|
185 |
|
|
user_save(drupal_anonymous_user(), $edit);
|
186 |
|
|
|
187 |
|
|
// Trigger the password reset through a programmatic submission.
|
188 |
|
|
$this->drupalGet('honeypot_test/submit_form');
|
189 |
|
|
|
190 |
|
|
// Verify that the submission did not return any validation errors.
|
191 |
|
|
$form_errors = drupal_json_decode($this->content);
|
192 |
|
|
$this->assertNoRaw('There was a problem with your form submission. Please wait 6 seconds and try again.');
|
193 |
|
|
$this->assertFalse($form_errors, 'The were no validation errors when submitting the form.');
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
/**
|
198 |
|
|
* Test the functionality of the Honeypot module for an admin user.
|
199 |
|
|
*/
|
200 |
|
|
class HoneypotAdminFormTestCase extends DrupalWebTestCase {
|
201 |
|
|
protected $adminUser;
|
202 |
|
|
|
203 |
|
|
public static function getInfo() {
|
204 |
|
|
return array(
|
205 |
|
|
'name' => 'Honeypot admin form',
|
206 |
|
|
'description' => 'Ensure the Honeypot admin form functions properly.',
|
207 |
|
|
'group' => 'Form API',
|
208 |
|
|
);
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
public function setUp() {
|
212 |
|
|
// Enable modules required for this test.
|
213 |
|
|
parent::setUp(array('honeypot'));
|
214 |
|
|
|
215 |
|
|
// Set up admin user.
|
216 |
|
|
$this->adminUser = $this->drupalCreateUser(array(
|
217 |
|
|
'administer honeypot',
|
218 |
|
|
'bypass honeypot protection',
|
219 |
|
|
));
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
/**
|
223 |
|
|
* Test a valid element name.
|
224 |
|
|
*/
|
225 |
|
|
public function testElementNameUpdateSuccess() {
|
226 |
|
|
// Log in the web user.
|
227 |
|
|
$this->drupalLogin($this->adminUser);
|
228 |
|
|
|
229 |
|
|
// Set up form and submit it.
|
230 |
|
|
$edit['honeypot_element_name'] = "test";
|
231 |
|
|
$this->drupalPost('admin/config/content/honeypot', $edit, t('Save configuration'));
|
232 |
|
|
|
233 |
|
|
// Form should have been submitted successfully.
|
234 |
|
|
$this->assertText(t('The configuration options have been saved.'), 'Honeypot element name assertion works for valid names.');
|
235 |
|
|
|
236 |
|
|
// Set up form and submit it.
|
237 |
|
|
$edit['honeypot_element_name'] = "test-1";
|
238 |
|
|
$this->drupalPost('admin/config/content/honeypot', $edit, t('Save configuration'));
|
239 |
|
|
|
240 |
|
|
// Form should have been submitted successfully.
|
241 |
|
|
$this->assertText(t('The configuration options have been saved.'), 'Honeypot element name assertion works for valid names with dashes and numbers.');
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
/**
|
245 |
|
|
* Test an invalid element name (invalid first character).
|
246 |
|
|
*/
|
247 |
|
|
public function testElementNameUpdateFirstCharacterFail() {
|
248 |
|
|
// Log in the admin user.
|
249 |
|
|
$this->drupalLogin($this->adminUser);
|
250 |
|
|
|
251 |
|
|
// Set up form and submit it.
|
252 |
|
|
$edit['honeypot_element_name'] = "1test";
|
253 |
|
|
$this->drupalPost('admin/config/content/honeypot', $edit, t('Save configuration'));
|
254 |
|
|
|
255 |
|
|
// Form submission should fail.
|
256 |
|
|
$this->assertText(t('The element name must start with a letter.'), 'Honeypot element name assertion works for invalid names.');
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
/**
|
260 |
|
|
* Test an invalid element name (invalid character in name).
|
261 |
|
|
*/
|
262 |
|
|
public function testElementNameUpdateInvalidCharacterFail() {
|
263 |
|
|
// Log in the admin user.
|
264 |
|
|
$this->drupalLogin($this->adminUser);
|
265 |
|
|
|
266 |
|
|
// Set up form and submit it.
|
267 |
|
|
$edit['honeypot_element_name'] = "special-character-&";
|
268 |
|
|
$this->drupalPost('admin/config/content/honeypot', $edit, t('Save configuration'));
|
269 |
|
|
|
270 |
|
|
// Form submission should fail.
|
271 |
|
|
$this->assertText(t('The element name cannot contain spaces or other special characters.'), 'Honeypot element name assertion works for invalid names with special characters.');
|
272 |
|
|
|
273 |
|
|
// Set up form and submit it.
|
274 |
|
|
$edit['honeypot_element_name'] = "space in name";
|
275 |
|
|
$this->drupalPost('admin/config/content/honeypot', $edit, t('Save configuration'));
|
276 |
|
|
|
277 |
|
|
// Form submission should fail.
|
278 |
|
|
$this->assertText(t('The element name cannot contain spaces or other special characters.'), 'Honeypot element name assertion works for invalid names with spaces.');
|
279 |
|
|
}
|
280 |
11987c58
|
Florent Torregrosa
|
}
|
281 |
55670b15
|
Assos Assos
|
|
282 |
0ef18d70
|
Assos Assos
|
/**
|
283 |
|
|
* Test Honeypot's CSS generation routines.
|
284 |
|
|
*/
|
285 |
|
|
class HoneypotCssTestCase extends DrupalWebTestCase {
|
286 |
|
|
public static function getInfo() {
|
287 |
|
|
return array(
|
288 |
|
|
'name' => 'Honeypot CSS tests',
|
289 |
|
|
'description' => 'Ensure that Honeypot rebuilds its CSS file correctly.',
|
290 |
|
|
'group' => 'Form API',
|
291 |
|
|
);
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
public function setUp() {
|
295 |
|
|
// Enable modules required for this test.
|
296 |
|
|
parent::setUp(array('honeypot'));
|
297 |
|
|
|
298 |
|
|
// Set up required Honeypot variables.
|
299 |
|
|
variable_set('honeypot_element_name', 'url');
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
/**
|
303 |
|
|
* Test CSS file regeneration.
|
304 |
|
|
*/
|
305 |
|
|
public function testHoneypotCssRegeneration() {
|
306 |
|
|
$honeypot_css = honeypot_get_css_file_path();
|
307 |
|
|
|
308 |
|
|
// Delete the Honeypot CSS file (if it exists).
|
309 |
|
|
file_unmanaged_delete($honeypot_css);
|
310 |
|
|
|
311 |
|
|
// Make sure the Honeypot CSS file doesn't exist.
|
312 |
|
|
$this->assertFalse(file_exists($honeypot_css));
|
313 |
|
|
|
314 |
|
|
// Create the CSS file.
|
315 |
|
|
honeypot_create_css(variable_get('honeypot_element_name', 'url'));
|
316 |
|
|
|
317 |
|
|
// Make sure the Honeypot CSS file exists.
|
318 |
|
|
$this->assertTrue(file_exists($honeypot_css));
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
/**
|
322 |
ca92d645
|
Assos Assos
|
* Test cron-based CSS file regeneration.
|
323 |
0ef18d70
|
Assos Assos
|
*/
|
324 |
|
|
public function testHoneypotCssRegenerationOnCron() {
|
325 |
|
|
$honeypot_css = honeypot_get_css_file_path();
|
326 |
|
|
|
327 |
|
|
// Delete the Honeypot CSS file (if it exists).
|
328 |
|
|
file_unmanaged_delete($honeypot_css);
|
329 |
|
|
|
330 |
|
|
// Make sure the Honeypot CSS file doesn't exist.
|
331 |
|
|
$this->assertFalse(file_exists($honeypot_css));
|
332 |
|
|
|
333 |
|
|
// Run cron.
|
334 |
|
|
honeypot_cron();
|
335 |
|
|
|
336 |
|
|
// Make sure the Honeypot CSS file exists.
|
337 |
|
|
$this->assertTrue(file_exists($honeypot_css));
|
338 |
|
|
}
|
339 |
ca92d645
|
Assos Assos
|
|
340 |
|
|
/**
|
341 |
|
|
* Test cron-based CSS file update.
|
342 |
|
|
*/
|
343 |
|
|
public function testHoneypotCssUpdateOnCron() {
|
344 |
|
|
$honeypot_css = honeypot_get_css_file_path();
|
345 |
|
|
$original_element_name = variable_get('honeypot_element_name', 'url');
|
346 |
|
|
|
347 |
|
|
// Update the honeypot element name.
|
348 |
|
|
variable_set('honeypot_element_name', 'test');
|
349 |
|
|
|
350 |
|
|
// Make sure the Honeypot CSS file still exists.
|
351 |
|
|
$this->assertTrue(file_exists($honeypot_css));
|
352 |
|
|
|
353 |
|
|
// Run cron.
|
354 |
|
|
honeypot_cron();
|
355 |
|
|
|
356 |
|
|
// Make sure the Honeypot CSS file was updated with the new element name.
|
357 |
|
|
$handle = fopen($honeypot_css, 'r');
|
358 |
|
|
$contents = fread($handle, filesize($honeypot_css));
|
359 |
|
|
fclose($handle);
|
360 |
|
|
$updated_element_name_in_css = (strpos($contents, 'test') === 1);
|
361 |
|
|
$this->assertTrue($updated_element_name_in_css);
|
362 |
|
|
|
363 |
|
|
// For debug.
|
364 |
|
|
$this->verbose($contents);
|
365 |
|
|
|
366 |
|
|
// Revert the honeypot element name back to the original.
|
367 |
|
|
variable_set('honeypot_element_name', $original_element_name);
|
368 |
|
|
}
|
369 |
0ef18d70
|
Assos Assos
|
}
|
370 |
|
|
|
371 |
55670b15
|
Assos Assos
|
/**
|
372 |
|
|
* Test the functionality of the Honeypot module's integration with Trigger.
|
373 |
|
|
*/
|
374 |
|
|
class HoneypotTriggerTestCase extends DrupalWebTestCase {
|
375 |
|
|
public static function getInfo() {
|
376 |
|
|
return array(
|
377 |
|
|
'name' => 'Honeypot Trigger integration',
|
378 |
|
|
'description' => 'Ensure that Honeypot triggers events correctly.',
|
379 |
|
|
'group' => 'Form API',
|
380 |
|
|
);
|
381 |
|
|
}
|
382 |
|
|
|
383 |
|
|
public function setUp() {
|
384 |
|
|
// Enable modules required for this test.
|
385 |
|
|
parent::setUp(array('honeypot', 'trigger'));
|
386 |
|
|
|
387 |
|
|
// Set up required Honeypot variables.
|
388 |
|
|
variable_set('honeypot_element_name', 'url');
|
389 |
|
|
// Disable time_limit protection.
|
390 |
|
|
variable_set('honeypot_time_limit', 0);
|
391 |
|
|
// Test protecting all forms.
|
392 |
|
|
variable_set('honeypot_protect_all_forms', TRUE);
|
393 |
|
|
variable_set('honeypot_log', FALSE);
|
394 |
|
|
|
395 |
|
|
// Set up other required variables.
|
396 |
|
|
variable_set('user_email_verification', TRUE);
|
397 |
|
|
variable_set('user_register', USER_REGISTER_VISITORS);
|
398 |
|
|
|
399 |
|
|
// Assign new action to Honeypot form rejection Trigger.
|
400 |
|
|
db_insert('trigger_assignments')
|
401 |
|
|
->fields(array(
|
402 |
|
|
'hook' => 'honeypot_reject',
|
403 |
|
|
'aid' => 'system_block_ip_action',
|
404 |
|
|
'weight' => 1,
|
405 |
|
|
))
|
406 |
|
|
->execute();
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
/**
|
410 |
|
|
* Test trigger integration.
|
411 |
|
|
*/
|
412 |
|
|
public function testHoneypotTriggerIntegration() {
|
413 |
|
|
// Set up form and submit it.
|
414 |
|
|
$edit['name'] = $this->randomName();
|
415 |
|
|
$edit['mail'] = $edit['name'] . '@example.com';
|
416 |
|
|
$edit['url'] = 'http://www.example.com/';
|
417 |
|
|
$this->drupalPost('user/register', $edit, t('Create new account'));
|
418 |
|
|
|
419 |
|
|
// Make sure Honeypot is working.
|
420 |
|
|
$this->assertText(t('There was a problem with your form submission.'), 'Honeypot working correctly.');
|
421 |
|
|
|
422 |
|
|
// Visit the home page and make sure the user is banned.
|
423 |
|
|
$this->drupalGet('node');
|
424 |
|
|
$this->assertText(t('has been banned'), 'User banned successfully.');
|
425 |
|
|
}
|
426 |
|
|
} |