MDL-40975 ActionMenu: Ensure we maintain consistent tab order
[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,
0e575f01 33 Behat\Mink\Exception\DriverException as DriverException,
18c84063 34 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
a1990e50
DM
35
36/**
37 * Course-related steps definitions.
38 *
39 * @package core_course
40 * @category test
41 * @copyright 2012 David MonllaĆ³
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class behat_course extends behat_base {
45
46 /**
47 * Turns editing mode on.
48 * @Given /^I turn editing mode on$/
49 */
50 public function i_turn_editing_mode_on() {
dedb9738 51 return new Given('I press "' . get_string('turneditingon') . '"');
a1990e50
DM
52 }
53
54 /**
55 * Turns editing mode off.
56 * @Given /^I turn editing mode off$/
57 */
58 public function i_turn_editing_mode_off() {
dedb9738 59 return new Given('I press "' . get_string('turneditingoff') . '"');
a1990e50
DM
60 }
61
df1ff55d
DM
62 /**
63 * Creates a new course with the provided table data matching course settings names with the desired values.
64 *
65 * @Given /^I create a course with:$/
66 * @param TableNode $table The course data
5dc361e1 67 * @return Given[]
df1ff55d
DM
68 */
69 public function i_create_a_course_with(TableNode $table) {
70 return array(
71 new Given('I go to the courses management page'),
8aa3aa3d
SH
72 new Given('I should see the "'.get_string('categories').'" management page'),
73 new Given('I click on category "'.get_string('miscellaneous').'" in the management interface'),
74 new Given('I should see the "'.get_string('categoriesandcoures').'" management page'),
38a15200 75 new Given('I click on "'.get_string('createnewcourse').'" "link" in the "#course-listing" "css_element"'),
df1ff55d 76 new Given('I fill the moodle form with:', $table),
dedb9738 77 new Given('I press "' . get_string('savechanges') . '"')
df1ff55d
DM
78 );
79 }
80
81 /**
82 * Goes to the system courses/categories management page.
83 *
84 * @Given /^I go to the courses management page$/
5dc361e1 85 * @return Given[]
df1ff55d
DM
86 */
87 public function i_go_to_the_courses_management_page() {
bafcd3f1
RT
88 if ($this->running_javascript()) {
89 $scenario = array(new Given('I expand "' . get_string('administrationsite') . '" node'));
90 } else {
91 $scenario = array(new Given('I follow "' . get_string('administrationsite') . '"'));
92 }
93 $scenario[] = new Given('I expand "' . get_string('courses', 'admin') . '" node');
94 $scenario[] = new Given('I follow "' . get_string('coursemgmt', 'admin') . '"');
95 return $scenario;
df1ff55d
DM
96 }
97
a1990e50
DM
98 /**
99 * Adds the selected activity/resource filling the form data with the specified field/value pairs.
100 *
44d5af38 101 * @When /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)" and I fill the form with:$/
a1990e50 102 * @param string $activity The activity name
0e575f01 103 * @param int $section The section number
a1990e50 104 * @param TableNode $data The activity field/value data
5dc361e1 105 * @return Given[]
a1990e50
DM
106 */
107 public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) {
108
a1990e50 109 return array(
38976081 110 new Given('I add a "' . $this->escape($activity) . '" to section "' . $this->escape($section) . '"'),
a1990e50 111 new Given('I fill the moodle form with:', $data),
dedb9738 112 new Given('I press "' . get_string('savechangesandreturntocourse') . '"')
a1990e50
DM
113 );
114 }
115
116 /**
117 * Opens the activity chooser and opens the activity/resource form page.
118 *
44d5af38 119 * @Given /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)"$/
1f9ffbdb 120 * @throws ElementNotFoundException Thrown by behat_base::find
a1990e50 121 * @param string $activity
0e575f01 122 * @param int $section
a1990e50
DM
123 */
124 public function i_add_to_section($activity, $section) {
125
38976081
DM
126 $sectionxpath = "//li[@id='section-" . $section . "']";
127
128 $activityliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral(ucfirst($activity));
1c00d6f6
DM
129
130 if ($this->running_javascript()) {
131
132 // Clicks add activity or resource section link.
133 $sectionxpath = $sectionxpath . "/descendant::div[@class='section-modchooser']/span/a";
134 $sectionnode = $this->find('xpath', $sectionxpath);
135 $sectionnode->click();
136
137 // Clicks the selected activity if it exists.
00ea74cb 138 $activityxpath = "//div[@id='chooseform']/descendant::label" .
38976081
DM
139 "/descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' typename ')]" .
140 "[contains(., $activityliteral)]" .
00ea74cb 141 "/parent::label/child::input";
1c00d6f6
DM
142 $activitynode = $this->find('xpath', $activityxpath);
143 $activitynode->doubleClick();
144
145 } else {
146 // Without Javascript.
147
148 // Selecting the option from the select box which contains the option.
38976081
DM
149 $selectxpath = $sectionxpath . "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' section_add_menus ')]" .
150 "/descendant::select[contains(., $activityliteral)]";
1c00d6f6
DM
151 $selectnode = $this->find('xpath', $selectxpath);
152 $selectnode->selectOption($activity);
153
154 // Go button.
155 $gobuttonxpath = $selectxpath . "/ancestor::form/descendant::input[@type='submit']";
156 $gobutton = $this->find('xpath', $gobuttonxpath);
157 $gobutton->click();
158 }
159
a1990e50
DM
160 }
161
18c84063
DM
162 /**
163 * Turns course section highlighting on.
164 *
165 * @Given /^I turn section "(?P<section_number>\d+)" highlighting on$/
166 * @param int $sectionnumber The section number
5dc361e1 167 * @return Given[]
18c84063
DM
168 */
169 public function i_turn_section_highlighting_on($sectionnumber) {
170
171 // Ensures the section exists.
172 $xpath = $this->section_exists($sectionnumber);
173
174 return array(
38976081 175 new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"'),
18c84063
DM
176 new Given('I wait "2" seconds')
177 );
178 }
179
180 /**
181 * Turns course section highlighting off.
182 *
183 * @Given /^I turn section "(?P<section_number>\d+)" highlighting off$/
184 * @param int $sectionnumber The section number
5dc361e1 185 * @return Given[]
18c84063
DM
186 */
187 public function i_turn_section_highlighting_off($sectionnumber) {
188
189 // Ensures the section exists.
190 $xpath = $this->section_exists($sectionnumber);
191
192 return array(
38976081 193 new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"'),
18c84063
DM
194 new Given('I wait "2" seconds')
195 );
196 }
197
198 /**
b918f9f7
DM
199 * Shows the specified hidden section. You need to be in the course page and on editing mode.
200 *
201 * @Given /^I show section "(?P<section_number>\d+)"$/
202 * @param int $sectionnumber
203 */
204 public function i_show_section($sectionnumber) {
0e575f01
DM
205 $showlink = $this->show_section_icon_exists($sectionnumber);
206 $showlink->click();
b918f9f7
DM
207
208 // It requires time.
0e575f01
DM
209 if ($this->running_javascript()) {
210 $this->getSession()->wait(5000, false);
211 }
b918f9f7
DM
212 }
213
214 /**
215 * Hides the specified visible section. You need to be in the course page and on editing mode.
216 *
217 * @Given /^I hide section "(?P<section_number>\d+)"$/
218 * @param int $sectionnumber
219 */
220 public function i_hide_section($sectionnumber) {
0e575f01
DM
221 $hidelink = $this->hide_section_icon_exists($sectionnumber);
222 $hidelink->click();
b918f9f7
DM
223
224 // It requires time.
0e575f01
DM
225 if ($this->running_javascript()) {
226 $this->getSession()->wait(5000, false);
227 }
b918f9f7
DM
228 }
229
3f038d6d
RT
230 /**
231 * Go to editing section page for specified section number. You need to be in the course page and on editing mode.
232 *
233 * @Given /^I edit the section "(?P<section_number>\d+)"$/
234 * @param int $sectionnumber
235 */
236 public function i_edit_the_section($sectionnumber) {
237 return new Given('I click on "' . get_string('editsummary') . '" "link" in the "#section-' . $sectionnumber . '" "css_element"');
238 }
239
240 /**
241 * Edit specified section and fill the form data with the specified field/value pairs.
242 *
243 * @When /^I edit the section "(?P<section_number>\d+)" and I fill the form with:$/
244 * @param int $sectionnumber The section number
245 * @param TableNode $data The activity field/value data
246 * @return Given[]
247 */
248 public function i_edit_the_section_and_i_fill_the_form_with($sectionnumber, TableNode $data) {
249
250 return array(
251 new Given('I edit the section "' . $sectionnumber . '"'),
252 new Given('I fill the moodle form with:', $data),
253 new Given('I press "' . get_string('savechanges') . '"')
254 );
255 }
256
b918f9f7
DM
257 /**
258 * Checks if the specified course section hightlighting is turned on. You need to be in the course page on editing mode.
18c84063 259 *
18c84063 260 * @Then /^section "(?P<section_number>\d+)" should be highlighted$/
b918f9f7 261 * @throws ExpectationException
18c84063
DM
262 * @param int $sectionnumber The section number
263 */
264 public function section_should_be_highlighted($sectionnumber) {
265
266 // Ensures the section exists.
267 $xpath = $this->section_exists($sectionnumber);
268
269 // The important checking, we can not check the img.
270 $xpath = $xpath . "/descendant::img[@alt='" . get_string('markedthistopic') . "'][contains(@src, 'marked')]";
271 $exception = new ExpectationException('The "' . $sectionnumber . '" section is not highlighted', $this->getSession());
272 $this->find('xpath', $xpath, $exception);
273 }
274
275 /**
b918f9f7 276 * Checks if the specified course section highlighting is turned off. You need to be in the course page on editing mode.
18c84063
DM
277 *
278 * @Then /^section "(?P<section_number>\d+)" should not be highlighted$/
b918f9f7 279 * @throws ExpectationException
18c84063
DM
280 * @param int $sectionnumber The section number
281 */
282 public function section_should_not_be_highlighted($sectionnumber) {
283
284 // We only catch ExpectationException, ElementNotFoundException should be thrown if the specified section does not exist.
285 try {
286 $this->section_should_be_highlighted($sectionnumber);
287 } catch (ExpectationException $e) {
288 // ExpectedException means that it is not highlighted.
289 return;
290 }
291
292 throw new ExpectationException('The "' . $sectionnumber . '" section is highlighted', $this->getSession());
293 }
294
b918f9f7
DM
295 /**
296 * 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.
297 *
298 * @Then /^section "(?P<section_number>\d+)" should be hidden$/
299 * @throws ExpectationException
300 * @throws ElementNotFoundException Thrown by behat_base::find
301 * @param int $sectionnumber
302 */
303 public function section_should_be_hidden($sectionnumber) {
304
305 $sectionxpath = $this->section_exists($sectionnumber);
306
307 // Section should be hidden.
308 $exception = new ExpectationException('The section is not hidden', $this->getSession());
38976081 309 $this->find('xpath', $sectionxpath . "[contains(concat(' ', normalize-space(@class), ' '), ' hidden ')]", $exception);
b918f9f7
DM
310
311 // The checking are different depending on user permissions.
312 if ($this->is_course_editor()) {
313
314 // The section must be hidden.
315 $this->show_section_icon_exists($sectionnumber);
316
317 // If there are activities they should be hidden and the visibility icon should not be available.
318 if ($activities = $this->get_section_activities($sectionxpath)) {
319
320 $dimmedexception = new ExpectationException('There are activities that are not dimmed', $this->getSession());
321 $visibilityexception = new ExpectationException('There are activities which visibility icons are clickable', $this->getSession());
322 foreach ($activities as $activity) {
323
324 // Dimmed.
38976081
DM
325 $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' activityinstance ')]" .
326 "/a[contains(concat(' ', normalize-space(@class), ' '), ' dimmed ')]", $dimmedexception, $activity);
b918f9f7 327
0e575f01
DM
328 // Non-JS browsers can not click on img elements.
329 if ($this->running_javascript()) {
330 // To check that the visibility is not clickable we check the funcionality rather than the applied style.
331 $visibilityiconnode = $this->find('css', 'a.editing_show img', false, $activity);
332 $visibilityiconnode->click();
333 }
b918f9f7
DM
334
335 // We ensure that we still see the show icon.
336 $visibilityiconnode = $this->find('css', 'a.editing_show img', $visibilityexception, $activity);
337 }
338 }
339
340 } else {
341 // There shouldn't be activities.
342 if ($this->get_section_activities($sectionxpath)) {
343 throw new ExpectationException('There are activities in the section and they should be hidden', $this->getSession());
344 }
345 }
346 }
347
348 /**
349 * 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.
350 *
351 * @Then /^section "(?P<section_number>\d+)" should be visible$/
352 * @throws ExpectationException
353 * @param int $sectionnumber
354 */
355 public function section_should_be_visible($sectionnumber) {
356
357 $sectionxpath = $this->section_exists($sectionnumber);
358
359 // Section should not be hidden.
38976081
DM
360 $xpath = $sectionxpath . "[not(contains(concat(' ', normalize-space(@class), ' '), ' hidden '))]";
361 if (!$this->getSession()->getPage()->find('xpath', $xpath)) {
b918f9f7
DM
362 throw new ExpectationException('The section is hidden', $this->getSession());
363 }
364
365 // Hide section button should be visible.
366 if ($this->is_course_editor()) {
367 $this->hide_section_icon_exists($sectionnumber);
368 }
369 }
370
0e575f01
DM
371 /**
372 * Moves up the specified section, this step only works with Javascript disabled. Editing mode should be on.
373 *
374 * @Given /^I move up section "(?P<section_number>\d+)"$/
375 * @throws DriverException Step not available when Javascript is enabled
376 * @param int $sectionnumber
377 */
378 public function i_move_up_section($sectionnumber) {
379
380 if ($this->running_javascript()) {
381 throw new DriverException('Move a section up step is not available with Javascript enabled');
382 }
383
384 // Ensures the section exists.
385 $sectionxpath = $this->section_exists($sectionnumber);
386
387 // Follows the link
388 $moveuplink = $this->get_node_in_container('link', get_string('moveup'), 'xpath_element', $sectionxpath);
389 $moveuplink->click();
390 }
391
392 /**
393 * Moves down the specified section, this step only works with Javascript disabled. Editing mode should be on.
394 *
395 * @Given /^I move down section "(?P<section_number>\d+)"$/
396 * @throws DriverException Step not available when Javascript is enabled
397 * @param int $sectionnumber
398 */
399 public function i_move_down_section($sectionnumber) {
400
401 if ($this->running_javascript()) {
402 throw new DriverException('Move a section down step is not available with Javascript enabled');
403 }
404
405 // Ensures the section exists.
406 $sectionxpath = $this->section_exists($sectionnumber);
407
408 // Follows the link
409 $movedownlink = $this->get_node_in_container('link', get_string('movedown'), 'xpath_element', $sectionxpath);
410 $movedownlink->click();
411 }
412
bf648567
DM
413 /**
414 * 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.
415 *
416 * @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be visible$/
417 * @param string $activityname
5dc361e1 418 * @throws ExpectationException
bf648567
DM
419 */
420 public function activity_should_be_visible($activityname) {
421
422 // The activity must exists and be visible.
423 $activitynode = $this->get_activity_node($activityname);
424
425 if ($this->is_course_editor()) {
426
427 // The activity should not be dimmed.
428 try {
429 $this->find('css', 'a.dimmed', false, $activitynode);
430 throw new ExpectationException('"' . $activityname . '" is hidden', $this->getSession());
431 } catch (ElementNotFoundException $e) {
432 // All ok.
433 }
434
435 // The 'Hide' button should be available.
436 $nohideexception = new ExpectationException('"' . $activityname . '" don\'t have a "' . get_string('hide') . '" icon', $this->getSession());
437 $this->find('named', array('link', get_string('hide')), $nohideexception, $activitynode);
438 }
439 }
440
441 /**
442 * 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.
443 *
444 * @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be hidden$/
445 * @param string $activityname
5dc361e1 446 * @throws ExpectationException
bf648567
DM
447 */
448 public function activity_should_be_hidden($activityname) {
449
450 if ($this->is_course_editor()) {
451
452 // The activity should exists.
453 $activitynode = $this->get_activity_node($activityname);
454
455 // Should be hidden.
456 $exception = new ExpectationException('"' . $activityname . '" is not dimmed', $this->getSession());
457 $this->find('css', 'a.dimmed', $exception, $activitynode);
458
459 // Also 'Show' icon.
460 $noshowexception = new ExpectationException('"' . $activityname . '" don\'t have a "' . get_string('show') . '" icon', $this->getSession());
461 $this->find('named', array('link', get_string('show')), $noshowexception, $activitynode);
462
463 } else {
464
465 // It should not exists at all.
466 try {
467 $this->find_link($activityname);
468 throw new ExpectationException('The "' . $activityname . '" should not appear');
469 } catch (ElementNotFoundException $e) {
470 // This is good, the activity should not be there.
471 }
472 }
473
474 }
475
0e575f01 476 /**
7daab401 477 * Moves the specified activity to the first slot of a section. This step is experimental when using it in Javascript tests. Editing mode should be on.
0e575f01
DM
478 *
479 * @Given /^I move "(?P<activity_name_string>(?:[^"]|\\")*)" activity to section "(?P<section_number>\d+)"$/
480 * @param string $activityname The activity name
481 * @param int $sectionnumber The number of section
5dc361e1 482 * @return Given[]
0e575f01
DM
483 */
484 public function i_move_activity_to_section($activityname, $sectionnumber) {
485
486 // Ensure the destination is valid.
487 $sectionxpath = $this->section_exists($sectionnumber);
488
489 $activitynode = $this->get_activity_element('.editing_move img', 'css_element', $activityname);
490
491 // JS enabled.
492 if ($this->running_javascript()) {
493
38976081 494 $destinationxpath = $sectionxpath . "/descendant::ul[contains(concat(' ', normalize-space(@class), ' '), ' yui3-dd-drop ')]";
0e575f01
DM
495
496 return array(
38976081
DM
497 new Given('I drag "' . $this->escape($activitynode->getXpath()) . '" "xpath_element" ' .
498 'and I drop it in "' . $this->escape($destinationxpath) . '" "xpath_element"'),
0e575f01
DM
499 );
500
501 } else {
502 // Following links with no-JS.
503
504 // Moving to the fist spot of the section (before all other section's activities).
505 return array(
38976081
DM
506 new Given('I click on "a.editing_move" "css_element" in the "' . $this->escape($activityname) . '" activity'),
507 new Given('I click on "li.movehere a" "css_element" in the "' . $this->escape($sectionxpath) . '" "xpath_element"'),
0e575f01
DM
508 );
509 }
510 }
511
512 /**
513 * Edits the activity name through the edit activity; this step only works with Javascript enabled. Editing mode should be on.
514 *
515 * @Given /^I change "(?P<activity_name_string>(?:[^"]|\\")*)" activity name to "(?P<new_name_string>(?:[^"]|\\")*)"$/
516 * @throws DriverException Step not available when Javascript is disabled
517 * @param string $activityname
518 * @param string $newactivityname
5dc361e1 519 * @return Given[]
0e575f01
DM
520 */
521 public function i_change_activity_name_to($activityname, $newactivityname) {
522
523 if (!$this->running_javascript()) {
524 throw new DriverException('Change activity name step is not available with Javascript disabled');
525 }
526
527 // Adding chr(10) to save changes.
e5de4933 528 $activity = $this->escape($activityname);
0e575f01 529 return array(
2a9275c4 530 new Given('I open "' . $activity . '" actions menu'),
e5de4933 531 new Given('I click on "' . get_string('edittitle') . '" "link" in the "' . $activity .'" activity'),
38976081 532 new Given('I fill in "title" with "' . $this->escape($newactivityname) . chr(10) . '"'),
0e575f01
DM
533 new Given('I wait "2" seconds')
534 );
535 }
536
2a9275c4
DM
537 /**
538 * Opens an activity actions menu if it is not already opened.
539 *
540 * @Given /^I open "(?P<activity_name_string>(?:[^"]|\\")*)" actions menu$/
541 * @throws DriverException The step is not available when Javascript is disabled
542 * @param string $activityname
543 * @return Given
544 */
545 public function i_open_actions_menu($activityname) {
546
547 if (!$this->running_javascript()) {
548 throw new DriverException('Activities actions menu not available when Javascript is disabled');
549 }
550
551 // If it is already opened we do nothing.
552 $activitynode = $this->get_activity_node($activityname);
553 $classes = array_flip(explode(' ', $activitynode->getAttribute('class')));
554 if (!empty($classes['action-menu-shown'])) {
555 return;
556 }
557
558 return new Given('I click on "' . get_string('actions', 'moodle') . '" "link" in the "' . $this->escape($activityname) . '" activity');
559 }
560
0e575f01
DM
561 /**
562 * Indents to the right the activity or resource specified by it's name. Editing mode should be on.
563 *
564 * @Given /^I indent right "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
565 * @param string $activityname
5dc361e1 566 * @return Given[]
0e575f01
DM
567 */
568 public function i_indent_right_activity($activityname) {
569
e5de4933
SH
570 $steps = array();
571 $activity = $this->escape($activityname);
572 if ($this->running_javascript()) {
2a9275c4 573 $steps[] = new Given('I open "' . $activity . '" actions menu');
e5de4933
SH
574 }
575 $steps[] = new Given('I click on "' . get_string('moveright') . '" "link" in the "' . $activity . '" activity');
0e575f01
DM
576
577 if ($this->running_javascript()) {
578 $steps[] = new Given('I wait "2" seconds');
579 }
580
581 return $steps;
582 }
583
584 /**
585 * Indents to the left the activity or resource specified by it's name. Editing mode should be on.
586 *
587 * @Given /^I indent left "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
588 * @param string $activityname
5dc361e1 589 * @return Given[]
0e575f01
DM
590 */
591 public function i_indent_left_activity($activityname) {
592
e5de4933
SH
593 $steps = array();
594 $activity = $this->escape($activityname);
595 if ($this->running_javascript()) {
2a9275c4 596 $steps[] = new Given('I open "' . $activity . '" actions menu');
e5de4933
SH
597 }
598 $steps[] = new Given('I click on "' . get_string('moveleft') . '" "link" in the "' . $activity . '" activity');
0e575f01
DM
599
600 if ($this->running_javascript()) {
601 $steps[] = new Given('I wait "2" seconds');
602 }
603
604 return $steps;
605
606 }
607
608 /**
7daab401 609 * Deletes the activity or resource specified by it's name. This step is experimental when using it in Javascript tests. You should be in the course page with editing mode on.
0e575f01
DM
610 *
611 * @Given /^I delete "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
612 * @param string $activityname
5dc361e1 613 * @return Given[]
0e575f01
DM
614 */
615 public function i_delete_activity($activityname) {
616
617 $deletestring = get_string('delete');
618
619 // JS enabled.
620 // Not using chain steps here because the exceptions catcher have problems detecting
621 // JS modal windows and avoiding interacting them at the same time.
622 if ($this->running_javascript()) {
623
624 $element = $this->get_activity_element($deletestring, 'link', $activityname);
625 $element->click();
626
627 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
628
629 $this->getSession()->wait(2 * 1000, false);
630
631 } else {
632
633 // With JS disabled.
634 $steps = array(
38976081 635 new Given('I click on "' . $this->escape($deletestring) . '" "link" in the "' . $this->escape($activityname) . '" activity'),
0e575f01
DM
636 new Given('I press "' . get_string('yes') . '"')
637 );
638
639 return $steps;
640 }
641 }
642
643 /**
644 * Duplicates the activity or resource specified by it's name. You should be in the course page with editing mode on.
645 *
646 * @Given /^I duplicate "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
647 * @param string $activityname
5dc361e1 648 * @return Given[]
0e575f01
DM
649 */
650 public function i_duplicate_activity($activityname) {
e5de4933
SH
651 $steps = array();
652 $activity = $this->escape($activityname);
653 if ($this->running_javascript()) {
2a9275c4 654 $steps[] = new Given('I open "' . $activity . '" actions menu');
e5de4933
SH
655 }
656 $steps[] = new Given('I click on "' . get_string('duplicate') . '" "link" in the "' . $activity . '" activity');
657 $steps[] = new Given('I press "' . get_string('continue') .'"');
658 $steps[] = new Given('I press "' . get_string('duplicatecontcourse') .'"');
659 return $steps;
0e575f01
DM
660 }
661
662 /**
663 * Duplicates the activity or resource and modifies the new activity with the provided data. You should be in the course page with editing mode on.
664 *
665 * @Given /^I duplicate "(?P<activity_name_string>(?:[^"]|\\")*)" activity editing the new copy with:$/
666 * @param string $activityname
667 * @param TableNode $data
5dc361e1 668 * @return Given[]
0e575f01
DM
669 */
670 public function i_duplicate_activity_editing_the_new_copy_with($activityname, TableNode $data) {
e5de4933
SH
671 $steps = array();
672 $activity = $this->escape($activityname);
673 if ($this->running_javascript()) {
2a9275c4 674 $steps[] = new Given('I open "' . $activity . '" actions menu');
e5de4933
SH
675 }
676 $steps[] = new Given('I click on "' . get_string('duplicate') . '" "link" in the "' . $activity . '" activity');
677 $steps[] = new Given('I press "' . get_string('continue') .'"');
678 $steps[] = new Given('I press "' . get_string('duplicatecontedit') . '"');
679 $steps[] = new Given('I fill the moodle form with:', $data);
680 $steps[] = new Given('I press "' . get_string('savechangesandreturntocourse') . '"');
681 return $steps;
0e575f01
DM
682 }
683
684 /**
685 * Clicks on the specified element of the activity. You should be in the course page with editing mode turned on.
686 *
687 * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<activity_name_string>[^"]*)" activity$/
688 * @param string $element
689 * @param string $selectortype
690 * @param string $activityname
691 */
692 public function i_click_on_in_the_activity($element, $selectortype, $activityname) {
693 $element = $this->get_activity_element($element, $selectortype, $activityname);
694 $element->click();
695 }
696
697 /**
698 * Clicks on the specified element inside the activity container.
699 *
700 * @throws ElementNotFoundException
701 * @param string $element
702 * @param string $selectortype
703 * @param string $activityname
704 * @return NodeElement
705 */
706 protected function get_activity_element($element, $selectortype, $activityname) {
707 $activitynode = $this->get_activity_node($activityname);
708
709 // Transforming to Behat selector/locator.
710 list($selector, $locator) = $this->transform_selector($selectortype, $element);
711 $exception = new ElementNotFoundException($this->getSession(), '"' . $element . '" "' . $selectortype . '" in "' . $activityname . '" ');
712
713 return $this->find($selector, $locator, $exception, $activitynode);
714 }
715
18c84063
DM
716 /**
717 * Checks if the course section exists.
718 *
719 * @throws ElementNotFoundException Thrown by behat_base::find
720 * @param int $sectionnumber
b918f9f7 721 * @return string The xpath of the section.
18c84063
DM
722 */
723 protected function section_exists($sectionnumber) {
724
725 // Just to give more info in case it does not exist.
726 $xpath = "//li[@id='section-" . $sectionnumber . "']";
727 $exception = new ElementNotFoundException($this->getSession(), "Section $sectionnumber ");
728 $this->find('xpath', $xpath, $exception);
729
730 return $xpath;
731 }
b918f9f7
DM
732
733 /**
734 * Returns the show section icon or throws an exception.
735 *
736 * @throws ElementNotFoundException Thrown by behat_base::find
737 * @param int $sectionnumber
738 * @return NodeElement
739 */
740 protected function show_section_icon_exists($sectionnumber) {
741
742 // Gets the section xpath and ensure it exists.
743 $xpath = $this->section_exists($sectionnumber);
744
745 // We need to know the course format as the text strings depends on them.
746 $courseformat = $this->get_course_format();
747
748 // Checking the show button alt text and show icon.
38976081
DM
749 $showtext = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('showfromothers', $courseformat));
750 $linkxpath = $xpath . "/descendant::a[@title=$showtext]";
751 $imgxpath = $linkxpath . "/descendant::img[@alt=$showtext][contains(@src, 'show')]";
b918f9f7
DM
752
753 $exception = new ElementNotFoundException($this->getSession(), 'Show section icon ');
0e575f01
DM
754 $this->find('xpath', $imgxpath, $exception);
755
756 // Returing the link so both Non-JS and JS browsers can interact with it.
757 return $this->find('xpath', $linkxpath, $exception);
b918f9f7
DM
758 }
759
760 /**
761 * Returns the hide section icon link if it exists or throws exception.
762 *
763 * @throws ElementNotFoundException Thrown by behat_base::find
764 * @param int $sectionnumber
765 * @return NodeElement
766 */
767 protected function hide_section_icon_exists($sectionnumber) {
768
769 // Gets the section xpath and ensure it exists.
770 $xpath = $this->section_exists($sectionnumber);
771
772 // We need to know the course format as the text strings depends on them.
773 $courseformat = $this->get_course_format();
774
775 // Checking the hide button alt text and hide icon.
38976081
DM
776 $hidetext = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('hidefromothers', $courseformat));
777 $linkxpath = $xpath . "/descendant::a[@title=$hidetext]";
778 $imgxpath = $linkxpath . "/descendant::img[@alt=$hidetext][contains(@src, 'hide')]";
b918f9f7
DM
779
780 $exception = new ElementNotFoundException($this->getSession(), 'Hide section icon ');
0e575f01
DM
781 $this->find('xpath', $imgxpath, $exception);
782
783 // Returing the link so both Non-JS and JS browsers can interact with it.
784 return $this->find('xpath', $linkxpath, $exception);
b918f9f7
DM
785 }
786
787 /**
788 * Gets the current course format.
789 *
790 * @throws ExpectationException If we are not in the course view page.
791 * @return string The course format in a frankenstyled name.
792 */
793 protected function get_course_format() {
794
795 $exception = new ExpectationException('You are not in a course page', $this->getSession());
796
797 // The moodle body's id attribute contains the course format.
798 $node = $this->getSession()->getPage()->find('css', 'body');
799 if (!$node) {
800 throw $exception;
801 }
802
803 if (!$bodyid = $node->getAttribute('id')) {
804 throw $exception;
805 }
806
807 if (strstr($bodyid, 'page-course-view-') === false) {
808 throw $exception;
809 }
810
811 return 'format_' . str_replace('page-course-view-', '', $bodyid);
812 }
813
814 /**
815 * Gets the section's activites DOM nodes.
816 *
817 * @param string $sectionxpath
818 * @return array NodeElement instances
819 */
820 protected function get_section_activities($sectionxpath) {
821
38976081 822 $xpath = $sectionxpath . "/descendant::li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')]";
b918f9f7
DM
823
824 // We spin here, as activities usually require a lot of time to load.
825 try {
826 $activities = $this->find_all('xpath', $xpath);
827 } catch (ElementNotFoundException $e) {
828 return false;
829 }
830
831 return $activities;
832 }
833
bf648567
DM
834 /**
835 * Returns the DOM node of the activity from <li>.
836 *
837 * @throws ElementNotFoundException Thrown by behat_base::find
838 * @param string $activityname The activity name
839 * @return NodeElement
840 */
841 protected function get_activity_node($activityname) {
842
38976081
DM
843 $activityname = $this->getSession()->getSelectorsHandler()->xpathLiteral($activityname);
844 $xpath = "//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][contains(., $activityname)]";
bf648567
DM
845
846 return $this->find('xpath', $xpath);
847 }
848
b918f9f7
DM
849 /**
850 * Returns whether the user can edit the course contents or not.
851 *
852 * @return bool
853 */
854 protected function is_course_editor() {
855
856 // We don't need to behat_base::spin() here as all is already loaded.
dedb9738
DM
857 if (!$this->getSession()->getPage()->findButton(get_string('turneditingoff')) &&
858 !$this->getSession()->getPage()->findButton(get_string('turneditingon'))) {
b918f9f7
DM
859 return false;
860 }
861
862 return true;
863 }
864
5dc361e1
SH
865 /**
866 * Returns the id of the category with the given idnumber.
8aa3aa3d
SH
867 *
868 * Please note that this function requires the category to exist. If it does not exist an ExpectationException is thrown.
869 *
5dc361e1
SH
870 * @param string $idnumber
871 * @return string
8aa3aa3d 872 * @throws ExpectationException
5dc361e1
SH
873 */
874 protected function get_category_id($idnumber) {
875 global $DB;
8aa3aa3d
SH
876 try {
877 return $DB->get_field('course_categories', 'id', array('idnumber' => $idnumber), MUST_EXIST);
878 } catch (dml_missing_record_exception $ex) {
879 throw new ExpectationException(sprintf("There is no category in the database with the idnumber '%s'", $idnumber));
880 }
5dc361e1
SH
881 }
882
883 /**
884 * Returns the id of the course with the given idnumber.
8aa3aa3d
SH
885 *
886 * Please note that this function requires the category to exist. If it does not exist an ExpectationException is thrown.
887 *
5dc361e1
SH
888 * @param string $idnumber
889 * @return string
8aa3aa3d 890 * @throws ExpectationException
5dc361e1
SH
891 */
892 protected function get_course_id($idnumber) {
893 global $DB;
8aa3aa3d
SH
894 try {
895 return $DB->get_field('course', 'id', array('idnumber' => $idnumber), MUST_EXIST);
896 } catch (dml_missing_record_exception $ex) {
897 throw new ExpectationException(sprintf("There is no course in the database with the idnumber '%s'", $idnumber));
898 }
5dc361e1
SH
899 }
900
901 /**
902 * Returns the category node from within the listing on the management page.
903 *
904 * @param string $idnumber
905 * @return \Behat\Mink\Element\NodeElement
906 */
907 protected function get_management_category_listing_node_by_idnumber($idnumber) {
908 $id = $this->get_category_id($idnumber);
909 $selector = sprintf('#category-listing .listitem-category[data-id="%d"] > div', $id);
910 return $this->find('css', $selector);
911 }
912
913 /**
8aa3aa3d
SH
914 * Returns a category node from within the management interface.
915 *
916 * @param string $name The name of the category.
b155a170 917 * @param bool $link If set to true we'll resolve to the link rather than just the node.
5dc361e1
SH
918 * @return \Behat\Mink\Element\NodeElement
919 */
b155a170
SH
920 protected function get_management_category_listing_node_by_name($name, $link = false) {
921 $selector = "//div[@id='category-listing']//li[contains(concat(' ', normalize-space(@class), ' '), ' listitem-category ')]//a[text()='{$name}']";
922 if ($link === false) {
923 $selector .= "/ancestor::li[@data-id]";
924 }
5dc361e1
SH
925 return $this->find('xpath', $selector);
926 }
927
928 /**
8aa3aa3d
SH
929 * Returns a course node from within the management interface.
930 *
931 * @param string $name The name of the course.
b155a170 932 * @param bool $link If set to true we'll resolve to the link rather than just the node.
5dc361e1
SH
933 * @return \Behat\Mink\Element\NodeElement
934 */
b155a170
SH
935 protected function get_management_course_listing_node_by_name($name, $link = false) {
936 $selector = "//div[@id='course-listing']//li[contains(concat(' ', @class, ' '), ' listitem-course ')]//a[text()='{$name}']";
937 if ($link === false) {
938 $selector .= "/ancestor::li[@data-id]";
939 }
5dc361e1
SH
940 return $this->find('xpath', $selector);
941 }
942
943 /**
944 * Returns the course node from within the listing on the management page.
945 *
946 * @param string $idnumber
947 * @return \Behat\Mink\Element\NodeElement
948 */
949 protected function get_management_course_listing_node_by_idnumber($idnumber) {
950 $id = $this->get_course_id($idnumber);
951 $selector = sprintf('#course-listing .listitem-course[data-id="%d"] > div', $id);
952 return $this->find('css', $selector);
953 }
954
955 /**
8aa3aa3d 956 * Clicks on a category in the management interface.
5dc361e1 957 *
8aa3aa3d 958 * @Given /^I click on category "(?P<name>[^"]*)" in the management interface$/
5dc361e1 959 * @param string $name
5dc361e1 960 */
8aa3aa3d 961 public function i_click_on_category_in_the_management_interface($name) {
b155a170
SH
962 $node = $this->get_management_category_listing_node_by_name($name, true);
963 $node->click();
5dc361e1
SH
964 }
965
966 /**
8aa3aa3d 967 * Clicks on a course in the management interface.
5dc361e1 968 *
8aa3aa3d
SH
969 * @Given /^I click on course "(?P<name>[^"]*)" in the management interface$/
970 * @param string $name
971 */
972 public function i_click_on_course_in_the_management_interface($name) {
b155a170
SH
973 $node = $this->get_management_course_listing_node_by_name($name, true);
974 $node->click();
8aa3aa3d
SH
975 }
976
3b732cd6
RT
977 /**
978 * Clicks on a category checkbox in the management interface.
979 *
980 * @Given /^I select category "(?P<name>[^"]*)" in the management interface$/
981 * @param string $name
982 */
983 public function i_select_category_in_the_management_interface($name) {
984 $node = $this->get_management_category_listing_node_by_name($name);
985 $node->checkField('bcat[]');
986 }
987
988 /**
989 * Clicks course checkbox in the management interface.
990 *
991 * @Given /^I select course "(?P<name>[^"]*)" in the management interface$/
992 * @param string $name
993 */
994 public function i_select_course_in_the_management_interface($name) {
995 $node = $this->get_management_course_listing_node_by_name($name);
996 $node->checkField('bc[]');
997 }
998
999 /**
1000 * Move selected categories to top level in the management interface.
1001 *
1002 * @Given /^I move category "(?P<idnumber>[^"]*)" to top level in the management interface$/
1003 * @param string $idnumber
1004 * @return Given[]
1005 */
1006 public function i_move_category_to_top_level_in_the_management_interface($idnumber) {
1007 $id = $this->get_category_id($idnumber);
1008 $selector = sprintf('.listitem-category[data-id="%d"] > div', $id);
1009 $node = $this->find('css', $selector);
1010 $node->checkField('bcat[]');
1011 return array(
1012 new Given('I select "' . coursecat::get(0)->get_formatted_name() . '" from "menumovecategoriesto"'),
1013 new Given('I press "bulkmovecategories"'),
1014 );
1015 }
1016
1017 /**
1018 * Checks that a category is a subcategory of specific category.
1019 *
1020 * @Given /^I should see category "(?P<subcatidnumber>[^"]*)" as subcategory of "(?P<catidnumber>[^"]*)" in the management interface$/
1021 * @throws ExpectationException
1022 * @param string $subcatidnumber
1023 * @param string $catidnumber
1024 */
1025 public function i_should_see_category_as_subcategory_of_in_the_management_interface($subcatidnumber, $catidnumber) {
1026 $categorynodeid = $this->get_category_id($catidnumber);
1027 $subcategoryid = $this->get_category_id($subcatidnumber);
1028 $exception = new ExpectationException('The category '.$subcatidnumber.' is not a subcategory of '.$catidnumber, $this->getSession());
1029 $selector = sprintf('#category-listing .listitem-category[data-id="%d"] .listitem-category[data-id="%d"]', $categorynodeid, $subcategoryid);
1030 $this->find('css', $selector, $exception);
1031 }
1032
1033 /**
1034 * Checks that a category is not a subcategory of specific category.
1035 *
1036 * @Given /^I should not see category "(?P<subcatidnumber>[^"]*)" as subcategory of "(?P<catidnumber>[^"]*)" in the management interface$/
1037 * @throws ExpectationException
1038 * @param string $subcatidnumber
1039 * @param string $catidnumber
1040 */
1041 public function i_should_not_see_category_as_subcategory_of_in_the_management_interface($subcatidnumber, $catidnumber) {
1042 try {
1043 $this->i_should_see_category_as_subcategory_of_in_the_management_interface($subcatidnumber, $catidnumber);
1044 } catch (ExpectationException $e) {
1045 // ExpectedException means that it is not highlighted.
1046 return;
1047 }
1048 throw new ExpectationException('The category '.$subcatidnumber.' is a subcategory of '.$catidnumber, $this->getSession());
1049 }
1050
8aa3aa3d
SH
1051 /**
1052 * Click to expand a category revealing its sub categories within the management UI.
1053 *
1054 * @Given /^I click to expand category "(?P<idnumber>[^"]*)" in the management interface$/
5dc361e1
SH
1055 * @param string $idnumber
1056 */
8aa3aa3d 1057 public function i_click_to_expand_category_in_the_management_interface($idnumber) {
5dc361e1
SH
1058 $categorynode = $this->get_management_category_listing_node_by_idnumber($idnumber);
1059 $exception = new ExpectationException('Category "' . $idnumber . '" does not contain an expand or collapse toggle.', $this->getSession());
1060 $togglenode = $this->find('css', 'a[data-action=collapse],a[data-action=expand]', $exception, $categorynode);
1061 $togglenode->click();
1062 }
1063
1064 /**
8aa3aa3d 1065 * Checks that a category within the management interface is visible.
5dc361e1
SH
1066 *
1067 * @Given /^category in management listing should be visible "(?P<idnumber>[^"]*)"$/
1068 * @param string $idnumber
1069 */
1070 public function category_in_management_listing_should_be_visible($idnumber) {
1071 $id = $this->get_category_id($idnumber);
1072 $exception = new ExpectationException('The category '.$idnumber.' is not visible.', $this->getSession());
1073 $selector = sprintf('#category-listing .listitem-category[data-id="%d"][data-visible="1"]', $id);
1074 $this->find('css', $selector, $exception);
1075 }
1076
1077 /**
8aa3aa3d 1078 * Checks that a category within the management interface is dimmed.
5dc361e1
SH
1079 *
1080 * @Given /^category in management listing should be dimmed "(?P<idnumber>[^"]*)"$/
1081 * @param string $idnumber
1082 */
1083 public function category_in_management_listing_should_be_dimmed($idnumber) {
1084 $id = $this->get_category_id($idnumber);
1085 $selector = sprintf('#category-listing .listitem-category[data-id="%d"][data-visible="0"]', $id);
1086 $exception = new ExpectationException('The category '.$idnumber.' is visible.', $this->getSession());
1087 $this->find('css', $selector, $exception);
1088 }
1089
1090 /**
8aa3aa3d 1091 * Checks that a course within the management interface is visible.
5dc361e1
SH
1092 *
1093 * @Given /^course in management listing should be visible "(?P<idnumber>[^"]*)"$/
1094 * @param string $idnumber
1095 */
1096 public function course_in_management_listing_should_be_visible($idnumber) {
1097 $id = $this->get_course_id($idnumber);
1098 $exception = new ExpectationException('The course '.$idnumber.' is not visible.', $this->getSession());
1099 $selector = sprintf('#course-listing .listitem-course[data-id="%d"][data-visible="1"]', $id);
1100 $this->find('css', $selector, $exception);
1101 }
1102
1103 /**
8aa3aa3d 1104 * Checks that a course within the management interface is dimmed.
5dc361e1
SH
1105 *
1106 * @Given /^course in management listing should be dimmed "(?P<idnumber>[^"]*)"$/
1107 * @param string $idnumber
1108 */
1109 public function course_in_management_listing_should_be_dimmed($idnumber) {
1110 $id = $this->get_course_id($idnumber);
1111 $exception = new ExpectationException('The course '.$idnumber.' is visible.', $this->getSession());
1112 $selector = sprintf('#course-listing .listitem-course[data-id="%d"][data-visible="0"]', $id);
1113 $this->find('css', $selector, $exception);
1114 }
1115
1116 /**
1117 * Toggles the visibility of a course in the management UI.
1118 *
1119 * If it was visible it will be hidden. If it is hidden it will be made visible.
1120 *
1121 * @Given /^I toggle visibility of course "(?P<idnumber>[^"]*)" in management listing$/
1122 * @param string $idnumber
1123 */
1124 public function i_toggle_visibility_of_course_in_management_listing($idnumber) {
1125 $id = $this->get_course_id($idnumber);
1126 $selector = sprintf('#course-listing .listitem-course[data-id="%d"][data-visible]', $id);
1127 $node = $this->find('css', $selector);
1128 $exception = new ExpectationException('Course listing "' . $idnumber . '" does not contain a show or hide toggle.', $this->getSession());
1129 if ($node->getAttribute('data-visible') === '1') {
1130 $toggle = $this->find('css', '.action-hide', $exception, $node);
1131 } else {
1132 $toggle = $this->find('css', '.action-show', $exception, $node);
1133 }
1134 $toggle->click();
1135 }
1136
1137 /**
1138 * Toggles the visibility of a category in the management UI.
1139 *
1140 * If it was visible it will be hidden. If it is hidden it will be made visible.
1141 *
1142 * @Given /^I toggle visibility of category "(?P<idnumber>[^"]*)" in management listing$/
1143 */
1144 public function i_toggle_visibility_of_category_in_management_listing($idnumber) {
1145 $id = $this->get_category_id($idnumber);
1146 $selector = sprintf('#category-listing .listitem-category[data-id="%d"][data-visible]', $id);
1147 $node = $this->find('css', $selector);
1148 $exception = new ExpectationException('Category listing "' . $idnumber . '" does not contain a show or hide toggle.', $this->getSession());
1149 if ($node->getAttribute('data-visible') === '1') {
1150 $toggle = $this->find('css', '.action-hide', $exception, $node);
1151 } else {
1152 $toggle = $this->find('css', '.action-show', $exception, $node);
1153 }
1154 $toggle->click();
1155 }
1156
1157 /**
8aa3aa3d
SH
1158 * Moves a category displayed in the management interface up or down one place.
1159 *
1160 * @Given /^I click to move category "(?P<idnumber>[^"]*)" (?P<direction>up|down) one$/
1161 *
1162 * @param string $idnumber The category idnumber
1163 * @param string $direction The direction to move in, either up or down
5dc361e1 1164 */
8aa3aa3d
SH
1165 public function i_click_to_move_category_by_one($idnumber, $direction) {
1166 $node = $this->get_management_category_listing_node_by_idnumber($idnumber);
1167 $this->user_moves_listing_by_one('category', $node, $direction);
1168 }
1169
1170 /**
1171 * Moves a course displayed in the management interface up or down one place.
1172 *
1173 * @Given /^I click to move course "(?P<idnumber>[^"]*)" (?P<direction>up|down) one$/
1174 *
1175 * @param string $idnumber The course idnumber
1176 * @param string $direction The direction to move in, either up or down
1177 */
1178 public function i_click_to_move_course_by_one($idnumber, $direction) {
1179 $node = $this->get_management_course_listing_node_by_idnumber($idnumber);
1180 $this->user_moves_listing_by_one('course', $node, $direction);
1181 }
1182
1183 /**
1184 * Moves a course or category listing within the management interface up or down by one.
1185 *
1186 * @param string $listingtype One of course or category
1187 * @param \Behat\Mink\Element\NodeElement $listingnode
1188 * @param string $direction One of up or down.
1189 * @param bool $highlight If set to false we don't check the node has been highlighted.
1190 */
1191 protected function user_moves_listing_by_one($listingtype, $listingnode, $direction, $highlight = true) {
1192 $up = (strtolower($direction) === 'up');
5dc361e1 1193 if ($up) {
8aa3aa3d
SH
1194 $exception = new ExpectationException($listingtype.' listing does not contain a moveup button.', $this->getSession());
1195 $button = $this->find('css', 'a.action-moveup', $exception, $listingnode);
5dc361e1 1196 } else {
8aa3aa3d
SH
1197 $exception = new ExpectationException($listingtype.' listing does not contain a movedown button.', $this->getSession());
1198 $button = $this->find('css', 'a.action-movedown', $exception, $listingnode);
5dc361e1
SH
1199 }
1200 $button->click();
8aa3aa3d
SH
1201 if ($this->running_javascript() && $highlight) {
1202 $listitem = $listingnode->getParent();
5dc361e1
SH
1203 $exception = new ExpectationException('Nothing was highlighted, ajax didn\'t occur or didn\'t succeed.', $this->getSession());
1204 $this->spin(array($this, 'listing_is_highlighted'), $listitem->getTagName().'#'.$listitem->getAttribute('id'), 2, $exception, true);
1205 }
1206 }
1207
1208 /**
8aa3aa3d
SH
1209 * Used by spin to determine the callback has been highlighted.
1210 *
1211 * @param behat_course $self A self reference (default first arg from a spin callback)
1212 * @param \Behat\Mink\Element\NodeElement $selector
1213 * @return bool
5dc361e1
SH
1214 */
1215 protected function listing_is_highlighted($self, $selector) {
1216 $listitem = $this->find('css', $selector);
1217 return $listitem->hasClass('highlight');
1218 }
1219
1220 /**
8aa3aa3d 1221 * Check that one course appears before another in the course category management listings.
5dc361e1 1222 *
8aa3aa3d
SH
1223 * @Given /^I should see course listing "(?P<preceedingcourse>[^"]*)" before "(?P<followingcourse>[^"]*)"$/
1224 *
1225 * @param string $preceedingcourse The first course to find
1226 * @param string $followingcourse The second course to find (should be AFTER the first course)
1227 * @throws ExpectationException
1228 */
1229 public function i_should_see_course_listing_before($preceedingcourse, $followingcourse) {
1230 $xpath = "//div[@id='course-listing']//li[contains(concat(' ', @class, ' '), ' listitem-course ')]//a[text()='{$preceedingcourse}']/ancestor::li[@data-id]//following::a[text()='{$followingcourse}']";
1231 $msg = "{$preceedingcourse} course does not appear before {$followingcourse} course";
1232 if (!$this->getSession()->getDriver()->find($xpath)) {
1233 throw new ExpectationException($msg, $this->getSession());
1234 }
1235 }
1236
1237 /**
1238 * Check that one category appears before another in the course category management listings.
1239 *
1240 * @Given /^I should see category listing "(?P<preceedingcategory>[^"]*)" before "(?P<followingcategory>[^"]*)"$/
1241 *
1242 * @param string $preceedingcategory The first category to find
1243 * @param string $followingcategory The second category to find (should be after the first category)
1244 * @throws ExpectationException
5dc361e1 1245 */
8aa3aa3d
SH
1246 public function i_should_see_category_listing_before($preceedingcategory, $followingcategory) {
1247 $xpath = "//div[@id='category-listing']//li[contains(concat(' ', @class, ' '), ' listitem-category ')]//a[text()='{$preceedingcategory}']/ancestor::li[@data-id]//following::a[text()='{$followingcategory}']";
1248 $msg = "{$preceedingcategory} category does not appear before {$followingcategory} category";
5dc361e1
SH
1249 if (!$this->getSession()->getDriver()->find($xpath)) {
1250 throw new ExpectationException($msg, $this->getSession());
1251 }
1252 }
1253
1254 /**
8aa3aa3d 1255 * Checks that we are on the course management page that we expect to be on and that no course has been selected.
5dc361e1 1256 *
8aa3aa3d
SH
1257 * @Given /^I should see the "(?P<mode>[^"]*)" management page$/
1258 * @param string $mode The mode to expected. One of 'Courses', 'Course categories' or 'Course categories and courses'
5dc361e1
SH
1259 * @return Given[]
1260 */
8aa3aa3d 1261 public function i_should_see_the_courses_management_page($mode) {
5dc361e1
SH
1262 $return = array(
1263 new Given('I should see "Course and category management" in the "h2" "css_element"')
1264 );
1265 switch ($mode) {
1266 case "Courses":
1267 $return[] = new Given('"#category-listing" "css_element" should not exists');
1268 $return[] = new Given('"#course-listing" "css_element" should exists');
1269 break;
1270 case "Course categories":
1271 $return[] = new Given('"#category-listing" "css_element" should exists');
1272 $return[] = new Given('"#course-listing" "css_element" should not exists');
1273 break;
1274 case "Courses categories and courses":
1275 default:
1276 $return[] = new Given('"#category-listing" "css_element" should exists');
1277 $return[] = new Given('"#course-listing" "css_element" should exists');
1278 break;
1279 }
8aa3aa3d 1280 $return[] = new Given('"#course-detail" "css_element" should not exists');
5dc361e1
SH
1281 return $return;
1282 }
1283
1284 /**
8aa3aa3d
SH
1285 * Checks that we are on the course management page that we expect to be on and that a course has been selected.
1286 *
1287 * @Given /^I should see the "(?P<mode>[^"]*)" management page with a course selected$/
1288 * @param string $mode The mode to expected. One of 'Courses', 'Course categories' or 'Course categories and courses'
1289 * @return Given[]
5dc361e1 1290 */
8aa3aa3d
SH
1291 public function i_should_see_the_courses_management_page_with_a_course_selected($mode) {
1292 $return = $this->i_should_see_the_courses_management_page($mode);
1293 array_pop($return);
1294 $return[] = new Given('"#course-detail" "css_element" should exists');
1295 return $return;
1296 }
1297
1298 /**
1299 * Locates a course in the course category management interface and then triggers an action for it.
1300 *
1301 * @Given /^I click on "(?P<action>[^"]*)" action for "(?P<name>[^"]*)" in management course listing$/
1302 *
1303 * @param string $action The action to take. One of
1304 * @param string $name The name of the course as it is displayed in the management interface.
1305 */
1306 public function i_click_on_action_for_item_in_management_course_listing($action, $name) {
1307 $node = $this->get_management_course_listing_node_by_name($name);
1308 $this->user_clicks_on_management_listing_action('course', $node, $action);
1309 }
1310
1311 /**
1312 * Locates a category in the course category management interface and then triggers an action for it.
1313 *
1314 * @Given /^I click on "(?P<action>[^"]*)" action for "(?P<name>[^"]*)" in management category listing$/
1315 *
1316 * @param string $action The action to take. One of
1317 * @param string $name The name of the category as it is displayed in the management interface.
1318 */
1319 public function i_click_on_action_for_item_in_management_category_listing($action, $name) {
1320 $node = $this->get_management_category_listing_node_by_name($name);
1321 $this->user_clicks_on_management_listing_action('category', $node, $action);
1322 }
1323
32fcea74
DM
1324 /**
1325 * Clicks to expand or collapse a category displayed on the frontpage
1326 *
1327 * @Given /^I toggle "(?P<categoryname_string>(?:[^"]|\\")*)" category children visibility in frontpage$/
1328 * @throws ExpectationException
1329 * @param string $categoryname
1330 */
1331 public function i_toggle_category_children_visibility_in_frontpage($categoryname) {
1332
1333 $headingtags = array();
1334 for ($i = 1; $i <= 6; $i++) {
1335 $headingtags[] = 'self::h' . $i;
1336 }
1337
1338 $exception = new ExpectationException('"' . $categoryname . '" category can not be found', $this->getSession());
1339 $categoryliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($categoryname);
1340 $xpath = "//div[@class='info']/descendant::*[" . implode(' or ', $headingtags) . "][@class='categoryname'][./descendant::a[.=$categoryliteral]]";
1341 $node = $this->find('xpath', $xpath, $exception);
1342 $node->click();
1343
1344 // Smooth expansion.
1345 $this->getSession()->wait(1000, false);
1346 }
1347
8aa3aa3d
SH
1348 /**
1349 * Finds the node to use for a management listitem action and clicks it.
1350 *
1351 * @param string $listingtype Either course or category.
1352 * @param \Behat\Mink\Element\NodeElement $listingnode
1353 * @param string $action The action being taken
1354 * @throws Behat\Mink\Exception\ExpectationException
1355 */
1356 protected function user_clicks_on_management_listing_action($listingtype, $listingnode, $action) {
1357 $actionsnode = $listingnode->find('xpath', "//*[contains(concat(' ', normalize-space(@class), ' '), '{$listingtype}-item-actions')]");
5dc361e1 1358 if (!$actionsnode) {
8aa3aa3d 1359 throw new ExpectationException("Could not find the actions for $listingtype", $this->getSession());
5dc361e1
SH
1360 }
1361 $actionnode = $actionsnode->find('css', '.action-'.$action);
5dc361e1
SH
1362 if (!$actionnode) {
1363 throw new ExpectationException("Expected action was not available or not found ($action)", $this->getSession());
1364 }
cda49969
SH
1365 if ($this->running_javascript() && !$actionnode->isVisible()) {
1366 $actionsnode->find('css', 'a.toggle-display')->click();
1367 $actionnode = $actionsnode->find('css', '.action-'.$action);
1368 }
5dc361e1
SH
1369 $actionnode->click();
1370 }
a1990e50 1371}