diff --git a/core/modules/simpletest/src/BrowserTestBase.php b/core/modules/simpletest/src/BrowserTestBase.php index bcc3cd5..70ea1f9 100644 --- a/core/modules/simpletest/src/BrowserTestBase.php +++ b/core/modules/simpletest/src/BrowserTestBase.php @@ -36,7 +36,6 @@ * Note that this class does not yet have feature parity with WebTestBase, so * WebTestBase should be used where possible. In particular, this class does * not yet have the following features: - * - verbose output - see https://www.drupal.org/node/2469721 * - ajax form emulation - see https://www.drupal.org/node/2469713 * * Tests extending BrowserTestBase must exist in the @@ -227,41 +226,6 @@ protected $preserveGlobalState = FALSE; /** - * Class name for verbose logging. - * - * @var string - */ - protected $verboseClassName; - - /** - * Directory name for verbose logging. - * - * @var string - */ - protected $verboseDirectory; - - /** - * Counter storage for verbose logging. - * - * @var string - */ - protected $verboseCounterStorage; - - /** - * Counter for verbose logging. - * - * @var int - */ - protected $verboseCounter = 1; - - /** - * Verbose output enabled. - * - * @var bool - */ - protected $verboseOutput = FALSE; - - /** * Initializes Mink sessions. */ protected function initMink() { @@ -403,15 +367,6 @@ protected function setUp() { } } } - - $verbose_directory = getenv('SIMPLETEST_VERBOSE_DIRECTORY'); - $this->verboseOutput = (bool) $verbose_directory; - $this->verboseClassName = str_replace("\\", "_", get_called_class()); - $this->verboseDirectory = rtrim($verbose_directory, '/'); - $this->verboseCounterStorage = $this->verboseDirectory . '/' . $this->verboseClassName . '.counter'; - if (is_file($this->verboseCounterStorage)) { - $this->verboseCounter = max(1, (int) file_get_contents($this->verboseCounterStorage)); - } } /** @@ -550,14 +505,6 @@ protected function drupalGet($path, array $options = array()) { // Ensure that any changes to variables in the other thread are picked up. $this->refreshVariables(); - if ($this->verboseOutput) { - $verbose = 'GET request to: ' . $url . - '
Ending URL: ' . $this->getSession()->getCurrentUrl(); - $verbose .= '
' . $out; - $verbose .= $this->getVerboseHeaders(); - $this->verbose($verbose); - } - return $out; } @@ -843,14 +790,6 @@ protected function submitForm(array $edit, $submit, $form_html_id = NULL) { // Ensure that any changes to variables in the other thread are picked up. $this->refreshVariables(); - if ($this->verboseOutput) { - $out = $this->getSession()->getPage()->getContent(); - $verbose = 'POST request to: ' . $action . - '
Ending URL: ' . $this->getSession()->getCurrentUrl(); - $verbose .= '
' . $out; - $verbose .= $this->getVerboseHeaders(); - $this->verbose($verbose); - } } /** @@ -1416,36 +1355,4 @@ public function __sleep() { return []; } - /** - * Logs a verbose message in a text file. - * - * The link to the verbose message will be printed by the results printer. - * - * @param string $message - * The verbose message to be stored. - * - * @see \Drupal\Tests\Listeners\VerbosePrinter::printResult() - */ - protected function verbose($message) { - if (!$this->verboseOutput) { - return; - } - $message = '
ID #' . $this->verboseCounter . ' (Previous | Next)
' . $message; - $verbose_filename = $this->verboseClassName . '-' . $this->verboseCounter . '.html'; - file_put_contents($this->verboseDirectory . '/' . $verbose_filename, $message); - file_put_contents($this->verboseCounterStorage, $this->verboseCounter++); - } - - /** - * Returns headers in verbose format. - * - * @return string - * Verbose headers. - */ - protected function getVerboseHeaders() { - return '
Headers:
' . Html::escape(var_export(array_map(function($headers) {
-      return implode(';', array_map('trim', $headers));
-    }, $this->getSession()->getResponseHeaders()), TRUE)) . '
'; - } - } diff --git a/core/modules/simpletest/src/DebugBrowserTestBase.php b/core/modules/simpletest/src/DebugBrowserTestBase.php new file mode 100644 index 0000000..b0d55ed --- /dev/null +++ b/core/modules/simpletest/src/DebugBrowserTestBase.php @@ -0,0 +1,181 @@ +verboseOutput = (bool) $verbose_directory; + $this->verboseClassName = str_replace("\\", "_", get_called_class()); + $this->verboseDirectory = rtrim($verbose_directory, '/'); + $this->verboseCounterStorage = $this->verboseDirectory . '/' . $this->verboseClassName . '.counter'; + $this->verboseTestId = str_replace('sites/simpletest/', '', $this->siteDirectory); + if (is_file($this->verboseCounterStorage)) { + $this->verboseCounter = max(1, (int) file_get_contents($this->verboseCounterStorage)) + 1; + } + } + + /** + * {@inheritdoc} + */ + protected function drupalGet($path, array $options = array()) { + $out = parent::drupalGet($path, $options); + // The URL generator service is not necessarily available yet; e.g., in + // interactive installer tests. + if ($this->container->has('url_generator')) { + if (UrlHelper::isExternal($path)) { + $url = Url::fromUri($path, $options)->toString(); + } + else { + // This is needed for language prefixing. + $options['path_processing'] = TRUE; + $url = Url::fromUri('base:/' . $path, $options)->toString(); + } + } + else { + $url = $this->getAbsoluteUrl($path); + } + if ($this->verboseOutput) { + $verbose = 'GET request to: ' . $url . + '
Ending URL: ' . $this->getSession()->getCurrentUrl(); + $verbose .= '
' . $out; + $verbose .= $this->getVerboseHeaders(); + $this->verbose($verbose); + } + return $out; + } + + /** + * {@inheritdoc} + */ + protected function submitForm(array $edit, $submit, $form_html_id = NULL) { + // Get the form. + $assert_session = $this->assertSession(); + if (isset($form_html_id)) { + $form = $assert_session->elementExists('xpath', "//form[@id='$form_html_id']"); + $action = $form->getAttribute('action'); + } + else { + $submit_button = $assert_session->buttonExists($submit); + $form = $assert_session->elementExists('xpath', './ancestor::form', $submit_button); + $action = $form->getAttribute('action'); + } + parent::submitForm($edit, $submit, $form_html_id); + if ($this->verboseOutput) { + $out = $this->getSession()->getPage()->getContent(); + $verbose = 'POST request to: ' . $action . + '
Ending URL: ' . $this->getSession()->getCurrentUrl(); + $verbose .= '
' . $out; + $verbose .= $this->getVerboseHeaders(); + $this->verbose($verbose); + } + } + + /** + * Logs a verbose message in a text file. + * + * The link to the verbose message will be printed by the results printer. + * + * @param string $message + * The verbose message to be stored. + * + * @see \Drupal\Tests\Listeners\VerbosePrinter::printResult() + */ + protected function verbose($message) { + if (!$this->verboseOutput) { + return; + } + $message = '
ID #' . $this->verboseCounter . ' (Previous | Next)
' . $message; + $verbose_filename = $this->verboseClassName . '-' . $this->verboseCounter . '-' . $this->verboseTestId . '.html'; + file_put_contents($this->verboseDirectory . '/' . $verbose_filename, $message); + file_put_contents($this->verboseCounterStorage, $this->verboseCounter++); + } + + /** + * Returns headers in verbose format. + * + * @return string + * Verbose headers. + */ + protected function getVerboseHeaders() { + return '
Headers:
' . Html::escape(var_export(array_map(function($headers) {
+      return implode(';', array_map('trim', $headers));
+    }, $this->getSession()->getResponseHeaders()), TRUE)) . '
'; + } + +} diff --git a/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php b/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php index d660b1f..e78e40c 100644 --- a/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php +++ b/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php @@ -7,14 +7,14 @@ namespace Drupal\Tests\simpletest\Functional; -use Drupal\simpletest\BrowserTestBase; +use Drupal\simpletest\DebugBrowserTestBase; /** * Tests BrowserTestBase functionality. * * @group simpletest */ -class BrowserTestBaseTest extends BrowserTestBase { +class BrowserTestBaseTest extends DebugBrowserTestBase { /** * Modules to enable. diff --git a/core/tests/Drupal/Tests/Listeners/VerbosePrinter.php b/core/tests/Drupal/Tests/Listeners/VerbosePrinter.php index ea3483b..723bf5f 100644 --- a/core/tests/Drupal/Tests/Listeners/VerbosePrinter.php +++ b/core/tests/Drupal/Tests/Listeners/VerbosePrinter.php @@ -17,7 +17,7 @@ class VerbosePrinter extends \PHPUnit_TextUI_ResultPrinter { */ public function __construct($out, $verbose, $colors, $debug, $numberOfColumns) { parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns); - if ($verbose_directory = getenv('SIMPLETEST_VERBOSE_DIRECTORY')) { + if ($verbose_directory = getenv('BROWSERTEST_OUTPUT_DIRECTORY')) { // Initialize verbose debugging. $this->directory = rtrim($verbose_directory, '/'); // Check if directory exists. @@ -48,9 +48,9 @@ public function printResult(\PHPUnit_Framework_TestResult $result) { if (!$this->directory) { $this->writeWithColor('bg-yellow, fg-black', 'No verbose output was generated'); $this->writeNewLine(); - $this->write('To generate verbose output for functional tests, set the SIMPLETEST_VERBOSE_DIRECTORY environment variable.'); + $this->write('To generate verbose output for functional tests, set the BROWSERTEST_OUTPUT_DIRECTORY environment variable.'); $this->writeNewLine(); - $this->write('e.g export SIMPLETEST_VERBOSE_DIRECTORY="/path/to/webroot/sites/default/files/simpletest/verbose".'); + $this->write('e.g export BROWSERTEST_OUTPUT_DIRECTORY="/path/to/webroot/sites/default/files/simpletest/verbose".'); $this->writeNewLine(); return; }