| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | /**
 | 
        
           |  |  | 18 |  * CLI script to set up all the behat test environment.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    tool_behat
 | 
        
           |  |  | 21 |  * @copyright  2013 David Monllaó
 | 
        
           |  |  | 22 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | if (isset($_SERVER['REMOTE_ADDR'])) {
 | 
        
           |  |  | 26 |     die(); // No access from web!
 | 
        
           |  |  | 27 | }
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | // Force OPcache reset if used, we do not want any stale caches
 | 
        
           |  |  | 30 | // when preparing test environment.
 | 
        
           |  |  | 31 | if (function_exists('opcache_reset')) {
 | 
        
           |  |  | 32 |     opcache_reset();
 | 
        
           |  |  | 33 | }
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | // Is not really necessary but adding it as is a CLI_SCRIPT.
 | 
        
           |  |  | 36 | define('CLI_SCRIPT', true);
 | 
        
           |  |  | 37 | define('CACHE_DISABLE_ALL', true);
 | 
        
           |  |  | 38 |   | 
        
           | 1441 | ariadna | 39 | // It makes no sense to use BEHAT_CLI for this script (the Behat launch scripts expect to start
 | 
        
           |  |  | 40 | // from the normal environment), so in case user has set tne environment variable, disable it.
 | 
        
           |  |  | 41 | putenv('BEHAT_CLI=0');
 | 
        
           |  |  | 42 |   | 
        
           | 1 | efrain | 43 | // Basic functions.
 | 
        
           |  |  | 44 | require_once(__DIR__ . '/../../../../lib/clilib.php');
 | 
        
           |  |  | 45 | require_once(__DIR__ . '/../../../../lib/behat/lib.php');
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | list($options, $unrecognized) = cli_get_params(
 | 
        
           |  |  | 48 |     array(
 | 
        
           |  |  | 49 |         'parallel' => 0,
 | 
        
           |  |  | 50 |         'maxruns'  => false,
 | 
        
           |  |  | 51 |         'help'     => false,
 | 
        
           |  |  | 52 |         'fromrun'  => 1,
 | 
        
           |  |  | 53 |         'torun'    => 0,
 | 
        
           |  |  | 54 |         'optimize-runs' => '',
 | 
        
           |  |  | 55 |         'add-core-features-to-theme' => false,
 | 
        
           |  |  | 56 |         'axe'      => null,
 | 
        
           |  |  | 57 |         'disable-composer' => false,
 | 
        
           |  |  | 58 |         'composer-upgrade' => true,
 | 
        
           |  |  | 59 |         'composer-self-update' => true,
 | 
        
           |  |  | 60 |         'scss-deprecations' => false,
 | 
        
           | 1441 | ariadna | 61 |         'no-icon-deprecations' => false,
 | 
        
           | 1 | efrain | 62 |     ),
 | 
        
           |  |  | 63 |     array(
 | 
        
           |  |  | 64 |         'j' => 'parallel',
 | 
        
           |  |  | 65 |         'm' => 'maxruns',
 | 
        
           |  |  | 66 |         'h' => 'help',
 | 
        
           |  |  | 67 |         'o' => 'optimize-runs',
 | 
        
           |  |  | 68 |         'a' => 'add-core-features-to-theme',
 | 
        
           |  |  | 69 |     )
 | 
        
           |  |  | 70 | );
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | // Checking run.php CLI script usage.
 | 
        
           |  |  | 73 | $help = "
 | 
        
           |  |  | 74 | Behat utilities to initialise behat tests
 | 
        
           |  |  | 75 |   | 
        
           |  |  | 76 | Usage:
 | 
        
           |  |  | 77 |   php init.php      [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]]
 | 
        
           | 1441 | ariadna | 78 |                     [--no-axe] [--scss-deprecations] [--no-icon-deprecations] [-o | --optimize-runs]
 | 
        
           |  |  | 79 |                     [-a | --add-core-features-to-theme]
 | 
        
           | 1 | efrain | 80 |                     [--no-composer-self-update] [--no-composer-upgrade]
 | 
        
           |  |  | 81 |                     [--disable-composer]
 | 
        
           |  |  | 82 |                     [--help]
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 | Options:
 | 
        
           | 1441 | ariadna | 85 | -j, --parallel         Number of parallel behat run to initialise
 | 
        
           |  |  | 86 | -m, --maxruns          Max parallel processes to be executed at one time
 | 
        
           |  |  | 87 | --fromrun              Execute run starting from (Used for parallel runs on different vms)
 | 
        
           |  |  | 88 | --torun                Execute run till (Used for parallel runs on different vms)
 | 
        
           |  |  | 89 | --no-axe               Disable axe accessibility tests.
 | 
        
           |  |  | 90 | --scss-deprecations    Enable SCSS deprecation checks.
 | 
        
           |  |  | 91 | --no-icon-deprecations Disable icon deprecation checks.
 | 
        
           | 1 | efrain | 92 |   | 
        
           |  |  | 93 | -o, --optimize-runs
 | 
        
           |  |  | 94 |                     Split features with specified tags in all parallel runs.
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | -a, --add-core-features-to-theme
 | 
        
           |  |  | 97 |                     Add all core features to specified theme's
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 | --no-composer-self-update
 | 
        
           |  |  | 100 |                     Prevent upgrade of the composer utility using its self-update command
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 | --no-composer-upgrade
 | 
        
           |  |  | 103 |                     Prevent update development dependencies using composer
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 | --disable-composer
 | 
        
           |  |  | 106 |                     A shortcut to disable composer self-update and dependency update
 | 
        
           |  |  | 107 |                     Note: Installation of composer and/or dependencies will still happen as required
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 | -h, --help          Print out this help
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 | Example from Moodle root directory:
 | 
        
           |  |  | 112 | \$ php admin/tool/behat/cli/init.php --parallel=2
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 | More info in https://moodledev.io/general/development/tools/behat/running
 | 
        
           |  |  | 115 | ";
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 | if (!empty($options['help'])) {
 | 
        
           |  |  | 118 |     echo $help;
 | 
        
           |  |  | 119 |     exit(0);
 | 
        
           |  |  | 120 | }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | if ($options['axe']) {
 | 
        
           |  |  | 123 |     echo "Axe accessibility tests are enabled by default, to disable them, use the --no-axe option.\n";
 | 
        
           |  |  | 124 | } else if ($options['axe'] === false) {
 | 
        
           |  |  | 125 |     echo "Axe accessibility tests have been disabled.\n";
 | 
        
           |  |  | 126 | }
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 | // Check which util file to call.
 | 
        
           |  |  | 129 | $utilfile = 'util_single_run.php';
 | 
        
           |  |  | 130 | $commandoptions = "";
 | 
        
           |  |  | 131 | // If parallel run then use utilparallel.
 | 
        
           |  |  | 132 | if ($options['parallel'] && $options['parallel'] > 1) {
 | 
        
           |  |  | 133 |     $utilfile = 'util.php';
 | 
        
           |  |  | 134 |     // Sanitize all input options, so they can be passed to util.
 | 
        
           |  |  | 135 |     foreach ($options as $option => $value) {
 | 
        
           |  |  | 136 |         $commandoptions .= behat_get_command_flags($option, $value);
 | 
        
           |  |  | 137 |     }
 | 
        
           |  |  | 138 | } else {
 | 
        
           |  |  | 139 |     // Only sanitize options for single run.
 | 
        
           |  |  | 140 |     $cmdoptionsforsinglerun = [
 | 
        
           |  |  | 141 |         'add-core-features-to-theme',
 | 
        
           |  |  | 142 |         'axe',
 | 
        
           |  |  | 143 |         'scss-deprecations',
 | 
        
           | 1441 | ariadna | 144 |         'no-icon-deprecations',
 | 
        
           | 1 | efrain | 145 |     ];
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |     foreach ($cmdoptionsforsinglerun as $option) {
 | 
        
           |  |  | 148 |         $commandoptions .= behat_get_command_flags($option, $options[$option]);
 | 
        
           |  |  | 149 |     }
 | 
        
           |  |  | 150 | }
 | 
        
           |  |  | 151 |   | 
        
           |  |  | 152 | // Changing the cwd to admin/tool/behat/cli.
 | 
        
           |  |  | 153 | $cwd = getcwd();
 | 
        
           |  |  | 154 | $output = null;
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 | if ($options['disable-composer']) {
 | 
        
           |  |  | 157 |     // Disable self-update and upgrade easily.
 | 
        
           |  |  | 158 |     // Note: Installation will still occur regardless of this setting.
 | 
        
           |  |  | 159 |     $options['composer-self-update'] = false;
 | 
        
           |  |  | 160 |     $options['composer-upgrade'] = false;
 | 
        
           |  |  | 161 | }
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 | // Install and update composer and dependencies as required.
 | 
        
           |  |  | 164 | testing_update_composer_dependencies($options['composer-self-update'], $options['composer-upgrade']);
 | 
        
           |  |  | 165 |   | 
        
           |  |  | 166 | // Check whether the behat test environment needs to be updated.
 | 
        
           |  |  | 167 | chdir(__DIR__);
 | 
        
           |  |  | 168 | exec("php $utilfile --diag $commandoptions", $output, $code);
 | 
        
           |  |  | 169 |   | 
        
           |  |  | 170 | if ($code == 0) {
 | 
        
           |  |  | 171 |     echo "Behat test environment already installed\n";
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 | } else if ($code == BEHAT_EXITCODE_INSTALL) {
 | 
        
           |  |  | 174 |     // Behat and dependencies are installed and we need to install the test site.
 | 
        
           |  |  | 175 |     chdir(__DIR__);
 | 
        
           |  |  | 176 |     passthru("php $utilfile --install $commandoptions", $code);
 | 
        
           |  |  | 177 |     if ($code != 0) {
 | 
        
           |  |  | 178 |         chdir($cwd);
 | 
        
           |  |  | 179 |         exit($code);
 | 
        
           |  |  | 180 |     }
 | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 | } else if ($code == BEHAT_EXITCODE_REINSTALL) {
 | 
        
           |  |  | 183 |     // Test site data is outdated.
 | 
        
           |  |  | 184 |     chdir(__DIR__);
 | 
        
           |  |  | 185 |     passthru("php $utilfile --drop $commandoptions", $code);
 | 
        
           |  |  | 186 |     if ($code != 0) {
 | 
        
           |  |  | 187 |         chdir($cwd);
 | 
        
           |  |  | 188 |         exit($code);
 | 
        
           |  |  | 189 |     }
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |     chdir(__DIR__);
 | 
        
           |  |  | 192 |     passthru("php $utilfile --install $commandoptions", $code);
 | 
        
           |  |  | 193 |     if ($code != 0) {
 | 
        
           |  |  | 194 |         chdir($cwd);
 | 
        
           |  |  | 195 |         exit($code);
 | 
        
           |  |  | 196 |     }
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 | } else {
 | 
        
           |  |  | 199 |     // Generic error, we just output it.
 | 
        
           |  |  | 200 |     echo implode("\n", $output)."\n";
 | 
        
           |  |  | 201 |     chdir($cwd);
 | 
        
           |  |  | 202 |     exit($code);
 | 
        
           |  |  | 203 | }
 | 
        
           |  |  | 204 |   | 
        
           |  |  | 205 | // Enable editing mode according to config.php vars.
 | 
        
           |  |  | 206 | chdir(__DIR__);
 | 
        
           |  |  | 207 | passthru("php $utilfile --enable $commandoptions", $code);
 | 
        
           |  |  | 208 | if ($code != 0) {
 | 
        
           |  |  | 209 |     echo "Error enabling site" . PHP_EOL;
 | 
        
           |  |  | 210 |     chdir($cwd);
 | 
        
           |  |  | 211 |     exit($code);
 | 
        
           |  |  | 212 | }
 | 
        
           |  |  | 213 |   | 
        
           |  |  | 214 | exit(0);
 |