MDL-37656 behat: Adding hooks
[moodle.git] / lib / tests / behat / behat_hooks.php
CommitLineData
f5ceb6c2
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Behat hooks steps definitions.
20 *
21 * This methods are used by Behat CLI command.
22 *
23 * @package core
24 * @category test
25 * @copyright 2012 David MonllaĆ³
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
30
31require_once(__DIR__ . '/../../behat/behat_base.php');
32
33use Behat\Behat\Event\SuiteEvent as SuiteEvent;
34use Behat\Behat\Event\ScenarioEvent as ScenarioEvent;
35use Behat\Behat\Event\StepEvent as StepEvent;
36
37/**
38 * Hooks to the behat process.
39 *
40 * Behat accepts hooks after and before each
41 * suite, feature, scenario and step.
42 *
43 * They can not call other steps as part of their process
44 * like regular steps definitions does.
45 *
46 * Throws generic Exception because they are captured by Behat.
47 *
48 * @package core
49 * @category test
50 * @copyright 2012 David MonllaĆ³
51 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52 */
53class behat_hooks extends behat_base {
54
55 /**
56 * @var string The last visited URL.
57 */
58 private $lasturl = null;
59
60 /**
61 * Gives access to moodle codebase, ensures all is ready and sets up the test lock.
62 *
63 * Includes config.php to use moodle codebase with $CFG->behat_*
64 * instead of $CFG->prefix and $CFG->dataroot, called once per suite.
65 *
66 * @static
67 * @throws Exception
68 * @BeforeSuite
69 */
70 public static function before_suite($event) {
71 global $CFG;
72
73 // To work with behat_dataroot and behat_prefix instead of the regular environment.
74 define('BEHAT_RUNNING', 1);
75 define('CLI_SCRIPT', 1);
76
77 require_once(__DIR__ . '/../../../config.php');
78
79 // Now that we are MOODLE_INTERNAL.
80 require_once(__DIR__ . '/../../behat/classes/behat_command.php');
81 require_once(__DIR__ . '/../../behat/classes/util.php');
82 require_once(__DIR__ . '/../../testing/classes/test_lock.php');
83
84 // Avoids vendor/bin/behat to be executed directly without test environment enabled
85 // to prevent undesired db & dataroot modifications, this is also checked
86 // before each scenario (accidental user deletes) in the BeforeScenario hook.
87
88 if (!behat_util::is_test_mode_enabled()) {
89 throw new Exception('Behat only can run if test mode is enabled. More info in ' . behat_command::DOCS_URL . '#Running_tests');
90 }
91
92 if (!behat_util::is_server_running()) {
93 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');
94 }
95
96 // Avoid parallel tests execution, it continues when the previous lock is released.
97 test_lock::acquire('behat');
98 }
99
100 /**
101 * Resets the test environment.
102 *
103 * @throws coding_exception If here we are not using the test database it should be because of a coding error
104 * @BeforeScenario
105 */
106 public function before_scenario($event) {
107 global $DB, $SESSION, $CFG;
108
109 // As many checks as we can.
110 if (!defined('BEHAT_RUNNING') ||
111 php_sapi_name() != 'cli' ||
112 !behat_util::is_test_mode_enabled() ||
113 !behat_util::is_test_site() ||
114 !isset($CFG->originaldataroot)) {
115 throw new coding_exception('Behat only can modify the test database and the test dataroot!');
116 }
117
118 behat_util::reset_database();
119 behat_util::reset_dataroot();
120
121 // Assing valid data to admin user (some generator-related code needs a valid user).
122 $user = $DB->get_record('user', array('username' => 'admin'));
123 session_set_user($user);
124
125 // Avoid some notices / warnings.
126 $SESSION = new stdClass();
127 }
128
129 /**
130 * Ensures selenium is running.
131 *
132 * Is only executed in scenarios which requires Javascript to run,
133 * it returns a direct error message about what's going on.
134 *
135 * @throws Exception
136 * @BeforeScenario @javascript
137 */
138 public function before_scenario_javascript($event) {
139
140 // Just trying if server responds.
141 try {
142 $this->getSession()->executeScript('// empty comment');
143 } catch (Exception $e) {
144 $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
145 $msg = 'Selenium server is not running, you need to start it to run tests that involves Javascript. ' . $moreinfo;
146 throw new Exception($msg);
147 }
148 }
149
150 /**
151 * Checks that all DOM is ready.
152 *
153 * Executed only when running against a real browser.
154 *
155 * @AfterStep @javascript
156 */
157 public function after_step_javascript($event) {
158
159 // If it doesn't have definition or it fails there is no need to check it.
160 if ($event->getResult() != StepEvent::PASSED ||
161 !$event->hasDefinition()) {
162 return;
163 }
164
165 // Wait until the page is ready if we are in a new URL.
166 $currenturl = $this->getSession()->getCurrentUrl();
167 if (is_null($this->lasturl) || $currenturl !== $this->lasturl) {
168 $this->lasturl = $currenturl;
169 $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
170 }
171 }
172
173}