CaptchaCacheTest.php 2.52 KB
Newer Older
1
2
<?php

3
4
5
namespace Drupal\Tests\captcha\Functional;

use Drupal\Tests\captcha\Functional\CaptchaWebTestBase;
6
7
8
9
10
11

/**
 * Tests CAPTCHA caching on various pages.
 *
 * @group captcha
 */
12
class CaptchaCacheTest extends CaptchaWebTestBase {
13
14

  /**
15
16
17
   * Modules to install for this Test class.
   *
   * @var array
18
19
20
21
22
23
   */
  public static $modules = ['block', 'image_captcha'];

  /**
   * {@inheritdoc}
   */
24
  public function setUp() {
25
26
    parent::setUp();

27
    $this->drupalPlaceBlock('user_login_block', ['id' => 'login']);
28
29
30
31
32
33
  }

  /**
   * Test the cache tags.
   */
  public function testCacheTags() {
34
    global $base_path;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    // Check caching without captcha as anonymous user.
    $this->drupalGet('');
    $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'MISS');
    $this->drupalGet('');
    $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'HIT');

    // Enable captcha on login block and test caching.
    captcha_set_form_id_setting('user_login_form', 'captcha/Math');
    $this->drupalGet('');
    $sid = $this->getCaptchaSidFromForm();
    $this->assertFalse($this->drupalGetHeader('x-drupal-cache'), 'Cache is disabled');
    $this->drupalGet('');
    $this->assertNotEqual($sid, $this->getCaptchaSidFromForm());

    // Switch challenge to captcha/Test, check the captcha isn't cached.
    captcha_set_form_id_setting('user_login_form', 'captcha/Test');
    $this->drupalGet('');
    $sid = $this->getCaptchaSidFromForm();
    $this->assertFalse($this->drupalGetHeader('x-drupal-cache'), 'Cache is disabled');
    $this->drupalGet('');
    $this->assertNotEqual($sid, $this->getCaptchaSidFromForm());

    // Switch challenge to image_captcha/Image, check the captcha isn't cached.
    captcha_set_form_id_setting('user_login_form', 'image_captcha/Image');
    $this->drupalGet('');
60
    $image_path = $this->xpath('//div[@class="details-wrapper"]/img')[0]->getAttribute('src');
61
62
63
    $this->assertFalse($this->drupalGetHeader('x-drupal-cache'), 'Cache disabled');
    // Check that we get a new image when vising the page again.
    $this->drupalGet('');
64
    $this->assertNotEqual($image_path, $this->xpath('//div[@class="details-wrapper"]/img')[0]->getAttribute('src'));
65
66
67
    // Check image caching, remove the base path since drupalGet() expects the
    // internal path.
    $this->drupalGet(substr($image_path, strlen($base_path)));
68
69
    $this->assertResponse(200);
    // Request image twice to make sure no errors happen (due to page caching).
70
    $this->drupalGet(substr($image_path, strlen($base_path)));
71
72
73
74
    $this->assertResponse(200);
  }

}