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