diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 5775c1d83da..11d2eb5e5e4 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -5,14 +5,20 @@ * Provides testing functionality. */ +use Drupal\Component\Uuid\Php; +use Drupal\Core\Asset\AttachedAssets; 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\StreamWrapper\PublicStream; use Drupal\Core\Test\JUnitConverter; use Drupal\Core\Test\PhpUnitTestRunner; use Drupal\Core\Test\TestDatabase; use Drupal\Core\Url; +use Drupal\simpletest\Form\SimpletestResultsForm; use Drupal\simpletest\TestDiscovery; use PHPUnit\Framework\TestCase; @@ -732,3 +738,91 @@ function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $test_ca @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\JUnitConverter::convertTestCaseToSimpletestRow() instead. See https://www.drupal.org/node/2948547', E_USER_DEPRECATED); return JUnitConverter::convertTestCaseToSimpletestRow($test_id, $test_case); } + +/** + * Display test results from run-tests.sh in a browser. + * + * @internal + * This function is only used by run-tests.sh + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. This function + * supports the --browser option in this script. Use the --verbose option + * instead. + * + * @see https://www.drupal.org/node/3083549 + */ +function _simpletest_run_tests_script_open_browser() { + global $test_ids; + + try { + $connection = Database::getConnection('default', 'test-runner'); + $results = $connection->select('simpletest') + ->fields('simpletest') + ->condition('test_id', $test_ids, 'IN') + ->orderBy('test_class') + ->orderBy('message_id') + ->execute() + ->fetchAll(); + } + catch (Exception $e) { + echo (string) $e; + exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION); + } + + // Get the results form. + $form = []; + SimpletestResultsForm::addResultForm($form, $results); + + // Get the assets to make the details element collapsible and theme the result + // form. + $assets = new AttachedAssets(); + $assets->setLibraries([ + 'core/drupal.collapse', + 'system/admin', + 'simpletest/drupal.simpletest', + ]); + $resolver = \Drupal::service('asset.resolver'); + list($js_assets_header, $js_assets_footer) = $resolver->getJsAssets($assets, FALSE); + $js_collection_renderer = \Drupal::service('asset.js.collection_renderer'); + $js_assets_header = $js_collection_renderer->render($js_assets_header); + $js_assets_footer = $js_collection_renderer->render($js_assets_footer); + $css_assets = \Drupal::service('asset.css.collection_renderer')->render($resolver->getCssAssets($assets, FALSE)); + + // Make the html page to write to disk. + $render_service = \Drupal::service('renderer'); + $html = '' . $render_service->renderPlain($js_assets_header) . $render_service->renderPlain($css_assets) . '' . $render_service->renderPlain($form) . $render_service->renderPlain($js_assets_footer) . ''; + + // Ensure we have assets verbose directory - tests with no verbose output will + // not have created one. + $directory = PublicStream::basePath() . '/simpletest/verbose'; + \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); + $php = new Php(); + $uuid = $php->generate(); + $filename = $directory . '/results-' . $uuid . '.html'; + $base_url = getenv('SIMPLETEST_BASE_URL'); + if (empty($base_url)) { + simpletest_script_print_error("--browser needs argument --url."); + } + $url = $base_url . '/' . PublicStream::basePath() . '/simpletest/verbose/results-' . $uuid . '.html'; + file_put_contents($filename, $html); + + // See if we can find an OS helper to open URLs in default browser. + $browser = FALSE; + if (shell_exec('which xdg-open')) { + $browser = 'xdg-open'; + } + elseif (shell_exec('which open')) { + $browser = 'open'; + } + elseif (substr(PHP_OS, 0, 3) == 'WIN') { + $browser = 'start'; + } + + if ($browser) { + shell_exec($browser . ' ' . escapeshellarg($url)); + } + else { + // Can't find assets valid browser. + print 'Open file://' . realpath($filename) . ' in your browser to see the verbose output.'; + } +} diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 9d3f3894eba..45fcd3bac3c 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -8,18 +8,13 @@ use Drupal\Component\FileSystem\FileSystem; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Timer; -use Drupal\Component\Uuid\Php; -use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\Database\Database; use Drupal\Core\File\Exception\FileException; -use Drupal\Core\File\FileSystemInterface; -use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\Test\EnvironmentCleaner; use Drupal\Core\Test\PhpUnitTestRunner; use Drupal\Core\Test\RunTests\TestFileParser; use Drupal\Core\Test\TestDatabase; use Drupal\Core\Test\TestRunnerKernel; -use Drupal\simpletest\Form\SimpletestResultsForm; use Drupal\Core\Test\TestDiscovery; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Output\ConsoleOutput; @@ -317,7 +312,7 @@ All arguments are long options. test database and configuration directories. Use in combination with --repeat for debugging random test failures. - --browser Opens the results in the browser. This enforces --keep-results and + --browser Deprecated, use --verbose instead. This enforces --keep-results and if you want to also view any pages rendered in the simpletest browser you need to add --verbose to the command line. @@ -450,6 +445,7 @@ function simpletest_script_parse_args() { } if ($args['browser']) { + simpletest_script_print_error('The --browser option is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use --verbose instead. See https://www.drupal.org/node/3083549'); $args['keep-results'] = TRUE; } return [$args, $count]; @@ -815,6 +811,9 @@ function simpletest_script_run_one_test($test_id, $test_class) { global $args; try { + // Default to status = success. This could mean that we didn't discover any + // tests and that none ran. + $status = SIMPLETEST_SCRIPT_EXIT_SUCCESS; if (strpos($test_class, '::') > 0) { list($class_name, $method) = explode('::', $test_class, 2); $methods = [$method]; @@ -831,7 +830,10 @@ function simpletest_script_run_one_test($test_id, $test_class) { if (is_subclass_of($test_class, TestCase::class)) { $status = simpletest_script_run_phpunit($test_id, $test_class); } - else { + // If we aren't running a PHPUnit-based test, then we might have a + // Simpletest-based one. Ensure that: 1) The simpletest framework exists, + // and 2) that our test belongs to that framework. + elseif (class_exists('Drupal\simpletest\TestBase') && is_subclass_of($test_class, 'Drupal\simpletest\TestBase')) { $test->dieOnFail = (bool) $args['die-on-fail']; $test->verbose = (bool) $args['verbose']; $test->run($methods); @@ -843,6 +845,12 @@ function simpletest_script_run_one_test($test_id, $test_class) { $status = SIMPLETEST_SCRIPT_EXIT_FAILURE; } } + // If the test is not a PHPUnit test, and either we don't have the + // Simpletest module or the \Drupal\simpletest\TestBase class available. + else { + simpletest_script_print_error(sprintf('Can not run %s. If this is a WebTestBase test the simpletest module must be installed. See https://www.drupal.org/node/3030340', $test_class)); + $status = SIMPLETEST_SCRIPT_EXIT_FAILURE; + } exit($status); } @@ -1494,79 +1502,21 @@ function simpletest_script_load_messages_by_test_id($test_ids) { /** * Display test results. + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. This function + * supports the --browser option in this script. Use the --verbose option + * instead. + * + * @see https://www.drupal.org/node/3083549 + * + * @todo Remove this in https://www.drupal.org/project/drupal/issues/3075490. */ function simpletest_script_open_browser() { - global $test_ids; - - try { - $connection = Database::getConnection('default', 'test-runner'); - $results = $connection->select('simpletest') - ->fields('simpletest') - ->condition('test_id', $test_ids, 'IN') - ->orderBy('test_class') - ->orderBy('message_id') - ->execute() - ->fetchAll(); - } - catch (Exception $e) { - echo (string) $e; - exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION); - } - - // Get the results form. - $form = []; - SimpletestResultsForm::addResultForm($form, $results); - - // Get the assets to make the details element collapsible and theme the result - // form. - $assets = new AttachedAssets(); - $assets->setLibraries([ - 'core/drupal.collapse', - 'system/admin', - 'simpletest/drupal.simpletest', - ]); - $resolver = \Drupal::service('asset.resolver'); - list($js_assets_header, $js_assets_footer) = $resolver->getJsAssets($assets, FALSE); - $js_collection_renderer = \Drupal::service('asset.js.collection_renderer'); - $js_assets_header = $js_collection_renderer->render($js_assets_header); - $js_assets_footer = $js_collection_renderer->render($js_assets_footer); - $css_assets = \Drupal::service('asset.css.collection_renderer')->render($resolver->getCssAssets($assets, FALSE)); - - // Make the html page to write to disk. - $render_service = \Drupal::service('renderer'); - $html = '' . $render_service->renderPlain($js_assets_header) . $render_service->renderPlain($css_assets) . '' . $render_service->renderPlain($form) . $render_service->renderPlain($js_assets_footer) . ''; - - // Ensure we have assets verbose directory - tests with no verbose output will - // not have created one. - $directory = PublicStream::basePath() . '/simpletest/verbose'; - \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); - $php = new Php(); - $uuid = $php->generate(); - $filename = $directory . '/results-' . $uuid . '.html'; - $base_url = getenv('SIMPLETEST_BASE_URL'); - if (empty($base_url)) { - simpletest_script_print_error("--browser needs argument --url."); - } - $url = $base_url . '/' . PublicStream::basePath() . '/simpletest/verbose/results-' . $uuid . '.html'; - file_put_contents($filename, $html); - - // See if we can find an OS helper to open URLs in default browser. - $browser = FALSE; - if (shell_exec('which xdg-open')) { - $browser = 'xdg-open'; - } - elseif (shell_exec('which open')) { - $browser = 'open'; - } - elseif (substr(PHP_OS, 0, 3) == 'WIN') { - $browser = 'start'; - } - - if ($browser) { - shell_exec($browser . ' ' . escapeshellarg($url)); - } - else { - // Can't find assets valid browser. - print 'Open file://' . realpath($filename) . ' in your browser to see the verbose output.'; + // Note: the user already has received a message about the deprecation in CLI + // so we trigger an error just in case this method has been used as API. + @trigger_error('The --browser option is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use --verbose instead. See https://www.drupal.org/node/3083549', E_USER_DEPRECATED); + if (function_exists('_simpletest_run_tests_script_open_browser')) { + return _simpletest_run_tests_script_open_browser(); } + simpletest_script_print_error('In order to use the --browser option the Simpletest module must be available. See https://www.drupal.org/node/3083549.'); }