Mercurial > hg > isophonics-drupal-site
comparison core/tests/Drupal/Tests/BrowserTestBase.php @ 16:c2387f117808
Routine composer update
author | Chris Cannam |
---|---|
date | Tue, 10 Jul 2018 15:07:59 +0100 |
parents | 1fec387a4317 |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
15:e200cb7efeb3 | 16:c2387f117808 |
---|---|
319 $this->mink = new Mink(); | 319 $this->mink = new Mink(); |
320 $this->mink->registerSession('default', $session); | 320 $this->mink->registerSession('default', $session); |
321 $this->mink->setDefaultSessionName('default'); | 321 $this->mink->setDefaultSessionName('default'); |
322 $this->registerSessions(); | 322 $this->registerSessions(); |
323 | 323 |
324 // According to the W3C WebDriver specification a cookie can only be set if | 324 $this->initFrontPage(); |
325 // the cookie domain is equal to the domain of the active document. When the | 325 |
326 // browser starts up the active document is not our domain but 'about:blank' | 326 return $session; |
327 // or similar. To be able to set our User-Agent and Xdebug cookies at the | 327 } |
328 // start of the test we now do a request to the front page so the active | 328 |
329 // document matches the domain. | 329 /** |
330 // @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie | 330 * Visits the front page when initializing Mink. |
331 // @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975 | 331 * |
332 * According to the W3C WebDriver specification a cookie can only be set if | |
333 * the cookie domain is equal to the domain of the active document. When the | |
334 * browser starts up the active document is not our domain but 'about:blank' | |
335 * or similar. To be able to set our User-Agent and Xdebug cookies at the | |
336 * start of the test we now do a request to the front page so the active | |
337 * document matches the domain. | |
338 * | |
339 * @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie | |
340 * @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975 | |
341 */ | |
342 protected function initFrontPage() { | |
332 $session = $this->getSession(); | 343 $session = $this->getSession(); |
333 $session->visit($this->baseUrl); | 344 $session->visit($this->baseUrl); |
334 | |
335 return $session; | |
336 } | 345 } |
337 | 346 |
338 /** | 347 /** |
339 * Gets an instance of the default Mink driver. | 348 * Gets an instance of the default Mink driver. |
340 * | 349 * |
582 public function getSession($name = NULL) { | 591 public function getSession($name = NULL) { |
583 return $this->mink->getSession($name); | 592 return $this->mink->getSession($name); |
584 } | 593 } |
585 | 594 |
586 /** | 595 /** |
596 * Obtain the HTTP client for the system under test. | |
597 * | |
598 * Use this method for arbitrary HTTP requests to the site under test. For | |
599 * most tests, you should not get the HTTP client and instead use navigation | |
600 * methods such as drupalGet() and clickLink() in order to benefit from | |
601 * assertions. | |
602 * | |
603 * Subclasses which substitute a different Mink driver should override this | |
604 * method and provide a Guzzle client if the Mink driver provides one. | |
605 * | |
606 * @return \GuzzleHttp\ClientInterface | |
607 * The client with BrowserTestBase configuration. | |
608 * | |
609 * @throws \RuntimeException | |
610 * If the Mink driver does not support a Guzzle HTTP client, throw an | |
611 * exception. | |
612 */ | |
613 protected function getHttpClient() { | |
614 /* @var $mink_driver \Behat\Mink\Driver\DriverInterface */ | |
615 $mink_driver = $this->getSession()->getDriver(); | |
616 if ($mink_driver instanceof GoutteDriver) { | |
617 return $mink_driver->getClient()->getClient(); | |
618 } | |
619 throw new \RuntimeException('The Mink client type ' . get_class($mink_driver) . ' does not support getHttpClient().'); | |
620 } | |
621 | |
622 /** | |
587 * Returns WebAssert object. | 623 * Returns WebAssert object. |
588 * | 624 * |
589 * @param string $name | 625 * @param string $name |
590 * (optional) Name of the session. Defaults to the active session. | 626 * (optional) Name of the session. Defaults to the active session. |
591 * | 627 * |
592 * @return \Drupal\Tests\WebAssert | 628 * @return \Drupal\Tests\WebAssert |
593 * A new web-assert option for asserting the presence of elements with. | 629 * A new web-assert option for asserting the presence of elements with. |
594 */ | 630 */ |
595 public function assertSession($name = NULL) { | 631 public function assertSession($name = NULL) { |
632 $this->addToAssertionCount(1); | |
596 return new WebAssert($this->getSession($name), $this->baseUrl); | 633 return new WebAssert($this->getSession($name), $this->baseUrl); |
597 } | 634 } |
598 | 635 |
599 /** | 636 /** |
600 * Prepare for a request to testing site. | 637 * Prepare for a request to testing site. |
660 * An array containing additional HTTP request headers, the array keys are | 697 * An array containing additional HTTP request headers, the array keys are |
661 * the header names and the array values the header values. This is useful | 698 * the header names and the array values the header values. This is useful |
662 * to set for example the "Accept-Language" header for requesting the page | 699 * to set for example the "Accept-Language" header for requesting the page |
663 * in a different language. Note that not all headers are supported, for | 700 * in a different language. Note that not all headers are supported, for |
664 * example the "Accept" header is always overridden by the browser. For | 701 * example the "Accept" header is always overridden by the browser. For |
665 * testing REST APIs it is recommended to directly use an HTTP client such | 702 * testing REST APIs it is recommended to obtain a separate HTTP client |
666 * as Guzzle instead. | 703 * using getHttpClient() and performing requests that way. |
667 * | 704 * |
668 * @return string | 705 * @return string |
669 * The retrieved HTML string, also available as $this->getRawContent() | 706 * The retrieved HTML string, also available as $this->getRawContent() |
707 * | |
708 * @see \Drupal\Tests\BrowserTestBase::getHttpClient() | |
670 */ | 709 */ |
671 protected function drupalGet($path, array $options = [], array $headers = []) { | 710 protected function drupalGet($path, array $options = [], array $headers = []) { |
672 $options['absolute'] = TRUE; | 711 $options['absolute'] = TRUE; |
673 $url = $this->buildUrl($path, $options); | 712 $url = $this->buildUrl($path, $options); |
674 | 713 |
1165 * text. Defaults to 0. | 1204 * text. Defaults to 0. |
1166 */ | 1205 */ |
1167 protected function clickLink($label, $index = 0) { | 1206 protected function clickLink($label, $index = 0) { |
1168 $label = (string) $label; | 1207 $label = (string) $label; |
1169 $links = $this->getSession()->getPage()->findAll('named', ['link', $label]); | 1208 $links = $this->getSession()->getPage()->findAll('named', ['link', $label]); |
1209 $this->assertArrayHasKey($index, $links, 'The link ' . $label . ' was not found on the page.'); | |
1170 $links[$index]->click(); | 1210 $links[$index]->click(); |
1171 } | 1211 } |
1172 | 1212 |
1173 /** | 1213 /** |
1174 * Retrieves the plain-text content from the current page. | 1214 * Retrieves the plain-text content from the current page. |
1306 | 1346 |
1307 return $caller; | 1347 return $caller; |
1308 } | 1348 } |
1309 | 1349 |
1310 /** | 1350 /** |
1351 * Transforms a nested array into a flat array suitable for drupalPostForm(). | |
1352 * | |
1353 * @param array $values | |
1354 * A multi-dimensional form values array to convert. | |
1355 * | |
1356 * @return array | |
1357 * The flattened $edit array suitable for BrowserTestBase::drupalPostForm(). | |
1358 */ | |
1359 protected function translatePostValues(array $values) { | |
1360 $edit = []; | |
1361 // The easiest and most straightforward way to translate values suitable for | |
1362 // BrowserTestBase::drupalPostForm() is to actually build the POST data | |
1363 // string and convert the resulting key/value pairs back into a flat array. | |
1364 $query = http_build_query($values); | |
1365 foreach (explode('&', $query) as $item) { | |
1366 list($key, $value) = explode('=', $item); | |
1367 $edit[urldecode($key)] = urldecode($value); | |
1368 } | |
1369 return $edit; | |
1370 } | |
1371 | |
1372 /** | |
1311 * Checks for meta refresh tag and if found call drupalGet() recursively. | 1373 * Checks for meta refresh tag and if found call drupalGet() recursively. |
1312 * | 1374 * |
1313 * This function looks for the http-equiv attribute to be set to "Refresh" and | 1375 * This function looks for the http-equiv attribute to be set to "Refresh" and |
1314 * is case-insensitive. | 1376 * is case-insensitive. |
1315 * | 1377 * |