Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\simpletest;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\block\Entity\Block;
|
Chris@0
|
6 use Drupal\Component\Serialization\Json;
|
Chris@0
|
7 use Drupal\Component\Utility\Html;
|
Chris@0
|
8 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
9 use Drupal\Component\Utility\UrlHelper;
|
Chris@0
|
10 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
11 use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
|
Chris@0
|
12 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
|
Chris@0
|
13 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
14 use Drupal\Core\Session\AnonymousUserSession;
|
Chris@0
|
15 use Drupal\Core\Test\AssertMailTrait;
|
Chris@0
|
16 use Drupal\Core\Test\FunctionalTestSetupTrait;
|
Chris@0
|
17 use Drupal\Core\Url;
|
Chris@0
|
18 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
|
Chris@0
|
19 use Drupal\Tests\EntityViewTrait;
|
Chris@0
|
20 use Drupal\Tests\block\Traits\BlockCreationTrait as BaseBlockCreationTrait;
|
Chris@0
|
21 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
|
Chris@0
|
22 use Drupal\Tests\node\Traits\NodeCreationTrait;
|
Chris@0
|
23 use Drupal\Tests\Traits\Core\CronRunTrait;
|
Chris@0
|
24 use Drupal\Tests\TestFileCreationTrait;
|
Chris@0
|
25 use Drupal\Tests\user\Traits\UserCreationTrait;
|
Chris@0
|
26 use Drupal\Tests\XdebugRequestTrait;
|
Chris@0
|
27 use Zend\Diactoros\Uri;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Test case for typical Drupal tests.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @ingroup testing
|
Chris@0
|
33 */
|
Chris@0
|
34 abstract class WebTestBase extends TestBase {
|
Chris@0
|
35
|
Chris@0
|
36 use FunctionalTestSetupTrait;
|
Chris@0
|
37 use AssertContentTrait;
|
Chris@0
|
38 use TestFileCreationTrait {
|
Chris@0
|
39 getTestFiles as drupalGetTestFiles;
|
Chris@0
|
40 compareFiles as drupalCompareFiles;
|
Chris@0
|
41 }
|
Chris@0
|
42 use AssertPageCacheContextsAndTagsTrait;
|
Chris@0
|
43 use BaseBlockCreationTrait {
|
Chris@0
|
44 placeBlock as drupalPlaceBlock;
|
Chris@0
|
45 }
|
Chris@0
|
46 use ContentTypeCreationTrait {
|
Chris@0
|
47 createContentType as drupalCreateContentType;
|
Chris@0
|
48 }
|
Chris@0
|
49 use CronRunTrait;
|
Chris@0
|
50 use AssertMailTrait {
|
Chris@0
|
51 getMails as drupalGetMails;
|
Chris@0
|
52 }
|
Chris@0
|
53 use NodeCreationTrait {
|
Chris@0
|
54 getNodeByTitle as drupalGetNodeByTitle;
|
Chris@0
|
55 createNode as drupalCreateNode;
|
Chris@0
|
56 }
|
Chris@0
|
57 use UserCreationTrait {
|
Chris@0
|
58 createUser as drupalCreateUser;
|
Chris@0
|
59 createRole as drupalCreateRole;
|
Chris@0
|
60 createAdminRole as drupalCreateAdminRole;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 use XdebugRequestTrait;
|
Chris@0
|
64 use EntityViewTrait {
|
Chris@0
|
65 buildEntityView as drupalBuildEntityView;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * The profile to install as a basis for testing.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @var string
|
Chris@0
|
72 */
|
Chris@0
|
73 protected $profile = 'testing';
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * The URL currently loaded in the internal browser.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @var string
|
Chris@0
|
79 */
|
Chris@0
|
80 protected $url;
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * The handle of the current cURL connection.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @var resource
|
Chris@0
|
86 */
|
Chris@0
|
87 protected $curlHandle;
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Whether or not to assert the presence of the X-Drupal-Ajax-Token.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @var bool
|
Chris@0
|
93 */
|
Chris@0
|
94 protected $assertAjaxHeader = TRUE;
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * The headers of the page currently loaded in the internal browser.
|
Chris@0
|
98 *
|
Chris@12
|
99 * @var array
|
Chris@0
|
100 */
|
Chris@0
|
101 protected $headers;
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * The cookies of the page currently loaded in the internal browser.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @var array
|
Chris@0
|
107 */
|
Chris@0
|
108 protected $cookies = [];
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Indicates that headers should be dumped if verbose output is enabled.
|
Chris@0
|
112 *
|
Chris@0
|
113 * Headers are dumped to verbose by drupalGet(), drupalHead(), and
|
Chris@0
|
114 * drupalPostForm().
|
Chris@0
|
115 *
|
Chris@0
|
116 * @var bool
|
Chris@0
|
117 */
|
Chris@0
|
118 protected $dumpHeaders = FALSE;
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * The current user logged in using the internal browser.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @var \Drupal\Core\Session\AccountInterface|bool
|
Chris@0
|
124 */
|
Chris@0
|
125 protected $loggedInUser = FALSE;
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * The current cookie file used by cURL.
|
Chris@0
|
129 *
|
Chris@0
|
130 * We do not reuse the cookies in further runs, so we do not need a file
|
Chris@0
|
131 * but we still need cookie handling, so we set the jar to NULL.
|
Chris@0
|
132 */
|
Chris@0
|
133 protected $cookieFile = NULL;
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Additional cURL options.
|
Chris@0
|
137 *
|
Chris@0
|
138 * \Drupal\simpletest\WebTestBase itself never sets this but always obeys what
|
Chris@0
|
139 * is set.
|
Chris@0
|
140 */
|
Chris@0
|
141 protected $additionalCurlOptions = [];
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * The original batch, before it was changed for testing purposes.
|
Chris@0
|
145 *
|
Chris@0
|
146 * @var array
|
Chris@0
|
147 */
|
Chris@0
|
148 protected $originalBatch;
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * The original user, before it was changed to a clean uid = 1 for testing.
|
Chris@0
|
152 *
|
Chris@0
|
153 * @var object
|
Chris@0
|
154 */
|
Chris@0
|
155 protected $originalUser = NULL;
|
Chris@0
|
156
|
Chris@0
|
157 /**
|
Chris@0
|
158 * The original shutdown handlers array, before it was cleaned for testing.
|
Chris@0
|
159 *
|
Chris@0
|
160 * @var array
|
Chris@0
|
161 */
|
Chris@0
|
162 protected $originalShutdownCallbacks = [];
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * The current session ID, if available.
|
Chris@0
|
166 */
|
Chris@0
|
167 protected $sessionId = NULL;
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * The maximum number of redirects to follow when handling responses.
|
Chris@12
|
171 *
|
Chris@12
|
172 * @var int
|
Chris@0
|
173 */
|
Chris@0
|
174 protected $maximumRedirects = 5;
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * The number of redirects followed during the handling of a request.
|
Chris@0
|
178 */
|
Chris@0
|
179 protected $redirectCount;
|
Chris@0
|
180
|
Chris@0
|
181
|
Chris@0
|
182 /**
|
Chris@0
|
183 * The number of meta refresh redirects to follow, or NULL if unlimited.
|
Chris@0
|
184 *
|
Chris@0
|
185 * @var null|int
|
Chris@0
|
186 */
|
Chris@0
|
187 protected $maximumMetaRefreshCount = NULL;
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * The number of meta refresh redirects followed during ::drupalGet().
|
Chris@0
|
191 *
|
Chris@0
|
192 * @var int
|
Chris@0
|
193 */
|
Chris@0
|
194 protected $metaRefreshCount = 0;
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Cookies to set on curl requests.
|
Chris@0
|
198 *
|
Chris@0
|
199 * @var array
|
Chris@0
|
200 */
|
Chris@0
|
201 protected $curlCookies = [];
|
Chris@0
|
202
|
Chris@0
|
203 /**
|
Chris@0
|
204 * An array of custom translations suitable for drupal_rewrite_settings().
|
Chris@0
|
205 *
|
Chris@0
|
206 * @var array
|
Chris@0
|
207 */
|
Chris@0
|
208 protected $customTranslations;
|
Chris@0
|
209
|
Chris@0
|
210 /**
|
Chris@0
|
211 * Constructor for \Drupal\simpletest\WebTestBase.
|
Chris@0
|
212 */
|
Chris@0
|
213 public function __construct($test_id = NULL) {
|
Chris@0
|
214 parent::__construct($test_id);
|
Chris@0
|
215 $this->skipClasses[__CLASS__] = TRUE;
|
Chris@0
|
216 $this->classLoader = require DRUPAL_ROOT . '/autoload.php';
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * Checks to see whether a block appears on the page.
|
Chris@0
|
221 *
|
Chris@0
|
222 * @param \Drupal\block\Entity\Block $block
|
Chris@0
|
223 * The block entity to find on the page.
|
Chris@0
|
224 */
|
Chris@0
|
225 protected function assertBlockAppears(Block $block) {
|
Chris@0
|
226 $result = $this->findBlockInstance($block);
|
Chris@0
|
227 $this->assertTrue(!empty($result), format_string('Ensure the block @id appears on the page', ['@id' => $block->id()]));
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@0
|
231 * Checks to see whether a block does not appears on the page.
|
Chris@0
|
232 *
|
Chris@0
|
233 * @param \Drupal\block\Entity\Block $block
|
Chris@0
|
234 * The block entity to find on the page.
|
Chris@0
|
235 */
|
Chris@0
|
236 protected function assertNoBlockAppears(Block $block) {
|
Chris@0
|
237 $result = $this->findBlockInstance($block);
|
Chris@0
|
238 $this->assertFalse(!empty($result), format_string('Ensure the block @id does not appear on the page', ['@id' => $block->id()]));
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 /**
|
Chris@0
|
242 * Find a block instance on the page.
|
Chris@0
|
243 *
|
Chris@0
|
244 * @param \Drupal\block\Entity\Block $block
|
Chris@0
|
245 * The block entity to find on the page.
|
Chris@0
|
246 *
|
Chris@0
|
247 * @return array
|
Chris@0
|
248 * The result from the xpath query.
|
Chris@0
|
249 */
|
Chris@0
|
250 protected function findBlockInstance(Block $block) {
|
Chris@0
|
251 return $this->xpath('//div[@id = :id]', [':id' => 'block-' . $block->id()]);
|
Chris@0
|
252 }
|
Chris@0
|
253
|
Chris@0
|
254 /**
|
Chris@0
|
255 * Log in a user with the internal browser.
|
Chris@0
|
256 *
|
Chris@0
|
257 * If a user is already logged in, then the current user is logged out before
|
Chris@0
|
258 * logging in the specified user.
|
Chris@0
|
259 *
|
Chris@0
|
260 * Please note that neither the current user nor the passed-in user object is
|
Chris@0
|
261 * populated with data of the logged in user. If you need full access to the
|
Chris@0
|
262 * user object after logging in, it must be updated manually. If you also need
|
Chris@0
|
263 * access to the plain-text password of the user (set by drupalCreateUser()),
|
Chris@0
|
264 * e.g. to log in the same user again, then it must be re-assigned manually.
|
Chris@0
|
265 * For example:
|
Chris@0
|
266 * @code
|
Chris@0
|
267 * // Create a user.
|
Chris@0
|
268 * $account = $this->drupalCreateUser(array());
|
Chris@0
|
269 * $this->drupalLogin($account);
|
Chris@0
|
270 * // Load real user object.
|
Chris@0
|
271 * $pass_raw = $account->pass_raw;
|
Chris@0
|
272 * $account = User::load($account->id());
|
Chris@0
|
273 * $account->pass_raw = $pass_raw;
|
Chris@0
|
274 * @endcode
|
Chris@0
|
275 *
|
Chris@0
|
276 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
277 * User object representing the user to log in.
|
Chris@0
|
278 *
|
Chris@0
|
279 * @see drupalCreateUser()
|
Chris@0
|
280 */
|
Chris@0
|
281 protected function drupalLogin(AccountInterface $account) {
|
Chris@0
|
282 if ($this->loggedInUser) {
|
Chris@0
|
283 $this->drupalLogout();
|
Chris@0
|
284 }
|
Chris@0
|
285
|
Chris@0
|
286 $edit = [
|
Chris@0
|
287 'name' => $account->getUsername(),
|
Chris@0
|
288 'pass' => $account->pass_raw
|
Chris@0
|
289 ];
|
Chris@0
|
290 $this->drupalPostForm('user/login', $edit, t('Log in'));
|
Chris@0
|
291
|
Chris@0
|
292 // @see WebTestBase::drupalUserIsLoggedIn()
|
Chris@0
|
293 if (isset($this->sessionId)) {
|
Chris@0
|
294 $account->session_id = $this->sessionId;
|
Chris@0
|
295 }
|
Chris@0
|
296 $pass = $this->assert($this->drupalUserIsLoggedIn($account), format_string('User %name successfully logged in.', ['%name' => $account->getUsername()]), 'User login');
|
Chris@0
|
297 if ($pass) {
|
Chris@0
|
298 $this->loggedInUser = $account;
|
Chris@0
|
299 $this->container->get('current_user')->setAccount($account);
|
Chris@0
|
300 }
|
Chris@0
|
301 }
|
Chris@0
|
302
|
Chris@0
|
303 /**
|
Chris@0
|
304 * Returns whether a given user account is logged in.
|
Chris@0
|
305 *
|
Chris@0
|
306 * @param \Drupal\user\UserInterface $account
|
Chris@0
|
307 * The user account object to check.
|
Chris@0
|
308 */
|
Chris@0
|
309 protected function drupalUserIsLoggedIn($account) {
|
Chris@0
|
310 $logged_in = FALSE;
|
Chris@0
|
311
|
Chris@0
|
312 if (isset($account->session_id)) {
|
Chris@0
|
313 $session_handler = $this->container->get('session_handler.storage');
|
Chris@0
|
314 $logged_in = (bool) $session_handler->read($account->session_id);
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 return $logged_in;
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 /**
|
Chris@0
|
321 * Logs a user out of the internal browser and confirms.
|
Chris@0
|
322 *
|
Chris@0
|
323 * Confirms logout by checking the login page.
|
Chris@0
|
324 */
|
Chris@0
|
325 protected function drupalLogout() {
|
Chris@0
|
326 // Make a request to the logout page, and redirect to the user page, the
|
Chris@0
|
327 // idea being if you were properly logged out you should be seeing a login
|
Chris@0
|
328 // screen.
|
Chris@0
|
329 $this->drupalGet('user/logout', ['query' => ['destination' => 'user/login']]);
|
Chris@0
|
330 $this->assertResponse(200, 'User was logged out.');
|
Chris@0
|
331 $pass = $this->assertField('name', 'Username field found.', 'Logout');
|
Chris@0
|
332 $pass = $pass && $this->assertField('pass', 'Password field found.', 'Logout');
|
Chris@0
|
333
|
Chris@0
|
334 if ($pass) {
|
Chris@0
|
335 // @see WebTestBase::drupalUserIsLoggedIn()
|
Chris@0
|
336 unset($this->loggedInUser->session_id);
|
Chris@0
|
337 $this->loggedInUser = FALSE;
|
Chris@0
|
338 $this->container->get('current_user')->setAccount(new AnonymousUserSession());
|
Chris@0
|
339 }
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 /**
|
Chris@0
|
343 * Sets up a Drupal site for running functional and integration tests.
|
Chris@0
|
344 *
|
Chris@0
|
345 * Installs Drupal with the installation profile specified in
|
Chris@0
|
346 * \Drupal\simpletest\WebTestBase::$profile into the prefixed database.
|
Chris@0
|
347 *
|
Chris@0
|
348 * Afterwards, installs any additional modules specified in the static
|
Chris@0
|
349 * \Drupal\simpletest\WebTestBase::$modules property of each class in the
|
Chris@0
|
350 * class hierarchy.
|
Chris@0
|
351 *
|
Chris@0
|
352 * After installation all caches are flushed and several configuration values
|
Chris@0
|
353 * are reset to the values of the parent site executing the test, since the
|
Chris@0
|
354 * default values may be incompatible with the environment in which tests are
|
Chris@0
|
355 * being executed.
|
Chris@0
|
356 */
|
Chris@0
|
357 protected function setUp() {
|
Chris@0
|
358 // Set an explicit time zone to not rely on the system one, which may vary
|
Chris@0
|
359 // from setup to setup. The Australia/Sydney time zone is chosen so all
|
Chris@0
|
360 // tests are run using an edge case scenario (UTC+10 and DST). This choice
|
Chris@0
|
361 // is made to prevent time zone related regressions and reduce the
|
Chris@0
|
362 // fragility of the testing system in general. This is also set in config in
|
Chris@0
|
363 // \Drupal\simpletest\WebTestBase::initConfig().
|
Chris@0
|
364 date_default_timezone_set('Australia/Sydney');
|
Chris@0
|
365
|
Chris@0
|
366 // Preserve original batch for later restoration.
|
Chris@0
|
367 $this->setBatch();
|
Chris@0
|
368
|
Chris@0
|
369 // Initialize user 1 and session name.
|
Chris@0
|
370 $this->initUserSession();
|
Chris@0
|
371
|
Chris@0
|
372 // Prepare the child site settings.
|
Chris@0
|
373 $this->prepareSettings();
|
Chris@0
|
374
|
Chris@0
|
375 // Execute the non-interactive installer.
|
Chris@0
|
376 $this->doInstall();
|
Chris@0
|
377
|
Chris@0
|
378 // Import new settings.php written by the installer.
|
Chris@0
|
379 $this->initSettings();
|
Chris@0
|
380
|
Chris@0
|
381 // Initialize the request and container post-install.
|
Chris@0
|
382 $container = $this->initKernel(\Drupal::request());
|
Chris@0
|
383
|
Chris@0
|
384 // Initialize and override certain configurations.
|
Chris@0
|
385 $this->initConfig($container);
|
Chris@0
|
386
|
Chris@0
|
387 // Collect modules to install.
|
Chris@0
|
388 $this->installModulesFromClassProperty($container);
|
Chris@0
|
389
|
Chris@0
|
390 // Restore the original batch.
|
Chris@0
|
391 $this->restoreBatch();
|
Chris@0
|
392
|
Chris@0
|
393 // Reset/rebuild everything.
|
Chris@0
|
394 $this->rebuildAll();
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * Preserve the original batch, and instantiate the test batch.
|
Chris@0
|
399 */
|
Chris@0
|
400 protected function setBatch() {
|
Chris@0
|
401 // When running tests through the Simpletest UI (vs. on the command line),
|
Chris@0
|
402 // Simpletest's batch conflicts with the installer's batch. Batch API does
|
Chris@0
|
403 // not support the concept of nested batches (in which the nested is not
|
Chris@0
|
404 // progressive), so we need to temporarily pretend there was no batch.
|
Chris@0
|
405 // Backup the currently running Simpletest batch.
|
Chris@0
|
406 $this->originalBatch = batch_get();
|
Chris@0
|
407
|
Chris@0
|
408 // Reset the static batch to remove Simpletest's batch operations.
|
Chris@0
|
409 $batch = &batch_get();
|
Chris@0
|
410 $batch = [];
|
Chris@0
|
411 }
|
Chris@0
|
412
|
Chris@0
|
413 /**
|
Chris@0
|
414 * Restore the original batch.
|
Chris@0
|
415 *
|
Chris@0
|
416 * @see ::setBatch
|
Chris@0
|
417 */
|
Chris@0
|
418 protected function restoreBatch() {
|
Chris@0
|
419 // Restore the original Simpletest batch.
|
Chris@0
|
420 $batch = &batch_get();
|
Chris@0
|
421 $batch = $this->originalBatch;
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@0
|
424 /**
|
Chris@0
|
425 * Queues custom translations to be written to settings.php.
|
Chris@0
|
426 *
|
Chris@0
|
427 * Use WebTestBase::writeCustomTranslations() to apply and write the queued
|
Chris@0
|
428 * translations.
|
Chris@0
|
429 *
|
Chris@0
|
430 * @param string $langcode
|
Chris@0
|
431 * The langcode to add translations for.
|
Chris@0
|
432 * @param array $values
|
Chris@0
|
433 * Array of values containing the untranslated string and its translation.
|
Chris@0
|
434 * For example:
|
Chris@0
|
435 * @code
|
Chris@0
|
436 * array(
|
Chris@0
|
437 * '' => array('Sunday' => 'domingo'),
|
Chris@0
|
438 * 'Long month name' => array('March' => 'marzo'),
|
Chris@0
|
439 * );
|
Chris@0
|
440 * @endcode
|
Chris@0
|
441 * Pass an empty array to remove all existing custom translations for the
|
Chris@0
|
442 * given $langcode.
|
Chris@0
|
443 */
|
Chris@0
|
444 protected function addCustomTranslations($langcode, array $values) {
|
Chris@0
|
445 // If $values is empty, then the test expects all custom translations to be
|
Chris@0
|
446 // cleared.
|
Chris@0
|
447 if (empty($values)) {
|
Chris@0
|
448 $this->customTranslations[$langcode] = [];
|
Chris@0
|
449 }
|
Chris@0
|
450 // Otherwise, $values are expected to be merged into previously passed
|
Chris@0
|
451 // values, while retaining keys that are not explicitly set.
|
Chris@0
|
452 else {
|
Chris@0
|
453 foreach ($values as $context => $translations) {
|
Chris@0
|
454 foreach ($translations as $original => $translation) {
|
Chris@0
|
455 $this->customTranslations[$langcode][$context][$original] = $translation;
|
Chris@0
|
456 }
|
Chris@0
|
457 }
|
Chris@0
|
458 }
|
Chris@0
|
459 }
|
Chris@0
|
460
|
Chris@0
|
461 /**
|
Chris@0
|
462 * Writes custom translations to the test site's settings.php.
|
Chris@0
|
463 *
|
Chris@0
|
464 * Use TestBase::addCustomTranslations() to queue custom translations before
|
Chris@0
|
465 * calling this method.
|
Chris@0
|
466 */
|
Chris@0
|
467 protected function writeCustomTranslations() {
|
Chris@0
|
468 $settings = [];
|
Chris@0
|
469 foreach ($this->customTranslations as $langcode => $values) {
|
Chris@0
|
470 $settings_key = 'locale_custom_strings_' . $langcode;
|
Chris@0
|
471
|
Chris@0
|
472 // Update in-memory settings directly.
|
Chris@0
|
473 $this->settingsSet($settings_key, $values);
|
Chris@0
|
474
|
Chris@0
|
475 $settings['settings'][$settings_key] = (object) [
|
Chris@0
|
476 'value' => $values,
|
Chris@0
|
477 'required' => TRUE,
|
Chris@0
|
478 ];
|
Chris@0
|
479 }
|
Chris@0
|
480 // Only rewrite settings if there are any translation changes to write.
|
Chris@0
|
481 if (!empty($settings)) {
|
Chris@0
|
482 $this->writeSettings($settings);
|
Chris@0
|
483 }
|
Chris@0
|
484 }
|
Chris@0
|
485
|
Chris@0
|
486 /**
|
Chris@0
|
487 * Cleans up after testing.
|
Chris@0
|
488 *
|
Chris@0
|
489 * Deletes created files and temporary files directory, deletes the tables
|
Chris@0
|
490 * created by setUp(), and resets the database prefix.
|
Chris@0
|
491 */
|
Chris@0
|
492 protected function tearDown() {
|
Chris@0
|
493 // Destroy the testing kernel.
|
Chris@0
|
494 if (isset($this->kernel)) {
|
Chris@0
|
495 $this->kernel->shutdown();
|
Chris@0
|
496 }
|
Chris@0
|
497 parent::tearDown();
|
Chris@0
|
498
|
Chris@0
|
499 // Ensure that the maximum meta refresh count is reset.
|
Chris@0
|
500 $this->maximumMetaRefreshCount = NULL;
|
Chris@0
|
501
|
Chris@0
|
502 // Ensure that internal logged in variable and cURL options are reset.
|
Chris@0
|
503 $this->loggedInUser = FALSE;
|
Chris@0
|
504 $this->additionalCurlOptions = [];
|
Chris@0
|
505
|
Chris@0
|
506 // Close the CURL handler and reset the cookies array used for upgrade
|
Chris@0
|
507 // testing so test classes containing multiple tests are not polluted.
|
Chris@0
|
508 $this->curlClose();
|
Chris@0
|
509 $this->curlCookies = [];
|
Chris@0
|
510 $this->cookies = [];
|
Chris@0
|
511 }
|
Chris@0
|
512
|
Chris@0
|
513 /**
|
Chris@0
|
514 * Initializes the cURL connection.
|
Chris@0
|
515 *
|
Chris@0
|
516 * If the simpletest_httpauth_credentials variable is set, this function will
|
Chris@0
|
517 * add HTTP authentication headers. This is necessary for testing sites that
|
Chris@0
|
518 * are protected by login credentials from public access.
|
Chris@0
|
519 * See the description of $curl_options for other options.
|
Chris@0
|
520 */
|
Chris@0
|
521 protected function curlInitialize() {
|
Chris@0
|
522 global $base_url;
|
Chris@0
|
523
|
Chris@0
|
524 if (!isset($this->curlHandle)) {
|
Chris@0
|
525 $this->curlHandle = curl_init();
|
Chris@0
|
526
|
Chris@0
|
527 // Some versions/configurations of cURL break on a NULL cookie jar, so
|
Chris@0
|
528 // supply a real file.
|
Chris@0
|
529 if (empty($this->cookieFile)) {
|
Chris@0
|
530 $this->cookieFile = $this->publicFilesDirectory . '/cookie.jar';
|
Chris@0
|
531 }
|
Chris@0
|
532
|
Chris@0
|
533 $curl_options = [
|
Chris@0
|
534 CURLOPT_COOKIEJAR => $this->cookieFile,
|
Chris@0
|
535 CURLOPT_URL => $base_url,
|
Chris@0
|
536 CURLOPT_FOLLOWLOCATION => FALSE,
|
Chris@0
|
537 CURLOPT_RETURNTRANSFER => TRUE,
|
Chris@0
|
538 // Required to make the tests run on HTTPS.
|
Chris@0
|
539 CURLOPT_SSL_VERIFYPEER => FALSE,
|
Chris@0
|
540 // Required to make the tests run on HTTPS.
|
Chris@0
|
541 CURLOPT_SSL_VERIFYHOST => FALSE,
|
Chris@0
|
542 CURLOPT_HEADERFUNCTION => [&$this, 'curlHeaderCallback'],
|
Chris@0
|
543 CURLOPT_USERAGENT => $this->databasePrefix,
|
Chris@0
|
544 // Disable support for the @ prefix for uploading files.
|
Chris@0
|
545 CURLOPT_SAFE_UPLOAD => TRUE,
|
Chris@0
|
546 ];
|
Chris@0
|
547 if (isset($this->httpAuthCredentials)) {
|
Chris@0
|
548 $curl_options[CURLOPT_HTTPAUTH] = $this->httpAuthMethod;
|
Chris@0
|
549 $curl_options[CURLOPT_USERPWD] = $this->httpAuthCredentials;
|
Chris@0
|
550 }
|
Chris@0
|
551 // curl_setopt_array() returns FALSE if any of the specified options
|
Chris@0
|
552 // cannot be set, and stops processing any further options.
|
Chris@0
|
553 $result = curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
|
Chris@0
|
554 if (!$result) {
|
Chris@0
|
555 throw new \UnexpectedValueException('One or more cURL options could not be set.');
|
Chris@0
|
556 }
|
Chris@0
|
557 }
|
Chris@0
|
558 // We set the user agent header on each request so as to use the current
|
Chris@0
|
559 // time and a new uniqid.
|
Chris@0
|
560 curl_setopt($this->curlHandle, CURLOPT_USERAGENT, drupal_generate_test_ua($this->databasePrefix));
|
Chris@0
|
561 }
|
Chris@0
|
562
|
Chris@0
|
563 /**
|
Chris@0
|
564 * Initializes and executes a cURL request.
|
Chris@0
|
565 *
|
Chris@0
|
566 * @param $curl_options
|
Chris@0
|
567 * An associative array of cURL options to set, where the keys are constants
|
Chris@0
|
568 * defined by the cURL library. For a list of valid options, see
|
Chris@0
|
569 * http://php.net/manual/function.curl-setopt.php
|
Chris@0
|
570 * @param $redirect
|
Chris@0
|
571 * FALSE if this is an initial request, TRUE if this request is the result
|
Chris@0
|
572 * of a redirect.
|
Chris@0
|
573 *
|
Chris@0
|
574 * @return
|
Chris@0
|
575 * The content returned from the call to curl_exec().
|
Chris@0
|
576 *
|
Chris@0
|
577 * @see curlInitialize()
|
Chris@0
|
578 */
|
Chris@0
|
579 protected function curlExec($curl_options, $redirect = FALSE) {
|
Chris@0
|
580 $this->curlInitialize();
|
Chris@0
|
581
|
Chris@0
|
582 if (!empty($curl_options[CURLOPT_URL])) {
|
Chris@0
|
583 // cURL incorrectly handles URLs with a fragment by including the
|
Chris@0
|
584 // fragment in the request to the server, causing some web servers
|
Chris@0
|
585 // to reject the request citing "400 - Bad Request". To prevent
|
Chris@0
|
586 // this, we strip the fragment from the request.
|
Chris@0
|
587 // TODO: Remove this for Drupal 8, since fixed in curl 7.20.0.
|
Chris@0
|
588 if (strpos($curl_options[CURLOPT_URL], '#')) {
|
Chris@0
|
589 $original_url = $curl_options[CURLOPT_URL];
|
Chris@0
|
590 $curl_options[CURLOPT_URL] = strtok($curl_options[CURLOPT_URL], '#');
|
Chris@0
|
591 }
|
Chris@0
|
592 }
|
Chris@0
|
593
|
Chris@0
|
594 $url = empty($curl_options[CURLOPT_URL]) ? curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL) : $curl_options[CURLOPT_URL];
|
Chris@0
|
595
|
Chris@0
|
596 if (!empty($curl_options[CURLOPT_POST])) {
|
Chris@0
|
597 // This is a fix for the Curl library to prevent Expect: 100-continue
|
Chris@0
|
598 // headers in POST requests, that may cause unexpected HTTP response
|
Chris@0
|
599 // codes from some webservers (like lighttpd that returns a 417 error
|
Chris@0
|
600 // code). It is done by setting an empty "Expect" header field that is
|
Chris@0
|
601 // not overwritten by Curl.
|
Chris@0
|
602 $curl_options[CURLOPT_HTTPHEADER][] = 'Expect:';
|
Chris@0
|
603 }
|
Chris@0
|
604
|
Chris@0
|
605 $cookies = [];
|
Chris@0
|
606 if (!empty($this->curlCookies)) {
|
Chris@0
|
607 $cookies = $this->curlCookies;
|
Chris@0
|
608 }
|
Chris@0
|
609
|
Chris@0
|
610 foreach ($this->extractCookiesFromRequest(\Drupal::request()) as $cookie_name => $values) {
|
Chris@0
|
611 foreach ($values as $value) {
|
Chris@0
|
612 $cookies[] = $cookie_name . '=' . $value;
|
Chris@0
|
613 }
|
Chris@0
|
614 }
|
Chris@0
|
615
|
Chris@0
|
616 // Merge additional cookies in.
|
Chris@0
|
617 if (!empty($cookies)) {
|
Chris@0
|
618 $curl_options += [
|
Chris@0
|
619 CURLOPT_COOKIE => '',
|
Chris@0
|
620 ];
|
Chris@0
|
621 // Ensure any existing cookie data string ends with the correct separator.
|
Chris@0
|
622 if (!empty($curl_options[CURLOPT_COOKIE])) {
|
Chris@0
|
623 $curl_options[CURLOPT_COOKIE] = rtrim($curl_options[CURLOPT_COOKIE], '; ') . '; ';
|
Chris@0
|
624 }
|
Chris@0
|
625 $curl_options[CURLOPT_COOKIE] .= implode('; ', $cookies) . ';';
|
Chris@0
|
626 }
|
Chris@0
|
627
|
Chris@0
|
628 curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
|
Chris@0
|
629
|
Chris@0
|
630 if (!$redirect) {
|
Chris@0
|
631 // Reset headers, the session ID and the redirect counter.
|
Chris@0
|
632 $this->sessionId = NULL;
|
Chris@0
|
633 $this->headers = [];
|
Chris@0
|
634 $this->redirectCount = 0;
|
Chris@0
|
635 }
|
Chris@0
|
636
|
Chris@0
|
637 $content = curl_exec($this->curlHandle);
|
Chris@0
|
638 $status = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
|
Chris@0
|
639
|
Chris@0
|
640 // cURL incorrectly handles URLs with fragments, so instead of
|
Chris@0
|
641 // letting cURL handle redirects we take of them ourselves to
|
Chris@0
|
642 // to prevent fragments being sent to the web server as part
|
Chris@0
|
643 // of the request.
|
Chris@0
|
644 // TODO: Remove this for Drupal 8, since fixed in curl 7.20.0.
|
Chris@0
|
645 if (in_array($status, [300, 301, 302, 303, 305, 307]) && $this->redirectCount < $this->maximumRedirects) {
|
Chris@0
|
646 if ($this->drupalGetHeader('location')) {
|
Chris@0
|
647 $this->redirectCount++;
|
Chris@0
|
648 $curl_options = [];
|
Chris@0
|
649 $curl_options[CURLOPT_URL] = $this->drupalGetHeader('location');
|
Chris@0
|
650 $curl_options[CURLOPT_HTTPGET] = TRUE;
|
Chris@0
|
651 return $this->curlExec($curl_options, TRUE);
|
Chris@0
|
652 }
|
Chris@0
|
653 }
|
Chris@0
|
654
|
Chris@0
|
655 $this->setRawContent($content);
|
Chris@0
|
656 $this->url = isset($original_url) ? $original_url : curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL);
|
Chris@0
|
657
|
Chris@0
|
658 $message_vars = [
|
Chris@0
|
659 '@method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'),
|
Chris@0
|
660 '@url' => isset($original_url) ? $original_url : $url,
|
Chris@0
|
661 '@status' => $status,
|
Chris@0
|
662 '@length' => format_size(strlen($this->getRawContent()))
|
Chris@0
|
663 ];
|
Chris@0
|
664 $message = SafeMarkup::format('@method @url returned @status (@length).', $message_vars);
|
Chris@0
|
665 $this->assertTrue($this->getRawContent() !== FALSE, $message, 'Browser');
|
Chris@0
|
666 return $this->getRawContent();
|
Chris@0
|
667 }
|
Chris@0
|
668
|
Chris@0
|
669 /**
|
Chris@0
|
670 * Reads headers and registers errors received from the tested site.
|
Chris@0
|
671 *
|
Chris@0
|
672 * @param $curlHandler
|
Chris@0
|
673 * The cURL handler.
|
Chris@0
|
674 * @param $header
|
Chris@0
|
675 * An header.
|
Chris@0
|
676 *
|
Chris@0
|
677 * @see _drupal_log_error()
|
Chris@0
|
678 */
|
Chris@0
|
679 protected function curlHeaderCallback($curlHandler, $header) {
|
Chris@0
|
680 // Header fields can be extended over multiple lines by preceding each
|
Chris@0
|
681 // extra line with at least one SP or HT. They should be joined on receive.
|
Chris@0
|
682 // Details are in RFC2616 section 4.
|
Chris@0
|
683 if ($header[0] == ' ' || $header[0] == "\t") {
|
Chris@0
|
684 // Normalize whitespace between chucks.
|
Chris@0
|
685 $this->headers[] = array_pop($this->headers) . ' ' . trim($header);
|
Chris@0
|
686 }
|
Chris@0
|
687 else {
|
Chris@0
|
688 $this->headers[] = $header;
|
Chris@0
|
689 }
|
Chris@0
|
690
|
Chris@0
|
691 // Errors are being sent via X-Drupal-Assertion-* headers,
|
Chris@0
|
692 // generated by _drupal_log_error() in the exact form required
|
Chris@0
|
693 // by \Drupal\simpletest\WebTestBase::error().
|
Chris@0
|
694 if (preg_match('/^X-Drupal-Assertion-[0-9]+: (.*)$/', $header, $matches)) {
|
Chris@0
|
695 // Call \Drupal\simpletest\WebTestBase::error() with the parameters from
|
Chris@0
|
696 // the header.
|
Chris@0
|
697 call_user_func_array([&$this, 'error'], unserialize(urldecode($matches[1])));
|
Chris@0
|
698 }
|
Chris@0
|
699
|
Chris@0
|
700 // Save cookies.
|
Chris@0
|
701 if (preg_match('/^Set-Cookie: ([^=]+)=(.+)/', $header, $matches)) {
|
Chris@0
|
702 $name = $matches[1];
|
Chris@0
|
703 $parts = array_map('trim', explode(';', $matches[2]));
|
Chris@0
|
704 $value = array_shift($parts);
|
Chris@0
|
705 $this->cookies[$name] = ['value' => $value, 'secure' => in_array('secure', $parts)];
|
Chris@0
|
706 if ($name === $this->getSessionName()) {
|
Chris@0
|
707 if ($value != 'deleted') {
|
Chris@0
|
708 $this->sessionId = $value;
|
Chris@0
|
709 }
|
Chris@0
|
710 else {
|
Chris@0
|
711 $this->sessionId = NULL;
|
Chris@0
|
712 }
|
Chris@0
|
713 }
|
Chris@0
|
714 }
|
Chris@0
|
715
|
Chris@0
|
716 // This is required by cURL.
|
Chris@0
|
717 return strlen($header);
|
Chris@0
|
718 }
|
Chris@0
|
719
|
Chris@0
|
720 /**
|
Chris@0
|
721 * Close the cURL handler and unset the handler.
|
Chris@0
|
722 */
|
Chris@0
|
723 protected function curlClose() {
|
Chris@0
|
724 if (isset($this->curlHandle)) {
|
Chris@0
|
725 curl_close($this->curlHandle);
|
Chris@0
|
726 unset($this->curlHandle);
|
Chris@0
|
727 }
|
Chris@0
|
728 }
|
Chris@0
|
729
|
Chris@0
|
730 /**
|
Chris@0
|
731 * Returns whether the test is being executed from within a test site.
|
Chris@0
|
732 *
|
Chris@0
|
733 * Mainly used by recursive tests (i.e. to test the testing framework).
|
Chris@0
|
734 *
|
Chris@0
|
735 * @return bool
|
Chris@0
|
736 * TRUE if this test was instantiated in a request within the test site,
|
Chris@0
|
737 * FALSE otherwise.
|
Chris@0
|
738 *
|
Chris@0
|
739 * @see \Drupal\Core\DrupalKernel::bootConfiguration()
|
Chris@0
|
740 */
|
Chris@0
|
741 protected function isInChildSite() {
|
Chris@0
|
742 return DRUPAL_TEST_IN_CHILD_SITE;
|
Chris@0
|
743 }
|
Chris@0
|
744
|
Chris@0
|
745 /**
|
Chris@0
|
746 * Retrieves a Drupal path or an absolute path.
|
Chris@0
|
747 *
|
Chris@0
|
748 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
749 * Drupal path or URL to load into internal browser
|
Chris@0
|
750 * @param $options
|
Chris@0
|
751 * Options to be forwarded to the url generator.
|
Chris@0
|
752 * @param $headers
|
Chris@0
|
753 * An array containing additional HTTP request headers, each formatted as
|
Chris@0
|
754 * "name: value".
|
Chris@0
|
755 *
|
Chris@0
|
756 * @return string
|
Chris@0
|
757 * The retrieved HTML string, also available as $this->getRawContent()
|
Chris@0
|
758 */
|
Chris@0
|
759 protected function drupalGet($path, array $options = [], array $headers = []) {
|
Chris@0
|
760 // We re-using a CURL connection here. If that connection still has certain
|
Chris@0
|
761 // options set, it might change the GET into a POST. Make sure we clear out
|
Chris@0
|
762 // previous options.
|
Chris@0
|
763 $out = $this->curlExec([CURLOPT_HTTPGET => TRUE, CURLOPT_URL => $this->buildUrl($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => $headers]);
|
Chris@0
|
764 // Ensure that any changes to variables in the other thread are picked up.
|
Chris@0
|
765 $this->refreshVariables();
|
Chris@0
|
766
|
Chris@0
|
767 // Replace original page output with new output from redirected page(s).
|
Chris@0
|
768 if ($new = $this->checkForMetaRefresh()) {
|
Chris@0
|
769 $out = $new;
|
Chris@0
|
770 // We are finished with all meta refresh redirects, so reset the counter.
|
Chris@0
|
771 $this->metaRefreshCount = 0;
|
Chris@0
|
772 }
|
Chris@0
|
773
|
Chris@0
|
774 if ($path instanceof Url) {
|
Chris@0
|
775 $path = $path->setAbsolute()->toString(TRUE)->getGeneratedUrl();
|
Chris@0
|
776 }
|
Chris@0
|
777
|
Chris@0
|
778 $verbose = 'GET request to: ' . $path .
|
Chris@0
|
779 '<hr />Ending URL: ' . $this->getUrl();
|
Chris@0
|
780 if ($this->dumpHeaders) {
|
Chris@0
|
781 $verbose .= '<hr />Headers: <pre>' . Html::escape(var_export(array_map('trim', $this->headers), TRUE)) . '</pre>';
|
Chris@0
|
782 }
|
Chris@0
|
783 $verbose .= '<hr />' . $out;
|
Chris@0
|
784
|
Chris@0
|
785 $this->verbose($verbose);
|
Chris@0
|
786 return $out;
|
Chris@0
|
787 }
|
Chris@0
|
788
|
Chris@0
|
789 /**
|
Chris@0
|
790 * Retrieves a Drupal path or an absolute path and JSON decodes the result.
|
Chris@0
|
791 *
|
Chris@0
|
792 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
793 * Drupal path or URL to request AJAX from.
|
Chris@0
|
794 * @param array $options
|
Chris@0
|
795 * Array of URL options.
|
Chris@0
|
796 * @param array $headers
|
Chris@0
|
797 * Array of headers. Eg array('Accept: application/vnd.drupal-ajax').
|
Chris@0
|
798 *
|
Chris@0
|
799 * @return array
|
Chris@0
|
800 * Decoded json.
|
Chris@0
|
801 */
|
Chris@0
|
802 protected function drupalGetJSON($path, array $options = [], array $headers = []) {
|
Chris@0
|
803 return Json::decode($this->drupalGetWithFormat($path, 'json', $options, $headers));
|
Chris@0
|
804 }
|
Chris@0
|
805
|
Chris@0
|
806 /**
|
Chris@0
|
807 * Retrieves a Drupal path or an absolute path for a given format.
|
Chris@0
|
808 *
|
Chris@0
|
809 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
810 * Drupal path or URL to request given format from.
|
Chris@0
|
811 * @param string $format
|
Chris@0
|
812 * The wanted request format.
|
Chris@0
|
813 * @param array $options
|
Chris@0
|
814 * Array of URL options.
|
Chris@0
|
815 * @param array $headers
|
Chris@0
|
816 * Array of headers.
|
Chris@0
|
817 *
|
Chris@0
|
818 * @return mixed
|
Chris@0
|
819 * The result of the request.
|
Chris@0
|
820 */
|
Chris@0
|
821 protected function drupalGetWithFormat($path, $format, array $options = [], array $headers = []) {
|
Chris@0
|
822 $options += ['query' => ['_format' => $format]];
|
Chris@0
|
823 return $this->drupalGet($path, $options, $headers);
|
Chris@0
|
824 }
|
Chris@0
|
825
|
Chris@0
|
826 /**
|
Chris@0
|
827 * Requests a path or URL in drupal_ajax format and JSON-decodes the response.
|
Chris@0
|
828 *
|
Chris@0
|
829 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
830 * Drupal path or URL to request from.
|
Chris@0
|
831 * @param array $options
|
Chris@0
|
832 * Array of URL options.
|
Chris@0
|
833 * @param array $headers
|
Chris@0
|
834 * Array of headers.
|
Chris@0
|
835 *
|
Chris@0
|
836 * @return array
|
Chris@0
|
837 * Decoded JSON.
|
Chris@0
|
838 */
|
Chris@0
|
839 protected function drupalGetAjax($path, array $options = [], array $headers = []) {
|
Chris@0
|
840 if (!isset($options['query'][MainContentViewSubscriber::WRAPPER_FORMAT])) {
|
Chris@0
|
841 $options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax';
|
Chris@0
|
842 }
|
Chris@0
|
843 return Json::decode($this->drupalGetXHR($path, $options, $headers));
|
Chris@0
|
844 }
|
Chris@0
|
845
|
Chris@0
|
846 /**
|
Chris@0
|
847 * Requests a Drupal path or an absolute path as if it is a XMLHttpRequest.
|
Chris@0
|
848 *
|
Chris@0
|
849 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
850 * Drupal path or URL to request from.
|
Chris@0
|
851 * @param array $options
|
Chris@0
|
852 * Array of URL options.
|
Chris@0
|
853 * @param array $headers
|
Chris@0
|
854 * Array of headers.
|
Chris@0
|
855 *
|
Chris@0
|
856 * @return string
|
Chris@0
|
857 * The retrieved content.
|
Chris@0
|
858 */
|
Chris@0
|
859 protected function drupalGetXHR($path, array $options = [], array $headers = []) {
|
Chris@0
|
860 $headers[] = 'X-Requested-With: XMLHttpRequest';
|
Chris@0
|
861 return $this->drupalGet($path, $options, $headers);
|
Chris@0
|
862 }
|
Chris@0
|
863
|
Chris@0
|
864 /**
|
Chris@0
|
865 * Executes a form submission.
|
Chris@0
|
866 *
|
Chris@0
|
867 * It will be done as usual POST request with SimpleBrowser.
|
Chris@0
|
868 *
|
Chris@0
|
869 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
870 * Location of the post form. Either a Drupal path or an absolute path or
|
Chris@0
|
871 * NULL to post to the current page. For multi-stage forms you can set the
|
Chris@0
|
872 * path to NULL and have it post to the last received page. Example:
|
Chris@0
|
873 *
|
Chris@0
|
874 * @code
|
Chris@0
|
875 * // First step in form.
|
Chris@0
|
876 * $edit = array(...);
|
Chris@0
|
877 * $this->drupalPostForm('some_url', $edit, t('Save'));
|
Chris@0
|
878 *
|
Chris@0
|
879 * // Second step in form.
|
Chris@0
|
880 * $edit = array(...);
|
Chris@0
|
881 * $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
882 * @endcode
|
Chris@0
|
883 * @param $edit
|
Chris@0
|
884 * Field data in an associative array. Changes the current input fields
|
Chris@0
|
885 * (where possible) to the values indicated.
|
Chris@0
|
886 *
|
Chris@0
|
887 * When working with form tests, the keys for an $edit element should match
|
Chris@0
|
888 * the 'name' parameter of the HTML of the form. For example, the 'body'
|
Chris@0
|
889 * field for a node has the following HTML:
|
Chris@0
|
890 * @code
|
Chris@0
|
891 * <textarea id="edit-body-und-0-value" class="text-full form-textarea
|
Chris@0
|
892 * resize-vertical" placeholder="" cols="60" rows="9"
|
Chris@0
|
893 * name="body[0][value]"></textarea>
|
Chris@0
|
894 * @endcode
|
Chris@0
|
895 * When testing this field using an $edit parameter, the code becomes:
|
Chris@0
|
896 * @code
|
Chris@0
|
897 * $edit["body[0][value]"] = 'My test value';
|
Chris@0
|
898 * @endcode
|
Chris@0
|
899 *
|
Chris@0
|
900 * A checkbox can be set to TRUE to be checked and should be set to FALSE to
|
Chris@0
|
901 * be unchecked. Multiple select fields can be tested using 'name[]' and
|
Chris@0
|
902 * setting each of the desired values in an array:
|
Chris@0
|
903 * @code
|
Chris@0
|
904 * $edit = array();
|
Chris@0
|
905 * $edit['name[]'] = array('value1', 'value2');
|
Chris@0
|
906 * @endcode
|
Chris@0
|
907 * @param $submit
|
Chris@0
|
908 * Value of the submit button whose click is to be emulated. For example,
|
Chris@0
|
909 * t('Save'). The processing of the request depends on this value. For
|
Chris@0
|
910 * example, a form may have one button with the value t('Save') and another
|
Chris@0
|
911 * button with the value t('Delete'), and execute different code depending
|
Chris@0
|
912 * on which one is clicked.
|
Chris@0
|
913 *
|
Chris@0
|
914 * This function can also be called to emulate an Ajax submission. In this
|
Chris@0
|
915 * case, this value needs to be an array with the following keys:
|
Chris@0
|
916 * - path: A path to submit the form values to for Ajax-specific processing.
|
Chris@0
|
917 * - triggering_element: If the value for the 'path' key is a generic Ajax
|
Chris@0
|
918 * processing path, this needs to be set to the name of the element. If
|
Chris@0
|
919 * the name doesn't identify the element uniquely, then this should
|
Chris@0
|
920 * instead be an array with a single key/value pair, corresponding to the
|
Chris@0
|
921 * element name and value. The \Drupal\Core\Form\FormAjaxResponseBuilder
|
Chris@0
|
922 * uses this to find the #ajax information for the element, including
|
Chris@0
|
923 * which specific callback to use for processing the request.
|
Chris@0
|
924 *
|
Chris@0
|
925 * This can also be set to NULL in order to emulate an Internet Explorer
|
Chris@0
|
926 * submission of a form with a single text field, and pressing ENTER in that
|
Chris@0
|
927 * textfield: under these conditions, no button information is added to the
|
Chris@0
|
928 * POST data.
|
Chris@0
|
929 * @param $options
|
Chris@0
|
930 * Options to be forwarded to the url generator.
|
Chris@0
|
931 * @param $headers
|
Chris@0
|
932 * An array containing additional HTTP request headers, each formatted as
|
Chris@0
|
933 * "name: value".
|
Chris@0
|
934 * @param $form_html_id
|
Chris@0
|
935 * (optional) HTML ID of the form to be submitted. On some pages
|
Chris@0
|
936 * there are many identical forms, so just using the value of the submit
|
Chris@0
|
937 * button is not enough. For example: 'trigger-node-presave-assign-form'.
|
Chris@0
|
938 * Note that this is not the Drupal $form_id, but rather the HTML ID of the
|
Chris@0
|
939 * form, which is typically the same thing but with hyphens replacing the
|
Chris@0
|
940 * underscores.
|
Chris@0
|
941 * @param $extra_post
|
Chris@0
|
942 * (optional) A string of additional data to append to the POST submission.
|
Chris@0
|
943 * This can be used to add POST data for which there are no HTML fields, as
|
Chris@0
|
944 * is done by drupalPostAjaxForm(). This string is literally appended to the
|
Chris@0
|
945 * POST data, so it must already be urlencoded and contain a leading "&"
|
Chris@0
|
946 * (e.g., "&extra_var1=hello+world&extra_var2=you%26me").
|
Chris@0
|
947 */
|
Chris@0
|
948 protected function drupalPostForm($path, $edit, $submit, array $options = [], array $headers = [], $form_html_id = NULL, $extra_post = NULL) {
|
Chris@0
|
949 if (is_object($submit)) {
|
Chris@0
|
950 // Cast MarkupInterface objects to string.
|
Chris@0
|
951 $submit = (string) $submit;
|
Chris@0
|
952 }
|
Chris@0
|
953 if (is_array($edit)) {
|
Chris@0
|
954 $edit = $this->castSafeStrings($edit);
|
Chris@0
|
955 }
|
Chris@0
|
956
|
Chris@0
|
957 $submit_matches = FALSE;
|
Chris@0
|
958 $ajax = is_array($submit);
|
Chris@0
|
959 if (isset($path)) {
|
Chris@0
|
960 $this->drupalGet($path, $options);
|
Chris@0
|
961 }
|
Chris@0
|
962
|
Chris@0
|
963 if ($this->parse()) {
|
Chris@0
|
964 $edit_save = $edit;
|
Chris@0
|
965 // Let's iterate over all the forms.
|
Chris@0
|
966 $xpath = "//form";
|
Chris@0
|
967 if (!empty($form_html_id)) {
|
Chris@0
|
968 $xpath .= "[@id='" . $form_html_id . "']";
|
Chris@0
|
969 }
|
Chris@0
|
970 $forms = $this->xpath($xpath);
|
Chris@0
|
971 foreach ($forms as $form) {
|
Chris@0
|
972 // We try to set the fields of this form as specified in $edit.
|
Chris@0
|
973 $edit = $edit_save;
|
Chris@0
|
974 $post = [];
|
Chris@0
|
975 $upload = [];
|
Chris@0
|
976 $submit_matches = $this->handleForm($post, $edit, $upload, $ajax ? NULL : $submit, $form);
|
Chris@0
|
977 $action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : $this->getUrl();
|
Chris@0
|
978 if ($ajax) {
|
Chris@0
|
979 if (empty($submit['path'])) {
|
Chris@0
|
980 throw new \Exception('No #ajax path specified.');
|
Chris@0
|
981 }
|
Chris@0
|
982 $action = $this->getAbsoluteUrl($submit['path']);
|
Chris@0
|
983 // Ajax callbacks verify the triggering element if necessary, so while
|
Chris@0
|
984 // we may eventually want extra code that verifies it in the
|
Chris@0
|
985 // handleForm() function, it's not currently a requirement.
|
Chris@0
|
986 $submit_matches = TRUE;
|
Chris@0
|
987 }
|
Chris@0
|
988 // We post only if we managed to handle every field in edit and the
|
Chris@0
|
989 // submit button matches.
|
Chris@0
|
990 if (!$edit && ($submit_matches || !isset($submit))) {
|
Chris@0
|
991 $post_array = $post;
|
Chris@0
|
992 if ($upload) {
|
Chris@0
|
993 foreach ($upload as $key => $file) {
|
Chris@0
|
994 if (is_array($file) && count($file)) {
|
Chris@0
|
995 // There seems to be no way via php's API to cURL to upload
|
Chris@0
|
996 // several files with the same post field name. However, Drupal
|
Chris@0
|
997 // still sees array-index syntax in a similar way.
|
Chris@0
|
998 for ($i = 0; $i < count($file); $i++) {
|
Chris@0
|
999 $postfield = str_replace('[]', '', $key) . '[' . $i . ']';
|
Chris@0
|
1000 $file_path = $this->container->get('file_system')->realpath($file[$i]);
|
Chris@0
|
1001 $post[$postfield] = curl_file_create($file_path);
|
Chris@0
|
1002 }
|
Chris@0
|
1003 }
|
Chris@0
|
1004 else {
|
Chris@0
|
1005 $file = $this->container->get('file_system')->realpath($file);
|
Chris@0
|
1006 if ($file && is_file($file)) {
|
Chris@0
|
1007 $post[$key] = curl_file_create($file);
|
Chris@0
|
1008 }
|
Chris@0
|
1009 }
|
Chris@0
|
1010 }
|
Chris@0
|
1011 }
|
Chris@0
|
1012 else {
|
Chris@0
|
1013 $post = $this->serializePostValues($post) . $extra_post;
|
Chris@0
|
1014 }
|
Chris@0
|
1015 $out = $this->curlExec([CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post, CURLOPT_HTTPHEADER => $headers]);
|
Chris@0
|
1016 // Ensure that any changes to variables in the other thread are picked
|
Chris@0
|
1017 // up.
|
Chris@0
|
1018 $this->refreshVariables();
|
Chris@0
|
1019
|
Chris@0
|
1020 // Replace original page output with new output from redirected
|
Chris@0
|
1021 // page(s).
|
Chris@0
|
1022 if ($new = $this->checkForMetaRefresh()) {
|
Chris@0
|
1023 $out = $new;
|
Chris@0
|
1024 }
|
Chris@0
|
1025
|
Chris@0
|
1026 if ($path instanceof Url) {
|
Chris@0
|
1027 $path = $path->toString();
|
Chris@0
|
1028 }
|
Chris@0
|
1029 $verbose = 'POST request to: ' . $path;
|
Chris@0
|
1030 $verbose .= '<hr />Ending URL: ' . $this->getUrl();
|
Chris@0
|
1031 if ($this->dumpHeaders) {
|
Chris@0
|
1032 $verbose .= '<hr />Headers: <pre>' . Html::escape(var_export(array_map('trim', $this->headers), TRUE)) . '</pre>';
|
Chris@0
|
1033 }
|
Chris@0
|
1034 $verbose .= '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE);
|
Chris@0
|
1035 $verbose .= '<hr />' . $out;
|
Chris@0
|
1036
|
Chris@0
|
1037 $this->verbose($verbose);
|
Chris@0
|
1038 return $out;
|
Chris@0
|
1039 }
|
Chris@0
|
1040 }
|
Chris@0
|
1041 // We have not found a form which contained all fields of $edit.
|
Chris@0
|
1042 foreach ($edit as $name => $value) {
|
Chris@0
|
1043 $this->fail(SafeMarkup::format('Failed to set field @name to @value', ['@name' => $name, '@value' => $value]));
|
Chris@0
|
1044 }
|
Chris@0
|
1045 if (!$ajax && isset($submit)) {
|
Chris@0
|
1046 $this->assertTrue($submit_matches, format_string('Found the @submit button', ['@submit' => $submit]));
|
Chris@0
|
1047 }
|
Chris@0
|
1048 $this->fail(format_string('Found the requested form fields at @path', ['@path' => ($path instanceof Url) ? $path->toString() : $path]));
|
Chris@0
|
1049 }
|
Chris@0
|
1050 }
|
Chris@0
|
1051
|
Chris@0
|
1052 /**
|
Chris@0
|
1053 * Executes an Ajax form submission.
|
Chris@0
|
1054 *
|
Chris@0
|
1055 * This executes a POST as ajax.js does. The returned JSON data is used to
|
Chris@0
|
1056 * update $this->content via drupalProcessAjaxResponse(). It also returns
|
Chris@0
|
1057 * the array of AJAX commands received.
|
Chris@0
|
1058 *
|
Chris@0
|
1059 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
1060 * Location of the form containing the Ajax enabled element to test. Can be
|
Chris@0
|
1061 * either a Drupal path or an absolute path or NULL to use the current page.
|
Chris@0
|
1062 * @param $edit
|
Chris@0
|
1063 * Field data in an associative array. Changes the current input fields
|
Chris@0
|
1064 * (where possible) to the values indicated.
|
Chris@0
|
1065 * @param $triggering_element
|
Chris@0
|
1066 * The name of the form element that is responsible for triggering the Ajax
|
Chris@0
|
1067 * functionality to test. May be a string or, if the triggering element is
|
Chris@0
|
1068 * a button, an associative array where the key is the name of the button
|
Chris@0
|
1069 * and the value is the button label. i.e.) array('op' => t('Refresh')).
|
Chris@0
|
1070 * @param $ajax_path
|
Chris@0
|
1071 * (optional) Override the path set by the Ajax settings of the triggering
|
Chris@0
|
1072 * element.
|
Chris@0
|
1073 * @param $options
|
Chris@0
|
1074 * (optional) Options to be forwarded to the url generator.
|
Chris@0
|
1075 * @param $headers
|
Chris@0
|
1076 * (optional) An array containing additional HTTP request headers, each
|
Chris@0
|
1077 * formatted as "name: value". Forwarded to drupalPostForm().
|
Chris@0
|
1078 * @param $form_html_id
|
Chris@0
|
1079 * (optional) HTML ID of the form to be submitted, use when there is more
|
Chris@0
|
1080 * than one identical form on the same page and the value of the triggering
|
Chris@0
|
1081 * element is not enough to identify the form. Note this is not the Drupal
|
Chris@0
|
1082 * ID of the form but rather the HTML ID of the form.
|
Chris@0
|
1083 * @param $ajax_settings
|
Chris@0
|
1084 * (optional) An array of Ajax settings which if specified will be used in
|
Chris@0
|
1085 * place of the Ajax settings of the triggering element.
|
Chris@0
|
1086 *
|
Chris@0
|
1087 * @return
|
Chris@0
|
1088 * An array of Ajax commands.
|
Chris@0
|
1089 *
|
Chris@0
|
1090 * @see drupalPostForm()
|
Chris@0
|
1091 * @see drupalProcessAjaxResponse()
|
Chris@0
|
1092 * @see ajax.js
|
Chris@0
|
1093 */
|
Chris@0
|
1094 protected function drupalPostAjaxForm($path, $edit, $triggering_element, $ajax_path = NULL, array $options = [], array $headers = [], $form_html_id = NULL, $ajax_settings = NULL) {
|
Chris@0
|
1095
|
Chris@0
|
1096 // Get the content of the initial page prior to calling drupalPostForm(),
|
Chris@0
|
1097 // since drupalPostForm() replaces $this->content.
|
Chris@0
|
1098 if (isset($path)) {
|
Chris@0
|
1099 // Avoid sending the wrapper query argument to drupalGet so we can fetch
|
Chris@0
|
1100 // the form and populate the internal WebTest values.
|
Chris@0
|
1101 $get_options = $options;
|
Chris@0
|
1102 unset($get_options['query'][MainContentViewSubscriber::WRAPPER_FORMAT]);
|
Chris@0
|
1103 $this->drupalGet($path, $get_options);
|
Chris@0
|
1104 }
|
Chris@0
|
1105 $content = $this->content;
|
Chris@0
|
1106 $drupal_settings = $this->drupalSettings;
|
Chris@0
|
1107
|
Chris@0
|
1108 // Provide a default value for the wrapper envelope.
|
Chris@0
|
1109 $options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] =
|
Chris@0
|
1110 isset($options['query'][MainContentViewSubscriber::WRAPPER_FORMAT]) ?
|
Chris@0
|
1111 $options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] :
|
Chris@0
|
1112 'drupal_ajax';
|
Chris@0
|
1113
|
Chris@0
|
1114 // Get the Ajax settings bound to the triggering element.
|
Chris@0
|
1115 if (!isset($ajax_settings)) {
|
Chris@0
|
1116 if (is_array($triggering_element)) {
|
Chris@0
|
1117 $xpath = '//*[@name="' . key($triggering_element) . '" and @value="' . current($triggering_element) . '"]';
|
Chris@0
|
1118 }
|
Chris@0
|
1119 else {
|
Chris@0
|
1120 $xpath = '//*[@name="' . $triggering_element . '"]';
|
Chris@0
|
1121 }
|
Chris@0
|
1122 if (isset($form_html_id)) {
|
Chris@0
|
1123 $xpath = '//form[@id="' . $form_html_id . '"]' . $xpath;
|
Chris@0
|
1124 }
|
Chris@0
|
1125 $element = $this->xpath($xpath);
|
Chris@0
|
1126 $element_id = (string) $element[0]['id'];
|
Chris@0
|
1127 $ajax_settings = $drupal_settings['ajax'][$element_id];
|
Chris@0
|
1128 }
|
Chris@0
|
1129
|
Chris@0
|
1130 // Add extra information to the POST data as ajax.js does.
|
Chris@0
|
1131 $extra_post = [];
|
Chris@0
|
1132 if (isset($ajax_settings['submit'])) {
|
Chris@0
|
1133 foreach ($ajax_settings['submit'] as $key => $value) {
|
Chris@0
|
1134 $extra_post[$key] = $value;
|
Chris@0
|
1135 }
|
Chris@0
|
1136 }
|
Chris@0
|
1137 $extra_post[AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER] = 1;
|
Chris@0
|
1138 $extra_post += $this->getAjaxPageStatePostData();
|
Chris@0
|
1139 // Now serialize all the $extra_post values, and prepend it with an '&'.
|
Chris@0
|
1140 $extra_post = '&' . $this->serializePostValues($extra_post);
|
Chris@0
|
1141
|
Chris@0
|
1142 // Unless a particular path is specified, use the one specified by the
|
Chris@0
|
1143 // Ajax settings.
|
Chris@0
|
1144 if (!isset($ajax_path)) {
|
Chris@0
|
1145 if (isset($ajax_settings['url'])) {
|
Chris@0
|
1146 // In order to allow to set for example the wrapper envelope query
|
Chris@0
|
1147 // parameter we need to get the system path again.
|
Chris@0
|
1148 $parsed_url = UrlHelper::parse($ajax_settings['url']);
|
Chris@0
|
1149 $options['query'] = $parsed_url['query'] + $options['query'];
|
Chris@0
|
1150 $options += ['fragment' => $parsed_url['fragment']];
|
Chris@0
|
1151
|
Chris@0
|
1152 // We know that $parsed_url['path'] is already with the base path
|
Chris@0
|
1153 // attached.
|
Chris@0
|
1154 $ajax_path = preg_replace(
|
Chris@0
|
1155 '/^' . preg_quote(base_path(), '/') . '/',
|
Chris@0
|
1156 '',
|
Chris@0
|
1157 $parsed_url['path']
|
Chris@0
|
1158 );
|
Chris@0
|
1159 }
|
Chris@0
|
1160 }
|
Chris@0
|
1161
|
Chris@0
|
1162 if (empty($ajax_path)) {
|
Chris@0
|
1163 throw new \Exception('No #ajax path specified.');
|
Chris@0
|
1164 }
|
Chris@0
|
1165
|
Chris@0
|
1166 $ajax_path = $this->container->get('unrouted_url_assembler')->assemble('base://' . $ajax_path, $options);
|
Chris@0
|
1167
|
Chris@0
|
1168 // Submit the POST request.
|
Chris@0
|
1169 $return = Json::decode($this->drupalPostForm(NULL, $edit, ['path' => $ajax_path, 'triggering_element' => $triggering_element], $options, $headers, $form_html_id, $extra_post));
|
Chris@0
|
1170 if ($this->assertAjaxHeader) {
|
Chris@0
|
1171 $this->assertIdentical($this->drupalGetHeader('X-Drupal-Ajax-Token'), '1', 'Ajax response header found.');
|
Chris@0
|
1172 }
|
Chris@0
|
1173
|
Chris@0
|
1174 // Change the page content by applying the returned commands.
|
Chris@0
|
1175 if (!empty($ajax_settings) && !empty($return)) {
|
Chris@0
|
1176 $this->drupalProcessAjaxResponse($content, $return, $ajax_settings, $drupal_settings);
|
Chris@0
|
1177 }
|
Chris@0
|
1178
|
Chris@0
|
1179 $verbose = 'AJAX POST request to: ' . $path;
|
Chris@0
|
1180 $verbose .= '<br />AJAX controller path: ' . $ajax_path;
|
Chris@0
|
1181 $verbose .= '<hr />Ending URL: ' . $this->getUrl();
|
Chris@0
|
1182 $verbose .= '<hr />' . $this->content;
|
Chris@0
|
1183
|
Chris@0
|
1184 $this->verbose($verbose);
|
Chris@0
|
1185
|
Chris@0
|
1186 return $return;
|
Chris@0
|
1187 }
|
Chris@0
|
1188
|
Chris@0
|
1189 /**
|
Chris@0
|
1190 * Processes an AJAX response into current content.
|
Chris@0
|
1191 *
|
Chris@0
|
1192 * This processes the AJAX response as ajax.js does. It uses the response's
|
Chris@0
|
1193 * JSON data, an array of commands, to update $this->content using equivalent
|
Chris@0
|
1194 * DOM manipulation as is used by ajax.js.
|
Chris@0
|
1195 * It does not apply custom AJAX commands though, because emulation is only
|
Chris@0
|
1196 * implemented for the AJAX commands that ship with Drupal core.
|
Chris@0
|
1197 *
|
Chris@0
|
1198 * @param string $content
|
Chris@0
|
1199 * The current HTML content.
|
Chris@0
|
1200 * @param array $ajax_response
|
Chris@0
|
1201 * An array of AJAX commands.
|
Chris@0
|
1202 * @param array $ajax_settings
|
Chris@0
|
1203 * An array of AJAX settings which will be used to process the response.
|
Chris@0
|
1204 * @param array $drupal_settings
|
Chris@0
|
1205 * An array of settings to update the value of drupalSettings for the
|
Chris@0
|
1206 * currently-loaded page.
|
Chris@0
|
1207 *
|
Chris@0
|
1208 * @see drupalPostAjaxForm()
|
Chris@0
|
1209 * @see ajax.js
|
Chris@0
|
1210 */
|
Chris@0
|
1211 protected function drupalProcessAjaxResponse($content, array $ajax_response, array $ajax_settings, array $drupal_settings) {
|
Chris@0
|
1212
|
Chris@0
|
1213 // ajax.js applies some defaults to the settings object, so do the same
|
Chris@0
|
1214 // for what's used by this function.
|
Chris@0
|
1215 $ajax_settings += [
|
Chris@0
|
1216 'method' => 'replaceWith',
|
Chris@0
|
1217 ];
|
Chris@0
|
1218 // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
|
Chris@0
|
1219 // them.
|
Chris@0
|
1220 $dom = new \DOMDocument();
|
Chris@0
|
1221 @$dom->loadHTML($content);
|
Chris@0
|
1222 // XPath allows for finding wrapper nodes better than DOM does.
|
Chris@0
|
1223 $xpath = new \DOMXPath($dom);
|
Chris@0
|
1224 foreach ($ajax_response as $command) {
|
Chris@0
|
1225 // Error messages might be not commands.
|
Chris@0
|
1226 if (!is_array($command)) {
|
Chris@0
|
1227 continue;
|
Chris@0
|
1228 }
|
Chris@0
|
1229 switch ($command['command']) {
|
Chris@0
|
1230 case 'settings':
|
Chris@0
|
1231 $drupal_settings = NestedArray::mergeDeepArray([$drupal_settings, $command['settings']], TRUE);
|
Chris@0
|
1232 break;
|
Chris@0
|
1233
|
Chris@0
|
1234 case 'insert':
|
Chris@0
|
1235 $wrapperNode = NULL;
|
Chris@0
|
1236 // When a command doesn't specify a selector, use the
|
Chris@0
|
1237 // #ajax['wrapper'] which is always an HTML ID.
|
Chris@0
|
1238 if (!isset($command['selector'])) {
|
Chris@0
|
1239 $wrapperNode = $xpath->query('//*[@id="' . $ajax_settings['wrapper'] . '"]')->item(0);
|
Chris@0
|
1240 }
|
Chris@0
|
1241 // @todo Ajax commands can target any jQuery selector, but these are
|
Chris@0
|
1242 // hard to fully emulate with XPath. For now, just handle 'head'
|
Chris@0
|
1243 // and 'body', since these are used by the Ajax renderer.
|
Chris@0
|
1244 elseif (in_array($command['selector'], ['head', 'body'])) {
|
Chris@0
|
1245 $wrapperNode = $xpath->query('//' . $command['selector'])->item(0);
|
Chris@0
|
1246 }
|
Chris@0
|
1247 if ($wrapperNode) {
|
Chris@0
|
1248 // ajax.js adds an enclosing DIV to work around a Safari bug.
|
Chris@0
|
1249 $newDom = new \DOMDocument();
|
Chris@0
|
1250 // DOM can load HTML soup. But, HTML soup can throw warnings,
|
Chris@0
|
1251 // suppress them.
|
Chris@0
|
1252 @$newDom->loadHTML('<div>' . $command['data'] . '</div>');
|
Chris@0
|
1253 // Suppress warnings thrown when duplicate HTML IDs are encountered.
|
Chris@0
|
1254 // This probably means we are replacing an element with the same ID.
|
Chris@0
|
1255 $newNode = @$dom->importNode($newDom->documentElement->firstChild->firstChild, TRUE);
|
Chris@0
|
1256 $method = isset($command['method']) ? $command['method'] : $ajax_settings['method'];
|
Chris@0
|
1257 // The "method" is a jQuery DOM manipulation function. Emulate
|
Chris@0
|
1258 // each one using PHP's DOMNode API.
|
Chris@0
|
1259 switch ($method) {
|
Chris@0
|
1260 case 'replaceWith':
|
Chris@0
|
1261 $wrapperNode->parentNode->replaceChild($newNode, $wrapperNode);
|
Chris@0
|
1262 break;
|
Chris@0
|
1263 case 'append':
|
Chris@0
|
1264 $wrapperNode->appendChild($newNode);
|
Chris@0
|
1265 break;
|
Chris@0
|
1266 case 'prepend':
|
Chris@0
|
1267 // If no firstChild, insertBefore() falls back to
|
Chris@0
|
1268 // appendChild().
|
Chris@0
|
1269 $wrapperNode->insertBefore($newNode, $wrapperNode->firstChild);
|
Chris@0
|
1270 break;
|
Chris@0
|
1271 case 'before':
|
Chris@0
|
1272 $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode);
|
Chris@0
|
1273 break;
|
Chris@0
|
1274 case 'after':
|
Chris@0
|
1275 // If no nextSibling, insertBefore() falls back to
|
Chris@0
|
1276 // appendChild().
|
Chris@0
|
1277 $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode->nextSibling);
|
Chris@0
|
1278 break;
|
Chris@0
|
1279 case 'html':
|
Chris@0
|
1280 foreach ($wrapperNode->childNodes as $childNode) {
|
Chris@0
|
1281 $wrapperNode->removeChild($childNode);
|
Chris@0
|
1282 }
|
Chris@0
|
1283 $wrapperNode->appendChild($newNode);
|
Chris@0
|
1284 break;
|
Chris@0
|
1285 }
|
Chris@0
|
1286 }
|
Chris@0
|
1287 break;
|
Chris@0
|
1288
|
Chris@0
|
1289 // @todo Add suitable implementations for these commands in order to
|
Chris@0
|
1290 // have full test coverage of what ajax.js can do.
|
Chris@0
|
1291 case 'remove':
|
Chris@0
|
1292 break;
|
Chris@0
|
1293 case 'changed':
|
Chris@0
|
1294 break;
|
Chris@0
|
1295 case 'css':
|
Chris@0
|
1296 break;
|
Chris@0
|
1297 case 'data':
|
Chris@0
|
1298 break;
|
Chris@0
|
1299 case 'restripe':
|
Chris@0
|
1300 break;
|
Chris@0
|
1301 case 'add_css':
|
Chris@0
|
1302 break;
|
Chris@0
|
1303 case 'update_build_id':
|
Chris@0
|
1304 $buildId = $xpath->query('//input[@name="form_build_id" and @value="' . $command['old'] . '"]')->item(0);
|
Chris@0
|
1305 if ($buildId) {
|
Chris@0
|
1306 $buildId->setAttribute('value', $command['new']);
|
Chris@0
|
1307 }
|
Chris@0
|
1308 break;
|
Chris@0
|
1309 }
|
Chris@0
|
1310 }
|
Chris@0
|
1311 $content = $dom->saveHTML();
|
Chris@0
|
1312 $this->setRawContent($content);
|
Chris@0
|
1313 $this->setDrupalSettings($drupal_settings);
|
Chris@0
|
1314 }
|
Chris@0
|
1315
|
Chris@0
|
1316 /**
|
Chris@0
|
1317 * Perform a POST HTTP request.
|
Chris@0
|
1318 *
|
Chris@0
|
1319 * @param string|\Drupal\Core\Url $path
|
Chris@0
|
1320 * Drupal path or absolute path where the request should be POSTed.
|
Chris@0
|
1321 * @param string $accept
|
Chris@0
|
1322 * The value for the "Accept" header. Usually either 'application/json' or
|
Chris@0
|
1323 * 'application/vnd.drupal-ajax'.
|
Chris@0
|
1324 * @param array $post
|
Chris@0
|
1325 * The POST data. When making a 'application/vnd.drupal-ajax' request, the
|
Chris@0
|
1326 * Ajax page state data should be included. Use getAjaxPageStatePostData()
|
Chris@0
|
1327 * for that.
|
Chris@0
|
1328 * @param array $options
|
Chris@0
|
1329 * (optional) Options to be forwarded to the url generator. The 'absolute'
|
Chris@0
|
1330 * option will automatically be enabled.
|
Chris@0
|
1331 *
|
Chris@0
|
1332 * @return
|
Chris@0
|
1333 * The content returned from the call to curl_exec().
|
Chris@0
|
1334 *
|
Chris@0
|
1335 * @see WebTestBase::getAjaxPageStatePostData()
|
Chris@0
|
1336 * @see WebTestBase::curlExec()
|
Chris@0
|
1337 */
|
Chris@0
|
1338 protected function drupalPost($path, $accept, array $post, $options = []) {
|
Chris@0
|
1339 return $this->curlExec([
|
Chris@0
|
1340 CURLOPT_URL => $this->buildUrl($path, $options),
|
Chris@0
|
1341 CURLOPT_POST => TRUE,
|
Chris@0
|
1342 CURLOPT_POSTFIELDS => $this->serializePostValues($post),
|
Chris@0
|
1343 CURLOPT_HTTPHEADER => [
|
Chris@0
|
1344 'Accept: ' . $accept,
|
Chris@0
|
1345 'Content-Type: application/x-www-form-urlencoded',
|
Chris@0
|
1346 ],
|
Chris@0
|
1347 ]);
|
Chris@0
|
1348 }
|
Chris@0
|
1349
|
Chris@0
|
1350 /**
|
Chris@0
|
1351 * Performs a POST HTTP request with a specific format.
|
Chris@0
|
1352 *
|
Chris@0
|
1353 * @param string|\Drupal\Core\Url $path
|
Chris@0
|
1354 * Drupal path or absolute path where the request should be POSTed.
|
Chris@0
|
1355 * @param string $format
|
Chris@0
|
1356 * The request format.
|
Chris@0
|
1357 * @param array $post
|
Chris@0
|
1358 * The POST data. When making a 'application/vnd.drupal-ajax' request, the
|
Chris@0
|
1359 * Ajax page state data should be included. Use getAjaxPageStatePostData()
|
Chris@0
|
1360 * for that.
|
Chris@0
|
1361 * @param array $options
|
Chris@0
|
1362 * (optional) Options to be forwarded to the url generator. The 'absolute'
|
Chris@0
|
1363 * option will automatically be enabled.
|
Chris@0
|
1364 *
|
Chris@0
|
1365 * @return string
|
Chris@0
|
1366 * The content returned from the call to curl_exec().
|
Chris@0
|
1367 *
|
Chris@0
|
1368 * @see WebTestBase::drupalPost
|
Chris@0
|
1369 * @see WebTestBase::getAjaxPageStatePostData()
|
Chris@0
|
1370 * @see WebTestBase::curlExec()
|
Chris@0
|
1371 */
|
Chris@0
|
1372 protected function drupalPostWithFormat($path, $format, array $post, $options = []) {
|
Chris@0
|
1373 $options['query']['_format'] = $format;
|
Chris@0
|
1374 return $this->drupalPost($path, '', $post, $options);
|
Chris@0
|
1375 }
|
Chris@0
|
1376
|
Chris@0
|
1377 /**
|
Chris@0
|
1378 * Get the Ajax page state from drupalSettings and prepare it for POSTing.
|
Chris@0
|
1379 *
|
Chris@0
|
1380 * @return array
|
Chris@0
|
1381 * The Ajax page state POST data.
|
Chris@0
|
1382 */
|
Chris@0
|
1383 protected function getAjaxPageStatePostData() {
|
Chris@0
|
1384 $post = [];
|
Chris@0
|
1385 $drupal_settings = $this->drupalSettings;
|
Chris@0
|
1386 if (isset($drupal_settings['ajaxPageState']['theme'])) {
|
Chris@0
|
1387 $post['ajax_page_state[theme]'] = $drupal_settings['ajaxPageState']['theme'];
|
Chris@0
|
1388 }
|
Chris@0
|
1389 if (isset($drupal_settings['ajaxPageState']['theme_token'])) {
|
Chris@0
|
1390 $post['ajax_page_state[theme_token]'] = $drupal_settings['ajaxPageState']['theme_token'];
|
Chris@0
|
1391 }
|
Chris@0
|
1392 if (isset($drupal_settings['ajaxPageState']['libraries'])) {
|
Chris@0
|
1393 $post['ajax_page_state[libraries]'] = $drupal_settings['ajaxPageState']['libraries'];
|
Chris@0
|
1394 }
|
Chris@0
|
1395 return $post;
|
Chris@0
|
1396 }
|
Chris@0
|
1397
|
Chris@0
|
1398 /**
|
Chris@0
|
1399 * Serialize POST HTTP request values.
|
Chris@0
|
1400 *
|
Chris@0
|
1401 * Encode according to application/x-www-form-urlencoded. Both names and
|
Chris@0
|
1402 * values needs to be urlencoded, according to
|
Chris@0
|
1403 * http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
|
Chris@0
|
1404 *
|
Chris@0
|
1405 * @param array $post
|
Chris@0
|
1406 * The array of values to be POSTed.
|
Chris@0
|
1407 *
|
Chris@0
|
1408 * @return string
|
Chris@0
|
1409 * The serialized result.
|
Chris@0
|
1410 */
|
Chris@0
|
1411 protected function serializePostValues($post = []) {
|
Chris@0
|
1412 foreach ($post as $key => $value) {
|
Chris@0
|
1413 $post[$key] = urlencode($key) . '=' . urlencode($value);
|
Chris@0
|
1414 }
|
Chris@0
|
1415 return implode('&', $post);
|
Chris@0
|
1416 }
|
Chris@0
|
1417
|
Chris@0
|
1418 /**
|
Chris@0
|
1419 * Transforms a nested array into a flat array suitable for WebTestBase::drupalPostForm().
|
Chris@0
|
1420 *
|
Chris@0
|
1421 * @param array $values
|
Chris@0
|
1422 * A multi-dimensional form values array to convert.
|
Chris@0
|
1423 *
|
Chris@0
|
1424 * @return array
|
Chris@0
|
1425 * The flattened $edit array suitable for WebTestBase::drupalPostForm().
|
Chris@0
|
1426 */
|
Chris@0
|
1427 protected function translatePostValues(array $values) {
|
Chris@0
|
1428 $edit = [];
|
Chris@0
|
1429 // The easiest and most straightforward way to translate values suitable for
|
Chris@0
|
1430 // WebTestBase::drupalPostForm() is to actually build the POST data string
|
Chris@0
|
1431 // and convert the resulting key/value pairs back into a flat array.
|
Chris@0
|
1432 $query = http_build_query($values);
|
Chris@0
|
1433 foreach (explode('&', $query) as $item) {
|
Chris@0
|
1434 list($key, $value) = explode('=', $item);
|
Chris@0
|
1435 $edit[urldecode($key)] = urldecode($value);
|
Chris@0
|
1436 }
|
Chris@0
|
1437 return $edit;
|
Chris@0
|
1438 }
|
Chris@0
|
1439
|
Chris@0
|
1440 /**
|
Chris@0
|
1441 * Checks for meta refresh tag and if found call drupalGet() recursively.
|
Chris@0
|
1442 *
|
Chris@0
|
1443 * This function looks for the http-equiv attribute to be set to "Refresh" and
|
Chris@0
|
1444 * is case-sensitive.
|
Chris@0
|
1445 *
|
Chris@0
|
1446 * @return
|
Chris@0
|
1447 * Either the new page content or FALSE.
|
Chris@0
|
1448 */
|
Chris@0
|
1449 protected function checkForMetaRefresh() {
|
Chris@0
|
1450 if (strpos($this->getRawContent(), '<meta ') && $this->parse() && (!isset($this->maximumMetaRefreshCount) || $this->metaRefreshCount < $this->maximumMetaRefreshCount)) {
|
Chris@0
|
1451 $refresh = $this->xpath('//meta[@http-equiv="Refresh"]');
|
Chris@0
|
1452 if (!empty($refresh)) {
|
Chris@0
|
1453 // Parse the content attribute of the meta tag for the format:
|
Chris@0
|
1454 // "[delay]: URL=[page_to_redirect_to]".
|
Chris@0
|
1455 if (preg_match('/\d+;\s*URL=(?<url>.*)/i', $refresh[0]['content'], $match)) {
|
Chris@0
|
1456 $this->metaRefreshCount++;
|
Chris@0
|
1457 return $this->drupalGet($this->getAbsoluteUrl(Html::decodeEntities($match['url'])));
|
Chris@0
|
1458 }
|
Chris@0
|
1459 }
|
Chris@0
|
1460 }
|
Chris@0
|
1461 return FALSE;
|
Chris@0
|
1462 }
|
Chris@0
|
1463
|
Chris@0
|
1464 /**
|
Chris@0
|
1465 * Retrieves only the headers for a Drupal path or an absolute path.
|
Chris@0
|
1466 *
|
Chris@0
|
1467 * @param $path
|
Chris@0
|
1468 * Drupal path or URL to load into internal browser
|
Chris@0
|
1469 * @param $options
|
Chris@0
|
1470 * Options to be forwarded to the url generator.
|
Chris@0
|
1471 * @param $headers
|
Chris@0
|
1472 * An array containing additional HTTP request headers, each formatted as
|
Chris@0
|
1473 * "name: value".
|
Chris@0
|
1474 *
|
Chris@0
|
1475 * @return
|
Chris@0
|
1476 * The retrieved headers, also available as $this->getRawContent()
|
Chris@0
|
1477 */
|
Chris@0
|
1478 protected function drupalHead($path, array $options = [], array $headers = []) {
|
Chris@0
|
1479 $options['absolute'] = TRUE;
|
Chris@0
|
1480 $url = $this->buildUrl($path, $options);
|
Chris@0
|
1481 $out = $this->curlExec([CURLOPT_NOBODY => TRUE, CURLOPT_URL => $url, CURLOPT_HTTPHEADER => $headers]);
|
Chris@0
|
1482 // Ensure that any changes to variables in the other thread are picked up.
|
Chris@0
|
1483 $this->refreshVariables();
|
Chris@0
|
1484
|
Chris@0
|
1485 if ($this->dumpHeaders) {
|
Chris@0
|
1486 $this->verbose('GET request to: ' . $path .
|
Chris@0
|
1487 '<hr />Ending URL: ' . $this->getUrl() .
|
Chris@0
|
1488 '<hr />Headers: <pre>' . Html::escape(var_export(array_map('trim', $this->headers), TRUE)) . '</pre>');
|
Chris@0
|
1489 }
|
Chris@0
|
1490
|
Chris@0
|
1491 return $out;
|
Chris@0
|
1492 }
|
Chris@0
|
1493
|
Chris@0
|
1494 /**
|
Chris@0
|
1495 * Handles form input related to drupalPostForm().
|
Chris@0
|
1496 *
|
Chris@0
|
1497 * Ensure that the specified fields exist and attempt to create POST data in
|
Chris@0
|
1498 * the correct manner for the particular field type.
|
Chris@0
|
1499 *
|
Chris@0
|
1500 * @param $post
|
Chris@0
|
1501 * Reference to array of post values.
|
Chris@0
|
1502 * @param $edit
|
Chris@0
|
1503 * Reference to array of edit values to be checked against the form.
|
Chris@0
|
1504 * @param $submit
|
Chris@0
|
1505 * Form submit button value.
|
Chris@0
|
1506 * @param $form
|
Chris@0
|
1507 * Array of form elements.
|
Chris@0
|
1508 *
|
Chris@0
|
1509 * @return
|
Chris@0
|
1510 * Submit value matches a valid submit input in the form.
|
Chris@0
|
1511 */
|
Chris@0
|
1512 protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {
|
Chris@0
|
1513 // Retrieve the form elements.
|
Chris@0
|
1514 $elements = $form->xpath('.//input[not(@disabled)]|.//textarea[not(@disabled)]|.//select[not(@disabled)]');
|
Chris@0
|
1515 $submit_matches = FALSE;
|
Chris@0
|
1516 foreach ($elements as $element) {
|
Chris@0
|
1517 // SimpleXML objects need string casting all the time.
|
Chris@0
|
1518 $name = (string) $element['name'];
|
Chris@0
|
1519 // This can either be the type of <input> or the name of the tag itself
|
Chris@0
|
1520 // for <select> or <textarea>.
|
Chris@0
|
1521 $type = isset($element['type']) ? (string) $element['type'] : $element->getName();
|
Chris@0
|
1522 $value = isset($element['value']) ? (string) $element['value'] : '';
|
Chris@0
|
1523 $done = FALSE;
|
Chris@0
|
1524 if (isset($edit[$name])) {
|
Chris@0
|
1525 switch ($type) {
|
Chris@0
|
1526 case 'text':
|
Chris@0
|
1527 case 'tel':
|
Chris@0
|
1528 case 'textarea':
|
Chris@0
|
1529 case 'url':
|
Chris@0
|
1530 case 'number':
|
Chris@0
|
1531 case 'range':
|
Chris@0
|
1532 case 'color':
|
Chris@0
|
1533 case 'hidden':
|
Chris@0
|
1534 case 'password':
|
Chris@0
|
1535 case 'email':
|
Chris@0
|
1536 case 'search':
|
Chris@0
|
1537 case 'date':
|
Chris@0
|
1538 case 'time':
|
Chris@0
|
1539 case 'datetime':
|
Chris@0
|
1540 case 'datetime-local';
|
Chris@0
|
1541 $post[$name] = $edit[$name];
|
Chris@0
|
1542 unset($edit[$name]);
|
Chris@0
|
1543 break;
|
Chris@0
|
1544 case 'radio':
|
Chris@0
|
1545 if ($edit[$name] == $value) {
|
Chris@0
|
1546 $post[$name] = $edit[$name];
|
Chris@0
|
1547 unset($edit[$name]);
|
Chris@0
|
1548 }
|
Chris@0
|
1549 break;
|
Chris@0
|
1550 case 'checkbox':
|
Chris@0
|
1551 // To prevent checkbox from being checked.pass in a FALSE,
|
Chris@0
|
1552 // otherwise the checkbox will be set to its value regardless
|
Chris@0
|
1553 // of $edit.
|
Chris@0
|
1554 if ($edit[$name] === FALSE) {
|
Chris@0
|
1555 unset($edit[$name]);
|
Chris@0
|
1556 continue 2;
|
Chris@0
|
1557 }
|
Chris@0
|
1558 else {
|
Chris@0
|
1559 unset($edit[$name]);
|
Chris@0
|
1560 $post[$name] = $value;
|
Chris@0
|
1561 }
|
Chris@0
|
1562 break;
|
Chris@0
|
1563 case 'select':
|
Chris@0
|
1564 $new_value = $edit[$name];
|
Chris@0
|
1565 $options = $this->getAllOptions($element);
|
Chris@0
|
1566 if (is_array($new_value)) {
|
Chris@0
|
1567 // Multiple select box.
|
Chris@0
|
1568 if (!empty($new_value)) {
|
Chris@0
|
1569 $index = 0;
|
Chris@0
|
1570 $key = preg_replace('/\[\]$/', '', $name);
|
Chris@0
|
1571 foreach ($options as $option) {
|
Chris@0
|
1572 $option_value = (string) $option['value'];
|
Chris@0
|
1573 if (in_array($option_value, $new_value)) {
|
Chris@0
|
1574 $post[$key . '[' . $index++ . ']'] = $option_value;
|
Chris@0
|
1575 $done = TRUE;
|
Chris@0
|
1576 unset($edit[$name]);
|
Chris@0
|
1577 }
|
Chris@0
|
1578 }
|
Chris@0
|
1579 }
|
Chris@0
|
1580 else {
|
Chris@0
|
1581 // No options selected: do not include any POST data for the
|
Chris@0
|
1582 // element.
|
Chris@0
|
1583 $done = TRUE;
|
Chris@0
|
1584 unset($edit[$name]);
|
Chris@0
|
1585 }
|
Chris@0
|
1586 }
|
Chris@0
|
1587 else {
|
Chris@0
|
1588 // Single select box.
|
Chris@0
|
1589 foreach ($options as $option) {
|
Chris@0
|
1590 if ($new_value == $option['value']) {
|
Chris@0
|
1591 $post[$name] = $new_value;
|
Chris@0
|
1592 unset($edit[$name]);
|
Chris@0
|
1593 $done = TRUE;
|
Chris@0
|
1594 break;
|
Chris@0
|
1595 }
|
Chris@0
|
1596 }
|
Chris@0
|
1597 }
|
Chris@0
|
1598 break;
|
Chris@0
|
1599 case 'file':
|
Chris@0
|
1600 $upload[$name] = $edit[$name];
|
Chris@0
|
1601 unset($edit[$name]);
|
Chris@0
|
1602 break;
|
Chris@0
|
1603 }
|
Chris@0
|
1604 }
|
Chris@0
|
1605 if (!isset($post[$name]) && !$done) {
|
Chris@0
|
1606 switch ($type) {
|
Chris@0
|
1607 case 'textarea':
|
Chris@0
|
1608 $post[$name] = (string) $element;
|
Chris@0
|
1609 break;
|
Chris@0
|
1610 case 'select':
|
Chris@0
|
1611 $single = empty($element['multiple']);
|
Chris@0
|
1612 $first = TRUE;
|
Chris@0
|
1613 $index = 0;
|
Chris@0
|
1614 $key = preg_replace('/\[\]$/', '', $name);
|
Chris@0
|
1615 $options = $this->getAllOptions($element);
|
Chris@0
|
1616 foreach ($options as $option) {
|
Chris@0
|
1617 // For single select, we load the first option, if there is a
|
Chris@0
|
1618 // selected option that will overwrite it later.
|
Chris@0
|
1619 if ($option['selected'] || ($first && $single)) {
|
Chris@0
|
1620 $first = FALSE;
|
Chris@0
|
1621 if ($single) {
|
Chris@0
|
1622 $post[$name] = (string) $option['value'];
|
Chris@0
|
1623 }
|
Chris@0
|
1624 else {
|
Chris@0
|
1625 $post[$key . '[' . $index++ . ']'] = (string) $option['value'];
|
Chris@0
|
1626 }
|
Chris@0
|
1627 }
|
Chris@0
|
1628 }
|
Chris@0
|
1629 break;
|
Chris@0
|
1630 case 'file':
|
Chris@0
|
1631 break;
|
Chris@0
|
1632 case 'submit':
|
Chris@0
|
1633 case 'image':
|
Chris@0
|
1634 if (isset($submit) && $submit == $value) {
|
Chris@0
|
1635 $post[$name] = $value;
|
Chris@0
|
1636 $submit_matches = TRUE;
|
Chris@0
|
1637 }
|
Chris@0
|
1638 break;
|
Chris@0
|
1639 case 'radio':
|
Chris@0
|
1640 case 'checkbox':
|
Chris@0
|
1641 if (!isset($element['checked'])) {
|
Chris@0
|
1642 break;
|
Chris@0
|
1643 }
|
Chris@0
|
1644 // Deliberate no break.
|
Chris@0
|
1645 default:
|
Chris@0
|
1646 $post[$name] = $value;
|
Chris@0
|
1647 }
|
Chris@0
|
1648 }
|
Chris@0
|
1649 }
|
Chris@0
|
1650 // An empty name means the value is not sent.
|
Chris@0
|
1651 unset($post['']);
|
Chris@0
|
1652 return $submit_matches;
|
Chris@0
|
1653 }
|
Chris@0
|
1654
|
Chris@0
|
1655 /**
|
Chris@0
|
1656 * Follows a link by complete name.
|
Chris@0
|
1657 *
|
Chris@0
|
1658 * Will click the first link found with this link text by default, or a later
|
Chris@0
|
1659 * one if an index is given. Match is case sensitive with normalized space.
|
Chris@0
|
1660 * The label is translated label.
|
Chris@0
|
1661 *
|
Chris@0
|
1662 * If the link is discovered and clicked, the test passes. Fail otherwise.
|
Chris@0
|
1663 *
|
Chris@0
|
1664 * @param string|\Drupal\Component\Render\MarkupInterface $label
|
Chris@0
|
1665 * Text between the anchor tags.
|
Chris@0
|
1666 * @param int $index
|
Chris@0
|
1667 * Link position counting from zero.
|
Chris@0
|
1668 *
|
Chris@0
|
1669 * @return string|bool
|
Chris@0
|
1670 * Page contents on success, or FALSE on failure.
|
Chris@0
|
1671 */
|
Chris@0
|
1672 protected function clickLink($label, $index = 0) {
|
Chris@0
|
1673 return $this->clickLinkHelper($label, $index, '//a[normalize-space()=:label]');
|
Chris@0
|
1674 }
|
Chris@0
|
1675
|
Chris@0
|
1676 /**
|
Chris@0
|
1677 * Follows a link by partial name.
|
Chris@0
|
1678 *
|
Chris@0
|
1679 * If the link is discovered and clicked, the test passes. Fail otherwise.
|
Chris@0
|
1680 *
|
Chris@0
|
1681 * @param string|\Drupal\Component\Render\MarkupInterface $label
|
Chris@0
|
1682 * Text between the anchor tags, uses starts-with().
|
Chris@0
|
1683 * @param int $index
|
Chris@0
|
1684 * Link position counting from zero.
|
Chris@0
|
1685 *
|
Chris@0
|
1686 * @return string|bool
|
Chris@0
|
1687 * Page contents on success, or FALSE on failure.
|
Chris@0
|
1688 *
|
Chris@0
|
1689 * @see ::clickLink()
|
Chris@0
|
1690 */
|
Chris@0
|
1691 protected function clickLinkPartialName($label, $index = 0) {
|
Chris@0
|
1692 return $this->clickLinkHelper($label, $index, '//a[starts-with(normalize-space(), :label)]');
|
Chris@0
|
1693 }
|
Chris@0
|
1694
|
Chris@0
|
1695 /**
|
Chris@0
|
1696 * Provides a helper for ::clickLink() and ::clickLinkPartialName().
|
Chris@0
|
1697 *
|
Chris@0
|
1698 * @param string|\Drupal\Component\Render\MarkupInterface $label
|
Chris@0
|
1699 * Text between the anchor tags, uses starts-with().
|
Chris@0
|
1700 * @param int $index
|
Chris@0
|
1701 * Link position counting from zero.
|
Chris@0
|
1702 * @param string $pattern
|
Chris@0
|
1703 * A pattern to use for the XPath.
|
Chris@0
|
1704 *
|
Chris@0
|
1705 * @return bool|string
|
Chris@0
|
1706 * Page contents on success, or FALSE on failure.
|
Chris@0
|
1707 */
|
Chris@0
|
1708 protected function clickLinkHelper($label, $index, $pattern) {
|
Chris@0
|
1709 // Cast MarkupInterface objects to string.
|
Chris@0
|
1710 $label = (string) $label;
|
Chris@0
|
1711 $url_before = $this->getUrl();
|
Chris@0
|
1712 $urls = $this->xpath($pattern, [':label' => $label]);
|
Chris@0
|
1713 if (isset($urls[$index])) {
|
Chris@0
|
1714 $url_target = $this->getAbsoluteUrl($urls[$index]['href']);
|
Chris@0
|
1715 $this->pass(SafeMarkup::format('Clicked link %label (@url_target) from @url_before', ['%label' => $label, '@url_target' => $url_target, '@url_before' => $url_before]), 'Browser');
|
Chris@0
|
1716 return $this->drupalGet($url_target);
|
Chris@0
|
1717 }
|
Chris@0
|
1718 $this->fail(SafeMarkup::format('Link %label does not exist on @url_before', ['%label' => $label, '@url_before' => $url_before]), 'Browser');
|
Chris@0
|
1719 return FALSE;
|
Chris@0
|
1720 }
|
Chris@0
|
1721
|
Chris@0
|
1722 /**
|
Chris@0
|
1723 * Takes a path and returns an absolute path.
|
Chris@0
|
1724 *
|
Chris@0
|
1725 * This method is implemented in the way that browsers work, see
|
Chris@0
|
1726 * https://url.spec.whatwg.org/#relative-state for more information about the
|
Chris@0
|
1727 * possible cases.
|
Chris@0
|
1728 *
|
Chris@0
|
1729 * @param string $path
|
Chris@0
|
1730 * A path from the internal browser content.
|
Chris@0
|
1731 *
|
Chris@0
|
1732 * @return string
|
Chris@0
|
1733 * The $path with $base_url prepended, if necessary.
|
Chris@0
|
1734 */
|
Chris@0
|
1735 protected function getAbsoluteUrl($path) {
|
Chris@0
|
1736 global $base_url, $base_path;
|
Chris@0
|
1737
|
Chris@0
|
1738 $parts = parse_url($path);
|
Chris@0
|
1739
|
Chris@0
|
1740 // In case the $path has a host, it is already an absolute URL and we are
|
Chris@0
|
1741 // done.
|
Chris@0
|
1742 if (!empty($parts['host'])) {
|
Chris@0
|
1743 return $path;
|
Chris@0
|
1744 }
|
Chris@0
|
1745
|
Chris@0
|
1746 // In case the $path contains just a query, we turn it into an absolute URL
|
Chris@0
|
1747 // with the same scheme, host and path, see
|
Chris@0
|
1748 // https://url.spec.whatwg.org/#relative-state.
|
Chris@0
|
1749 if (array_keys($parts) === ['query']) {
|
Chris@0
|
1750 $current_uri = new Uri($this->getUrl());
|
Chris@0
|
1751 return (string) $current_uri->withQuery($parts['query']);
|
Chris@0
|
1752 }
|
Chris@0
|
1753
|
Chris@0
|
1754 if (empty($parts['host'])) {
|
Chris@0
|
1755 // Ensure that we have a string (and no xpath object).
|
Chris@0
|
1756 $path = (string) $path;
|
Chris@0
|
1757 // Strip $base_path, if existent.
|
Chris@0
|
1758 $length = strlen($base_path);
|
Chris@0
|
1759 if (substr($path, 0, $length) === $base_path) {
|
Chris@0
|
1760 $path = substr($path, $length);
|
Chris@0
|
1761 }
|
Chris@0
|
1762 // Ensure that we have an absolute path.
|
Chris@0
|
1763 if (empty($path) || $path[0] !== '/') {
|
Chris@0
|
1764 $path = '/' . $path;
|
Chris@0
|
1765 }
|
Chris@0
|
1766 // Finally, prepend the $base_url.
|
Chris@0
|
1767 $path = $base_url . $path;
|
Chris@0
|
1768 }
|
Chris@0
|
1769 return $path;
|
Chris@0
|
1770 }
|
Chris@0
|
1771
|
Chris@0
|
1772 /**
|
Chris@0
|
1773 * Gets the HTTP response headers of the requested page.
|
Chris@0
|
1774 *
|
Chris@0
|
1775 * Normally we are only interested in the headers returned by the last
|
Chris@0
|
1776 * request. However, if a page is redirected or HTTP authentication is in use,
|
Chris@0
|
1777 * multiple requests will be required to retrieve the page. Headers from all
|
Chris@0
|
1778 * requests may be requested by passing TRUE to this function.
|
Chris@0
|
1779 *
|
Chris@0
|
1780 * @param $all_requests
|
Chris@0
|
1781 * Boolean value specifying whether to return headers from all requests
|
Chris@0
|
1782 * instead of just the last request. Defaults to FALSE.
|
Chris@0
|
1783 *
|
Chris@0
|
1784 * @return
|
Chris@0
|
1785 * A name/value array if headers from only the last request are requested.
|
Chris@0
|
1786 * If headers from all requests are requested, an array of name/value
|
Chris@0
|
1787 * arrays, one for each request.
|
Chris@0
|
1788 *
|
Chris@0
|
1789 * The pseudonym ":status" is used for the HTTP status line.
|
Chris@0
|
1790 *
|
Chris@0
|
1791 * Values for duplicate headers are stored as a single comma-separated list.
|
Chris@0
|
1792 */
|
Chris@0
|
1793 protected function drupalGetHeaders($all_requests = FALSE) {
|
Chris@0
|
1794 $request = 0;
|
Chris@0
|
1795 $headers = [$request => []];
|
Chris@0
|
1796 foreach ($this->headers as $header) {
|
Chris@0
|
1797 $header = trim($header);
|
Chris@0
|
1798 if ($header === '') {
|
Chris@0
|
1799 $request++;
|
Chris@0
|
1800 }
|
Chris@0
|
1801 else {
|
Chris@0
|
1802 if (strpos($header, 'HTTP/') === 0) {
|
Chris@0
|
1803 $name = ':status';
|
Chris@0
|
1804 $value = $header;
|
Chris@0
|
1805 }
|
Chris@0
|
1806 else {
|
Chris@0
|
1807 list($name, $value) = explode(':', $header, 2);
|
Chris@0
|
1808 $name = strtolower($name);
|
Chris@0
|
1809 }
|
Chris@0
|
1810 if (isset($headers[$request][$name])) {
|
Chris@0
|
1811 $headers[$request][$name] .= ',' . trim($value);
|
Chris@0
|
1812 }
|
Chris@0
|
1813 else {
|
Chris@0
|
1814 $headers[$request][$name] = trim($value);
|
Chris@0
|
1815 }
|
Chris@0
|
1816 }
|
Chris@0
|
1817 }
|
Chris@0
|
1818 if (!$all_requests) {
|
Chris@0
|
1819 $headers = array_pop($headers);
|
Chris@0
|
1820 }
|
Chris@0
|
1821 return $headers;
|
Chris@0
|
1822 }
|
Chris@0
|
1823
|
Chris@0
|
1824 /**
|
Chris@0
|
1825 * Gets the value of an HTTP response header.
|
Chris@0
|
1826 *
|
Chris@0
|
1827 * If multiple requests were required to retrieve the page, only the headers
|
Chris@0
|
1828 * from the last request will be checked by default. However, if TRUE is
|
Chris@0
|
1829 * passed as the second argument, all requests will be processed from last to
|
Chris@0
|
1830 * first until the header is found.
|
Chris@0
|
1831 *
|
Chris@0
|
1832 * @param $name
|
Chris@0
|
1833 * The name of the header to retrieve. Names are case-insensitive (see RFC
|
Chris@0
|
1834 * 2616 section 4.2).
|
Chris@0
|
1835 * @param $all_requests
|
Chris@0
|
1836 * Boolean value specifying whether to check all requests if the header is
|
Chris@0
|
1837 * not found in the last request. Defaults to FALSE.
|
Chris@0
|
1838 *
|
Chris@0
|
1839 * @return
|
Chris@0
|
1840 * The HTTP header value or FALSE if not found.
|
Chris@0
|
1841 */
|
Chris@0
|
1842 protected function drupalGetHeader($name, $all_requests = FALSE) {
|
Chris@0
|
1843 $name = strtolower($name);
|
Chris@0
|
1844 $header = FALSE;
|
Chris@0
|
1845 if ($all_requests) {
|
Chris@0
|
1846 foreach (array_reverse($this->drupalGetHeaders(TRUE)) as $headers) {
|
Chris@0
|
1847 if (isset($headers[$name])) {
|
Chris@0
|
1848 $header = $headers[$name];
|
Chris@0
|
1849 break;
|
Chris@0
|
1850 }
|
Chris@0
|
1851 }
|
Chris@0
|
1852 }
|
Chris@0
|
1853 else {
|
Chris@0
|
1854 $headers = $this->drupalGetHeaders();
|
Chris@0
|
1855 if (isset($headers[$name])) {
|
Chris@0
|
1856 $header = $headers[$name];
|
Chris@0
|
1857 }
|
Chris@0
|
1858 }
|
Chris@0
|
1859 return $header;
|
Chris@0
|
1860 }
|
Chris@0
|
1861
|
Chris@0
|
1862 /**
|
Chris@0
|
1863 * Check if a HTTP response header exists and has the expected value.
|
Chris@0
|
1864 *
|
Chris@0
|
1865 * @param string $header
|
Chris@0
|
1866 * The header key, example: Content-Type
|
Chris@0
|
1867 * @param string $value
|
Chris@0
|
1868 * The header value.
|
Chris@0
|
1869 * @param string $message
|
Chris@0
|
1870 * (optional) A message to display with the assertion.
|
Chris@0
|
1871 * @param string $group
|
Chris@0
|
1872 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
1873 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
1874 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@0
|
1875 * this default.
|
Chris@0
|
1876 *
|
Chris@0
|
1877 * @return bool
|
Chris@0
|
1878 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@0
|
1879 */
|
Chris@0
|
1880 protected function assertHeader($header, $value, $message = '', $group = 'Browser') {
|
Chris@0
|
1881 $header_value = $this->drupalGetHeader($header);
|
Chris@0
|
1882 return $this->assertTrue($header_value == $value, $message ? $message : 'HTTP response header ' . $header . ' with value ' . $value . ' found, actual value: ' . $header_value, $group);
|
Chris@0
|
1883 }
|
Chris@0
|
1884
|
Chris@0
|
1885 /**
|
Chris@0
|
1886 * Passes if the internal browser's URL matches the given path.
|
Chris@0
|
1887 *
|
Chris@0
|
1888 * @param \Drupal\Core\Url|string $path
|
Chris@0
|
1889 * The expected system path or URL.
|
Chris@0
|
1890 * @param $options
|
Chris@0
|
1891 * (optional) Any additional options to pass for $path to the url generator.
|
Chris@0
|
1892 * @param $message
|
Chris@0
|
1893 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
1894 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
|
Chris@0
|
1895 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
1896 * will be displayed.
|
Chris@0
|
1897 * @param $group
|
Chris@0
|
1898 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
1899 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
1900 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@0
|
1901 * this default.
|
Chris@0
|
1902 *
|
Chris@0
|
1903 * @return
|
Chris@0
|
1904 * TRUE on pass, FALSE on fail.
|
Chris@0
|
1905 */
|
Chris@0
|
1906 protected function assertUrl($path, array $options = [], $message = '', $group = 'Other') {
|
Chris@0
|
1907 if ($path instanceof Url) {
|
Chris@0
|
1908 $url_obj = $path;
|
Chris@0
|
1909 }
|
Chris@0
|
1910 elseif (UrlHelper::isExternal($path)) {
|
Chris@0
|
1911 $url_obj = Url::fromUri($path, $options);
|
Chris@0
|
1912 }
|
Chris@0
|
1913 else {
|
Chris@0
|
1914 $uri = $path === '<front>' ? 'base:/' : 'base:/' . $path;
|
Chris@0
|
1915 // This is needed for language prefixing.
|
Chris@0
|
1916 $options['path_processing'] = TRUE;
|
Chris@0
|
1917 $url_obj = Url::fromUri($uri, $options);
|
Chris@0
|
1918 }
|
Chris@0
|
1919 $url = $url_obj->setAbsolute()->toString();
|
Chris@0
|
1920 if (!$message) {
|
Chris@0
|
1921 $message = SafeMarkup::format('Expected @url matches current URL (@current_url).', [
|
Chris@0
|
1922 '@url' => var_export($url, TRUE),
|
Chris@0
|
1923 '@current_url' => $this->getUrl(),
|
Chris@0
|
1924 ]);
|
Chris@0
|
1925 }
|
Chris@0
|
1926 // Paths in query strings can be encoded or decoded with no functional
|
Chris@0
|
1927 // difference, decode them for comparison purposes.
|
Chris@0
|
1928 $actual_url = urldecode($this->getUrl());
|
Chris@0
|
1929 $expected_url = urldecode($url);
|
Chris@0
|
1930 return $this->assertEqual($actual_url, $expected_url, $message, $group);
|
Chris@0
|
1931 }
|
Chris@0
|
1932
|
Chris@0
|
1933 /**
|
Chris@0
|
1934 * Asserts the page responds with the specified response code.
|
Chris@0
|
1935 *
|
Chris@0
|
1936 * @param $code
|
Chris@0
|
1937 * Response code. For example 200 is a successful page request. For a list
|
Chris@0
|
1938 * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
|
Chris@0
|
1939 * @param $message
|
Chris@0
|
1940 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
1941 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
|
Chris@0
|
1942 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
1943 * will be displayed.
|
Chris@0
|
1944 * @param $group
|
Chris@0
|
1945 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
1946 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
1947 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@0
|
1948 * this default.
|
Chris@0
|
1949 *
|
Chris@0
|
1950 * @return
|
Chris@0
|
1951 * Assertion result.
|
Chris@0
|
1952 */
|
Chris@0
|
1953 protected function assertResponse($code, $message = '', $group = 'Browser') {
|
Chris@0
|
1954 $curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
|
Chris@0
|
1955 $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
|
Chris@0
|
1956 return $this->assertTrue($match, $message ? $message : SafeMarkup::format('HTTP response expected @code, actual @curl_code', ['@code' => $code, '@curl_code' => $curl_code]), $group);
|
Chris@0
|
1957 }
|
Chris@0
|
1958
|
Chris@0
|
1959 /**
|
Chris@0
|
1960 * Asserts the page did not return the specified response code.
|
Chris@0
|
1961 *
|
Chris@0
|
1962 * @param $code
|
Chris@0
|
1963 * Response code. For example 200 is a successful page request. For a list
|
Chris@0
|
1964 * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
|
Chris@0
|
1965 * @param $message
|
Chris@0
|
1966 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
1967 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
|
Chris@0
|
1968 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
1969 * will be displayed.
|
Chris@0
|
1970 * @param $group
|
Chris@0
|
1971 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
1972 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
1973 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@0
|
1974 * this default.
|
Chris@0
|
1975 *
|
Chris@0
|
1976 * @return
|
Chris@0
|
1977 * Assertion result.
|
Chris@0
|
1978 */
|
Chris@0
|
1979 protected function assertNoResponse($code, $message = '', $group = 'Browser') {
|
Chris@0
|
1980 $curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
|
Chris@0
|
1981 $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
|
Chris@0
|
1982 return $this->assertFalse($match, $message ? $message : SafeMarkup::format('HTTP response not expected @code, actual @curl_code', ['@code' => $code, '@curl_code' => $curl_code]), $group);
|
Chris@0
|
1983 }
|
Chris@0
|
1984
|
Chris@0
|
1985 /**
|
Chris@0
|
1986 * Builds an a absolute URL from a system path or a URL object.
|
Chris@0
|
1987 *
|
Chris@0
|
1988 * @param string|\Drupal\Core\Url $path
|
Chris@0
|
1989 * A system path or a URL.
|
Chris@0
|
1990 * @param array $options
|
Chris@0
|
1991 * Options to be passed to Url::fromUri().
|
Chris@0
|
1992 *
|
Chris@0
|
1993 * @return string
|
Chris@0
|
1994 * An absolute URL string.
|
Chris@0
|
1995 */
|
Chris@0
|
1996 protected function buildUrl($path, array $options = []) {
|
Chris@0
|
1997 if ($path instanceof Url) {
|
Chris@0
|
1998 $url_options = $path->getOptions();
|
Chris@0
|
1999 $options = $url_options + $options;
|
Chris@0
|
2000 $path->setOptions($options);
|
Chris@0
|
2001 return $path->setAbsolute()->toString(TRUE)->getGeneratedUrl();
|
Chris@0
|
2002 }
|
Chris@0
|
2003 // The URL generator service is not necessarily available yet; e.g., in
|
Chris@0
|
2004 // interactive installer tests.
|
Chris@0
|
2005 elseif ($this->container->has('url_generator')) {
|
Chris@0
|
2006 $force_internal = isset($options['external']) && $options['external'] == FALSE;
|
Chris@0
|
2007 if (!$force_internal && UrlHelper::isExternal($path)) {
|
Chris@0
|
2008 return Url::fromUri($path, $options)->toString();
|
Chris@0
|
2009 }
|
Chris@0
|
2010 else {
|
Chris@0
|
2011 $uri = $path === '<front>' ? 'base:/' : 'base:/' . $path;
|
Chris@0
|
2012 // Path processing is needed for language prefixing. Skip it when a
|
Chris@0
|
2013 // path that may look like an external URL is being used as internal.
|
Chris@0
|
2014 $options['path_processing'] = !$force_internal;
|
Chris@0
|
2015 return Url::fromUri($uri, $options)
|
Chris@0
|
2016 ->setAbsolute()
|
Chris@0
|
2017 ->toString();
|
Chris@0
|
2018 }
|
Chris@0
|
2019 }
|
Chris@0
|
2020 else {
|
Chris@0
|
2021 return $this->getAbsoluteUrl($path);
|
Chris@0
|
2022 }
|
Chris@0
|
2023 }
|
Chris@0
|
2024
|
Chris@0
|
2025 /**
|
Chris@0
|
2026 * Asserts whether an expected cache tag was present in the last response.
|
Chris@0
|
2027 *
|
Chris@0
|
2028 * @param string $expected_cache_tag
|
Chris@0
|
2029 * The expected cache tag.
|
Chris@0
|
2030 */
|
Chris@0
|
2031 protected function assertCacheTag($expected_cache_tag) {
|
Chris@0
|
2032 $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
|
Chris@0
|
2033 $this->assertTrue(in_array($expected_cache_tag, $cache_tags), "'" . $expected_cache_tag . "' is present in the X-Drupal-Cache-Tags header.");
|
Chris@0
|
2034 }
|
Chris@0
|
2035
|
Chris@0
|
2036 /**
|
Chris@0
|
2037 * Asserts whether an expected cache tag was absent in the last response.
|
Chris@0
|
2038 *
|
Chris@0
|
2039 * @param string $cache_tag
|
Chris@0
|
2040 * The cache tag to check.
|
Chris@0
|
2041 */
|
Chris@0
|
2042 protected function assertNoCacheTag($cache_tag) {
|
Chris@0
|
2043 $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
|
Chris@0
|
2044 $this->assertFalse(in_array($cache_tag, $cache_tags), "'" . $cache_tag . "' is absent in the X-Drupal-Cache-Tags header.");
|
Chris@0
|
2045 }
|
Chris@0
|
2046
|
Chris@0
|
2047 /**
|
Chris@0
|
2048 * Enables/disables the cacheability headers.
|
Chris@0
|
2049 *
|
Chris@0
|
2050 * Sets the http.response.debug_cacheability_headers container parameter.
|
Chris@0
|
2051 *
|
Chris@0
|
2052 * @param bool $value
|
Chris@0
|
2053 * (optional) Whether the debugging cacheability headers should be sent.
|
Chris@0
|
2054 */
|
Chris@0
|
2055 protected function setHttpResponseDebugCacheabilityHeaders($value = TRUE) {
|
Chris@0
|
2056 $this->setContainerParameter('http.response.debug_cacheability_headers', $value);
|
Chris@0
|
2057 $this->rebuildContainer();
|
Chris@0
|
2058 $this->resetAll();
|
Chris@0
|
2059 }
|
Chris@0
|
2060
|
Chris@0
|
2061 }
|