MDL-39051 behat: New general steps definitions
[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
df1ff55d
DM
61 /**
62 * Creates a new course with the provided table data matching course settings names with the desired values.
63 *
64 * @Given /^I create a course with:$/
65 * @param TableNode $table The course data
66 */
67 public function i_create_a_course_with(TableNode $table) {
68 return array(
69 new Given('I go to the courses management page'),
70 new Given('I press "Add a new course"'),
71 new Given('I fill the moodle form with:', $table),
72 new Given('I press "Save changes"')
73 );
74 }
75
76 /**
77 * Goes to the system courses/categories management page.
78 *
79 * @Given /^I go to the courses management page$/
80 */
81 public function i_go_to_the_courses_management_page() {
82
83 return array(
84 new Given('I am on homepage'),
85 new Given('I expand "Site administration" node'),
86 new Given('I expand "Courses" node'),
87 new Given('I follow "Add/edit courses"'),
88 );
89 }
90
a1990e50
DM
91 /**
92 * Adds the selected activity/resource filling the form data with the specified field/value pairs.
93 *
44d5af38 94 * @When /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)" and I fill the form with:$/
a1990e50
DM
95 * @param string $activity The activity name
96 * @param string $section The section number
97 * @param TableNode $data The activity field/value data
98 */
99 public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) {
100
a1990e50
DM
101 return array(
102 new Given('I add a "'.$activity.'" to section "'.$section.'"'),
103 new Given('I fill the moodle form with:', $data),
104 new Given('I press "Save and return to course"')
105 );
106 }
107
108 /**
109 * Opens the activity chooser and opens the activity/resource form page.
110 *
44d5af38 111 * @Given /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)"$/
1f9ffbdb 112 * @throws ElementNotFoundException Thrown by behat_base::find
a1990e50
DM
113 * @param string $activity
114 * @param string $section
115 */
116 public function i_add_to_section($activity, $section) {
117
1c00d6f6
DM
118 $sectionxpath = "//*[@id='section-" . $section . "']";
119
120 if ($this->running_javascript()) {
121
122 // Clicks add activity or resource section link.
123 $sectionxpath = $sectionxpath . "/descendant::div[@class='section-modchooser']/span/a";
124 $sectionnode = $this->find('xpath', $sectionxpath);
125 $sectionnode->click();
126
127 // Clicks the selected activity if it exists.
44d5af38 128 $activity = ucfirst($activity);
00ea74cb
DM
129 $activityxpath = "//div[@id='chooseform']/descendant::label" .
130 "/descendant::span[contains(concat(' ', @class, ' '), ' typename ')][contains(.,'" . $activity . "')]" .
131 "/parent::label/child::input";
1c00d6f6
DM
132 $activitynode = $this->find('xpath', $activityxpath);
133 $activitynode->doubleClick();
134
135 } else {
136 // Without Javascript.
137
138 // Selecting the option from the select box which contains the option.
00ea74cb
DM
139 $selectxpath = $sectionxpath . "/descendant::div[contains(concat(' ', @class, ' '), ' section_add_menus ')]" .
140 "/descendant::select[contains(., '" . $activity . "')]";
1c00d6f6
DM
141 $selectnode = $this->find('xpath', $selectxpath);
142 $selectnode->selectOption($activity);
143
144 // Go button.
145 $gobuttonxpath = $selectxpath . "/ancestor::form/descendant::input[@type='submit']";
146 $gobutton = $this->find('xpath', $gobuttonxpath);
147 $gobutton->click();
148 }
149
a1990e50
DM
150 }
151
18c84063
DM
152 /**
153 * Turns course section highlighting on.
154 *
155 * @Given /^I turn section "(?P<section_number>\d+)" highlighting on$/
156 * @param int $sectionnumber The section number
157 */
158 public function i_turn_section_highlighting_on($sectionnumber) {
159
160 // Ensures the section exists.
161 $xpath = $this->section_exists($sectionnumber);
162
163 return array(
164 new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
165 new Given('I wait "2" seconds')
166 );
167 }
168
169 /**
170 * Turns course section highlighting off.
171 *
172 * @Given /^I turn section "(?P<section_number>\d+)" highlighting off$/
173 * @param int $sectionnumber The section number
174 */
175 public function i_turn_section_highlighting_off($sectionnumber) {
176
177 // Ensures the section exists.
178 $xpath = $this->section_exists($sectionnumber);
179
180 return array(
181 new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
182 new Given('I wait "2" seconds')
183 );
184 }
185
186 /**
b918f9f7
DM
187 * Shows the specified hidden section. You need to be in the course page and on editing mode.
188 *
189 * @Given /^I show section "(?P<section_number>\d+)"$/
190 * @param int $sectionnumber
191 */
192 public function i_show_section($sectionnumber) {
193 $showicon = $this->show_section_icon_exists($sectionnumber);
194 $showicon->click();
195
196 // It requires time.
197 $this->getSession()->wait(5000, false);
198 }
199
200 /**
201 * Hides the specified visible section. You need to be in the course page and on editing mode.
202 *
203 * @Given /^I hide section "(?P<section_number>\d+)"$/
204 * @param int $sectionnumber
205 */
206 public function i_hide_section($sectionnumber) {
207 $hideicon = $this->hide_section_icon_exists($sectionnumber);
208 $hideicon->click();
209
210 // It requires time.
211 $this->getSession()->wait(5000, false);
212 }
213
214 /**
215 * Checks if the specified course section hightlighting is turned on. You need to be in the course page on editing mode.
18c84063 216 *
18c84063 217 * @Then /^section "(?P<section_number>\d+)" should be highlighted$/
b918f9f7 218 * @throws ExpectationException
18c84063
DM
219 * @param int $sectionnumber The section number
220 */
221 public function section_should_be_highlighted($sectionnumber) {
222
223 // Ensures the section exists.
224 $xpath = $this->section_exists($sectionnumber);
225
226 // The important checking, we can not check the img.
227 $xpath = $xpath . "/descendant::img[@alt='" . get_string('markedthistopic') . "'][contains(@src, 'marked')]";
228 $exception = new ExpectationException('The "' . $sectionnumber . '" section is not highlighted', $this->getSession());
229 $this->find('xpath', $xpath, $exception);
230 }
231
232 /**
b918f9f7 233 * Checks if the specified course section highlighting is turned off. You need to be in the course page on editing mode.
18c84063
DM
234 *
235 * @Then /^section "(?P<section_number>\d+)" should not be highlighted$/
b918f9f7 236 * @throws ExpectationException
18c84063
DM
237 * @param int $sectionnumber The section number
238 */
239 public function section_should_not_be_highlighted($sectionnumber) {
240
241 // We only catch ExpectationException, ElementNotFoundException should be thrown if the specified section does not exist.
242 try {
243 $this->section_should_be_highlighted($sectionnumber);
244 } catch (ExpectationException $e) {
245 // ExpectedException means that it is not highlighted.
246 return;
247 }
248
249 throw new ExpectationException('The "' . $sectionnumber . '" section is highlighted', $this->getSession());
250 }
251
b918f9f7
DM
252 /**
253 * 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.
254 *
255 * @Then /^section "(?P<section_number>\d+)" should be hidden$/
256 * @throws ExpectationException
257 * @throws ElementNotFoundException Thrown by behat_base::find
258 * @param int $sectionnumber
259 */
260 public function section_should_be_hidden($sectionnumber) {
261
262 $sectionxpath = $this->section_exists($sectionnumber);
263
264 // Section should be hidden.
265 $exception = new ExpectationException('The section is not hidden', $this->getSession());
266 $this->find('xpath', $sectionxpath . "[contains(concat(' ', @class, ' '), ' hidden ')]", $exception);
267
268 // The checking are different depending on user permissions.
269 if ($this->is_course_editor()) {
270
271 // The section must be hidden.
272 $this->show_section_icon_exists($sectionnumber);
273
274 // If there are activities they should be hidden and the visibility icon should not be available.
275 if ($activities = $this->get_section_activities($sectionxpath)) {
276
277 $dimmedexception = new ExpectationException('There are activities that are not dimmed', $this->getSession());
278 $visibilityexception = new ExpectationException('There are activities which visibility icons are clickable', $this->getSession());
279 foreach ($activities as $activity) {
280
281 // Dimmed.
00ea74cb
DM
282 $this->find('xpath', "//div[contains(concat(' ', @class, ' '), ' activityinstance ')]" .
283 "/a[contains(concat(' ', @class, ' '), ' dimmed ')]", $dimmedexception, $activity);
b918f9f7
DM
284
285 // To check that the visibility is not clickable we check the funcionality rather than the applied style.
286 $visibilityiconnode = $this->find('css', 'a.editing_show img', false, $activity);
287 $visibilityiconnode->click();
288
289 // We ensure that we still see the show icon.
290 $visibilityiconnode = $this->find('css', 'a.editing_show img', $visibilityexception, $activity);
291 }
292 }
293
294 } else {
295 // There shouldn't be activities.
296 if ($this->get_section_activities($sectionxpath)) {
297 throw new ExpectationException('There are activities in the section and they should be hidden', $this->getSession());
298 }
299 }
300 }
301
302 /**
303 * 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.
304 *
305 * @Then /^section "(?P<section_number>\d+)" should be visible$/
306 * @throws ExpectationException
307 * @param int $sectionnumber
308 */
309 public function section_should_be_visible($sectionnumber) {
310
311 $sectionxpath = $this->section_exists($sectionnumber);
312
313 // Section should not be hidden.
314 if (!$this->getSession()->getPage()->find('xpath', $sectionxpath . "[not(contains(concat(' ', @class, ' '), ' hidden '))]")) {
315 throw new ExpectationException('The section is hidden', $this->getSession());
316 }
317
318 // Hide section button should be visible.
319 if ($this->is_course_editor()) {
320 $this->hide_section_icon_exists($sectionnumber);
321 }
322 }
323
bf648567
DM
324 /**
325 * Checks that the specified activity 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.
326 *
327 * @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be visible$/
328 * @param string $activityname
329 */
330 public function activity_should_be_visible($activityname) {
331
332 // The activity must exists and be visible.
333 $activitynode = $this->get_activity_node($activityname);
334
335 if ($this->is_course_editor()) {
336
337 // The activity should not be dimmed.
338 try {
339 $this->find('css', 'a.dimmed', false, $activitynode);
340 throw new ExpectationException('"' . $activityname . '" is hidden', $this->getSession());
341 } catch (ElementNotFoundException $e) {
342 // All ok.
343 }
344
345 // The 'Hide' button should be available.
346 $nohideexception = new ExpectationException('"' . $activityname . '" don\'t have a "' . get_string('hide') . '" icon', $this->getSession());
347 $this->find('named', array('link', get_string('hide')), $nohideexception, $activitynode);
348 }
349 }
350
351 /**
352 * Checks that the specified activity is hidden. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
353 *
354 * @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be hidden$/
355 * @param string $activityname
356 */
357 public function activity_should_be_hidden($activityname) {
358
359 if ($this->is_course_editor()) {
360
361 // The activity should exists.
362 $activitynode = $this->get_activity_node($activityname);
363
364 // Should be hidden.
365 $exception = new ExpectationException('"' . $activityname . '" is not dimmed', $this->getSession());
366 $this->find('css', 'a.dimmed', $exception, $activitynode);
367
368 // Also 'Show' icon.
369 $noshowexception = new ExpectationException('"' . $activityname . '" don\'t have a "' . get_string('show') . '" icon', $this->getSession());
370 $this->find('named', array('link', get_string('show')), $noshowexception, $activitynode);
371
372 } else {
373
374 // It should not exists at all.
375 try {
376 $this->find_link($activityname);
377 throw new ExpectationException('The "' . $activityname . '" should not appear');
378 } catch (ElementNotFoundException $e) {
379 // This is good, the activity should not be there.
380 }
381 }
382
383 }
384
18c84063
DM
385 /**
386 * Checks if the course section exists.
387 *
388 * @throws ElementNotFoundException Thrown by behat_base::find
389 * @param int $sectionnumber
b918f9f7 390 * @return string The xpath of the section.
18c84063
DM
391 */
392 protected function section_exists($sectionnumber) {
393
394 // Just to give more info in case it does not exist.
395 $xpath = "//li[@id='section-" . $sectionnumber . "']";
396 $exception = new ElementNotFoundException($this->getSession(), "Section $sectionnumber ");
397 $this->find('xpath', $xpath, $exception);
398
399 return $xpath;
400 }
b918f9f7
DM
401
402 /**
403 * Returns the show section icon or throws an exception.
404 *
405 * @throws ElementNotFoundException Thrown by behat_base::find
406 * @param int $sectionnumber
407 * @return NodeElement
408 */
409 protected function show_section_icon_exists($sectionnumber) {
410
411 // Gets the section xpath and ensure it exists.
412 $xpath = $this->section_exists($sectionnumber);
413
414 // We need to know the course format as the text strings depends on them.
415 $courseformat = $this->get_course_format();
416
417 // Checking the show button alt text and show icon.
418 $xpath = $xpath . "/descendant::a/descendant::img[@alt='". get_string('showfromothers', $courseformat) ."'][contains(@src, 'show')]";
419
420 $exception = new ElementNotFoundException($this->getSession(), 'Show section icon ');
421 return $this->find('xpath', $xpath, $exception);
422 }
423
424 /**
425 * Returns the hide section icon link if it exists or throws exception.
426 *
427 * @throws ElementNotFoundException Thrown by behat_base::find
428 * @param int $sectionnumber
429 * @return NodeElement
430 */
431 protected function hide_section_icon_exists($sectionnumber) {
432
433 // Gets the section xpath and ensure it exists.
434 $xpath = $this->section_exists($sectionnumber);
435
436 // We need to know the course format as the text strings depends on them.
437 $courseformat = $this->get_course_format();
438
439 // Checking the hide button alt text and hide icon.
440 $xpath = $xpath . "/descendant::a/descendant::img[@alt='". get_string('hidefromothers', $courseformat) ."'][contains(@src, 'hide')]";
441
442 $exception = new ElementNotFoundException($this->getSession(), 'Hide section icon ');
443 return $this->find('xpath', $xpath, $exception);
444 }
445
446 /**
447 * Gets the current course format.
448 *
449 * @throws ExpectationException If we are not in the course view page.
450 * @return string The course format in a frankenstyled name.
451 */
452 protected function get_course_format() {
453
454 $exception = new ExpectationException('You are not in a course page', $this->getSession());
455
456 // The moodle body's id attribute contains the course format.
457 $node = $this->getSession()->getPage()->find('css', 'body');
458 if (!$node) {
459 throw $exception;
460 }
461
462 if (!$bodyid = $node->getAttribute('id')) {
463 throw $exception;
464 }
465
466 if (strstr($bodyid, 'page-course-view-') === false) {
467 throw $exception;
468 }
469
470 return 'format_' . str_replace('page-course-view-', '', $bodyid);
471 }
472
473 /**
474 * Gets the section's activites DOM nodes.
475 *
476 * @param string $sectionxpath
477 * @return array NodeElement instances
478 */
479 protected function get_section_activities($sectionxpath) {
480
481 $xpath = $sectionxpath . "/descendant::li[contains(concat(' ', @class, ' '), ' activity ')]";
482
483 // We spin here, as activities usually require a lot of time to load.
484 try {
485 $activities = $this->find_all('xpath', $xpath);
486 } catch (ElementNotFoundException $e) {
487 return false;
488 }
489
490 return $activities;
491 }
492
bf648567
DM
493 /**
494 * Returns the DOM node of the activity from <li>.
495 *
496 * @throws ElementNotFoundException Thrown by behat_base::find
497 * @param string $activityname The activity name
498 * @return NodeElement
499 */
500 protected function get_activity_node($activityname) {
501
502 $activityname = str_replace("'", "\'", $activityname);
503 $xpath = "//li[contains(concat(' ', @class, ' '), ' activity ')][contains(., '" .$activityname. "')]";
504
505 return $this->find('xpath', $xpath);
506 }
507
b918f9f7
DM
508 /**
509 * Returns whether the user can edit the course contents or not.
510 *
511 * @return bool
512 */
513 protected function is_course_editor() {
514
515 // We don't need to behat_base::spin() here as all is already loaded.
516 if (!$this->getSession()->getPage()->findButton('Turn editing off') &&
517 !$this->getSession()->getPage()->findButton('Turn editing on')) {
518 return false;
519 }
520
521 return true;
522 }
523
a1990e50 524}