annotate core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 <?php
Chris@4 2
Chris@4 3 namespace Drupal\Tests;
Chris@4 4
Chris@4 5 use Drupal\Component\Utility\Html;
Chris@4 6 use Drupal\Core\Utility\Error;
Chris@4 7 use Psr\Http\Message\RequestInterface;
Chris@4 8 use Psr\Http\Message\ResponseInterface;
Chris@4 9
Chris@4 10 /**
Chris@4 11 * Provides the debug functions for browser tests.
Chris@4 12 */
Chris@4 13 trait BrowserHtmlDebugTrait {
Chris@4 14
Chris@4 15 /**
Chris@4 16 * Class name for HTML output logging.
Chris@4 17 *
Chris@4 18 * @var string
Chris@4 19 */
Chris@4 20 protected $htmlOutputClassName;
Chris@4 21
Chris@4 22 /**
Chris@4 23 * Directory name for HTML output logging.
Chris@4 24 *
Chris@4 25 * @var string
Chris@4 26 */
Chris@4 27 protected $htmlOutputDirectory;
Chris@4 28
Chris@4 29 /**
Chris@4 30 * Counter storage for HTML output logging.
Chris@4 31 *
Chris@4 32 * @var string
Chris@4 33 */
Chris@4 34 protected $htmlOutputCounterStorage;
Chris@4 35
Chris@4 36 /**
Chris@4 37 * Counter for HTML output logging.
Chris@4 38 *
Chris@4 39 * @var int
Chris@4 40 */
Chris@4 41 protected $htmlOutputCounter = 1;
Chris@4 42
Chris@4 43 /**
Chris@4 44 * HTML output output enabled.
Chris@4 45 *
Chris@4 46 * @var bool
Chris@4 47 */
Chris@4 48 protected $htmlOutputEnabled = FALSE;
Chris@4 49
Chris@4 50 /**
Chris@4 51 * The file name to write the list of URLs to.
Chris@4 52 *
Chris@4 53 * This file is read by the PHPUnit result printer.
Chris@4 54 *
Chris@4 55 * @var string
Chris@4 56 *
Chris@4 57 * @see \Drupal\Tests\Listeners\HtmlOutputPrinter
Chris@4 58 */
Chris@4 59 protected $htmlOutputFile;
Chris@4 60
Chris@4 61 /**
Chris@4 62 * HTML output test ID.
Chris@4 63 *
Chris@4 64 * @var int
Chris@4 65 */
Chris@4 66 protected $htmlOutputTestId;
Chris@4 67
Chris@4 68 /**
Chris@4 69 * Formats HTTP headers as string for HTML output logging.
Chris@4 70 *
Chris@4 71 * @param array[] $headers
Chris@4 72 * Headers that should be formatted.
Chris@4 73 *
Chris@4 74 * @return string
Chris@4 75 * The formatted HTML string.
Chris@4 76 */
Chris@4 77 protected function formatHtmlOutputHeaders(array $headers) {
Chris@4 78 $flattened_headers = array_map(function ($header) {
Chris@4 79 if (is_array($header)) {
Chris@4 80 return implode(';', array_map('trim', $header));
Chris@4 81 }
Chris@4 82 else {
Chris@4 83 return $header;
Chris@4 84 }
Chris@4 85 }, $headers);
Chris@4 86 return '<hr />Headers: <pre>' . Html::escape(var_export($flattened_headers, TRUE)) . '</pre>';
Chris@4 87 }
Chris@4 88
Chris@4 89 /**
Chris@4 90 * Returns headers in HTML output format.
Chris@4 91 *
Chris@4 92 * @return string
Chris@4 93 * HTML output headers.
Chris@4 94 */
Chris@4 95 protected function getHtmlOutputHeaders() {
Chris@4 96 return $this->formatHtmlOutputHeaders($this->getSession()->getResponseHeaders());
Chris@4 97 }
Chris@4 98
Chris@4 99 /**
Chris@4 100 * Logs a HTML output message in a text file.
Chris@4 101 *
Chris@4 102 * The link to the HTML output message will be printed by the results printer.
Chris@4 103 *
Chris@4 104 * @param string|null $message
Chris@4 105 * (optional) The HTML output message to be stored. If not supplied the
Chris@4 106 * current page content is used.
Chris@4 107 *
Chris@4 108 * @see \Drupal\Tests\Listeners\VerbosePrinter::printResult()
Chris@4 109 */
Chris@4 110 protected function htmlOutput($message = NULL) {
Chris@4 111 if (!$this->htmlOutputEnabled) {
Chris@4 112 return;
Chris@4 113 }
Chris@4 114 $message = $message ?: $this->getSession()->getPage()->getContent();
Chris@4 115 $message = '<hr />ID #' . $this->htmlOutputCounter . ' (<a href="' . $this->htmlOutputClassName . '-' . ($this->htmlOutputCounter - 1) . '-' . $this->htmlOutputTestId . '.html">Previous</a> | <a href="' . $this->htmlOutputClassName . '-' . ($this->htmlOutputCounter + 1) . '-' . $this->htmlOutputTestId . '.html">Next</a>)<hr />' . $message;
Chris@4 116 $html_output_filename = $this->htmlOutputClassName . '-' . $this->htmlOutputCounter . '-' . $this->htmlOutputTestId . '.html';
Chris@4 117 file_put_contents($this->htmlOutputDirectory . '/' . $html_output_filename, $message);
Chris@4 118 file_put_contents($this->htmlOutputCounterStorage, $this->htmlOutputCounter++);
Chris@4 119 // Do not use file_create_url() as the module_handler service might not be
Chris@4 120 // available.
Chris@4 121 $uri = $GLOBALS['base_url'] . '/sites/simpletest/browser_output/' . $html_output_filename;
Chris@4 122 file_put_contents($this->htmlOutputFile, $uri . "\n", FILE_APPEND);
Chris@4 123 }
Chris@4 124
Chris@4 125 /**
Chris@4 126 * Creates the directory to store browser output.
Chris@4 127 *
Chris@4 128 * Creates the directory to store browser output in if a file to write
Chris@4 129 * URLs to has been created by \Drupal\Tests\Listeners\HtmlOutputPrinter.
Chris@4 130 */
Chris@4 131 protected function initBrowserOutputFile() {
Chris@4 132 $browser_output_file = getenv('BROWSERTEST_OUTPUT_FILE');
Chris@4 133 $this->htmlOutputEnabled = is_file($browser_output_file);
Chris@4 134 if ($this->htmlOutputEnabled) {
Chris@4 135 $this->htmlOutputFile = $browser_output_file;
Chris@4 136 $this->htmlOutputClassName = str_replace("\\", "_", get_called_class());
Chris@4 137 $this->htmlOutputDirectory = DRUPAL_ROOT . '/sites/simpletest/browser_output';
Chris@4 138 // Do not use the file_system service so this method can be called before
Chris@5 139 // it is available. Checks !is_dir() twice around mkdir() because a
Chris@5 140 // concurrent test might have made the directory and caused mkdir() to
Chris@5 141 // fail. In this case we can still use the directory even though we failed
Chris@5 142 // to make it.
Chris@5 143 if (!is_dir($this->htmlOutputDirectory) && !mkdir($this->htmlOutputDirectory, 0775, TRUE) && !is_dir($this->htmlOutputDirectory)) {
Chris@5 144 throw new \RuntimeException(sprintf('Unable to create directory: %s', $this->htmlOutputDirectory));
Chris@4 145 }
Chris@4 146 if (!file_exists($this->htmlOutputDirectory . '/.htaccess')) {
Chris@4 147 file_put_contents($this->htmlOutputDirectory . '/.htaccess', "<IfModule mod_expires.c>\nExpiresActive Off\n</IfModule>\n");
Chris@4 148 }
Chris@4 149 $this->htmlOutputCounterStorage = $this->htmlOutputDirectory . '/' . $this->htmlOutputClassName . '.counter';
Chris@4 150 $this->htmlOutputTestId = str_replace('sites/simpletest/', '', $this->siteDirectory);
Chris@4 151 if (is_file($this->htmlOutputCounterStorage)) {
Chris@4 152 $this->htmlOutputCounter = max(1, (int) file_get_contents($this->htmlOutputCounterStorage)) + 1;
Chris@4 153 }
Chris@4 154 }
Chris@4 155 }
Chris@4 156
Chris@4 157 /**
Chris@4 158 * Provides a Guzzle middleware handler to log every response received.
Chris@4 159 *
Chris@4 160 * @return callable
Chris@4 161 * The callable handler that will do the logging.
Chris@4 162 */
Chris@4 163 protected function getResponseLogHandler() {
Chris@4 164 return function (callable $handler) {
Chris@4 165 return function (RequestInterface $request, array $options) use ($handler) {
Chris@4 166 return $handler($request, $options)
Chris@4 167 ->then(function (ResponseInterface $response) use ($request) {
Chris@4 168 if ($this->htmlOutputEnabled) {
Chris@4 169
Chris@4 170 $caller = $this->getTestMethodCaller();
Chris@4 171 $html_output = 'Called from ' . $caller['function'] . ' line ' . $caller['line'];
Chris@4 172 $html_output .= '<hr />' . $request->getMethod() . ' request to: ' . $request->getUri();
Chris@4 173
Chris@4 174 // On redirect responses (status code starting with '3') we need
Chris@4 175 // to remove the meta tag that would do a browser refresh. We
Chris@4 176 // don't want to redirect developers away when they look at the
Chris@4 177 // debug output file in their browser.
Chris@4 178 $body = $response->getBody();
Chris@4 179 $status_code = (string) $response->getStatusCode();
Chris@4 180 if ($status_code[0] === '3') {
Chris@4 181 $body = preg_replace('#<meta http-equiv="refresh" content=.+/>#', '', $body, 1);
Chris@4 182 }
Chris@4 183 $html_output .= '<hr />' . $body;
Chris@4 184 $html_output .= $this->formatHtmlOutputHeaders($response->getHeaders());
Chris@4 185
Chris@4 186 $this->htmlOutput($html_output);
Chris@4 187 }
Chris@4 188 return $response;
Chris@4 189 });
Chris@4 190 };
Chris@4 191 };
Chris@4 192 }
Chris@4 193
Chris@4 194 /**
Chris@4 195 * Retrieves the current calling line in the class under test.
Chris@4 196 *
Chris@4 197 * @return array
Chris@4 198 * An associative array with keys 'file', 'line' and 'function'.
Chris@4 199 */
Chris@4 200 protected function getTestMethodCaller() {
Chris@4 201 $backtrace = debug_backtrace();
Chris@4 202 // Find the test class that has the test method.
Chris@4 203 while ($caller = Error::getLastCaller($backtrace)) {
Chris@4 204 if (isset($caller['class']) && $caller['class'] === get_class($this)) {
Chris@4 205 break;
Chris@4 206 }
Chris@4 207 // If the test method is implemented by a test class's parent then the
Chris@4 208 // class name of $this will not be part of the backtrace.
Chris@4 209 // In that case we process the backtrace until the caller is not a
Chris@4 210 // subclass of $this and return the previous caller.
Chris@4 211 if (isset($last_caller) && (!isset($caller['class']) || !is_subclass_of($this, $caller['class']))) {
Chris@4 212 // Return the last caller since that has to be the test class.
Chris@4 213 $caller = $last_caller;
Chris@4 214 break;
Chris@4 215 }
Chris@4 216 // Otherwise we have not reached our test class yet: save the last caller
Chris@4 217 // and remove an element from to backtrace to process the next call.
Chris@4 218 $last_caller = $caller;
Chris@4 219 array_shift($backtrace);
Chris@4 220 }
Chris@4 221
Chris@4 222 return $caller;
Chris@4 223 }
Chris@4 224
Chris@4 225 }