diff --git a/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/HtmlOutputLogger.php b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/HtmlOutputLogger.php index 9bc1a117b2..02162e9c83 100644 --- a/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/HtmlOutputLogger.php +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/HtmlOutputLogger.php @@ -33,6 +33,7 @@ * @throws \PHPUnit\Event\EventFacadeIsSealedException * @throws \PHPUnit\Util\Exception * @throws \PHPUnit\Event\UnknownSubscriberTypeException + * @throws \RuntimeException */ private function __construct(string $browserTestOutputDirectory) { $this->browserOutputFile = $this->determineBrowserOutputFile($browserTestOutputDirectory); @@ -47,6 +48,7 @@ private function __construct(string $browserTestOutputDirectory) { * @throws \PHPUnit\Event\EventFacadeIsSealedException * @throws \PHPUnit\Util\Exception * @throws \PHPUnit\Event\UnknownSubscriberTypeException + * @throws \RuntimeException */ public static function init(string $browserTestOutputDirectory): void { if (self::$instance === NULL) { @@ -57,9 +59,18 @@ public static function init(string $browserTestOutputDirectory): void { /** * @todo */ + public static function isEnabled(): bool { + return self::$instance !== NULL; + } + + /** + * @todo + * + * @throws \RuntimeException + */ public static function log(string $logEntry): void { - if (self::$instance === NULL) { - return; + if (!self::isEnabled()) { + throw new \RuntimeException("HTML output is not enabled"); } self::$instance->write($logEntry); } @@ -69,13 +80,13 @@ public static function log(string $logEntry): void { */ private function determineBrowserOutputFile(string $browserTestOutputDirectory): string { // Convert to a canonicalized absolute pathname just in case the current - // working directory is changed. + // working directory has changed. if (($html_output_directory = realpath(rtrim($browserTestOutputDirectory, '/'))) === FALSE) { throw new \RuntimeException("HTML output directory $browserTestOutputDirectory is not a writable directory"); } - if (($browserOutputFile = tempnam($browserTestOutputDirectory, 'browser_output_')) === FALSE) { - throw new \RuntimeException("Unable to create a temporary file in $browserTestOutputDirectory"); + if (($browserOutputFile = tempnam($html_output_directory, 'browser_output_')) === FALSE) { + throw new \RuntimeException("Unable to create a temporary file in $html_output_directory"); } return $browserOutputFile; diff --git a/core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php b/core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php index c2d51fb797..22c1fb67a0 100644 --- a/core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php +++ b/core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php @@ -57,6 +57,8 @@ * @var string * * @see \Drupal\Tests\Listeners\HtmlOutputPrinter + * + * @todo Remove when PHPUnit 9 is no longer supported. */ protected $htmlOutputFile; @@ -128,6 +130,7 @@ protected function htmlOutput($message = NULL) { // Do not use the file_url_generator service as the module_handler service // might not be available. $uri = $this->htmlOutputBaseUrl . '/sites/simpletest/browser_output/' . $html_output_filename; + // @todo Cleanup below when PHPUnit 9 is no longer supported. if (RunnerVersion::getMajor() > 9) { HtmlOutputLogger::log($uri . "\n"); } @@ -143,11 +146,19 @@ protected function htmlOutput($message = NULL) { * URLs to has been created by \Drupal\Tests\Listeners\HtmlOutputPrinter. */ protected function initBrowserOutputFile() { - $browser_output_file = getenv('BROWSERTEST_OUTPUT_FILE'); - $this->htmlOutputEnabled = is_file($browser_output_file); + // @todo Cleanup below when PHPUnit 9 is no longer supported. + if (RunnerVersion::getMajor() > 9) { + $this->htmlOutputEnabled = HtmlOutputLogger::isEnabled(); + } + else { + $browser_output_file = getenv('BROWSERTEST_OUTPUT_FILE'); + $this->htmlOutputEnabled = is_file($browser_output_file); + if ($this->htmlOutputEnabled) { + $this->htmlOutputFile = $browser_output_file; + } + } $this->htmlOutputBaseUrl = getenv('BROWSERTEST_OUTPUT_BASE_URL') ?: $GLOBALS['base_url']; if ($this->htmlOutputEnabled) { - $this->htmlOutputFile = $browser_output_file; $this->htmlOutputClassName = str_replace("\\", "_", static::class); $this->htmlOutputDirectory = DRUPAL_ROOT . '/sites/simpletest/browser_output'; // Do not use the file_system service so this method can be called before