honeypot.test 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 $admin_user;
  11. protected $web_user;
  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. variable_set('honeypot_time_limit', 0); // Disable time_limit protection.
  26. variable_set('honeypot_protect_all_forms', TRUE); // Test protecting all forms.
  27. variable_set('honeypot_log', FALSE);
  28. // Set up other required variables.
  29. variable_set('user_email_verification', TRUE);
  30. variable_set('user_register', USER_REGISTER_VISITORS);
  31. // Set up admin user.
  32. $this->admin_user = $this->drupalCreateUser(array(
  33. 'administer honeypot',
  34. 'bypass honeypot protection',
  35. 'administer content types',
  36. 'administer users',
  37. 'access comments',
  38. 'post comments',
  39. 'skip comment approval',
  40. 'administer comments',
  41. ));
  42. // Set up web user.
  43. $this->web_user = $this->drupalCreateUser(array(
  44. 'access comments',
  45. 'post comments',
  46. 'create article content',
  47. ));
  48. // Set up example node.
  49. $this->node = $this->drupalCreateNode(array(
  50. 'type' => 'article',
  51. 'promote' => 1,
  52. 'uid' => $this->web_user->uid,
  53. ));
  54. }
  55. /**
  56. * Test user registration (anonymous users).
  57. */
  58. public function testProtectRegisterUserNormal() {
  59. // Set up form and submit it.
  60. $edit['name'] = $this->randomName();
  61. $edit['mail'] = $edit['name'] . '@example.com';
  62. $this->drupalPost('user/register', $edit, t('Create new account'));
  63. // Form should have been submitted successfully.
  64. $this->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), 'User registered successfully.');
  65. }
  66. public function testProtectUserRegisterHoneypotFilled() {
  67. // Set up form and submit it.
  68. $edit['name'] = $this->randomName();
  69. $edit['mail'] = $edit['name'] . '@example.com';
  70. $edit['url'] = 'http://www.example.com/';
  71. $this->drupalPost('user/register', $edit, t('Create new account'));
  72. // Form should have error message.
  73. $this->assertText(t('There was a problem with your form submission. Please refresh the page and try again.'), 'Registration form protected by honeypot.');
  74. }
  75. public function testProtectRegisterUserTooFast() {
  76. // Enable time limit for honeypot.
  77. variable_set('honeypot_time_limit', 5);
  78. // Set up form and submit it.
  79. $edit['name'] = $this->randomName();
  80. $edit['mail'] = $edit['name'] . '@example.com';
  81. $this->drupalPost('user/register', $edit, t('Create new account'));
  82. // Form should have error message.
  83. $this->assertText(t('There was a problem with your form submission. Please wait'), 'Registration form protected by time limit.');
  84. }
  85. public function testProtectCommentFormNormal() {
  86. $comment = 'Test comment.';
  87. // Disable time limit for honeypot.
  88. variable_set('honeypot_time_limit', 0);
  89. // Log in the web user.
  90. $this->drupalLogin($this->web_user);
  91. // Set up form and submit it.
  92. $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment;
  93. $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
  94. $this->assertText(t('Your comment has been posted.'), 'Comment posted successfully.');
  95. }
  96. public function testProtectCommentFormHoneypotFilled() {
  97. $comment = 'Test comment.';
  98. // Log in the web user.
  99. $this->drupalLogin($this->web_user);
  100. // Set up form and submit it.
  101. $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment;
  102. $edit['url'] = 'http://www.example.com/';
  103. $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
  104. $this->assertText(t('There was a problem with your form submission. Please refresh the page and try again.'), 'Comment posted successfully.');
  105. }
  106. public function testProtectCommentFormHoneypotBypass() {
  107. // Log in the admin user.
  108. $this->drupalLogin($this->admin_user);
  109. // Get the comment reply form and ensure there's no 'url' field.
  110. $this->drupalGet('comment/reply/' . $this->node->nid);
  111. $this->assertNoText('id="edit-url" name="url"', 'Honeypot home page field not shown.');
  112. }
  113. }