MDL-38958 behat: Step definition to add questions
[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
32use Behat\Behat\Event\SuiteEvent as SuiteEvent;
33use Behat\Behat\Event\ScenarioEvent as ScenarioEvent;
34use Behat\Behat\Event\StepEvent as StepEvent;
35
36/**
37 * Hooks to the behat process.
38 *
39 * Behat accepts hooks after and before each
40 * suite, feature, scenario and step.
41 *
42 * They can not call other steps as part of their process
43 * like regular steps definitions does.
44 *
45 * Throws generic Exception because they are captured by Behat.
46 *
47 * @package core
48 * @category test
49 * @copyright 2012 David MonllaĆ³
50 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 */
52class behat_hooks extends behat_base {
53
f5ceb6c2
DM
54 /**
55 * Gives access to moodle codebase, ensures all is ready and sets up the test lock.
56 *
57 * Includes config.php to use moodle codebase with $CFG->behat_*
58 * instead of $CFG->prefix and $CFG->dataroot, called once per suite.
59 *
60 * @static
61 * @throws Exception
62 * @BeforeSuite
63 */
64 public static function before_suite($event) {
65 global $CFG;
66
67 // To work with behat_dataroot and behat_prefix instead of the regular environment.
68 define('BEHAT_RUNNING', 1);
69 define('CLI_SCRIPT', 1);
70
b831d479 71 // With BEHAT_RUNNING we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
f5ceb6c2
DM
72 require_once(__DIR__ . '/../../../config.php');
73
74 // Now that we are MOODLE_INTERNAL.
75 require_once(__DIR__ . '/../../behat/classes/behat_command.php');
76 require_once(__DIR__ . '/../../behat/classes/util.php');
77 require_once(__DIR__ . '/../../testing/classes/test_lock.php');
b08b0a28 78 require_once(__DIR__ . '/../../testing/classes/nasty_strings.php');
f5ceb6c2
DM
79
80 // Avoids vendor/bin/behat to be executed directly without test environment enabled
81 // to prevent undesired db & dataroot modifications, this is also checked
82 // before each scenario (accidental user deletes) in the BeforeScenario hook.
83
84 if (!behat_util::is_test_mode_enabled()) {
85 throw new Exception('Behat only can run if test mode is enabled. More info in ' . behat_command::DOCS_URL . '#Running_tests');
86 }
87
88 if (!behat_util::is_server_running()) {
89 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');
90 }
91
b831d479
DM
92 // Prevents using outdated data, upgrade script would start and tests would fail.
93 if (!behat_util::is_test_data_updated()) {
94 $commandpath = 'php admin/tool/behat/cli/util.php';
95 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.');
96 }
f5ceb6c2
DM
97 // Avoid parallel tests execution, it continues when the previous lock is released.
98 test_lock::acquire('behat');
99 }
100
101 /**
102 * Resets the test environment.
103 *
104 * @throws coding_exception If here we are not using the test database it should be because of a coding error
105 * @BeforeScenario
106 */
107 public function before_scenario($event) {
108 global $DB, $SESSION, $CFG;
109
110 // As many checks as we can.
111 if (!defined('BEHAT_RUNNING') ||
112 php_sapi_name() != 'cli' ||
113 !behat_util::is_test_mode_enabled() ||
114 !behat_util::is_test_site() ||
46ac40cd
DM
115 !isset($CFG->originaldataroot)) {
116 throw new coding_exception('Behat only can modify the test database and the test dataroot!');
f5ceb6c2
DM
117 }
118
119 behat_util::reset_database();
120 behat_util::reset_dataroot();
121
10dd80c2
DM
122 purge_all_caches();
123 accesslib_clear_all_caches(true);
124
b08b0a28
DM
125 // Reset the nasty strings list used during the last test.
126 nasty_strings::reset_used_strings();
127
f5ceb6c2
DM
128 // Assing valid data to admin user (some generator-related code needs a valid user).
129 $user = $DB->get_record('user', array('username' => 'admin'));
130 session_set_user($user);
131
132 // Avoid some notices / warnings.
133 $SESSION = new stdClass();
134 }
135
136 /**
137 * Ensures selenium is running.
138 *
139 * Is only executed in scenarios which requires Javascript to run,
140 * it returns a direct error message about what's going on.
141 *
142 * @throws Exception
143 * @BeforeScenario @javascript
144 */
145 public function before_scenario_javascript($event) {
146
147 // Just trying if server responds.
148 try {
149 $this->getSession()->executeScript('// empty comment');
150 } catch (Exception $e) {
151 $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
152 $msg = 'Selenium server is not running, you need to start it to run tests that involves Javascript. ' . $moreinfo;
153 throw new Exception($msg);
154 }
155 }
156
157 /**
158 * Checks that all DOM is ready.
159 *
160 * Executed only when running against a real browser.
161 *
162 * @AfterStep @javascript
163 */
164 public function after_step_javascript($event) {
165
166 // If it doesn't have definition or it fails there is no need to check it.
167 if ($event->getResult() != StepEvent::PASSED ||
168 !$event->hasDefinition()) {
169 return;
170 }
171
d56ae59b
DM
172 // Wait until the page is ready.
173 $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
f5ceb6c2
DM
174 }
175
176}