/admin/tool/behat/cli/init.php

https://bitbucket.org/moodle/moodle · PHP · 201 lines · 164 code · 11 blank · 26 comment · 4 complexity · 0d2572db4c86ac8f0cceb2c19a5ffa62 MD5 · raw file

  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. * CLI script to set up all the behat test environment.
  18. *
  19. * @package tool_behat
  20. * @copyright 2013 David Monllaó
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. if (isset($_SERVER['REMOTE_ADDR'])) {
  24. die(); // No access from web!
  25. }
  26. // Force OPcache reset if used, we do not want any stale caches
  27. // when preparing test environment.
  28. if (function_exists('opcache_reset')) {
  29. opcache_reset();
  30. }
  31. // Is not really necessary but adding it as is a CLI_SCRIPT.
  32. define('CLI_SCRIPT', true);
  33. define('CACHE_DISABLE_ALL', true);
  34. // Basic functions.
  35. require_once(__DIR__ . '/../../../../lib/clilib.php');
  36. require_once(__DIR__ . '/../../../../lib/behat/lib.php');
  37. list($options, $unrecognized) = cli_get_params(
  38. array(
  39. 'parallel' => 0,
  40. 'maxruns' => false,
  41. 'help' => false,
  42. 'fromrun' => 1,
  43. 'torun' => 0,
  44. 'optimize-runs' => '',
  45. 'add-core-features-to-theme' => false,
  46. 'axe' => false,
  47. 'disable-composer' => false,
  48. 'composer-upgrade' => true,
  49. 'composer-self-update' => true,
  50. ),
  51. array(
  52. 'j' => 'parallel',
  53. 'm' => 'maxruns',
  54. 'h' => 'help',
  55. 'o' => 'optimize-runs',
  56. 'a' => 'add-core-features-to-theme',
  57. )
  58. );
  59. // Checking run.php CLI script usage.
  60. $help = "
  61. Behat utilities to initialise behat tests
  62. Usage:
  63. php init.php [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]]
  64. [--axe] [-o | --optimize-runs] [-a | --add-core-features-to-theme]
  65. [--no-composer-self-update] [--no-composer-upgrade]
  66. [--disable-composer]
  67. [--help]
  68. Options:
  69. -j, --parallel Number of parallel behat run to initialise
  70. -m, --maxruns Max parallel processes to be executed at one time
  71. --fromrun Execute run starting from (Used for parallel runs on different vms)
  72. --torun Execute run till (Used for parallel runs on different vms)
  73. --axe Include axe accessibility tests
  74. -o, --optimize-runs
  75. Split features with specified tags in all parallel runs.
  76. -a, --add-core-features-to-theme
  77. Add all core features to specified theme's
  78. --no-composer-self-update
  79. Prevent upgrade of the composer utility using its self-update command
  80. --no-composer-upgrade
  81. Prevent update development dependencies using composer
  82. --disable-composer
  83. A shortcut to disable composer self-update and dependency update
  84. Note: Installation of composer and/or dependencies will still happen as required
  85. -h, --help Print out this help
  86. Example from Moodle root directory:
  87. \$ php admin/tool/behat/cli/init.php --parallel=2
  88. More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
  89. ";
  90. if (!empty($options['help'])) {
  91. echo $help;
  92. exit(0);
  93. }
  94. // Check which util file to call.
  95. $utilfile = 'util_single_run.php';
  96. $commandoptions = "";
  97. // If parallel run then use utilparallel.
  98. if ($options['parallel'] && $options['parallel'] > 1) {
  99. $utilfile = 'util.php';
  100. // Sanitize all input options, so they can be passed to util.
  101. foreach ($options as $option => $value) {
  102. if ($value) {
  103. $commandoptions .= " --$option=\"$value\"";
  104. }
  105. }
  106. } else {
  107. // Only sanitize options for single run.
  108. $cmdoptionsforsinglerun = [
  109. 'add-core-features-to-theme',
  110. 'axe',
  111. ];
  112. foreach ($cmdoptionsforsinglerun as $option) {
  113. if (!empty($options[$option])) {
  114. $commandoptions .= " --$option='$options[$option]'";
  115. }
  116. }
  117. }
  118. // Changing the cwd to admin/tool/behat/cli.
  119. $cwd = getcwd();
  120. $output = null;
  121. if ($options['disable-composer']) {
  122. // Disable self-update and upgrade easily.
  123. // Note: Installation will still occur regardless of this setting.
  124. $options['composer-self-update'] = false;
  125. $options['composer-upgrade'] = false;
  126. }
  127. // Install and update composer and dependencies as required.
  128. testing_update_composer_dependencies($options['composer-self-update'], $options['composer-upgrade']);
  129. // Check whether the behat test environment needs to be updated.
  130. chdir(__DIR__);
  131. exec("php $utilfile --diag $commandoptions", $output, $code);
  132. if ($code == 0) {
  133. echo "Behat test environment already installed\n";
  134. } else if ($code == BEHAT_EXITCODE_INSTALL) {
  135. // Behat and dependencies are installed and we need to install the test site.
  136. chdir(__DIR__);
  137. passthru("php $utilfile --install $commandoptions", $code);
  138. if ($code != 0) {
  139. chdir($cwd);
  140. exit($code);
  141. }
  142. } else if ($code == BEHAT_EXITCODE_REINSTALL) {
  143. // Test site data is outdated.
  144. chdir(__DIR__);
  145. passthru("php $utilfile --drop $commandoptions", $code);
  146. if ($code != 0) {
  147. chdir($cwd);
  148. exit($code);
  149. }
  150. chdir(__DIR__);
  151. passthru("php $utilfile --install $commandoptions", $code);
  152. if ($code != 0) {
  153. chdir($cwd);
  154. exit($code);
  155. }
  156. } else {
  157. // Generic error, we just output it.
  158. echo implode("\n", $output)."\n";
  159. chdir($cwd);
  160. exit($code);
  161. }
  162. // Enable editing mode according to config.php vars.
  163. chdir(__DIR__);
  164. passthru("php $utilfile --enable $commandoptions", $code);
  165. if ($code != 0) {
  166. echo "Error enabling site" . PHP_EOL;
  167. chdir($cwd);
  168. exit($code);
  169. }
  170. exit(0);