diff --git a/core/lib/Drupal/Core/Composer/Composer.php b/core/lib/Drupal/Core/Composer/Composer.php index 3e2dc6f83b..8f030865e5 100644 --- a/core/lib/Drupal/Core/Composer/Composer.php +++ b/core/lib/Drupal/Core/Composer/Composer.php @@ -332,7 +332,7 @@ public static function upgradePHPUnit(Event $event) { * TRUE if the PHPUnit needs to be upgraded, FALSE if not. */ public static function upgradePHPUnitCheck($phpunit_version) { - return !(version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '8.1') >= 0 && version_compare($phpunit_version, '10.0') < 0); + return !(version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '8.1') >= 0 && version_compare($phpunit_version, '10') < 0); } } diff --git a/core/phpunit.xml.dist b/core/phpunit.xml.dist index 0425d8e51f..7ba22e820e 100644 --- a/core/phpunit.xml.dist +++ b/core/phpunit.xml.dist @@ -6,14 +6,15 @@ or your current system user. See core/tests/README.md and https://www.drupal.org/node/2116263 for details. --> - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"> diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 9fe4bff2f7..7b82f5c0a4 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -149,10 +149,10 @@ } simpletest_script_print("PHP version: " . phpversion() . " - PHPUnit version: " . Version::id() . "\n", SIMPLETEST_SCRIPT_COLOR_PASS); -//if (!Composer::upgradePHPUnitCheck(Version::id())) { -// simpletest_script_print_error("PHPUnit testing framework version 10 or greater is required when running on PHP 8.1 or greater. Run the command 'composer run-script drupal-phpunit-upgrade' in order to fix this."); -// exit(SIMPLETEST_SCRIPT_EXIT_FAILURE); -//} +if (!Composer::upgradePHPUnitCheck(Version::id())) { + simpletest_script_print_error("PHPUnit testing framework version 10 or greater is required when running on PHP 8.1 or greater. Run the command 'composer run-script drupal-phpunit-upgrade' in order to fix this."); + exit(SIMPLETEST_SCRIPT_EXIT_FAILURE); +} $test_list = simpletest_script_get_test_list(); diff --git a/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php index 16041b4846..101930018e 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php @@ -95,7 +95,7 @@ protected function tearDown(): void { if ($this->mink) { // @todo ::getStatus was removed from PHPUnit 10, we probably need to // intercept an event instead. - if (RunnerVersion::getMajor < 10) { + if (RunnerVersion::getMajor() < 10) { $status = $this->getStatus(); if ($status === BaseTestRunner::STATUS_ERROR || $status === BaseTestRunner::STATUS_WARNING || $status === BaseTestRunner::STATUS_FAILURE) { // Ensure we capture the output at point of failure. diff --git a/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/HtmlOutputLogger.php b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/HtmlOutputLogger.php new file mode 100644 index 0000000000..2c732d14e3 --- /dev/null +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/HtmlOutputLogger.php @@ -0,0 +1,104 @@ +browserOutputFile = ''; + + if ($html_output_directory = getenv('BROWSERTEST_OUTPUT_DIRECTORY')) { + // Initialize html output debugging. + $html_output_directory = rtrim($html_output_directory, '/'); + + // Check if directory exists. + if (!is_dir($html_output_directory) || !is_writable($html_output_directory)) { + $this->writeWithColor('bg-red, fg-black', "HTML output directory $html_output_directory is not a writable directory."); + } + else { + // Convert to a canonicalized absolute pathname just in case the current + // working directory is changed. + $html_output_directory = realpath($html_output_directory); + $this->browserOutputFile = tempnam($html_output_directory, 'browser_output_'); + if ($this->browserOutputFile) { + touch($this->browserOutputFile); + } + else { + $this->writeWithColor('bg-red, fg-black', "Unable to create a temporary file in $html_output_directory."); + } + } + } + + if ($this->browserOutputFile !== '') { + putenv('BROWSERTEST_OUTPUT_FILE=' . $this->browserOutputFile); + } + else { + // Remove any environment variable. + putenv('BROWSERTEST_OUTPUT_FILE'); + } + } + + /** + * Prints the list of HTML output generated during the test. + */ + public function testRunnerFinished(TestRunnerFinished $event): void { + if ($this->browserOutputFile !== '') { + $contents = file_get_contents($this->browserOutputFile); + if ($contents) { + $this->writeNewLine(); + $this->writeWithColor('bg-yellow, fg-black', 'HTML output was generated'); + $this->write($contents); + } + // No need to keep the file around any more. + unlink($this->browserOutputFile); + } + } + + /** + * {@inheritdoc} + */ + public function write(string $buffer): void { + $buffer = Html::escape($buffer); + // Turn HTML output URLs into clickable link tags. + $url_pattern = '@https?://[^\s]+@'; + $buffer = preg_replace($url_pattern, '$0', $buffer); + // Make the output readable in HTML by breaking up lines properly. + $buffer = nl2br($buffer); + + print $buffer; + } + +} diff --git a/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/Subscriber.php b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/Subscriber.php new file mode 100644 index 0000000000..d2a29592b3 --- /dev/null +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/Subscriber.php @@ -0,0 +1,22 @@ +logger = $logger; + } + + protected function logger(): HtmlOutputLogger { + return $this->logger; + } + +} diff --git a/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/TestRunnerFinishedSubscriber.php b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/TestRunnerFinishedSubscriber.php new file mode 100644 index 0000000000..2e870f2955 --- /dev/null +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/TestRunnerFinishedSubscriber.php @@ -0,0 +1,16 @@ +logger()->testRunnerFinished($event); + } +} diff --git a/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/TestRunnerStartedSubscriber.php b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/TestRunnerStartedSubscriber.php new file mode 100644 index 0000000000..503473bb63 --- /dev/null +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit10/Logging/Subscriber/TestRunnerStartedSubscriber.php @@ -0,0 +1,16 @@ +logger()->testRunnerStarted($event); + } +} diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php index 5f5b494cfc..2175c2efee 100644 --- a/core/tests/bootstrap.php +++ b/core/tests/bootstrap.php @@ -9,6 +9,8 @@ use Drupal\Component\Assertion\Handle; use Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter; +use Drupal\TestTools\PhpUnitCompatibility\PhpUnit10\Logging\HtmlOutputLogger; +use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion; /** * Finds all valid extension directories recursively within a given directory. @@ -179,3 +181,7 @@ class_alias('\Drupal\Tests\DocumentElement', '\Behat\Mink\Element\DocumentElemen // thrown if an assert fails, but this call does not turn runtime assertions on // if they weren't on already. Handle::register(); + +if (RunnerVersion::getMajor() > 9) { + new HtmlOutputLogger(); +}