Projet

Général

Profil

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

root / drupal7 / sites / all / modules / honeypot / honeypot.test @ 27370441

1
<?php
2

    
3
/**
4
 * @file
5
 * Testing for Honeypot module.
6
 */
7

    
8
/**
9
 * Test the functionality of the Honeypot module for an admin user.
10
 */
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
    parent::setUp(array('honeypot', 'comment'));
27

    
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
    $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
  }
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
}
173

    
174
/**
175
 * Test Honeypot's CSS generation routines.
176
 */
177
class HoneypotCssTestCase extends DrupalWebTestCase {
178
  public static function getInfo() {
179
    return array(
180
      'name' => 'Honeypot CSS tests',
181
      'description' => 'Ensure that Honeypot rebuilds its CSS file correctly.',
182
      'group' => 'Form API',
183
    );
184
  }
185

    
186
  public function setUp() {
187
    // Enable modules required for this test.
188
    parent::setUp(array('honeypot'));
189

    
190
    // Set up required Honeypot variables.
191
    variable_set('honeypot_element_name', 'url');
192
  }
193

    
194
  /**
195
   * Test CSS file regeneration.
196
   */
197
  public function testHoneypotCssRegeneration() {
198
    $honeypot_css = honeypot_get_css_file_path();
199

    
200
    // Delete the Honeypot CSS file (if it exists).
201
    file_unmanaged_delete($honeypot_css);
202

    
203
    // Make sure the Honeypot CSS file doesn't exist.
204
    $this->assertFalse(file_exists($honeypot_css));
205

    
206
    // Create the CSS file.
207
    honeypot_create_css(variable_get('honeypot_element_name', 'url'));
208

    
209
    // Make sure the Honeypot CSS file exists.
210
    $this->assertTrue(file_exists($honeypot_css));
211
  }
212

    
213
  /**
214
   * Test cron-based CSS file regeneration
215
   */
216
  public function testHoneypotCssRegenerationOnCron() {
217
    $honeypot_css = honeypot_get_css_file_path();
218

    
219
    // Delete the Honeypot CSS file (if it exists).
220
    file_unmanaged_delete($honeypot_css);
221

    
222
    // Make sure the Honeypot CSS file doesn't exist.
223
    $this->assertFalse(file_exists($honeypot_css));
224

    
225
    // Run cron.
226
    honeypot_cron();
227

    
228
    // Make sure the Honeypot CSS file exists.
229
    $this->assertTrue(file_exists($honeypot_css));
230
  }
231
}
232

    
233
/**
234
 * Test the functionality of the Honeypot module's integration with Trigger.
235
 */
236
class HoneypotTriggerTestCase extends DrupalWebTestCase {
237
  public static function getInfo() {
238
    return array(
239
      'name' => 'Honeypot Trigger integration',
240
      'description' => 'Ensure that Honeypot triggers events correctly.',
241
      'group' => 'Form API',
242
    );
243
  }
244

    
245
  public function setUp() {
246
    // Enable modules required for this test.
247
    parent::setUp(array('honeypot', 'trigger'));
248

    
249
    // Set up required Honeypot variables.
250
    variable_set('honeypot_element_name', 'url');
251
    // Disable time_limit protection.
252
    variable_set('honeypot_time_limit', 0);
253
    // Test protecting all forms.
254
    variable_set('honeypot_protect_all_forms', TRUE);
255
    variable_set('honeypot_log', FALSE);
256

    
257
    // Set up other required variables.
258
    variable_set('user_email_verification', TRUE);
259
    variable_set('user_register', USER_REGISTER_VISITORS);
260

    
261
    // Assign new action to Honeypot form rejection Trigger.
262
    db_insert('trigger_assignments')
263
      ->fields(array(
264
        'hook' => 'honeypot_reject',
265
        'aid' => 'system_block_ip_action',
266
        'weight' => 1,
267
      ))
268
      ->execute();
269
  }
270

    
271
  /**
272
   * Test trigger integration.
273
   */
274
  public function testHoneypotTriggerIntegration() {
275
    // Set up form and submit it.
276
    $edit['name'] = $this->randomName();
277
    $edit['mail'] = $edit['name'] . '@example.com';
278
    $edit['url'] = 'http://www.example.com/';
279
    $this->drupalPost('user/register', $edit, t('Create new account'));
280

    
281
    // Make sure Honeypot is working.
282
    $this->assertText(t('There was a problem with your form submission.'), 'Honeypot working correctly.');
283

    
284
    // Visit the home page and make sure the user is banned.
285
    $this->drupalGet('node');
286
    $this->assertText(t('has been banned'), 'User banned successfully.');
287
  }
288
}