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