MDL-39752 fix whitespace errors
[moodle.git] / lib / behat / classes / behat_command.php
CommitLineData
b5c13009
DM
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 * Behat command utils
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
096858ed
DM
26defined('MOODLE_INTERNAL') || die();
27
28require_once(__DIR__ . '/../lib.php');
b5c13009
DM
29
30/**
31 * Behat command related utils
32 *
33 * @package core
34 * @category test
35 * @copyright 2013 David MonllaĆ³
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class behat_command {
39
096858ed
DM
40 /**
41 * Docs url
42 */
43 const DOCS_URL = 'http://docs.moodle.org/dev/Acceptance_testing';
44
b5c13009
DM
45 /**
46 * Ensures the behat dir exists in moodledata
3c71c15c 47 * @param int $runprocess run process for which behat dir is returned.
b5c13009
DM
48 * @return string Full path
49 */
3c71c15c 50 public static function get_behat_dir($runprocess = 0) {
b5c13009
DM
51 global $CFG;
52
9be958e2
DP
53 // If not set then return empty string.
54 if (!isset($CFG->behat_dataroot)) {
17e675a7 55 return "";
9be958e2 56 }
17e675a7 57
5b9e6b55
RT
58 if (empty($runprocess)) {
59 $behatdir = $CFG->behat_dataroot . '/behat';
60 } else if (isset($CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'])) {
61 $behatdir = $CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'] . '/behat';;
62 } else {
63 $behatdir = $CFG->behat_dataroot . $runprocess . '/behat';
64 }
b5c13009
DM
65
66 if (!is_dir($behatdir)) {
67 if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
096858ed 68 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' can not be created');
b5c13009
DM
69 }
70 }
71
72 if (!is_writable($behatdir)) {
096858ed 73 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' is not writable');
b5c13009
DM
74 }
75
76 return $behatdir;
77 }
78
79 /**
80 * Returns the executable path
19a40b14
DM
81 *
82 * Allows returning a customized command for cygwin when the
83 * command is just displayed, when using exec(), system() and
84 * friends we stay with DIRECTORY_SEPARATOR as they use the
85 * normal cmd.exe (in Windows).
86 *
87 * @param bool $custombyterm If the provided command should depend on the terminal where it runs
3c71c15c 88 * @param bool $parallelrun If parallel run is installed.
b5c13009
DM
89 * @return string
90 */
3c71c15c 91 public final static function get_behat_command($custombyterm = false, $parallerun = false) {
19a40b14
DM
92
93 $separator = DIRECTORY_SEPARATOR;
3c71c15c
RT
94 if (!$parallerun) {
95 $exec = 'behat';
19a40b14 96
3c71c15c
RT
97 // Cygwin uses linux-style directory separators.
98 if ($custombyterm && testing_is_cygwin()) {
99 $separator = '/';
ff7139d6 100
3c71c15c
RT
101 // MinGW can not execute .bat scripts.
102 if (!testing_is_mingw()) {
103 $exec = 'behat.bat';
104 }
ff7139d6 105 }
3c71c15c
RT
106 $command = 'vendor' . $separator . 'bin' . $separator . $exec;
107 } else {
108 $command = 'php admin' . $separator . 'tool' . $separator . 'behat' . $separator . 'cli' . $separator . 'run.php';
19a40b14 109 }
3c71c15c 110 return $command;
b5c13009
DM
111 }
112
096858ed
DM
113 /**
114 * Runs behat command with provided options
115 *
116 * Execution continues when the process finishes
117 *
118 * @param string $options Defaults to '' so tests would be executed
119 * @return array CLI command outputs [0] => string, [1] => integer
120 */
121 public final static function run($options = '') {
122 global $CFG;
123
124 $currentcwd = getcwd();
125 chdir($CFG->dirroot);
126 exec(self::get_behat_command() . ' ' . $options, $output, $code);
127 chdir($currentcwd);
128
129 return array($output, $code);
130 }
131
b5c13009
DM
132 /**
133 * Checks if behat is set up and working
134 *
b8c4d91c 135 * Notifies failures both from CLI and web interface.
096858ed 136 *
b5c13009
DM
137 * It checks behat dependencies have been installed and runs
138 * the behat help command to ensure it works as expected
139 *
fa7e4e2b 140 * @return int Error code or 0 if all ok
b5c13009 141 */
9bb80d20 142 public static function behat_setup_problem() {
b5c13009
DM
143 global $CFG;
144
b5c13009
DM
145 // Moodle setting.
146 if (!self::are_behat_dependencies_installed()) {
147
b8c4d91c
DM
148 // Returning composer error code to avoid conflicts with behat and moodle error codes.
149 self::output_msg(get_string('errorcomposer', 'tool_behat'));
fa7e4e2b 150 return BEHAT_EXITCODE_COMPOSER;
b5c13009
DM
151 }
152
153 // Behat test command.
096858ed 154 list($output, $code) = self::run(' --help');
b5c13009
DM
155
156 if ($code != 0) {
b8c4d91c 157
fa7e4e2b 158 // Returning composer error code to avoid conflicts with behat and moodle error codes.
b8c4d91c 159 self::output_msg(get_string('errorbehatcommand', 'tool_behat', self::get_behat_command()));
fa7e4e2b 160 return BEHAT_EXITCODE_COMPOSER;
b5c13009 161 }
33063f2e 162
7e3f5e79 163 // No empty values.
b8c4d91c
DM
164 if (empty($CFG->behat_dataroot) || empty($CFG->behat_prefix) || empty($CFG->behat_wwwroot)) {
165 self::output_msg(get_string('errorsetconfig', 'tool_behat'));
166 return BEHAT_EXITCODE_CONFIG;
7e3f5e79
DM
167
168 }
169
170 // Not repeated values.
171 // We only need to check this when the behat site is not running as
172 // at this point, when it is running, all $CFG->behat_* vars have
173 // already been copied to $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
174 if (!defined('BEHAT_SITE_RUNNING') &&
175 ($CFG->behat_prefix == $CFG->prefix ||
176 $CFG->behat_dataroot == $CFG->dataroot ||
177 $CFG->behat_wwwroot == $CFG->wwwroot ||
178 (!empty($CFG->phpunit_prefix) && $CFG->phpunit_prefix == $CFG->behat_prefix) ||
179 (!empty($CFG->phpunit_dataroot) && $CFG->phpunit_dataroot == $CFG->behat_dataroot)
180 )) {
181 self::output_msg(get_string('erroruniqueconfig', 'tool_behat'));
182 return BEHAT_EXITCODE_CONFIG;
b8c4d91c
DM
183 }
184
b32ca4ca 185 // Checking behat dataroot existence otherwise echo about admin/tool/behat/cli/init.php.
48ca887c
DM
186 if (!empty($CFG->behat_dataroot)) {
187 $CFG->behat_dataroot = realpath($CFG->behat_dataroot);
188 }
33063f2e 189 if (empty($CFG->behat_dataroot) || !is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) {
b8c4d91c 190 self::output_msg(get_string('errordataroot', 'tool_behat'));
fa7e4e2b 191 return BEHAT_EXITCODE_CONFIG;
33063f2e 192 }
fa7e4e2b
DM
193
194 return 0;
b5c13009
DM
195 }
196
197 /**
198 * Has the site installed composer with --dev option
096858ed 199 * @return bool
b5c13009
DM
200 */
201 public static function are_behat_dependencies_installed() {
202 if (!is_dir(__DIR__ . '/../../../vendor/behat')) {
203 return false;
204 }
205 return true;
206 }
207
fa7e4e2b
DM
208 /**
209 * Outputs a message.
210 *
211 * Used in CLI + web UI methods. Stops the
212 * execution in web.
213 *
214 * @param string $msg
215 * @return void
216 */
217 protected static function output_msg($msg) {
b8c4d91c 218 global $CFG, $PAGE;
fa7e4e2b 219
b8c4d91c 220 // If we are using the web interface we want pretty messages.
fa7e4e2b 221 if (!CLI_SCRIPT) {
b8c4d91c
DM
222
223 $renderer = $PAGE->get_renderer('tool_behat');
224 echo $renderer->render_error($msg);
225
226 // Stopping execution.
227 exit(1);
228
fa7e4e2b 229 } else {
b8c4d91c
DM
230
231 // We continue execution after this.
232 $clibehaterrorstr = "Ensure you set \$CFG->behat_* vars in config.php " .
233 "and you ran admin/tool/behat/cli/init.php.\n" .
234 "More info in " . self::DOCS_URL . "#Installation\n\n";
235
236 echo 'Error: ' . $msg . "\n\n" . $clibehaterrorstr;
fa7e4e2b
DM
237 }
238 }
239
b5c13009 240}