Projet

Général

Profil

Paste
Télécharger (7,41 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / honeypot / honeypot.test @ 55670b15

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 the functionality of the Honeypot module's integration with Trigger.
176
 */
177
class HoneypotTriggerTestCase extends DrupalWebTestCase {
178
  public static function getInfo() {
179
    return array(
180
      'name' => 'Honeypot Trigger integration',
181
      'description' => 'Ensure that Honeypot triggers events correctly.',
182
      'group' => 'Form API',
183
    );
184
  }
185

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

    
190
    // Set up required Honeypot variables.
191
    variable_set('honeypot_element_name', 'url');
192
    // Disable time_limit protection.
193
    variable_set('honeypot_time_limit', 0);
194
    // Test protecting all forms.
195
    variable_set('honeypot_protect_all_forms', TRUE);
196
    variable_set('honeypot_log', FALSE);
197

    
198
    // Set up other required variables.
199
    variable_set('user_email_verification', TRUE);
200
    variable_set('user_register', USER_REGISTER_VISITORS);
201

    
202
    // Assign new action to Honeypot form rejection Trigger.
203
    db_insert('trigger_assignments')
204
      ->fields(array(
205
        'hook' => 'honeypot_reject',
206
        'aid' => 'system_block_ip_action',
207
        'weight' => 1,
208
      ))
209
      ->execute();
210
  }
211

    
212
  /**
213
   * Test trigger integration.
214
   */
215
  public function testHoneypotTriggerIntegration() {
216
    // Set up form and submit it.
217
    $edit['name'] = $this->randomName();
218
    $edit['mail'] = $edit['name'] . '@example.com';
219
    $edit['url'] = 'http://www.example.com/';
220
    $this->drupalPost('user/register', $edit, t('Create new account'));
221

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

    
225
    // Visit the home page and make sure the user is banned.
226
    $this->drupalGet('node');
227
    $this->assertText(t('has been banned'), 'User banned successfully.');
228
  }
229
}