2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Behat course-related steps definitions.
20 * @package core_course
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__
. '/../../../lib/behat/behat_base.php');
30 use Behat\Behat\Context\Step\Given
as Given
,
31 Behat\Gherkin\Node\TableNode
as TableNode
,
32 Behat\Mink\Exception\ExpectationException
as ExpectationException
,
33 Behat\Mink\Exception\ElementNotFoundException
as ElementNotFoundException
;
36 * Course-related steps definitions.
38 * @package core_course
40 * @copyright 2012 David MonllaĆ³
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class behat_course
extends behat_base
{
46 * Turns editing mode on.
47 * @Given /^I turn editing mode on$/
49 public function i_turn_editing_mode_on() {
50 return new Given('I press "Turn editing on"');
54 * Turns editing mode off.
55 * @Given /^I turn editing mode off$/
57 public function i_turn_editing_mode_off() {
58 return new Given('I press "Turn editing off"');
62 * Creates a new course with the provided table data matching course settings names with the desired values.
64 * @Given /^I create a course with:$/
65 * @param TableNode $table The course data
67 public function i_create_a_course_with(TableNode
$table) {
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"')
77 * Goes to the system courses/categories management page.
79 * @Given /^I go to the courses management page$/
81 public function i_go_to_the_courses_management_page() {
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"'),
92 * Adds the selected activity/resource filling the form data with the specified field/value pairs.
94 * @When /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)" and I fill the form with:$/
95 * @param string $activity The activity name
96 * @param string $section The section number
97 * @param TableNode $data The activity field/value data
99 public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode
$data) {
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"')
109 * Opens the activity chooser and opens the activity/resource form page.
111 * @Given /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)"$/
112 * @throws ElementNotFoundException Thrown by behat_base::find
113 * @param string $activity
114 * @param string $section
116 public function i_add_to_section($activity, $section) {
118 $sectionxpath = "//*[@id='section-" . $section . "']";
120 if ($this->running_javascript()) {
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();
127 // Clicks the selected activity if it exists.
128 $activity = ucfirst($activity);
129 $activityxpath = "//div[@id='chooseform']/descendant::label" .
130 "/descendant::span[contains(concat(' ', @class, ' '), ' typename ')][contains(.,'" . $activity . "')]" .
131 "/parent::label/child::input";
132 $activitynode = $this->find('xpath', $activityxpath);
133 $activitynode->doubleClick();
136 // Without Javascript.
138 // Selecting the option from the select box which contains the option.
139 $selectxpath = $sectionxpath . "/descendant::div[contains(concat(' ', @class, ' '), ' section_add_menus ')]" .
140 "/descendant::select[contains(., '" . $activity . "')]";
141 $selectnode = $this->find('xpath', $selectxpath);
142 $selectnode->selectOption($activity);
145 $gobuttonxpath = $selectxpath . "/ancestor::form/descendant::input[@type='submit']";
146 $gobutton = $this->find('xpath', $gobuttonxpath);
153 * Turns course section highlighting on.
155 * @Given /^I turn section "(?P<section_number>\d+)" highlighting on$/
156 * @param int $sectionnumber The section number
158 public function i_turn_section_highlighting_on($sectionnumber) {
160 // Ensures the section exists.
161 $xpath = $this->section_exists($sectionnumber);
164 new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
165 new Given('I wait "2" seconds')
170 * Turns course section highlighting off.
172 * @Given /^I turn section "(?P<section_number>\d+)" highlighting off$/
173 * @param int $sectionnumber The section number
175 public function i_turn_section_highlighting_off($sectionnumber) {
177 // Ensures the section exists.
178 $xpath = $this->section_exists($sectionnumber);
181 new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
182 new Given('I wait "2" seconds')
187 * Shows the specified hidden section. You need to be in the course page and on editing mode.
189 * @Given /^I show section "(?P<section_number>\d+)"$/
190 * @param int $sectionnumber
192 public function i_show_section($sectionnumber) {
193 $showicon = $this->show_section_icon_exists($sectionnumber);
197 $this->getSession()->wait(5000, false);
201 * Hides the specified visible section. You need to be in the course page and on editing mode.
203 * @Given /^I hide section "(?P<section_number>\d+)"$/
204 * @param int $sectionnumber
206 public function i_hide_section($sectionnumber) {
207 $hideicon = $this->hide_section_icon_exists($sectionnumber);
211 $this->getSession()->wait(5000, false);
215 * Checks if the specified course section hightlighting is turned on. You need to be in the course page on editing mode.
217 * @Then /^section "(?P<section_number>\d+)" should be highlighted$/
218 * @throws ExpectationException
219 * @param int $sectionnumber The section number
221 public function section_should_be_highlighted($sectionnumber) {
223 // Ensures the section exists.
224 $xpath = $this->section_exists($sectionnumber);
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);
233 * Checks if the specified course section highlighting is turned off. You need to be in the course page on editing mode.
235 * @Then /^section "(?P<section_number>\d+)" should not be highlighted$/
236 * @throws ExpectationException
237 * @param int $sectionnumber The section number
239 public function section_should_not_be_highlighted($sectionnumber) {
241 // We only catch ExpectationException, ElementNotFoundException should be thrown if the specified section does not exist.
243 $this->section_should_be_highlighted($sectionnumber);
244 } catch (ExpectationException
$e) {
245 // ExpectedException means that it is not highlighted.
249 throw new ExpectationException('The "' . $sectionnumber . '" section is highlighted', $this->getSession());
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.
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
260 public function section_should_be_hidden($sectionnumber) {
262 $sectionxpath = $this->section_exists($sectionnumber);
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);
268 // The checking are different depending on user permissions.
269 if ($this->is_course_editor()) {
271 // The section must be hidden.
272 $this->show_section_icon_exists($sectionnumber);
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)) {
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) {
282 $this->find('xpath', "//div[contains(concat(' ', @class, ' '), ' activityinstance ')]" .
283 "/a[contains(concat(' ', @class, ' '), ' dimmed ')]", $dimmedexception, $activity);
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();
289 // We ensure that we still see the show icon.
290 $visibilityiconnode = $this->find('css', 'a.editing_show img', $visibilityexception, $activity);
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());
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.
305 * @Then /^section "(?P<section_number>\d+)" should be visible$/
306 * @throws ExpectationException
307 * @param int $sectionnumber
309 public function section_should_be_visible($sectionnumber) {
311 $sectionxpath = $this->section_exists($sectionnumber);
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());
318 // Hide section button should be visible.
319 if ($this->is_course_editor()) {
320 $this->hide_section_icon_exists($sectionnumber);
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.
327 * @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be visible$/
328 * @param string $activityname
330 public function activity_should_be_visible($activityname) {
332 // The activity must exists and be visible.
333 $activitynode = $this->get_activity_node($activityname);
335 if ($this->is_course_editor()) {
337 // The activity should not be dimmed.
339 $this->find('css', 'a.dimmed', false, $activitynode);
340 throw new ExpectationException('"' . $activityname . '" is hidden', $this->getSession());
341 } catch (ElementNotFoundException
$e) {
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);
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.
354 * @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be hidden$/
355 * @param string $activityname
357 public function activity_should_be_hidden($activityname) {
359 if ($this->is_course_editor()) {
361 // The activity should exists.
362 $activitynode = $this->get_activity_node($activityname);
365 $exception = new ExpectationException('"' . $activityname . '" is not dimmed', $this->getSession());
366 $this->find('css', 'a.dimmed', $exception, $activitynode);
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);
374 // It should not exists at all.
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.
386 * Checks if the course section exists.
388 * @throws ElementNotFoundException Thrown by behat_base::find
389 * @param int $sectionnumber
390 * @return string The xpath of the section.
392 protected function section_exists($sectionnumber) {
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);
403 * Returns the show section icon or throws an exception.
405 * @throws ElementNotFoundException Thrown by behat_base::find
406 * @param int $sectionnumber
407 * @return NodeElement
409 protected function show_section_icon_exists($sectionnumber) {
411 // Gets the section xpath and ensure it exists.
412 $xpath = $this->section_exists($sectionnumber);
414 // We need to know the course format as the text strings depends on them.
415 $courseformat = $this->get_course_format();
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')]";
420 $exception = new ElementNotFoundException($this->getSession(), 'Show section icon ');
421 return $this->find('xpath', $xpath, $exception);
425 * Returns the hide section icon link if it exists or throws exception.
427 * @throws ElementNotFoundException Thrown by behat_base::find
428 * @param int $sectionnumber
429 * @return NodeElement
431 protected function hide_section_icon_exists($sectionnumber) {
433 // Gets the section xpath and ensure it exists.
434 $xpath = $this->section_exists($sectionnumber);
436 // We need to know the course format as the text strings depends on them.
437 $courseformat = $this->get_course_format();
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')]";
442 $exception = new ElementNotFoundException($this->getSession(), 'Hide section icon ');
443 return $this->find('xpath', $xpath, $exception);
447 * Gets the current course format.
449 * @throws ExpectationException If we are not in the course view page.
450 * @return string The course format in a frankenstyled name.
452 protected function get_course_format() {
454 $exception = new ExpectationException('You are not in a course page', $this->getSession());
456 // The moodle body's id attribute contains the course format.
457 $node = $this->getSession()->getPage()->find('css', 'body');
462 if (!$bodyid = $node->getAttribute('id')) {
466 if (strstr($bodyid, 'page-course-view-') === false) {
470 return 'format_' . str_replace('page-course-view-', '', $bodyid);
474 * Gets the section's activites DOM nodes.
476 * @param string $sectionxpath
477 * @return array NodeElement instances
479 protected function get_section_activities($sectionxpath) {
481 $xpath = $sectionxpath . "/descendant::li[contains(concat(' ', @class, ' '), ' activity ')]";
483 // We spin here, as activities usually require a lot of time to load.
485 $activities = $this->find_all('xpath', $xpath);
486 } catch (ElementNotFoundException
$e) {
494 * Returns the DOM node of the activity from <li>.
496 * @throws ElementNotFoundException Thrown by behat_base::find
497 * @param string $activityname The activity name
498 * @return NodeElement
500 protected function get_activity_node($activityname) {
502 $activityname = str_replace("'", "\'", $activityname);
503 $xpath = "//li[contains(concat(' ', @class, ' '), ' activity ')][contains(., '" .$activityname. "')]";
505 return $this->find('xpath', $xpath);
509 * Returns whether the user can edit the course contents or not.
513 protected function is_course_editor() {
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')) {