MDL-38548 behat: New tests
[moodle.git] / course / tests / behat / behat_course.php
CommitLineData
a1990e50
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 course-related steps definitions.
19 *
20 * @package core_course
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
29
30use Behat\Behat\Context\Step\Given as Given,
18c84063
DM
31 Behat\Gherkin\Node\TableNode as TableNode,
32 Behat\Mink\Exception\ExpectationException as ExpectationException,
33 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
a1990e50
DM
34
35/**
36 * Course-related steps definitions.
37 *
38 * @package core_course
39 * @category test
40 * @copyright 2012 David MonllaĆ³
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class behat_course extends behat_base {
44
45 /**
46 * Turns editing mode on.
47 * @Given /^I turn editing mode on$/
48 */
49 public function i_turn_editing_mode_on() {
50 return new Given('I press "Turn editing on"');
51 }
52
53 /**
54 * Turns editing mode off.
55 * @Given /^I turn editing mode off$/
56 */
57 public function i_turn_editing_mode_off() {
58 return new Given('I press "Turn editing off"');
59 }
60
61 /**
62 * Adds the selected activity/resource filling the form data with the specified field/value pairs.
63 *
64 * @When /^I add a "(?P<activity_or_resource_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)" and I fill the form with:$/
65 * @param string $activity The activity name
66 * @param string $section The section number
67 * @param TableNode $data The activity field/value data
68 */
69 public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) {
70
a1990e50
DM
71 return array(
72 new Given('I add a "'.$activity.'" to section "'.$section.'"'),
73 new Given('I fill the moodle form with:', $data),
74 new Given('I press "Save and return to course"')
75 );
76 }
77
78 /**
79 * Opens the activity chooser and opens the activity/resource form page.
80 *
81 * @Given /^I add a "(?P<activity_or_resource_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)"$/
1f9ffbdb 82 * @throws ElementNotFoundException Thrown by behat_base::find
a1990e50
DM
83 * @param string $activity
84 * @param string $section
85 */
86 public function i_add_to_section($activity, $section) {
87
a1990e50 88 // Clicks add activity or resource section link.
1f9ffbdb
DM
89 $sectionxpath = "//*[@id='section-" . $section . "']/*/*/*/div[@class='section-modchooser']/span/a";
90 $sectionnode = $this->find('xpath', $sectionxpath);
91 $sectionnode->click();
a1990e50
DM
92
93 // Clicks the selected activity if it exists.
94 $activityxpath = ".//label[contains(.,'" . $activity . "')]/input";
1f9ffbdb
DM
95 $activitynode = $this->find('xpath', $activityxpath);
96 $activitynode->doubleClick();
a1990e50
DM
97 }
98
18c84063
DM
99 /**
100 * Turns course section highlighting on.
101 *
102 * @Given /^I turn section "(?P<section_number>\d+)" highlighting on$/
103 * @param int $sectionnumber The section number
104 */
105 public function i_turn_section_highlighting_on($sectionnumber) {
106
107 // Ensures the section exists.
108 $xpath = $this->section_exists($sectionnumber);
109
110 return array(
111 new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
112 new Given('I wait "2" seconds')
113 );
114 }
115
116 /**
117 * Turns course section highlighting off.
118 *
119 * @Given /^I turn section "(?P<section_number>\d+)" highlighting off$/
120 * @param int $sectionnumber The section number
121 */
122 public function i_turn_section_highlighting_off($sectionnumber) {
123
124 // Ensures the section exists.
125 $xpath = $this->section_exists($sectionnumber);
126
127 return array(
128 new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
129 new Given('I wait "2" seconds')
130 );
131 }
132
133 /**
b918f9f7
DM
134 * Shows the specified hidden section. You need to be in the course page and on editing mode.
135 *
136 * @Given /^I show section "(?P<section_number>\d+)"$/
137 * @param int $sectionnumber
138 */
139 public function i_show_section($sectionnumber) {
140 $showicon = $this->show_section_icon_exists($sectionnumber);
141 $showicon->click();
142
143 // It requires time.
144 $this->getSession()->wait(5000, false);
145 }
146
147 /**
148 * Hides the specified visible section. You need to be in the course page and on editing mode.
149 *
150 * @Given /^I hide section "(?P<section_number>\d+)"$/
151 * @param int $sectionnumber
152 */
153 public function i_hide_section($sectionnumber) {
154 $hideicon = $this->hide_section_icon_exists($sectionnumber);
155 $hideicon->click();
156
157 // It requires time.
158 $this->getSession()->wait(5000, false);
159 }
160
161 /**
162 * Checks if the specified course section hightlighting is turned on. You need to be in the course page on editing mode.
18c84063 163 *
18c84063 164 * @Then /^section "(?P<section_number>\d+)" should be highlighted$/
b918f9f7 165 * @throws ExpectationException
18c84063
DM
166 * @param int $sectionnumber The section number
167 */
168 public function section_should_be_highlighted($sectionnumber) {
169
170 // Ensures the section exists.
171 $xpath = $this->section_exists($sectionnumber);
172
173 // The important checking, we can not check the img.
174 $xpath = $xpath . "/descendant::img[@alt='" . get_string('markedthistopic') . "'][contains(@src, 'marked')]";
175 $exception = new ExpectationException('The "' . $sectionnumber . '" section is not highlighted', $this->getSession());
176 $this->find('xpath', $xpath, $exception);
177 }
178
179 /**
b918f9f7 180 * Checks if the specified course section highlighting is turned off. You need to be in the course page on editing mode.
18c84063
DM
181 *
182 * @Then /^section "(?P<section_number>\d+)" should not be highlighted$/
b918f9f7 183 * @throws ExpectationException
18c84063
DM
184 * @param int $sectionnumber The section number
185 */
186 public function section_should_not_be_highlighted($sectionnumber) {
187
188 // We only catch ExpectationException, ElementNotFoundException should be thrown if the specified section does not exist.
189 try {
190 $this->section_should_be_highlighted($sectionnumber);
191 } catch (ExpectationException $e) {
192 // ExpectedException means that it is not highlighted.
193 return;
194 }
195
196 throw new ExpectationException('The "' . $sectionnumber . '" section is highlighted', $this->getSession());
197 }
198
b918f9f7
DM
199 /**
200 * Checks that the specified section is visible. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
201 *
202 * @Then /^section "(?P<section_number>\d+)" should be hidden$/
203 * @throws ExpectationException
204 * @throws ElementNotFoundException Thrown by behat_base::find
205 * @param int $sectionnumber
206 */
207 public function section_should_be_hidden($sectionnumber) {
208
209 $sectionxpath = $this->section_exists($sectionnumber);
210
211 // Section should be hidden.
212 $exception = new ExpectationException('The section is not hidden', $this->getSession());
213 $this->find('xpath', $sectionxpath . "[contains(concat(' ', @class, ' '), ' hidden ')]", $exception);
214
215 // The checking are different depending on user permissions.
216 if ($this->is_course_editor()) {
217
218 // The section must be hidden.
219 $this->show_section_icon_exists($sectionnumber);
220
221 // If there are activities they should be hidden and the visibility icon should not be available.
222 if ($activities = $this->get_section_activities($sectionxpath)) {
223
224 $dimmedexception = new ExpectationException('There are activities that are not dimmed', $this->getSession());
225 $visibilityexception = new ExpectationException('There are activities which visibility icons are clickable', $this->getSession());
226 foreach ($activities as $activity) {
227
228 // Dimmed.
229 $this->find('xpath', "//div[contains(concat(' ', @class, ' '), ' activityinstance ')]
230/a[contains(concat(' ', @class, ' '), ' dimmed ')]", $dimmedexception, $activity);
231
232 // To check that the visibility is not clickable we check the funcionality rather than the applied style.
233 $visibilityiconnode = $this->find('css', 'a.editing_show img', false, $activity);
234 $visibilityiconnode->click();
235
236 // We ensure that we still see the show icon.
237 $visibilityiconnode = $this->find('css', 'a.editing_show img', $visibilityexception, $activity);
238 }
239 }
240
241 } else {
242 // There shouldn't be activities.
243 if ($this->get_section_activities($sectionxpath)) {
244 throw new ExpectationException('There are activities in the section and they should be hidden', $this->getSession());
245 }
246 }
247 }
248
249 /**
250 * Checks that the specified section is visible. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
251 *
252 * @Then /^section "(?P<section_number>\d+)" should be visible$/
253 * @throws ExpectationException
254 * @param int $sectionnumber
255 */
256 public function section_should_be_visible($sectionnumber) {
257
258 $sectionxpath = $this->section_exists($sectionnumber);
259
260 // Section should not be hidden.
261 if (!$this->getSession()->getPage()->find('xpath', $sectionxpath . "[not(contains(concat(' ', @class, ' '), ' hidden '))]")) {
262 throw new ExpectationException('The section is hidden', $this->getSession());
263 }
264
265 // Hide section button should be visible.
266 if ($this->is_course_editor()) {
267 $this->hide_section_icon_exists($sectionnumber);
268 }
269 }
270
18c84063
DM
271 /**
272 * Checks if the course section exists.
273 *
274 * @throws ElementNotFoundException Thrown by behat_base::find
275 * @param int $sectionnumber
b918f9f7 276 * @return string The xpath of the section.
18c84063
DM
277 */
278 protected function section_exists($sectionnumber) {
279
280 // Just to give more info in case it does not exist.
281 $xpath = "//li[@id='section-" . $sectionnumber . "']";
282 $exception = new ElementNotFoundException($this->getSession(), "Section $sectionnumber ");
283 $this->find('xpath', $xpath, $exception);
284
285 return $xpath;
286 }
b918f9f7
DM
287
288 /**
289 * Returns the show section icon or throws an exception.
290 *
291 * @throws ElementNotFoundException Thrown by behat_base::find
292 * @param int $sectionnumber
293 * @return NodeElement
294 */
295 protected function show_section_icon_exists($sectionnumber) {
296
297 // Gets the section xpath and ensure it exists.
298 $xpath = $this->section_exists($sectionnumber);
299
300 // We need to know the course format as the text strings depends on them.
301 $courseformat = $this->get_course_format();
302
303 // Checking the show button alt text and show icon.
304 $xpath = $xpath . "/descendant::a/descendant::img[@alt='". get_string('showfromothers', $courseformat) ."'][contains(@src, 'show')]";
305
306 $exception = new ElementNotFoundException($this->getSession(), 'Show section icon ');
307 return $this->find('xpath', $xpath, $exception);
308 }
309
310 /**
311 * Returns the hide section icon link if it exists or throws exception.
312 *
313 * @throws ElementNotFoundException Thrown by behat_base::find
314 * @param int $sectionnumber
315 * @return NodeElement
316 */
317 protected function hide_section_icon_exists($sectionnumber) {
318
319 // Gets the section xpath and ensure it exists.
320 $xpath = $this->section_exists($sectionnumber);
321
322 // We need to know the course format as the text strings depends on them.
323 $courseformat = $this->get_course_format();
324
325 // Checking the hide button alt text and hide icon.
326 $xpath = $xpath . "/descendant::a/descendant::img[@alt='". get_string('hidefromothers', $courseformat) ."'][contains(@src, 'hide')]";
327
328 $exception = new ElementNotFoundException($this->getSession(), 'Hide section icon ');
329 return $this->find('xpath', $xpath, $exception);
330 }
331
332 /**
333 * Gets the current course format.
334 *
335 * @throws ExpectationException If we are not in the course view page.
336 * @return string The course format in a frankenstyled name.
337 */
338 protected function get_course_format() {
339
340 $exception = new ExpectationException('You are not in a course page', $this->getSession());
341
342 // The moodle body's id attribute contains the course format.
343 $node = $this->getSession()->getPage()->find('css', 'body');
344 if (!$node) {
345 throw $exception;
346 }
347
348 if (!$bodyid = $node->getAttribute('id')) {
349 throw $exception;
350 }
351
352 if (strstr($bodyid, 'page-course-view-') === false) {
353 throw $exception;
354 }
355
356 return 'format_' . str_replace('page-course-view-', '', $bodyid);
357 }
358
359 /**
360 * Gets the section's activites DOM nodes.
361 *
362 * @param string $sectionxpath
363 * @return array NodeElement instances
364 */
365 protected function get_section_activities($sectionxpath) {
366
367 $xpath = $sectionxpath . "/descendant::li[contains(concat(' ', @class, ' '), ' activity ')]";
368
369 // We spin here, as activities usually require a lot of time to load.
370 try {
371 $activities = $this->find_all('xpath', $xpath);
372 } catch (ElementNotFoundException $e) {
373 return false;
374 }
375
376 return $activities;
377 }
378
379 /**
380 * Returns whether the user can edit the course contents or not.
381 *
382 * @return bool
383 */
384 protected function is_course_editor() {
385
386 // We don't need to behat_base::spin() here as all is already loaded.
387 if (!$this->getSession()->getPage()->findButton('Turn editing off') &&
388 !$this->getSession()->getPage()->findButton('Turn editing on')) {
389 return false;
390 }
391
392 return true;
393 }
394
a1990e50 395}