weekly release 2.6dev
[moodle.git] / lib / tests / behat / behat_hooks.php
CommitLineData
f5ceb6c2 1<?php
f5ceb6c2
DM
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 hooks steps definitions.
19 *
20 * This methods are used by Behat CLI command.
21 *
22 * @package core
23 * @category test
24 * @copyright 2012 David MonllaĆ³
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
29
30require_once(__DIR__ . '/../../behat/behat_base.php');
31
1303eb29
DM
32use Behat\Behat\Event\SuiteEvent as SuiteEvent,
33 Behat\Behat\Event\ScenarioEvent as ScenarioEvent,
34 Behat\Behat\Event\StepEvent as StepEvent,
870349ee
DM
35 WebDriver\Exception\NoSuchWindow as NoSuchWindow,
36 WebDriver\Exception\UnexpectedAlertOpen as UnexpectedAlertOpen,
37 WebDriver\Exception\NoAlertOpenError as NoAlertOpenError;
f5ceb6c2
DM
38
39/**
40 * Hooks to the behat process.
41 *
42 * Behat accepts hooks after and before each
43 * suite, feature, scenario and step.
44 *
45 * They can not call other steps as part of their process
46 * like regular steps definitions does.
47 *
48 * Throws generic Exception because they are captured by Behat.
49 *
50 * @package core
51 * @category test
52 * @copyright 2012 David MonllaĆ³
53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
54 */
55class behat_hooks extends behat_base {
56
41eb672b
DM
57 /**
58 * @var Last browser session start time.
59 */
60 protected static $lastbrowsersessionstart = 0;
61
95b43d6b
DM
62 /**
63 * @var For actions that should only run once.
64 */
65 protected static $initprocessesfinished = false;
66
f5ceb6c2
DM
67 /**
68 * Gives access to moodle codebase, ensures all is ready and sets up the test lock.
69 *
70 * Includes config.php to use moodle codebase with $CFG->behat_*
71 * instead of $CFG->prefix and $CFG->dataroot, called once per suite.
72 *
73 * @static
74 * @throws Exception
75 * @BeforeSuite
76 */
77 public static function before_suite($event) {
78 global $CFG;
79
cfcbc34a
DM
80 // Defined only when the behat CLI command is running, the moodle init setup process will
81 // read this value and switch to $CFG->behat_dataroot and $CFG->behat_prefix instead of
82 // the normal site.
83 define('BEHAT_TEST', 1);
84
f5ceb6c2
DM
85 define('CLI_SCRIPT', 1);
86
cfcbc34a 87 // With BEHAT_TEST we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
f5ceb6c2
DM
88 require_once(__DIR__ . '/../../../config.php');
89
90 // Now that we are MOODLE_INTERNAL.
91 require_once(__DIR__ . '/../../behat/classes/behat_command.php');
92 require_once(__DIR__ . '/../../behat/classes/util.php');
93 require_once(__DIR__ . '/../../testing/classes/test_lock.php');
b08b0a28 94 require_once(__DIR__ . '/../../testing/classes/nasty_strings.php');
f5ceb6c2
DM
95
96 // Avoids vendor/bin/behat to be executed directly without test environment enabled
97 // to prevent undesired db & dataroot modifications, this is also checked
98 // before each scenario (accidental user deletes) in the BeforeScenario hook.
99
100 if (!behat_util::is_test_mode_enabled()) {
101 throw new Exception('Behat only can run if test mode is enabled. More info in ' . behat_command::DOCS_URL . '#Running_tests');
102 }
103
104 if (!behat_util::is_server_running()) {
105 throw new Exception($CFG->behat_wwwroot . ' is not available, ensure you started your PHP built-in server. More info in ' . behat_command::DOCS_URL . '#Running_tests');
106 }
107
b831d479
DM
108 // Prevents using outdated data, upgrade script would start and tests would fail.
109 if (!behat_util::is_test_data_updated()) {
b32ca4ca 110 $commandpath = 'php admin/tool/behat/cli/init.php';
b831d479
DM
111 throw new Exception('Your behat test site is outdated, please run ' . $commandpath . ' from your moodle dirroot to drop and install the behat test site again.');
112 }
f5ceb6c2
DM
113 // Avoid parallel tests execution, it continues when the previous lock is released.
114 test_lock::acquire('behat');
41eb672b
DM
115
116 // Store the browser reset time if reset after N seconds is specified in config.php.
117 if (!empty($CFG->behat_restart_browser_after)) {
118 // Store the initial browser session opening.
119 self::$lastbrowsersessionstart = time();
120 }
f5ceb6c2
DM
121 }
122
123 /**
124 * Resets the test environment.
125 *
126 * @throws coding_exception If here we are not using the test database it should be because of a coding error
127 * @BeforeScenario
128 */
129 public function before_scenario($event) {
130 global $DB, $SESSION, $CFG;
131
132 // As many checks as we can.
cfcbc34a
DM
133 if (!defined('BEHAT_TEST') ||
134 !defined('BEHAT_SITE_RUNNING') ||
f5ceb6c2
DM
135 php_sapi_name() != 'cli' ||
136 !behat_util::is_test_mode_enabled() ||
cfcbc34a 137 !behat_util::is_test_site()) {
46ac40cd 138 throw new coding_exception('Behat only can modify the test database and the test dataroot!');
f5ceb6c2
DM
139 }
140
c8619f33
DM
141 // Avoid some notices / warnings.
142 $SESSION = new stdClass();
143
f5ceb6c2
DM
144 behat_util::reset_database();
145 behat_util::reset_dataroot();
146
10dd80c2
DM
147 purge_all_caches();
148 accesslib_clear_all_caches(true);
149
b08b0a28
DM
150 // Reset the nasty strings list used during the last test.
151 nasty_strings::reset_used_strings();
152
f5ceb6c2
DM
153 // Assing valid data to admin user (some generator-related code needs a valid user).
154 $user = $DB->get_record('user', array('username' => 'admin'));
155 session_set_user($user);
5f4b4e91 156
41eb672b
DM
157 // Reset the browser if specified in config.php.
158 if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) {
159 $now = time();
160 if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) {
161 $this->getSession()->restart();
162 self::$lastbrowsersessionstart = $now;
163 }
164 }
165
5f4b4e91
DM
166 // Start always in the the homepage.
167 $this->getSession()->visit($this->locate_path('/'));
870349ee 168
95b43d6b
DM
169 // Checking that the root path is a Moodle test site.
170 if (self::is_first_scenario()) {
171 $notestsiteexception = new Exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' .
172 'ensure you started the built-in web server in the correct directory');
173 $this->find("xpath", "//head/child::title[normalize-space(.)='Acceptance test site']", $notestsiteexception);
174
175 self::$initprocessesfinished = true;
176 }
177
870349ee
DM
178 // Closing JS dialogs if present. Otherwise they would block this scenario execution.
179 if ($this->running_javascript()) {
180 try {
181 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
182 } catch (NoAlertOpenError $e) {
183 // All ok, there should not be JS dialogs in theory.
184 }
185 }
186
f5ceb6c2
DM
187 }
188
189 /**
190 * Ensures selenium is running.
191 *
192 * Is only executed in scenarios which requires Javascript to run,
193 * it returns a direct error message about what's going on.
194 *
195 * @throws Exception
196 * @BeforeScenario @javascript
197 */
198 public function before_scenario_javascript($event) {
199
200 // Just trying if server responds.
201 try {
c59e52f5 202 $this->getSession()->wait(0, false);
f5ceb6c2
DM
203 } catch (Exception $e) {
204 $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
205 $msg = 'Selenium server is not running, you need to start it to run tests that involves Javascript. ' . $moreinfo;
206 throw new Exception($msg);
207 }
208 }
209
210 /**
211 * Checks that all DOM is ready.
212 *
213 * Executed only when running against a real browser.
214 *
215 * @AfterStep @javascript
216 */
217 public function after_step_javascript($event) {
218
219 // If it doesn't have definition or it fails there is no need to check it.
220 if ($event->getResult() != StepEvent::PASSED ||
221 !$event->hasDefinition()) {
222 return;
223 }
224
28abad1a
DM
225 // Wait until the page is ready.
226 // We are already checking that we use a JS browser, this could
227 // change in case we use another JS driver.
228 try {
229
230 // Safari and Internet Explorer requires time between steps,
231 // otherwise Selenium tries to click in the previous page's DOM.
232 if ($this->getSession()->getDriver()->getBrowserName() == 'safari' ||
233 $this->getSession()->getDriver()->getBrowserName() == 'internet explorer') {
234 $this->getSession()->wait(self::TIMEOUT * 1000, false);
235
236 } else {
237 // With other browsers we just wait for the DOM ready.
238 $this->getSession()->wait(self::TIMEOUT * 1000, '(document.readyState === "complete")');
239 }
240
1303eb29
DM
241 } catch (NoSuchWindow $e) {
242 // If we were interacting with a popup window it will not exists after closing it.
243 }
f5ceb6c2
DM
244 }
245
5f4b4e91
DM
246 /**
247 * Internal step definition to find exceptions, debugging() messages and PHP debug messages.
248 *
249 * Part of behat_hooks class as is part of the testing framework, is auto-executed
250 * after each step so no features will splicitly use it.
251 *
252 * @Given /^I look for exceptions$/
253 * @see Moodle\BehatExtension\Tester\MoodleStepTester
254 */
255 public function i_look_for_exceptions() {
256
217e8e59
DM
257 // Wrap in try in case we were interacting with a closed window.
258 try {
5f4b4e91 259
217e8e59 260 // Exceptions.
3e76c7fa 261 $exceptionsxpath = "//div[@data-rel='fatalerror']";
8aea365f 262 // Debugging messages.
3e76c7fa 263 $debuggingxpath = "//div[@data-rel='debugging']";
8aea365f 264 // PHP debug messages.
3e76c7fa 265 $phperrorxpath = "//div[@data-rel='phpdebugmessage']";
8aea365f 266 // Any other backtrace.
6cd21356 267 $othersxpath = "(//*[contains(., ': call to ')])[1]";
8aea365f
DM
268
269 $xpaths = array($exceptionsxpath, $debuggingxpath, $phperrorxpath, $othersxpath);
270 $joinedxpath = implode(' | ', $xpaths);
271
272 // Joined xpath expression. Most of the time there will be no exceptions, so this pre-check
273 // is faster than to send the 4 xpath queries for each step.
274 if (!$this->getSession()->getDriver()->find($joinedxpath)) {
275 return;
276 }
277
278 // Exceptions.
279 if ($errormsg = $this->getSession()->getPage()->find('xpath', $exceptionsxpath)) {
5f4b4e91 280
217e8e59
DM
281 // Getting the debugging info and the backtrace.
282 $errorinfoboxes = $this->getSession()->getPage()->findAll('css', 'div.notifytiny');
283 $errorinfo = $this->get_debug_text($errorinfoboxes[0]->getHtml()) . "\n" .
284 $this->get_debug_text($errorinfoboxes[1]->getHtml());
5f4b4e91 285
217e8e59
DM
286 $msg = "Moodle exception: " . $errormsg->getText() . "\n" . $errorinfo;
287 throw new \Exception(html_entity_decode($msg));
288 }
289
290 // Debugging messages.
8aea365f 291 if ($debuggingmessages = $this->getSession()->getPage()->findAll('xpath', $debuggingxpath)) {
217e8e59
DM
292 $msgs = array();
293 foreach ($debuggingmessages as $debuggingmessage) {
294 $msgs[] = $this->get_debug_text($debuggingmessage->getHtml());
295 }
296 $msg = "debugging() message/s found:\n" . implode("\n", $msgs);
297 throw new \Exception(html_entity_decode($msg));
5f4b4e91 298 }
90ed22ab 299
217e8e59 300 // PHP debug messages.
8aea365f 301 if ($phpmessages = $this->getSession()->getPage()->findAll('xpath', $phperrorxpath)) {
90ed22ab 302
217e8e59
DM
303 $msgs = array();
304 foreach ($phpmessages as $phpmessage) {
305 $msgs[] = $this->get_debug_text($phpmessage->getHtml());
306 }
307 $msg = "PHP debug message/s found:\n" . implode("\n", $msgs);
308 throw new \Exception(html_entity_decode($msg));
90ed22ab 309 }
bbd802f7 310
217e8e59 311 // Any other backtrace.
9a1f4922
DM
312 // First looking through xpath as it is faster than get and parse the whole page contents,
313 // we get the contents and look for matches once we found something to suspect that there is a backtrace.
8aea365f 314 if ($this->getSession()->getDriver()->find($othersxpath)) {
9a1f4922
DM
315 $backtracespattern = '/(line [0-9]* of [^:]*: call to [\->&;:a-zA-Z_\x7f-\xff][\->&;:a-zA-Z0-9_\x7f-\xff]*)/';
316 if (preg_match_all($backtracespattern, $this->getSession()->getPage()->getContent(), $backtraces)) {
317 $msgs = array();
318 foreach ($backtraces[0] as $backtrace) {
319 $msgs[] = $backtrace . '()';
320 }
321 $msg = "Other backtraces found:\n" . implode("\n", $msgs);
322 throw new \Exception(htmlentities($msg));
217e8e59 323 }
bbd802f7 324 }
217e8e59
DM
325
326 } catch (NoSuchWindow $e) {
327 // If we were interacting with a popup window it will not exists after closing it.
870349ee
DM
328 } catch (UnexpectedAlertOpen $e) {
329 // We fail the scenario if we find an opened JS alert/confirm, in most of the cases it
330 // will be there because we are leaving an edited form without submitting/cancelling
331 // it, but moodle is using JS confirms and we can not just cancel the JS dialog
332 // as in some cases (delete activity with JS enabled for example) the test writer should
333 // use extra steps to deal with moodle's behaviour.
334 throw new Exception('Modal window present. Ensure there are no edited forms pending to submit/cancel.');
bbd802f7 335 }
5f4b4e91
DM
336 }
337
338 /**
339 * Converts HTML tags to line breaks to display the info in CLI
340 *
341 * @param string $html
342 * @return string
343 */
344 protected function get_debug_text($html) {
345
346 // Replacing HTML tags for new lines and keeping only the text.
347 $notags = preg_replace('/<+\s*\/*\s*([A-Z][A-Z0-9]*)\b[^>]*\/*\s*>*/i', "\n", $html);
348 return preg_replace("/(\n)+/s", "\n", $notags);
349 }
350
95b43d6b
DM
351 /**
352 * Returns whether the first scenario of the suite is running
353 *
354 * @return bool
355 */
356 protected static function is_first_scenario() {
357 return !(self::$initprocessesfinished);
358 }
f5ceb6c2 359}