Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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  
  39  // Basic functions.
  40  require_once (__DIR__ . '/../../../../lib/clilib.php');
  41  require_once (__DIR__ . '/../../../../lib/behat/lib.php');
  42  
  43  list($options, $unrecognized) = cli_get_params(
  44      array(
  45          'parallel' => 0,
  46          'maxruns'  => false,
  47          'help'     => false,
  48          'fromrun'  => 1,
  49          'torun'    => 0,
  50          'optimize-runs' => '',
  51          'add-core-features-to-theme' => false,
  52          'axe'      => null,
  53          'disable-composer' => false,
  54          'composer-upgrade' => true,
  55          'composer-self-update' => true,
  56      ),
  57      array(
  58          'j' => 'parallel',
  59          'm' => 'maxruns',
  60          'h' => 'help',
  61          'o' => 'optimize-runs',
  62          'a' => 'add-core-features-to-theme',
  63      )
  64  );
  65  
  66  // Checking run.php CLI script usage.
  67  $help = "
  68  Behat utilities to initialise behat tests
  69  
  70  Usage:
  71    php init.php      [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]]
  72                      [--no-axe] [-o | --optimize-runs] [-a | --add-core-features-to-theme]
  73                      [--no-composer-self-update] [--no-composer-upgrade]
  74                      [--disable-composer]
  75                      [--help]
  76  
  77  Options:
  78  -j, --parallel      Number of parallel behat run to initialise
  79  -m, --maxruns       Max parallel processes to be executed at one time
  80  --fromrun           Execute run starting from (Used for parallel runs on different vms)
  81  --torun             Execute run till (Used for parallel runs on different vms)
  82  --no-axe            Disable axe accessibility tests.
  83  
  84  -o, --optimize-runs
  85                      Split features with specified tags in all parallel runs.
  86  
  87  -a, --add-core-features-to-theme
  88                      Add all core features to specified theme's
  89  
  90  --no-composer-self-update
  91                      Prevent upgrade of the composer utility using its self-update command
  92  
  93  --no-composer-upgrade
  94                      Prevent update development dependencies using composer
  95  
  96  --disable-composer
  97                      A shortcut to disable composer self-update and dependency update
  98                      Note: Installation of composer and/or dependencies will still happen as required
  99  
 100  -h, --help          Print out this help
 101  
 102  Example from Moodle root directory:
 103  \$ php admin/tool/behat/cli/init.php --parallel=2
 104  
 105  More info in https://moodledev.io/general/development/tools/behat/running
 106  ";
 107  
 108  if (!empty($options['help'])) {
 109      echo $help;
 110      exit(0);
 111  }
 112  
 113  if ($options['axe']) {
 114      echo "Axe accessibility tests are enabled by default, to disable them, use the --no-axe option.\n";
 115  } else if ($options['axe'] === false) {
 116      echo "Axe accessibility tests have been disabled.\n";
 117  }
 118  
 119  // Check which util file to call.
 120  $utilfile = 'util_single_run.php';
 121  $commandoptions = "";
 122  // If parallel run then use utilparallel.
 123  if ($options['parallel'] && $options['parallel'] > 1) {
 124      $utilfile = 'util.php';
 125      // Sanitize all input options, so they can be passed to util.
 126      foreach ($options as $option => $value) {
 127          $commandoptions .= behat_get_command_flags($option, $value);
 128      }
 129  } else {
 130      // Only sanitize options for single run.
 131      $cmdoptionsforsinglerun = [
 132          'add-core-features-to-theme',
 133          'axe',
 134      ];
 135  
 136      foreach ($cmdoptionsforsinglerun as $option) {
 137          $commandoptions .= behat_get_command_flags($option, $options[$option]);
 138      }
 139  }
 140  
 141  // Changing the cwd to admin/tool/behat/cli.
 142  $cwd = getcwd();
 143  $output = null;
 144  
 145  if ($options['disable-composer']) {
 146      // Disable self-update and upgrade easily.
 147      // Note: Installation will still occur regardless of this setting.
 148      $options['composer-self-update'] = false;
 149      $options['composer-upgrade'] = false;
 150  }
 151  
 152  // Install and update composer and dependencies as required.
 153  testing_update_composer_dependencies($options['composer-self-update'], $options['composer-upgrade']);
 154  
 155  // Check whether the behat test environment needs to be updated.
 156  chdir(__DIR__);
 157  exec("php $utilfile --diag $commandoptions", $output, $code);
 158  
 159  if ($code == 0) {
 160      echo "Behat test environment already installed\n";
 161  
 162  } else if ($code == BEHAT_EXITCODE_INSTALL) {
 163      // Behat and dependencies are installed and we need to install the test site.
 164      chdir(__DIR__);
 165      passthru("php $utilfile --install $commandoptions", $code);
 166      if ($code != 0) {
 167          chdir($cwd);
 168          exit($code);
 169      }
 170  
 171  } else if ($code == BEHAT_EXITCODE_REINSTALL) {
 172      // Test site data is outdated.
 173      chdir(__DIR__);
 174      passthru("php $utilfile --drop $commandoptions", $code);
 175      if ($code != 0) {
 176          chdir($cwd);
 177          exit($code);
 178      }
 179  
 180      chdir(__DIR__);
 181      passthru("php $utilfile --install $commandoptions", $code);
 182      if ($code != 0) {
 183          chdir($cwd);
 184          exit($code);
 185      }
 186  
 187  } else {
 188      // Generic error, we just output it.
 189      echo implode("\n", $output)."\n";
 190      chdir($cwd);
 191      exit($code);
 192  }
 193  
 194  // Enable editing mode according to config.php vars.
 195  chdir(__DIR__);
 196  passthru("php $utilfile --enable $commandoptions", $code);
 197  if ($code != 0) {
 198      echo "Error enabling site" . PHP_EOL;
 199      chdir($cwd);
 200      exit($code);
 201  }
 202  
 203  exit(0);