2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once(__DIR__
. '/../lib.php');
31 * Behat command related utils
35 * @copyright 2013 David MonllaĆ³
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 const DOCS_URL
= 'http://docs.moodle.org/dev/Acceptance_testing';
46 * @var Allowed types when using text selectors arguments.
48 public static $allowedtextselectors = array(
49 'css_element' => 'css_element',
50 'xpath_element' => 'xpath_element'
54 * @var Allowed types when using selector arguments.
56 public static $allowedselectors = array(
59 'link_or_button' => 'link_or_button',
61 'checkbox' => 'checkbox',
64 'optgroup' => 'optgroup',
68 'fieldset' => 'fieldset',
69 'css_element' => 'css_element',
70 'xpath_element' => 'xpath_element'
74 * Ensures the behat dir exists in moodledata
75 * @return string Full path
77 public static function get_behat_dir() {
80 $behatdir = $CFG->behat_dataroot
. '/behat';
82 if (!is_dir($behatdir)) {
83 if (!mkdir($behatdir, $CFG->directorypermissions
, true)) {
84 behat_error(BEHAT_EXITCODE_PERMISSIONS
, 'Directory ' . $behatdir . ' can not be created');
88 if (!is_writable($behatdir)) {
89 behat_error(BEHAT_EXITCODE_PERMISSIONS
, 'Directory ' . $behatdir . ' is not writable');
96 * Returns the executable path
99 public final static function get_behat_command() {
100 return 'vendor' . DIRECTORY_SEPARATOR
. 'bin' . DIRECTORY_SEPARATOR
. 'behat';
104 * Runs behat command with provided options
106 * Execution continues when the process finishes
108 * @param string $options Defaults to '' so tests would be executed
109 * @return array CLI command outputs [0] => string, [1] => integer
111 public final static function run($options = '') {
114 $currentcwd = getcwd();
115 chdir($CFG->dirroot
);
116 exec(self
::get_behat_command() . ' ' . $options, $output, $code);
119 return array($output, $code);
123 * Checks if behat is set up and working
125 * Uses notice() instead of behat_error() because is
126 * also called from web interface
128 * It checks behat dependencies have been installed and runs
129 * the behat help command to ensure it works as expected
131 * @param bool $checkphp Extra check for the PHP version
132 * @return int Error code or 0 if all ok
134 public static function behat_setup_problem($checkphp = false) {
137 // We don't check the PHP version if $CFG->behat_switchcompletely has been enabled.
138 // Here we are in CLI.
139 if (empty($CFG->behat_switchcompletely
) && $checkphp && version_compare(PHP_VERSION
, '5.4.0', '<')) {
140 behat_error(BEHAT_EXITCODE_REQUIREMENT
, 'PHP 5.4 is required. See config-dist.php for possible alternatives');
143 $clibehaterrorstr = "Behat dependencies not installed. Ensure you ran the composer installer. " . self
::DOCS_URL
. "#Installation\n";
146 if (!self
::are_behat_dependencies_installed()) {
152 $msg = get_string('wrongbehatsetup', 'tool_behat');
153 $docslink = self
::DOCS_URL
. '#Installation';
154 $docslink = html_writer
::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank'));
155 $msg .= get_string('moreinfoin', 'tool_behat', $docslink);
157 $msg = $clibehaterrorstr;
160 self
::output_msg($msg);
161 return BEHAT_EXITCODE_COMPOSER
;
164 // Behat test command.
165 list($output, $code) = self
::run(' --help');
168 // Returning composer error code to avoid conflicts with behat and moodle error codes.
170 $msg = get_string('wrongbehatsetup', 'tool_behat');
172 $msg = $clibehaterrorstr;
174 self
::output_msg($msg);
175 return BEHAT_EXITCODE_COMPOSER
;
178 // Checking behat dataroot existence otherwise echo about admin/tool/behat/cli/util.php.
179 if (empty($CFG->behat_dataroot
) ||
!is_dir($CFG->behat_dataroot
) ||
!is_writable($CFG->behat_dataroot
)) {
180 self
::output_msg(get_string('runclitool', 'tool_behat', 'php admin/tool/behat/cli/util.php'));
181 return BEHAT_EXITCODE_CONFIG
;
188 * Has the site installed composer with --dev option
191 public static function are_behat_dependencies_installed() {
192 if (!is_dir(__DIR__
. '/../../../vendor/behat')) {
201 * Used in CLI + web UI methods. Stops the
207 protected static function output_msg($msg) {