Commit | Line | Data |
---|---|---|
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 |
26 | defined('MOODLE_INTERNAL') || die(); |
27 | ||
28 | require_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 | */ | |
38 | class 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 | |
69 | * @return string | |
70 | */ | |
71 | public final static function get_behat_command() { | |
72 | return 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'behat'; | |
73 | } | |
74 | ||
096858ed DM |
75 | /** |
76 | * Runs behat command with provided options | |
77 | * | |
78 | * Execution continues when the process finishes | |
79 | * | |
80 | * @param string $options Defaults to '' so tests would be executed | |
81 | * @return array CLI command outputs [0] => string, [1] => integer | |
82 | */ | |
83 | public final static function run($options = '') { | |
84 | global $CFG; | |
85 | ||
86 | $currentcwd = getcwd(); | |
87 | chdir($CFG->dirroot); | |
88 | exec(self::get_behat_command() . ' ' . $options, $output, $code); | |
89 | chdir($currentcwd); | |
90 | ||
91 | return array($output, $code); | |
92 | } | |
93 | ||
b5c13009 DM |
94 | /** |
95 | * Checks if behat is set up and working | |
96 | * | |
096858ed DM |
97 | * Uses notice() instead of behat_error() because is |
98 | * also called from web interface | |
99 | * | |
b5c13009 DM |
100 | * It checks behat dependencies have been installed and runs |
101 | * the behat help command to ensure it works as expected | |
102 | * | |
096858ed DM |
103 | * @param bool $checkphp Extra check for the PHP version |
104 | * @return void | |
b5c13009 DM |
105 | */ |
106 | public static function check_behat_setup($checkphp = false) { | |
107 | global $CFG; | |
108 | ||
109 | // We don't check the PHP version if $CFG->behat_switchcompletely has been enabled. | |
096858ed | 110 | // Here we are in CLI. |
b5c13009 | 111 | if (empty($CFG->behat_switchcompletely) && $checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) { |
096858ed | 112 | behat_error(BEHAT_EXITCODE_REQUIREMENT, 'PHP 5.4 is required. See config-dist.php for possible alternatives'); |
b5c13009 DM |
113 | } |
114 | ||
115 | // Moodle setting. | |
116 | if (!self::are_behat_dependencies_installed()) { | |
117 | ||
118 | $msg = get_string('wrongbehatsetup', 'tool_behat'); | |
119 | ||
120 | // With HTML. | |
096858ed | 121 | $docslink = self::DOCS_URL . '#Installation'; |
b5c13009 DM |
122 | if (!CLI_SCRIPT) { |
123 | $docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank')); | |
124 | } | |
475ac3f8 | 125 | $msg .= '. ' . get_string('moreinfoin', 'tool_behat', $docslink); |
b5c13009 DM |
126 | notice($msg); |
127 | } | |
128 | ||
129 | // Behat test command. | |
096858ed | 130 | list($output, $code) = self::run(' --help'); |
b5c13009 DM |
131 | |
132 | if ($code != 0) { | |
133 | notice(get_string('wrongbehatsetup', 'tool_behat')); | |
134 | } | |
33063f2e DM |
135 | |
136 | // Checking behat dataroot existence otherwise notice about admin/tool/behat/cli/util.php. | |
137 | if (empty($CFG->behat_dataroot) || !is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) { | |
138 | notice(get_string('runclitool', 'tool_behat', 'php admin/tool/behat/cli/util.php')); | |
139 | } | |
b5c13009 DM |
140 | } |
141 | ||
142 | /** | |
143 | * Has the site installed composer with --dev option | |
096858ed | 144 | * @return bool |
b5c13009 DM |
145 | */ |
146 | public static function are_behat_dependencies_installed() { | |
147 | if (!is_dir(__DIR__ . '/../../../vendor/behat')) { | |
148 | return false; | |
149 | } | |
150 | return true; | |
151 | } | |
152 | ||
153 | } |