Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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 tool with utilities to manage Behat integration in Moodle
  19   *
  20   * All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as
  21   * $CFG->dataroot and $CFG->prefix
  22   * Same applies for $CFG->behat_dbname, $CFG->behat_dbuser, $CFG->behat_dbpass
  23   * and $CFG->behat_dbhost. But if any of those is not defined $CFG->dbname,
  24   * $CFG->dbuser, $CFG->dbpass and/or $CFG->dbhost will be used.
  25   *
  26   * @package    tool_behat
  27   * @copyright  2012 David MonllaĆ³
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  
  31  
  32  if (isset($_SERVER['REMOTE_ADDR'])) {
  33      die(); // No access from web!.
  34  }
  35  
  36  // Basic functions.
  37  require_once (__DIR__ . '/../../../../lib/clilib.php');
  38  require_once (__DIR__ . '/../../../../lib/behat/lib.php');
  39  
  40  // CLI options.
  41  list($options, $unrecognized) = cli_get_params(
  42      array(
  43          'help'        => false,
  44          'install'     => false,
  45          'parallel'    => 0,
  46          'run'         => 0,
  47          'drop'        => false,
  48          'enable'      => false,
  49          'disable'     => false,
  50          'diag'        => false,
  51          'tags'        => '',
  52          'updatesteps' => false,
  53          'optimize-runs' => '',
  54          'add-core-features-to-theme' => false,
  55          'axe'         => true,
  56      ),
  57      array(
  58          'h' => 'help',
  59          'o' => 'optimize-runs',
  60          'a' => 'add-core-features-to-theme',
  61      )
  62  );
  63  
  64  if ($options['install'] or $options['drop']) {
  65      define('CACHE_DISABLE_ALL', true);
  66  }
  67  
  68  // Checking util_single_run.php CLI script usage.
  69  $help = "
  70  Behat utilities to manage the test environment
  71  
  72  Usage:
  73    php util_single_run.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--help]
  74  
  75  Options:
  76  --install        Installs the test environment for acceptance tests
  77  --drop           Drops the database tables and the dataroot contents
  78  --enable         Enables test environment and updates tests list
  79  --disable        Disables test environment
  80  --diag           Get behat test environment status code
  81  --updatesteps    Update feature step file.
  82  --no-axe         Disable axe accessibility tests.
  83  
  84  -o, --optimize-runs Split features with specified tags in all parallel runs.
  85  -a, --add-core-features-to-theme Add all core features to specified theme's
  86  
  87  -h, --help Print out this help
  88  
  89  Example from Moodle root directory:
  90  \$ php admin/tool/behat/cli/util_single_run.php --enable
  91  
  92  More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
  93  ";
  94  
  95  if (!empty($options['help'])) {
  96      echo $help;
  97      exit(0);
  98  }
  99  
 100  // Describe this script.
 101  define('BEHAT_UTIL', true);
 102  define('CLI_SCRIPT', true);
 103  define('NO_OUTPUT_BUFFERING', true);
 104  define('IGNORE_COMPONENT_CACHE', true);
 105  
 106  // Set run value, to be used by setup for configuring proper CFG variables.
 107  if ($options['run']) {
 108      define('BEHAT_CURRENT_RUN', $options['run']);
 109  }
 110  
 111  // Only load CFG from config.php, stop ASAP in lib/setup.php.
 112  define('ABORT_AFTER_CONFIG', true);
 113  require_once(__DIR__ . '/../../../../config.php');
 114  
 115  // Remove error handling overrides done in config.php.
 116  $CFG->debug = (E_ALL | E_STRICT);
 117  $CFG->debugdisplay = 1;
 118  error_reporting($CFG->debug);
 119  ini_set('display_errors', '1');
 120  ini_set('log_errors', '1');
 121  
 122  // Finish moodle init.
 123  define('ABORT_AFTER_CONFIG_CANCEL', true);
 124  require("$CFG->dirroot/lib/setup.php");
 125  
 126  raise_memory_limit(MEMORY_HUGE);
 127  
 128  require_once($CFG->libdir.'/adminlib.php');
 129  require_once($CFG->libdir.'/upgradelib.php');
 130  require_once($CFG->libdir.'/clilib.php');
 131  require_once($CFG->libdir.'/installlib.php');
 132  require_once($CFG->libdir.'/testing/classes/test_lock.php');
 133  
 134  if ($unrecognized) {
 135      $unrecognized = implode(PHP_EOL . "  ", $unrecognized);
 136      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
 137  }
 138  
 139  // Behat utilities.
 140  require_once($CFG->libdir . '/behat/classes/util.php');
 141  require_once($CFG->libdir . '/behat/classes/behat_command.php');
 142  require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
 143  
 144  // Ensure run option is <= parallel run installed.
 145  $run = 0;
 146  $parallel = 0;
 147  if ($options['run']) {
 148      $run = $options['run'];
 149      // If parallel option is not passed, then try get it form config.
 150      if (!$options['parallel']) {
 151          $parallel = behat_config_manager::get_behat_run_config_value('parallel');
 152      } else {
 153          $parallel = $options['parallel'];
 154      }
 155  
 156      if (empty($parallel) || $run > $parallel) {
 157          echo "Parallel runs can't be more then ".$parallel.PHP_EOL;
 158          exit(1);
 159      }
 160      $CFG->behatrunprocess = $run;
 161  }
 162  
 163  // Run command (only one per time).
 164  if ($options['install']) {
 165      behat_util::install_site();
 166  
 167      // This is only displayed once for parallel install.
 168      if (empty($run)) {
 169          mtrace("Acceptance tests site installed");
 170      }
 171  
 172      // Note: Do not build the themes here. This is done during the 'enable' stage.
 173  
 174  } else if ($options['drop']) {
 175      // Ensure no tests are running.
 176      test_lock::acquire('behat');
 177      behat_util::drop_site();
 178      // This is only displayed once for parallel install.
 179      if (empty($run)) {
 180          mtrace("Acceptance tests site dropped");
 181      }
 182  
 183  } else if ($options['enable']) {
 184      if (!empty($parallel)) {
 185          // Save parallel site info for enable and install options.
 186          behat_config_manager::set_behat_run_config_value('behatsiteenabled', 1);
 187      }
 188  
 189      // Configure axe according to option.
 190      behat_config_manager::set_behat_run_config_value('axe', $options['axe']);
 191  
 192      // Enable test mode.
 193      $timestart = microtime(true);
 194      mtrace('Creating Behat configuration ...', '');
 195      behat_util::start_test_mode($options['add-core-features-to-theme'], $options['optimize-runs'], $parallel, $run);
 196      mtrace(' done in ' . round(microtime(true) - $timestart, 2) . ' seconds.');
 197  
 198      // Themes are only built in the 'enable' command.
 199      behat_util::build_themes(true);
 200      mtrace("Testing environment themes built");
 201  
 202      // This is only displayed once for parallel install.
 203      if (empty($run)) {
 204          // Notify user that 2.5 profile has been converted to 3.5.
 205          if (behat_config_manager::$autoprofileconversion) {
 206              mtrace("2.5 behat profile detected, automatically converted to current 3.x format");
 207          }
 208  
 209          $runtestscommand = behat_command::get_behat_command(true, !empty($run));
 210  
 211          $runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
 212          mtrace("Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use: " . PHP_EOL .
 213              $runtestscommand);
 214      }
 215  
 216  } else if ($options['disable']) {
 217      behat_util::stop_test_mode($run);
 218      // This is only displayed once for parallel install.
 219      if (empty($run)) {
 220          mtrace("Acceptance tests environment disabled");
 221      }
 222  
 223  } else if ($options['diag']) {
 224      $code = behat_util::get_behat_status();
 225      exit($code);
 226  
 227  } else if ($options['updatesteps']) {
 228      if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) {
 229          $behatstepfile = BEHAT_FEATURE_STEP_FILE;
 230      } else {
 231          echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL;
 232          exit(1);
 233      }
 234  
 235      // Run behat command to get steps in feature files.
 236      $featurestepscmd = behat_command::get_behat_command(true);
 237      $featurestepscmd .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
 238      $featurestepscmd .= ' --dry-run --format=moodle_stepcount';
 239      $processes = cli_execute_parallel(array($featurestepscmd), __DIR__ . "/../../../../");
 240      $status = print_update_step_output(array_pop($processes), $behatstepfile);
 241  
 242      exit($status);
 243  } else {
 244      echo $help;
 245      exit(1);
 246  }
 247  
 248  exit(0);
 249  
 250  /**
 251   * Print update progress as dots for updating feature file step list.
 252   *
 253   * @param Process $process process executing update step command.
 254   * @param string $featurestepfile feature step file in which steps will be saved.
 255   * @return int exitcode.
 256   */
 257  function print_update_step_output($process, $featurestepfile) {
 258      $printedlength = 0;
 259  
 260      echo "Updating steps feature file for parallel behat runs" . PHP_EOL;
 261  
 262      // Show progress while running command.
 263      while ($process->isRunning()) {
 264          usleep(10000);
 265          $op = $process->getIncrementalOutput();
 266          if (trim($op)) {
 267              echo ".";
 268              $printedlength++;
 269              if ($printedlength > 70) {
 270                  $printedlength = 0;
 271                  echo PHP_EOL;
 272              }
 273          }
 274      }
 275  
 276      // If any error then exit.
 277      $exitcode = $process->getExitCode();
 278      // Output err.
 279      if ($exitcode != 0) {
 280          echo $process->getErrorOutput();
 281          exit($exitcode);
 282      }
 283  
 284      // Extract features with step info and save it in file.
 285      $featuresteps = $process->getOutput();
 286      $featuresteps = explode(PHP_EOL, $featuresteps);
 287  
 288      $realroot = realpath(__DIR__.'/../../../../').'/';
 289      foreach ($featuresteps as $featurestep) {
 290          if (trim($featurestep)) {
 291              $step = explode("::", $featurestep);
 292              $step[0] = str_replace($realroot, '', $step[0]);
 293              $steps[$step[0]] = $step[1];
 294          }
 295      }
 296  
 297      if ($existing = @json_decode(file_get_contents($featurestepfile), true)) {
 298          $steps = array_merge($existing, $steps);
 299      }
 300      arsort($steps);
 301  
 302      if (!@file_put_contents($featurestepfile, json_encode($steps, JSON_PRETTY_PRINT))) {
 303          behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $featurestepfile . ' can not be created');
 304          $exitcode = -1;
 305      }
 306  
 307      echo PHP_EOL. "Updated step count in " . $featurestepfile . PHP_EOL;
 308  
 309      return $exitcode;
 310  }