function _simpletest_run_tests_script_open_browser

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 also

https://www.drupal.org/node/3083549

File

core/modules/simpletest/simpletest.module, line 755

Code

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 = '<head>' . $render_service->renderPlain($js_assets_header) . $render_service->renderPlain($css_assets) . '</head><body>' . $render_service->renderPlain($form) . $render_service->renderPlain($js_assets_footer) . '</body>';
    // 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.';
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.