honeypot.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 Honeypot's CSS generation routines.
  145. */
  146. class HoneypotCssTestCase extends DrupalWebTestCase {
  147. public static function getInfo() {
  148. return array(
  149. 'name' => 'Honeypot CSS tests',
  150. 'description' => 'Ensure that Honeypot rebuilds its CSS file correctly.',
  151. 'group' => 'Form API',
  152. );
  153. }
  154. public function setUp() {
  155. // Enable modules required for this test.
  156. parent::setUp(array('honeypot'));
  157. // Set up required Honeypot variables.
  158. variable_set('honeypot_element_name', 'url');
  159. }
  160. /**
  161. * Test CSS file regeneration.
  162. */
  163. public function testHoneypotCssRegeneration() {
  164. $honeypot_css = honeypot_get_css_file_path();
  165. // Delete the Honeypot CSS file (if it exists).
  166. file_unmanaged_delete($honeypot_css);
  167. // Make sure the Honeypot CSS file doesn't exist.
  168. $this->assertFalse(file_exists($honeypot_css));
  169. // Create the CSS file.
  170. honeypot_create_css(variable_get('honeypot_element_name', 'url'));
  171. // Make sure the Honeypot CSS file exists.
  172. $this->assertTrue(file_exists($honeypot_css));
  173. }
  174. /**
  175. * Test cron-based CSS file regeneration
  176. */
  177. public function testHoneypotCssRegenerationOnCron() {
  178. $honeypot_css = honeypot_get_css_file_path();
  179. // Delete the Honeypot CSS file (if it exists).
  180. file_unmanaged_delete($honeypot_css);
  181. // Make sure the Honeypot CSS file doesn't exist.
  182. $this->assertFalse(file_exists($honeypot_css));
  183. // Run cron.
  184. honeypot_cron();
  185. // Make sure the Honeypot CSS file exists.
  186. $this->assertTrue(file_exists($honeypot_css));
  187. }
  188. }
  189. /**
  190. * Test the functionality of the Honeypot module's integration with Trigger.
  191. */
  192. class HoneypotTriggerTestCase extends DrupalWebTestCase {
  193. public static function getInfo() {
  194. return array(
  195. 'name' => 'Honeypot Trigger integration',
  196. 'description' => 'Ensure that Honeypot triggers events correctly.',
  197. 'group' => 'Form API',
  198. );
  199. }
  200. public function setUp() {
  201. // Enable modules required for this test.
  202. parent::setUp(array('honeypot', 'trigger'));
  203. // Set up required Honeypot variables.
  204. variable_set('honeypot_element_name', 'url');
  205. // Disable time_limit protection.
  206. variable_set('honeypot_time_limit', 0);
  207. // Test protecting all forms.
  208. variable_set('honeypot_protect_all_forms', TRUE);
  209. variable_set('honeypot_log', FALSE);
  210. // Set up other required variables.
  211. variable_set('user_email_verification', TRUE);
  212. variable_set('user_register', USER_REGISTER_VISITORS);
  213. // Assign new action to Honeypot form rejection Trigger.
  214. db_insert('trigger_assignments')
  215. ->fields(array(
  216. 'hook' => 'honeypot_reject',
  217. 'aid' => 'system_block_ip_action',
  218. 'weight' => 1,
  219. ))
  220. ->execute();
  221. }
  222. /**
  223. * Test trigger integration.
  224. */
  225. public function testHoneypotTriggerIntegration() {
  226. // Set up form and submit it.
  227. $edit['name'] = $this->randomName();
  228. $edit['mail'] = $edit['name'] . '@example.com';
  229. $edit['url'] = 'http://www.example.com/';
  230. $this->drupalPost('user/register', $edit, t('Create new account'));
  231. // Make sure Honeypot is working.
  232. $this->assertText(t('There was a problem with your form submission.'), 'Honeypot working correctly.');
  233. // Visit the home page and make sure the user is banned.
  234. $this->drupalGet('node');
  235. $this->assertText(t('has been banned'), 'User banned successfully.');
  236. }
  237. }