image_captcha.test 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Image CAPTCHA module.
  5. */
  6. class ImageCaptchaWebTestCase extends CaptchaBaseWebTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'General Image CAPTCHA functionality',
  10. 'description' => 'Testing of the basic Image CAPTCHA functions.',
  11. 'group' => 'CAPTCHA',
  12. );
  13. }
  14. public function setUp() {
  15. parent::setUp('image_captcha');
  16. }
  17. /**
  18. * Helper function to get the CAPTCHA image element from the current form.
  19. */
  20. protected function getCaptchaImageFromForm() {
  21. $elements = $this->xpath('//input[@name="captcha_sid"]/../img');
  22. return $elements[0];
  23. }
  24. /**
  25. * Helper function to get a CAPTCHA form.
  26. */
  27. protected function getImageCaptchaForm($form_id = 'user_login', $page = 'user') {
  28. // Set a CAPTCHA on supplied form.
  29. captcha_set_form_id_setting($form_id, 'image_captcha/Image');
  30. // Fetch the page and make sure that we got a CAPTCHA.
  31. $this->drupalGet($page);
  32. $this->assertCaptchaPresence(TRUE);
  33. }
  34. /**
  35. * Asserts that the image URL actually returns an image.
  36. */
  37. protected function assertNonEmptyImage() {
  38. $img = $this->getCaptchaImageFromForm();
  39. // Try to fetch the image.
  40. $this->drupalGet($this->getAbsoluteUrl($img['src']));
  41. $this->assertTrue($this->drupalGetHeader('Content-Length') > 0,
  42. 'Image CAPTCHA image is not empty.');
  43. }
  44. /**
  45. * Tests if the image URL actually returns an image with clean URLs enabled.
  46. */
  47. public function testNonEmptyImageCleanURLs() {
  48. variable_set('clean_url', 1);
  49. $this->getImageCaptchaForm();
  50. $this->assertNonEmptyImage();
  51. }
  52. /**
  53. * Tests if the image URL actually returns an image with clean URLs disabled.
  54. */
  55. public function testNonEmptyImageDirtyURLs() {
  56. variable_set('clean_url', 0);
  57. $this->getImageCaptchaForm();
  58. $this->assertNonEmptyImage();
  59. }
  60. }