Chris@17: Headers:
' . Html::escape(var_export($flattened_headers, TRUE)) . '
'; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Returns headers in HTML output format. Chris@17: * Chris@17: * @return string Chris@17: * HTML output headers. Chris@17: */ Chris@17: protected function getHtmlOutputHeaders() { Chris@17: return $this->formatHtmlOutputHeaders($this->getSession()->getResponseHeaders()); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Logs a HTML output message in a text file. Chris@17: * Chris@17: * The link to the HTML output message will be printed by the results printer. Chris@17: * Chris@17: * @param string|null $message Chris@17: * (optional) The HTML output message to be stored. If not supplied the Chris@17: * current page content is used. Chris@17: * Chris@17: * @see \Drupal\Tests\Listeners\VerbosePrinter::printResult() Chris@17: */ Chris@17: protected function htmlOutput($message = NULL) { Chris@17: if (!$this->htmlOutputEnabled) { Chris@17: return; Chris@17: } Chris@17: $message = $message ?: $this->getSession()->getPage()->getContent(); Chris@17: $message = '
ID #' . $this->htmlOutputCounter . ' (Previous | Next)
' . $message; Chris@17: $html_output_filename = $this->htmlOutputClassName . '-' . $this->htmlOutputCounter . '-' . $this->htmlOutputTestId . '.html'; Chris@17: file_put_contents($this->htmlOutputDirectory . '/' . $html_output_filename, $message); Chris@17: file_put_contents($this->htmlOutputCounterStorage, $this->htmlOutputCounter++); Chris@17: // Do not use file_create_url() as the module_handler service might not be Chris@17: // available. Chris@17: $uri = $GLOBALS['base_url'] . '/sites/simpletest/browser_output/' . $html_output_filename; Chris@17: file_put_contents($this->htmlOutputFile, $uri . "\n", FILE_APPEND); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Creates the directory to store browser output. Chris@17: * Chris@17: * Creates the directory to store browser output in if a file to write Chris@17: * URLs to has been created by \Drupal\Tests\Listeners\HtmlOutputPrinter. Chris@17: */ Chris@17: protected function initBrowserOutputFile() { Chris@17: $browser_output_file = getenv('BROWSERTEST_OUTPUT_FILE'); Chris@17: $this->htmlOutputEnabled = is_file($browser_output_file); Chris@17: if ($this->htmlOutputEnabled) { Chris@17: $this->htmlOutputFile = $browser_output_file; Chris@17: $this->htmlOutputClassName = str_replace("\\", "_", get_called_class()); Chris@17: $this->htmlOutputDirectory = DRUPAL_ROOT . '/sites/simpletest/browser_output'; Chris@17: // Do not use the file_system service so this method can be called before Chris@18: // it is available. Checks !is_dir() twice around mkdir() because a Chris@18: // concurrent test might have made the directory and caused mkdir() to Chris@18: // fail. In this case we can still use the directory even though we failed Chris@18: // to make it. Chris@18: if (!is_dir($this->htmlOutputDirectory) && !mkdir($this->htmlOutputDirectory, 0775, TRUE) && !is_dir($this->htmlOutputDirectory)) { Chris@18: throw new \RuntimeException(sprintf('Unable to create directory: %s', $this->htmlOutputDirectory)); Chris@17: } Chris@17: if (!file_exists($this->htmlOutputDirectory . '/.htaccess')) { Chris@17: file_put_contents($this->htmlOutputDirectory . '/.htaccess', "\nExpiresActive Off\n\n"); Chris@17: } Chris@17: $this->htmlOutputCounterStorage = $this->htmlOutputDirectory . '/' . $this->htmlOutputClassName . '.counter'; Chris@17: $this->htmlOutputTestId = str_replace('sites/simpletest/', '', $this->siteDirectory); Chris@17: if (is_file($this->htmlOutputCounterStorage)) { Chris@17: $this->htmlOutputCounter = max(1, (int) file_get_contents($this->htmlOutputCounterStorage)) + 1; Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Provides a Guzzle middleware handler to log every response received. Chris@17: * Chris@17: * @return callable Chris@17: * The callable handler that will do the logging. Chris@17: */ Chris@17: protected function getResponseLogHandler() { Chris@17: return function (callable $handler) { Chris@17: return function (RequestInterface $request, array $options) use ($handler) { Chris@17: return $handler($request, $options) Chris@17: ->then(function (ResponseInterface $response) use ($request) { Chris@17: if ($this->htmlOutputEnabled) { Chris@17: Chris@17: $caller = $this->getTestMethodCaller(); Chris@17: $html_output = 'Called from ' . $caller['function'] . ' line ' . $caller['line']; Chris@17: $html_output .= '
' . $request->getMethod() . ' request to: ' . $request->getUri(); Chris@17: Chris@17: // On redirect responses (status code starting with '3') we need Chris@17: // to remove the meta tag that would do a browser refresh. We Chris@17: // don't want to redirect developers away when they look at the Chris@17: // debug output file in their browser. Chris@17: $body = $response->getBody(); Chris@17: $status_code = (string) $response->getStatusCode(); Chris@17: if ($status_code[0] === '3') { Chris@17: $body = preg_replace('##', '', $body, 1); Chris@17: } Chris@17: $html_output .= '
' . $body; Chris@17: $html_output .= $this->formatHtmlOutputHeaders($response->getHeaders()); Chris@17: Chris@17: $this->htmlOutput($html_output); Chris@17: } Chris@17: return $response; Chris@17: }); Chris@17: }; Chris@17: }; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Retrieves the current calling line in the class under test. Chris@17: * Chris@17: * @return array Chris@17: * An associative array with keys 'file', 'line' and 'function'. Chris@17: */ Chris@17: protected function getTestMethodCaller() { Chris@17: $backtrace = debug_backtrace(); Chris@17: // Find the test class that has the test method. Chris@17: while ($caller = Error::getLastCaller($backtrace)) { Chris@17: if (isset($caller['class']) && $caller['class'] === get_class($this)) { Chris@17: break; Chris@17: } Chris@17: // If the test method is implemented by a test class's parent then the Chris@17: // class name of $this will not be part of the backtrace. Chris@17: // In that case we process the backtrace until the caller is not a Chris@17: // subclass of $this and return the previous caller. Chris@17: if (isset($last_caller) && (!isset($caller['class']) || !is_subclass_of($this, $caller['class']))) { Chris@17: // Return the last caller since that has to be the test class. Chris@17: $caller = $last_caller; Chris@17: break; Chris@17: } Chris@17: // Otherwise we have not reached our test class yet: save the last caller Chris@17: // and remove an element from to backtrace to process the next call. Chris@17: $last_caller = $caller; Chris@17: array_shift($backtrace); Chris@17: } Chris@17: Chris@17: return $caller; Chris@17: } Chris@17: Chris@17: }