diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 1c63a94ff6..665b8baaba 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -9,6 +9,7 @@ use Drupal\Core\Asset\AttachedAssetsInterface; use Drupal\Core\Database\Database; use Drupal\Core\File\Exception\FileException; +use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Test\TestDatabase; @@ -16,6 +17,7 @@ use Drupal\Tests\Listeners\SimpletestUiPrinter; use PHPUnit\Framework\TestCase; use Symfony\Component\Process\PhpExecutableFinder; +use Symfony\Component\Process\Process; use Drupal\Core\Test\JUnitHelper; use Drupal\Core\Test\JUnitHelperLegacy; use Drupal\Core\Test\TestStatus; @@ -217,7 +219,7 @@ function simpletest_run_phpunit_tests($test_id, array $unescaped_test_classnames 'test_id' => $test_id, 'test_class' => implode(",", $unescaped_test_classnames), 'status' => TestStatus::label($status), - 'message' => 'PHPunit Test failed to complete; Error: ' . implode("\n", $output), + 'message' => 'PHPunit Test failed to complete; Error: ' . $output, 'message_group' => 'Other', 'function' => implode(",", $unescaped_test_classnames), 'line' => '0', @@ -314,7 +316,10 @@ function simpletest_summarize_phpunit_result($results) { * Path to the PHPUnit XML file to use for the current $test_id. */ function simpletest_phpunit_xml_filepath($test_id) { - return \Drupal::service('file_system')->realpath('public://simpletest') . '/phpunit-' . $test_id . '.xml'; + $file_system = \Drupal::service('file_system'); + $simpletest_path = $file_system->realpath('public://simpletest'); + $file_system->prepareDirectory($simpletest_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); + return $simpletest_path . '/phpunit-' . $test_id . '.xml'; } /** @@ -355,29 +360,29 @@ function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpun global $base_url; // Setup an environment variable containing the database connection so that // functional tests can connect to the database. - putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl()); - putenv('SIMPLETEST_JUNIT_FILE=' . $phpunit_file); + $process_environment_variables = [ + 'SIMPLETEST_DB' => Database::getConnectionInfoAsUrl(), + 'SIMPLETEST_JUNIT_FILE' => $phpunit_file, + ]; // Setup an environment variable containing the base URL, if it is available. // This allows functional tests to browse the site under test. When running // tests via CLI, core/phpunit.xml.dist or core/scripts/run-tests.sh can set // this variable. if ($base_url) { - putenv('SIMPLETEST_BASE_URL=' . $base_url); - putenv('BROWSERTEST_OUTPUT_DIRECTORY=' . \Drupal::service('file_system')->realpath('public://simpletest')); + $process_environment_variables['SIMPLETEST_BASE_URL'] = $base_url; + $process_environment_variables['BROWSERTEST_OUTPUT_DIRECTORY'] = \Drupal::service('file_system')->realpath('public://simpletest'); } $phpunit_bin = simpletest_phpunit_command(); $command = [ $phpunit_bin, - '--printer', - escapeshellarg(SimpletestUiPrinter::class), ]; // Optimized for running a single test. if (count($unescaped_test_classnames) == 1) { $class = new \ReflectionClass($unescaped_test_classnames[0]); - $command[] = escapeshellarg($class->getFileName()); + $command[] = $class->getFileName(); } else { // Double escape namespaces so they'll work in a regexp. @@ -388,27 +393,18 @@ function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpun $filter_string = implode("|", $escaped_test_classnames); $command = array_merge($command, [ '--filter', - escapeshellarg($filter_string), + $filter_string, ]); } - // Need to change directories before running the command so that we can use - // relative paths in the configuration file's exclusions. - $old_cwd = getcwd(); - chdir(\Drupal::root() . "/core"); - - // exec in a subshell so that the environment is isolated when running tests - // via the simpletest UI. - $ret = exec(implode(" ", $command), $output, $status); + $process = new Process($command, \Drupal::root() . "/core", $process_environment_variables); + $process->inheritEnvironmentVariables(); + $process->setTimeout(NULL); + $process->run(); + $output = $process->getOutput(); + $status = $process->getExitCode(); - chdir($old_cwd); - putenv('SIMPLETEST_DB='); - putenv('SIMPLETEST_JUNIT_FILE='); - if ($base_url) { - putenv('SIMPLETEST_BASE_URL='); - putenv('BROWSERTEST_OUTPUT_DIRECTORY='); - } - return $ret; + return ''; } /** @@ -426,7 +422,7 @@ function simpletest_phpunit_command() { // The file in Composer's bin dir is a *nix link, which does not work when // extracted from a tarball and generally not on Windows. - $command = escapeshellarg($vendor_dir . '/phpunit/phpunit/phpunit'); + $command = $vendor_dir . '/phpunit/phpunit/phpunit'; if (substr(PHP_OS, 0, 3) == 'WIN') { // On Windows it is necessary to run the script using the PHP executable. $php_executable_finder = new PhpExecutableFinder(); diff --git a/core/modules/simpletest/tests/src/Functional/SimpletestUiTest.php b/core/modules/simpletest/tests/src/Functional/SimpletestUiTest.php index 69593a269e..74c8bf957c 100644 --- a/core/modules/simpletest/tests/src/Functional/SimpletestUiTest.php +++ b/core/modules/simpletest/tests/src/Functional/SimpletestUiTest.php @@ -56,7 +56,7 @@ public function testTestingThroughUI() { "tests[$test]" => TRUE, ]; $this->drupalPostForm($url, $edit, t('Run tests')); - $assertion->pageTextContains('0 fails, 0 skipped, 0 exceptions'); + $assertion->pageTextContains('0 fails, 0 risky, 0 skipped, 0 incomplete, 0 exceptions'); } }