CaptchaPersistenceTest.php 7.33 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 Persistence.
 *
 * @group captcha
 */
12
class CaptchaPersistenceTest extends CaptchaWebTestBase {
13
14
15

  /**
   * Set up the persistence and CAPTCHA settings.
16
17
18
   *
   * @param int $persistence
   *   The persistence value.
19
20
   */
  private function setUpPersistence($persistence) {
21
    $this->drupalLogin($this->adminUser);
22
    // Set persistence.
23
    $edit = ['persistence' => $persistence];
24
    $this->drupalPostForm(self::CAPTCHA_ADMIN_PATH, $edit, 'Save configuration');
25
26
27
28
29
30
31
32
33
34
    // Log admin out.
    $this->drupalLogout();

    // Set the Test123 CAPTCHA on user register and comment form.
    // We have to do this with the function captcha_set_form_id_setting()
    // (because the CATCHA admin form does not show the Test123 option).
    // We also have to do this after all usage of the CAPTCHA admin form
    // (because posting the CAPTCHA admin form would set the CAPTCHA to 'none').
    captcha_set_form_id_setting('user_login_form', 'captcha/Test');
    $this->drupalGet('user');
35
    $this->assertCaptchaPresence(TRUE);
36
37
38
39
40
    captcha_set_form_id_setting('user_register_form', 'captcha/Test');
    $this->drupalGet('user/register');
    $this->assertCaptchaPresence(TRUE);
  }

41
42
43
44
45
46
  /**
   * Check if Captcha sid present in form.
   *
   * @param string $captcha_sid_initial
   *   Captcha SID token.
   */
47
48
49
50
51
52
  protected function assertPreservedCsid($captcha_sid_initial) {
    $captcha_sid = $this->getCaptchaSidFromForm();
    $this->assertEqual($captcha_sid_initial, $captcha_sid,
      "CAPTCHA session ID should be preserved (expected: $captcha_sid_initial, found: $captcha_sid).");
  }

53
54
55
56
57
58
  /**
   * Check if message about SID present.
   *
   * @param string $captcha_sid_initial
   *   Captcha SID token.
   */
59
60
  protected function assertDifferentCsid($captcha_sid_initial) {
    $captcha_sid = $this->getCaptchaSidFromForm();
61
    $this->assertNotEqual($captcha_sid_initial, $captcha_sid, "CAPTCHA session ID should be different.");
62
63
  }

64
65
66
67
  /**
   * Test persistence always.
   */
  public function testPersistenceAlways() {
68
69
70
    // Set up of persistence and CAPTCHAs.
    $this->setUpPersistence(CAPTCHA_PERSISTENCE_SHOW_ALWAYS);

71
72
73
    // Go to login form and check if there is a CAPTCHA
    // on the login form (look for the title).
    $this->drupalGet('<front>');
74
75
76
77
    $this->assertCaptchaPresence(TRUE);
    $captcha_sid_initial = $this->getCaptchaSidFromForm();

    // Try to with wrong user name and password, but correct CAPTCHA.
78
    $edit = [
79
80
81
      'name' => 'foobar',
      'pass' => 'bazlaz',
      'captcha_response' => 'Test 123',
82
    ];
83
    $this->drupalPostForm(NULL, $edit, t('Log in'), [], self::LOGIN_HTML_FORM_ID);
84
85
86
    // Check that there was no error message for the CAPTCHA.
    $this->assertCaptchaResponseAccepted();

87
88
    // Name and password were wrong, we should get an updated
    // form with a fresh CAPTCHA.
89
90
91
92
    $this->assertCaptchaPresence(TRUE);
    $this->assertPreservedCsid($captcha_sid_initial);

    // Post from again.
93
    $this->drupalPostForm(NULL, $edit, t('Log in'), [], self::LOGIN_HTML_FORM_ID);
94
95
96
97
98
    // Check that there was no error message for the CAPTCHA.
    $this->assertCaptchaResponseAccepted();
    $this->assertPreservedCsid($captcha_sid_initial);
  }

99
100
101
102
  /**
   * Test persistence per form instance.
   */
  public function testPersistencePerFormInstance() {
103
104
105
106
    // Set up of persistence and CAPTCHAs.
    $this->setUpPersistence(CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE);

    // Go to login form and check if there is a CAPTCHA on the login form.
107
    $this->drupalGet('<front>');
108
109
110
111
    $this->assertCaptchaPresence(TRUE);
    $captcha_sid_initial = $this->getCaptchaSidFromForm();

    // Try to with wrong user name and password, but correct CAPTCHA.
112
    $edit = [
113
114
115
      'name' => 'foobar',
      'pass' => 'bazlaz',
      'captcha_response' => 'Test 123',
116
    ];
117
    $this->drupalPostForm(NULL, $edit, t('Log in'), [], self::LOGIN_HTML_FORM_ID);
118
119
120
121
122
123
    // Check that there was no error message for the CAPTCHA.
    $this->assertCaptchaResponseAccepted();
    // There shouldn't be a CAPTCHA on the new form.
    $this->assertCaptchaPresence(FALSE);
    $this->assertPreservedCsid($captcha_sid_initial);

124
    // Start a new form instance/session.
125
126
127
128
129
    $this->drupalGet('node');
    $this->drupalGet('user');
    $this->assertCaptchaPresence(TRUE);
    $this->assertDifferentCsid($captcha_sid_initial);

130
    // Check another form.
131
132
133
134
135
    $this->drupalGet('user/register');
    $this->assertCaptchaPresence(TRUE);
    $this->assertDifferentCsid($captcha_sid_initial);
  }

136
137
138
139
  /**
   * Test Persistence per form type.
   */
  public function testPersistencePerFormType() {
140
141
142
143
    // Set up of persistence and CAPTCHAs.
    $this->setUpPersistence(CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE);

    // Go to login form and check if there is a CAPTCHA on the login form.
144
    $this->drupalGet('<front>');
145
146
147
148
    $this->assertCaptchaPresence(TRUE);
    $captcha_sid_initial = $this->getCaptchaSidFromForm();

    // Try to with wrong user name and password, but correct CAPTCHA.
149
    $edit = [
150
151
152
      'name' => 'foobar',
      'pass' => 'bazlaz',
      'captcha_response' => 'Test 123',
153
    ];
154
    $this->drupalPostForm(NULL, $edit, t('Log in'), [], self::LOGIN_HTML_FORM_ID);
155
156
157
158
159
160
    // Check that there was no error message for the CAPTCHA.
    $this->assertCaptchaResponseAccepted();
    // There shouldn't be a CAPTCHA on the new form.
    $this->assertCaptchaPresence(FALSE);
    $this->assertPreservedCsid($captcha_sid_initial);

161
    // Start a new form instance/session.
162
163
164
165
166
    $this->drupalGet('node');
    $this->drupalGet('user');
    $this->assertCaptchaPresence(FALSE);
    $this->assertDifferentCsid($captcha_sid_initial);

167
    // Check another form.
168
    /* @var \Drupal\captcha\Entity\CaptchaPoint $captcha_point */
169
170
171
    $captcha_point = \Drupal::entityTypeManager()
      ->getStorage('captcha_point')
      ->load('user_register_form');
172
    $captcha_point->enable()->save();
173
174
175
176
177
    $this->drupalGet('user/register');
    $this->assertCaptchaPresence(TRUE);
    $this->assertDifferentCsid($captcha_sid_initial);
  }

178
179
180
181
  /**
   * Test Persistence "Only once".
   */
  public function testPersistenceOnlyOnce() {
182
183
184
185
    // Set up of persistence and CAPTCHAs.
    $this->setUpPersistence(CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL);

    // Go to login form and check if there is a CAPTCHA on the login form.
186
    $this->drupalGet('<front>');
187
188
189
190
    $this->assertCaptchaPresence(TRUE);
    $captcha_sid_initial = $this->getCaptchaSidFromForm();

    // Try to with wrong user name and password, but correct CAPTCHA.
191
    $edit = [
192
193
194
      'name' => 'foobar',
      'pass' => 'bazlaz',
      'captcha_response' => 'Test 123',
195
    ];
196
    $this->drupalPostForm(NULL, $edit, t('Log in'), [], self::LOGIN_HTML_FORM_ID);
197
198
199
200
201
202
    // Check that there was no error message for the CAPTCHA.
    $this->assertCaptchaResponseAccepted();
    // There shouldn't be a CAPTCHA on the new form.
    $this->assertCaptchaPresence(FALSE);
    $this->assertPreservedCsid($captcha_sid_initial);

203
    // Start a new form instance/session.
204
    $this->drupalGet('node');
205
206
207
208
    $this->drupalGet('user');
    $this->assertCaptchaPresence(FALSE);
    $this->assertDifferentCsid($captcha_sid_initial);

209
    // Check another form.
210
211
212
213
    $this->drupalGet('user/register');
    $this->assertCaptchaPresence(FALSE);
    $this->assertDifferentCsid($captcha_sid_initial);
  }
214

215
}