Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 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      ),
  53      array(
  54          'j' => 'parallel',
  55          'm' => 'maxruns',
  56          'h' => 'help',
  57          'o' => 'optimize-runs',
  58          'a' => 'add-core-features-to-theme',
  59      )
  60  );
  61  
  62  // Checking run.php CLI script usage.
  63  $help = "
  64  Behat utilities to initialise behat tests
  65  
  66  Usage:
  67    php init.php [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]] [--help]
  68  
  69  Options:
  70  -j, --parallel   Number of parallel behat run to initialise
  71  -m, --maxruns    Max parallel processes to be executed at one time.
  72  --fromrun        Execute run starting from (Used for parallel runs on different vms)
  73  --torun          Execute run till (Used for parallel runs on different vms)
  74  
  75  -o, --optimize-runs Split features with specified tags in all parallel runs.
  76  -a, --add-core-features-to-theme Add all core features to specified theme's
  77  
  78  -h, --help     Print out this help
  79  
  80  Example from Moodle root directory:
  81  \$ php admin/tool/behat/cli/init.php --parallel=2
  82  
  83  More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
  84  ";
  85  
  86  if (!empty($options['help'])) {
  87      echo $help;
  88      exit(0);
  89  }
  90  
  91  // Check which util file to call.
  92  $utilfile = 'util_single_run.php';
  93  $commandoptions = "";
  94  // If parallel run then use utilparallel.
  95  if ($options['parallel'] && $options['parallel'] > 1) {
  96      $utilfile = 'util.php';
  97      // Sanitize all input options, so they can be passed to util.
  98      foreach ($options as $option => $value) {
  99          if ($value) {
 100              $commandoptions .= " --$option=\"$value\"";
 101          }
 102      }
 103  } else {
 104      // Only sanitize options for single run.
 105      $cmdoptionsforsinglerun = array('add-core-features-to-theme');
 106  
 107      foreach ($cmdoptionsforsinglerun as $option) {
 108          if (!empty($options[$option])) {
 109              $commandoptions .= " --$option='$options[$option]'";
 110          }
 111      }
 112  }
 113  
 114  // Changing the cwd to admin/tool/behat/cli.
 115  $cwd = getcwd();
 116  $output = null;
 117  
 118  // If behat dependencies not downloaded then do it first, else symfony/process can't be used.
 119  testing_update_composer_dependencies();
 120  
 121  // Check whether the behat test environment needs to be updated.
 122  chdir(__DIR__);
 123  exec("php $utilfile --diag $commandoptions", $output, $code);
 124  
 125  if ($code == 0) {
 126      echo "Behat test environment already installed\n";
 127  
 128  } else if ($code == BEHAT_EXITCODE_INSTALL) {
 129      // Behat and dependencies are installed and we need to install the test site.
 130      chdir(__DIR__);
 131      passthru("php $utilfile --install $commandoptions", $code);
 132      if ($code != 0) {
 133          chdir($cwd);
 134          exit($code);
 135      }
 136  
 137  } else if ($code == BEHAT_EXITCODE_REINSTALL) {
 138      // Test site data is outdated.
 139      chdir(__DIR__);
 140      passthru("php $utilfile --drop $commandoptions", $code);
 141      if ($code != 0) {
 142          chdir($cwd);
 143          exit($code);
 144      }
 145  
 146      chdir(__DIR__);
 147      passthru("php $utilfile --install $commandoptions", $code);
 148      if ($code != 0) {
 149          chdir($cwd);
 150          exit($code);
 151      }
 152  
 153  } else {
 154      // Generic error, we just output it.
 155      echo implode("\n", $output)."\n";
 156      chdir($cwd);
 157      exit($code);
 158  }
 159  
 160  // Enable editing mode according to config.php vars.
 161  chdir(__DIR__);
 162  passthru("php $utilfile --enable $commandoptions", $code);
 163  if ($code != 0) {
 164      echo "Error enabling site" . PHP_EOL;
 165      chdir($cwd);
 166      exit($code);
 167  }
 168  
 169  exit(0);