Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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'      => false,
  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                      [--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  --axe               Include 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 http://docs.moodle.org/dev/Acceptance_testing#Running_tests
 106  ";
 107  
 108  if (!empty($options['help'])) {
 109      echo $help;
 110      exit(0);
 111  }
 112  
 113  // Check which util file to call.
 114  $utilfile = 'util_single_run.php';
 115  $commandoptions = "";
 116  // If parallel run then use utilparallel.
 117  if ($options['parallel'] && $options['parallel'] > 1) {
 118      $utilfile = 'util.php';
 119      // Sanitize all input options, so they can be passed to util.
 120      foreach ($options as $option => $value) {
 121          if ($value) {
 122              $commandoptions .= " --$option=\"$value\"";
 123          }
 124      }
 125  } else {
 126      // Only sanitize options for single run.
 127      $cmdoptionsforsinglerun = [
 128          'add-core-features-to-theme',
 129          'axe',
 130      ];
 131  
 132      foreach ($cmdoptionsforsinglerun as $option) {
 133          if (!empty($options[$option])) {
 134              $commandoptions .= " --$option='$options[$option]'";
 135          }
 136      }
 137  }
 138  
 139  // Changing the cwd to admin/tool/behat/cli.
 140  $cwd = getcwd();
 141  $output = null;
 142  
 143  if ($options['disable-composer']) {
 144      // Disable self-update and upgrade easily.
 145      // Note: Installation will still occur regardless of this setting.
 146      $options['composer-self-update'] = false;
 147      $options['composer-upgrade'] = false;
 148  }
 149  
 150  // Install and update composer and dependencies as required.
 151  testing_update_composer_dependencies($options['composer-self-update'], $options['composer-upgrade']);
 152  
 153  // Check whether the behat test environment needs to be updated.
 154  chdir(__DIR__);
 155  exec("php $utilfile --diag $commandoptions", $output, $code);
 156  
 157  if ($code == 0) {
 158      echo "Behat test environment already installed\n";
 159  
 160  } else if ($code == BEHAT_EXITCODE_INSTALL) {
 161      // Behat and dependencies are installed and we need to install the test site.
 162      chdir(__DIR__);
 163      passthru("php $utilfile --install $commandoptions", $code);
 164      if ($code != 0) {
 165          chdir($cwd);
 166          exit($code);
 167      }
 168  
 169  } else if ($code == BEHAT_EXITCODE_REINSTALL) {
 170      // Test site data is outdated.
 171      chdir(__DIR__);
 172      passthru("php $utilfile --drop $commandoptions", $code);
 173      if ($code != 0) {
 174          chdir($cwd);
 175          exit($code);
 176      }
 177  
 178      chdir(__DIR__);
 179      passthru("php $utilfile --install $commandoptions", $code);
 180      if ($code != 0) {
 181          chdir($cwd);
 182          exit($code);
 183      }
 184  
 185  } else {
 186      // Generic error, we just output it.
 187      echo implode("\n", $output)."\n";
 188      chdir($cwd);
 189      exit($code);
 190  }
 191  
 192  // Enable editing mode according to config.php vars.
 193  chdir(__DIR__);
 194  passthru("php $utilfile --enable $commandoptions", $code);
 195  if ($code != 0) {
 196      echo "Error enabling site" . PHP_EOL;
 197      chdir($cwd);
 198      exit($code);
 199  }
 200  
 201  exit(0);