diff 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
line wrap: on
line diff
--- a/core/tests/Drupal/Tests/BrowserTestBase.php	Thu Apr 26 11:26:54 2018 +0100
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php	Tue Jul 10 15:07:59 2018 +0100
@@ -321,18 +321,27 @@
     $this->mink->setDefaultSessionName('default');
     $this->registerSessions();
 
-    // According to the W3C WebDriver specification a cookie can only be set if
-    // the cookie domain is equal to the domain of the active document. When the
-    // browser starts up the active document is not our domain but 'about:blank'
-    // or similar. To be able to set our User-Agent and Xdebug cookies at the
-    // start of the test we now do a request to the front page so the active
-    // document matches the domain.
-    // @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie
-    // @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975
+    $this->initFrontPage();
+
+    return $session;
+  }
+
+  /**
+   * Visits the front page when initializing Mink.
+   *
+   * According to the W3C WebDriver specification a cookie can only be set if
+   * the cookie domain is equal to the domain of the active document. When the
+   * browser starts up the active document is not our domain but 'about:blank'
+   * or similar. To be able to set our User-Agent and Xdebug cookies at the
+   * start of the test we now do a request to the front page so the active
+   * document matches the domain.
+   *
+   * @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie
+   * @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975
+   */
+  protected function initFrontPage() {
     $session = $this->getSession();
     $session->visit($this->baseUrl);
-
-    return $session;
   }
 
   /**
@@ -584,6 +593,33 @@
   }
 
   /**
+   * Obtain the HTTP client for the system under test.
+   *
+   * Use this method for arbitrary HTTP requests to the site under test. For
+   * most tests, you should not get the HTTP client and instead use navigation
+   * methods such as drupalGet() and clickLink() in order to benefit from
+   * assertions.
+   *
+   * Subclasses which substitute a different Mink driver should override this
+   * method and provide a Guzzle client if the Mink driver provides one.
+   *
+   * @return \GuzzleHttp\ClientInterface
+   *   The client with BrowserTestBase configuration.
+   *
+   * @throws \RuntimeException
+   *   If the Mink driver does not support a Guzzle HTTP client, throw an
+   *   exception.
+   */
+  protected function getHttpClient() {
+    /* @var $mink_driver \Behat\Mink\Driver\DriverInterface */
+    $mink_driver = $this->getSession()->getDriver();
+    if ($mink_driver instanceof GoutteDriver) {
+      return $mink_driver->getClient()->getClient();
+    }
+    throw new \RuntimeException('The Mink client type ' . get_class($mink_driver) . ' does not support getHttpClient().');
+  }
+
+  /**
    * Returns WebAssert object.
    *
    * @param string $name
@@ -593,6 +629,7 @@
    *   A new web-assert option for asserting the presence of elements with.
    */
   public function assertSession($name = NULL) {
+    $this->addToAssertionCount(1);
     return new WebAssert($this->getSession($name), $this->baseUrl);
   }
 
@@ -662,11 +699,13 @@
    *   to set for example the "Accept-Language" header for requesting the page
    *   in a different language. Note that not all headers are supported, for
    *   example the "Accept" header is always overridden by the browser. For
-   *   testing REST APIs it is recommended to directly use an HTTP client such
-   *   as Guzzle instead.
+   *   testing REST APIs it is recommended to obtain a separate HTTP client
+   *   using getHttpClient() and performing requests that way.
    *
    * @return string
    *   The retrieved HTML string, also available as $this->getRawContent()
+   *
+   * @see \Drupal\Tests\BrowserTestBase::getHttpClient()
    */
   protected function drupalGet($path, array $options = [], array $headers = []) {
     $options['absolute'] = TRUE;
@@ -1167,6 +1206,7 @@
   protected function clickLink($label, $index = 0) {
     $label = (string) $label;
     $links = $this->getSession()->getPage()->findAll('named', ['link', $label]);
+    $this->assertArrayHasKey($index, $links, 'The link ' . $label . ' was not found on the page.');
     $links[$index]->click();
   }
 
@@ -1308,6 +1348,28 @@
   }
 
   /**
+   * Transforms a nested array into a flat array suitable for drupalPostForm().
+   *
+   * @param array $values
+   *   A multi-dimensional form values array to convert.
+   *
+   * @return array
+   *   The flattened $edit array suitable for BrowserTestBase::drupalPostForm().
+   */
+  protected function translatePostValues(array $values) {
+    $edit = [];
+    // The easiest and most straightforward way to translate values suitable for
+    // BrowserTestBase::drupalPostForm() is to actually build the POST data
+    // string and convert the resulting key/value pairs back into a flat array.
+    $query = http_build_query($values);
+    foreach (explode('&', $query) as $item) {
+      list($key, $value) = explode('=', $item);
+      $edit[urldecode($key)] = urldecode($value);
+    }
+    return $edit;
+  }
+
+  /**
    * Checks for meta refresh tag and if found call drupalGet() recursively.
    *
    * This function looks for the http-equiv attribute to be set to "Refresh" and