honeypot.test 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * @file
  4. * Testing for Honeypot module.
  5. */
  6. /**
  7. * Test the functionality of the Honeypot module for an admin user.
  8. */
  9. class HoneypotFormTestCase extends DrupalWebTestCase {
  10. protected $adminUser;
  11. protected $webUser;
  12. protected $node;
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Honeypot form protections',
  16. 'description' => 'Ensure that Honeypot protects site forms properly.',
  17. 'group' => 'Form API',
  18. );
  19. }
  20. public function setUp() {
  21. // Enable modules required for this test.
  22. parent::setUp(array('honeypot', 'comment'));
  23. // Set up required Honeypot variables.
  24. variable_set('honeypot_element_name', 'url');
  25. // Disable time_limit protection.
  26. variable_set('honeypot_time_limit', 0);
  27. // Test protecting all forms.
  28. variable_set('honeypot_protect_all_forms', TRUE);
  29. variable_set('honeypot_log', FALSE);
  30. // Set up other required variables.
  31. variable_set('user_email_verification', TRUE);
  32. variable_set('user_register', USER_REGISTER_VISITORS);
  33. // Set up admin user.
  34. $this->adminUser = $this->drupalCreateUser(array(
  35. 'administer honeypot',
  36. 'bypass honeypot protection',
  37. 'administer content types',
  38. 'administer users',
  39. 'access comments',
  40. 'post comments',
  41. 'skip comment approval',
  42. 'administer comments',
  43. ));
  44. // Set up web user.
  45. $this->webUser = $this->drupalCreateUser(array(
  46. 'access comments',
  47. 'post comments',
  48. 'create article content',
  49. ));
  50. // Set up example node.
  51. $this->node = $this->drupalCreateNode(array(
  52. 'type' => 'article',
  53. 'promote' => 1,
  54. 'uid' => $this->webUser->uid,
  55. ));
  56. }
  57. /**
  58. * Test user registration (anonymous users).
  59. */
  60. public function testProtectRegisterUserNormal() {
  61. // Set up form and submit it.
  62. $edit['name'] = $this->randomName();
  63. $edit['mail'] = $edit['name'] . '@example.com';
  64. $this->drupalPost('user/register', $edit, t('Create new account'));
  65. // Form should have been submitted successfully.
  66. $this->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), 'User registered successfully.');
  67. }
  68. public function testProtectUserRegisterHoneypotFilled() {
  69. // Set up form and submit it.
  70. $edit['name'] = $this->randomName();
  71. $edit['mail'] = $edit['name'] . '@example.com';
  72. $edit['url'] = 'http://www.example.com/';
  73. $this->drupalPost('user/register', $edit, t('Create new account'));
  74. // Form should have error message.
  75. $this->assertText(t('There was a problem with your form submission. Please refresh the page and try again.'), 'Registration form protected by honeypot.');
  76. }
  77. public function testProtectRegisterUserTooFast() {
  78. // Enable time limit for honeypot.
  79. variable_set('honeypot_time_limit', 5);
  80. // Set up form and submit it.
  81. $edit['name'] = $this->randomName();
  82. $edit['mail'] = $edit['name'] . '@example.com';
  83. $this->drupalPost('user/register', $edit, t('Create new account'));
  84. // Form should have error message.
  85. $this->assertText(t('There was a problem with your form submission. Please wait 6 seconds and try again.'), 'Registration form protected by time limit.');
  86. }
  87. /**
  88. * Test comment form protection.
  89. */
  90. public function testProtectCommentFormNormal() {
  91. $comment = 'Test comment.';
  92. // Disable time limit for honeypot.
  93. variable_set('honeypot_time_limit', 0);
  94. // Log in the web user.
  95. $this->drupalLogin($this->webUser);
  96. // Set up form and submit it.
  97. $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment;
  98. $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
  99. $this->assertText(t('Your comment has been posted.'), 'Comment posted successfully.');
  100. }
  101. public function testProtectCommentFormHoneypotFilled() {
  102. $comment = 'Test comment.';
  103. // Log in the web user.
  104. $this->drupalLogin($this->webUser);
  105. // Set up form and submit it.
  106. $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment;
  107. $edit['url'] = 'http://www.example.com/';
  108. $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
  109. $this->assertText(t('There was a problem with your form submission. Please refresh the page and try again.'), 'Comment posted successfully.');
  110. }
  111. public function testProtectCommentFormHoneypotBypass() {
  112. // Log in the admin user.
  113. $this->drupalLogin($this->adminUser);
  114. // Get the comment reply form and ensure there's no 'url' field.
  115. $this->drupalGet('comment/reply/' . $this->node->nid);
  116. $this->assertNoText('id="edit-url" name="url"', 'Honeypot home page field not shown.');
  117. }
  118. /**
  119. * Test node form protection.
  120. */
  121. public function testProtectNodeFormTooFast() {
  122. // Log in the admin user.
  123. $this->drupalLogin($this->webUser);
  124. // Reset the time limit to 5 seconds.
  125. variable_set('honeypot_time_limit', 5);
  126. // Set up the form and submit it.
  127. $edit["title"] = 'Test Page';
  128. $this->drupalPost('node/add/article', $edit, t('Save'));
  129. $this->assertText(t('There was a problem with your form submission.'), 'Honeypot node form timestamp protection works.');
  130. }
  131. /**
  132. * Test node form protection.
  133. */
  134. public function testProtectNodeFormPreviewPassthru() {
  135. // Log in the admin user.
  136. $this->drupalLogin($this->webUser);
  137. // Post a node form using the 'Preview' button and make sure it's allowed.
  138. $edit["title"] = 'Test Page';
  139. $this->drupalPost('node/add/article', $edit, t('Preview'));
  140. $this->assertNoText(t('There was a problem with your form submission.'), 'Honeypot not blocking node form previews.');
  141. }
  142. }
  143. /**
  144. * Test the functionality of the Honeypot module's integration with Trigger.
  145. */
  146. class HoneypotTriggerTestCase extends DrupalWebTestCase {
  147. public static function getInfo() {
  148. return array(
  149. 'name' => 'Honeypot Trigger integration',
  150. 'description' => 'Ensure that Honeypot triggers events correctly.',
  151. 'group' => 'Form API',
  152. );
  153. }
  154. public function setUp() {
  155. // Enable modules required for this test.
  156. parent::setUp(array('honeypot', 'trigger'));
  157. // Set up required Honeypot variables.
  158. variable_set('honeypot_element_name', 'url');
  159. // Disable time_limit protection.
  160. variable_set('honeypot_time_limit', 0);
  161. // Test protecting all forms.
  162. variable_set('honeypot_protect_all_forms', TRUE);
  163. variable_set('honeypot_log', FALSE);
  164. // Set up other required variables.
  165. variable_set('user_email_verification', TRUE);
  166. variable_set('user_register', USER_REGISTER_VISITORS);
  167. // Assign new action to Honeypot form rejection Trigger.
  168. db_insert('trigger_assignments')
  169. ->fields(array(
  170. 'hook' => 'honeypot_reject',
  171. 'aid' => 'system_block_ip_action',
  172. 'weight' => 1,
  173. ))
  174. ->execute();
  175. }
  176. /**
  177. * Test trigger integration.
  178. */
  179. public function testHoneypotTriggerIntegration() {
  180. // Set up form and submit it.
  181. $edit['name'] = $this->randomName();
  182. $edit['mail'] = $edit['name'] . '@example.com';
  183. $edit['url'] = 'http://www.example.com/';
  184. $this->drupalPost('user/register', $edit, t('Create new account'));
  185. // Make sure Honeypot is working.
  186. $this->assertText(t('There was a problem with your form submission.'), 'Honeypot working correctly.');
  187. // Visit the home page and make sure the user is banned.
  188. $this->drupalGet('node');
  189. $this->assertText(t('has been banned'), 'User banned successfully.');
  190. }
  191. }