MDL-38499 behat: New test
[moodle.git] / course / tests / behat / behat_course.php
CommitLineData
a1990e50
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Behat course-related steps definitions.
19 *
20 * @package core_course
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
29
30use Behat\Behat\Context\Step\Given as Given,
18c84063
DM
31 Behat\Gherkin\Node\TableNode as TableNode,
32 Behat\Mink\Exception\ExpectationException as ExpectationException,
33 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
a1990e50
DM
34
35/**
36 * Course-related steps definitions.
37 *
38 * @package core_course
39 * @category test
40 * @copyright 2012 David MonllaĆ³
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class behat_course extends behat_base {
44
45 /**
46 * Turns editing mode on.
47 * @Given /^I turn editing mode on$/
48 */
49 public function i_turn_editing_mode_on() {
50 return new Given('I press "Turn editing on"');
51 }
52
53 /**
54 * Turns editing mode off.
55 * @Given /^I turn editing mode off$/
56 */
57 public function i_turn_editing_mode_off() {
58 return new Given('I press "Turn editing off"');
59 }
60
61 /**
62 * Adds the selected activity/resource filling the form data with the specified field/value pairs.
63 *
64 * @When /^I add a "(?P<activity_or_resource_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)" and I fill the form with:$/
65 * @param string $activity The activity name
66 * @param string $section The section number
67 * @param TableNode $data The activity field/value data
68 */
69 public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) {
70
a1990e50
DM
71 return array(
72 new Given('I add a "'.$activity.'" to section "'.$section.'"'),
73 new Given('I fill the moodle form with:', $data),
74 new Given('I press "Save and return to course"')
75 );
76 }
77
78 /**
79 * Opens the activity chooser and opens the activity/resource form page.
80 *
81 * @Given /^I add a "(?P<activity_or_resource_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)"$/
1f9ffbdb 82 * @throws ElementNotFoundException Thrown by behat_base::find
a1990e50
DM
83 * @param string $activity
84 * @param string $section
85 */
86 public function i_add_to_section($activity, $section) {
87
a1990e50 88 // Clicks add activity or resource section link.
1f9ffbdb
DM
89 $sectionxpath = "//*[@id='section-" . $section . "']/*/*/*/div[@class='section-modchooser']/span/a";
90 $sectionnode = $this->find('xpath', $sectionxpath);
91 $sectionnode->click();
a1990e50
DM
92
93 // Clicks the selected activity if it exists.
94 $activityxpath = ".//label[contains(.,'" . $activity . "')]/input";
1f9ffbdb
DM
95 $activitynode = $this->find('xpath', $activityxpath);
96 $activitynode->doubleClick();
a1990e50
DM
97 }
98
18c84063
DM
99 /**
100 * Turns course section highlighting on.
101 *
102 * @Given /^I turn section "(?P<section_number>\d+)" highlighting on$/
103 * @param int $sectionnumber The section number
104 */
105 public function i_turn_section_highlighting_on($sectionnumber) {
106
107 // Ensures the section exists.
108 $xpath = $this->section_exists($sectionnumber);
109
110 return array(
111 new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
112 new Given('I wait "2" seconds')
113 );
114 }
115
116 /**
117 * Turns course section highlighting off.
118 *
119 * @Given /^I turn section "(?P<section_number>\d+)" highlighting off$/
120 * @param int $sectionnumber The section number
121 */
122 public function i_turn_section_highlighting_off($sectionnumber) {
123
124 // Ensures the section exists.
125 $xpath = $this->section_exists($sectionnumber);
126
127 return array(
128 new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'),
129 new Given('I wait "2" seconds')
130 );
131 }
132
133 /**
134 * Checks if the specified course section hightlighting is turned on.
135 *
136 * @throws ElementNotFoundException
137 * @throws ExpectationException
138 * @Then /^section "(?P<section_number>\d+)" should be highlighted$/
139 * @param int $sectionnumber The section number
140 */
141 public function section_should_be_highlighted($sectionnumber) {
142
143 // Ensures the section exists.
144 $xpath = $this->section_exists($sectionnumber);
145
146 // The important checking, we can not check the img.
147 $xpath = $xpath . "/descendant::img[@alt='" . get_string('markedthistopic') . "'][contains(@src, 'marked')]";
148 $exception = new ExpectationException('The "' . $sectionnumber . '" section is not highlighted', $this->getSession());
149 $this->find('xpath', $xpath, $exception);
150 }
151
152 /**
153 * Checks if the specified course section highlighting is turned off.
154 *
155 * @Then /^section "(?P<section_number>\d+)" should not be highlighted$/
156 * @param int $sectionnumber The section number
157 */
158 public function section_should_not_be_highlighted($sectionnumber) {
159
160 // We only catch ExpectationException, ElementNotFoundException should be thrown if the specified section does not exist.
161 try {
162 $this->section_should_be_highlighted($sectionnumber);
163 } catch (ExpectationException $e) {
164 // ExpectedException means that it is not highlighted.
165 return;
166 }
167
168 throw new ExpectationException('The "' . $sectionnumber . '" section is highlighted', $this->getSession());
169 }
170
171 /**
172 * Checks if the course section exists.
173 *
174 * @throws ElementNotFoundException Thrown by behat_base::find
175 * @param int $sectionnumber
176 * @return string The xpath of the existing section.
177 */
178 protected function section_exists($sectionnumber) {
179
180 // Just to give more info in case it does not exist.
181 $xpath = "//li[@id='section-" . $sectionnumber . "']";
182 $exception = new ElementNotFoundException($this->getSession(), "Section $sectionnumber ");
183 $this->find('xpath', $xpath, $exception);
184
185 return $xpath;
186 }
a1990e50 187}