Merge branch 'MDL-38282' of git://github.com/timhunt/moodle
[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
40923977
DM
45 /**
46 * @var Allowed types when using text selectors arguments.
47 */
48 public static $allowedtextselectors = array(
49 'css_element' => 'css_element',
50 'xpath_element' => 'xpath_element'
51 );
52
53 /**
54 * @var Allowed types when using selector arguments.
55 */
56 public static $allowedselectors = array(
57 'link' => 'link',
58 'button' => 'button',
59 'link_or_button' => 'link_or_button',
60 'select' => 'select',
61 'checkbox' => 'checkbox',
62 'radio' => 'radio',
63 'file' => 'file',
64 'optgroup' => 'optgroup',
65 'option' => 'option',
66 'table' => 'table',
67 'field' => 'field',
68 'fieldset' => 'fieldset',
69 'css_element' => 'css_element',
70 'xpath_element' => 'xpath_element'
71 );
72
b5c13009
DM
73 /**
74 * Ensures the behat dir exists in moodledata
b5c13009
DM
75 * @return string Full path
76 */
77 public static function get_behat_dir() {
78 global $CFG;
79
096858ed 80 $behatdir = $CFG->behat_dataroot . '/behat';
b5c13009
DM
81
82 if (!is_dir($behatdir)) {
83 if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
096858ed 84 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' can not be created');
b5c13009
DM
85 }
86 }
87
88 if (!is_writable($behatdir)) {
096858ed 89 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' is not writable');
b5c13009
DM
90 }
91
92 return $behatdir;
93 }
94
95 /**
96 * Returns the executable path
97 * @return string
98 */
99 public final static function get_behat_command() {
100 return 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'behat';
101 }
102
096858ed
DM
103 /**
104 * Runs behat command with provided options
105 *
106 * Execution continues when the process finishes
107 *
108 * @param string $options Defaults to '' so tests would be executed
109 * @return array CLI command outputs [0] => string, [1] => integer
110 */
111 public final static function run($options = '') {
112 global $CFG;
113
114 $currentcwd = getcwd();
115 chdir($CFG->dirroot);
116 exec(self::get_behat_command() . ' ' . $options, $output, $code);
117 chdir($currentcwd);
118
119 return array($output, $code);
120 }
121
b5c13009
DM
122 /**
123 * Checks if behat is set up and working
124 *
096858ed
DM
125 * Uses notice() instead of behat_error() because is
126 * also called from web interface
127 *
b5c13009
DM
128 * It checks behat dependencies have been installed and runs
129 * the behat help command to ensure it works as expected
130 *
096858ed 131 * @param bool $checkphp Extra check for the PHP version
fa7e4e2b 132 * @return int Error code or 0 if all ok
b5c13009 133 */
fa7e4e2b 134 public static function behat_setup_problem($checkphp = false) {
b5c13009
DM
135 global $CFG;
136
137 // We don't check the PHP version if $CFG->behat_switchcompletely has been enabled.
096858ed 138 // Here we are in CLI.
b5c13009 139 if (empty($CFG->behat_switchcompletely) && $checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) {
096858ed 140 behat_error(BEHAT_EXITCODE_REQUIREMENT, 'PHP 5.4 is required. See config-dist.php for possible alternatives');
b5c13009
DM
141 }
142
143 // Moodle setting.
144 if (!self::are_behat_dependencies_installed()) {
145
146 $msg = get_string('wrongbehatsetup', 'tool_behat');
147
148 // With HTML.
096858ed 149 $docslink = self::DOCS_URL . '#Installation';
b5c13009
DM
150 if (!CLI_SCRIPT) {
151 $docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank'));
152 }
475ac3f8 153 $msg .= '. ' . get_string('moreinfoin', 'tool_behat', $docslink);
fa7e4e2b
DM
154
155 self::output_msg($msg);
156 return BEHAT_EXITCODE_COMPOSER;
b5c13009
DM
157 }
158
159 // Behat test command.
096858ed 160 list($output, $code) = self::run(' --help');
b5c13009
DM
161
162 if ($code != 0) {
fa7e4e2b
DM
163 // Returning composer error code to avoid conflicts with behat and moodle error codes.
164 self::output_msg(get_string('wrongbehatsetup', 'tool_behat'));
165 return BEHAT_EXITCODE_COMPOSER;
b5c13009 166 }
33063f2e 167
fa7e4e2b 168 // Checking behat dataroot existence otherwise echo about admin/tool/behat/cli/util.php.
33063f2e 169 if (empty($CFG->behat_dataroot) || !is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) {
fa7e4e2b
DM
170 self::output_msg(get_string('runclitool', 'tool_behat', 'php admin/tool/behat/cli/util.php'));
171 return BEHAT_EXITCODE_CONFIG;
33063f2e 172 }
fa7e4e2b
DM
173
174 return 0;
b5c13009
DM
175 }
176
177 /**
178 * Has the site installed composer with --dev option
096858ed 179 * @return bool
b5c13009
DM
180 */
181 public static function are_behat_dependencies_installed() {
182 if (!is_dir(__DIR__ . '/../../../vendor/behat')) {
183 return false;
184 }
185 return true;
186 }
187
fa7e4e2b
DM
188 /**
189 * Outputs a message.
190 *
191 * Used in CLI + web UI methods. Stops the
192 * execution in web.
193 *
194 * @param string $msg
195 * @return void
196 */
197 protected static function output_msg($msg) {
198
199 if (!CLI_SCRIPT) {
200 notice($msg);
201 } else {
202 echo $msg;
203 }
204 }
205
b5c13009 206}