MDL-69149 core_grade: Correct xpath attribute selector
[moodle.git] / grade / tests / behat / behat_grade.php
CommitLineData
4f5eaa55
MN
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 grade related steps definitions.
19 *
20 * @package core_grades
21 * @category test
22 * @copyright 2014 Mark Nelson <markn@moodle.com>
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
eb9ca848 30use Behat\Gherkin\Node\TableNode as TableNode;
4f5eaa55
MN
31
32class behat_grade extends behat_base {
33
34 /**
35 * Enters a grade via the gradebook for a specific grade item and user when viewing the 'Grader report' with editing mode turned on.
36 *
37 * @Given /^I give the grade "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
38 * @param int $grade
39 * @param string $userfullname the user's fullname as returned by fullname()
40 * @param string $itemname
4f5eaa55
MN
41 */
42 public function i_give_the_grade($grade, $userfullname, $itemname) {
43 $gradelabel = $userfullname . ' ' . $itemname;
44 $fieldstr = get_string('useractivitygrade', 'gradereport_grader', $gradelabel);
45
eb9ca848 46 $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $grade));
4f5eaa55 47 }
d06aec10 48
12f946d9
EM
49 /**
50 * Enters a quick feedback via the gradebook for a specific grade item and user when viewing
51 * the 'Grader report' with editing mode turned on.
52 *
53 * @Given /^I give the feedback "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
54 * @param string $feedback
55 * @param string $userfullname the user's fullname as returned by fullname()
56 * @param string $itemname
12f946d9
EM
57 */
58 public function i_give_the_feedback($feedback, $userfullname, $itemname) {
59 $gradelabel = $userfullname . ' ' . $itemname;
60 $fieldstr = get_string('useractivityfeedback', 'gradereport_grader', $gradelabel);
61
eb9ca848 62 $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $this->escape($feedback)));
12f946d9
EM
63 }
64
d06aec10
MG
65 /**
66 * Changes the settings of a grade item or category or the course.
67 *
68 * Teacher must be either on the grade setup page or on the Grader report page with editing mode turned on.
69 *
70 * @Given /^I set the following settings for grade item "(?P<grade_item_string>(?:[^"]|\\")*)":$/
71 * @param string $gradeitem
72 * @param TableNode $data
d06aec10
MG
73 */
74 public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {
079b2e52 75
921faad9 76 $gradeitem = behat_context_helper::escape($gradeitem);
079b2e52
MG
77
78 if ($this->running_javascript()) {
e3652936 79 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]";
079b2e52 80 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
e3652936
MM
81 $this->execute("behat_action_menu::i_open_the_action_menu_in",
82 array("//tr[contains(.,$gradeitem)]",
83 "xpath_element"));
079b2e52
MG
84 }
85 }
86
d06aec10 87 $savechanges = get_string('savechanges', 'grades');
921faad9 88 $edit = behat_context_helper::escape(get_string('edit') . ' ');
e3652936
MM
89 $linkxpath = "//a[./*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') " .
90 "and starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848
RT
91
92 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
93 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
94 $this->execute('behat_forms::press_button', $this->escape($savechanges));
079b2e52
MG
95 }
96
ca2cb90f
EM
97 /**
98 * Hids a grade item or category.
99 *
8857c715 100 * Teacher must be on the grade setup page.
ca2cb90f
EM
101 *
102 * @Given /^I hide the grade item "(?P<grade_item_string>(?:[^"]|\\")*)"$/
103 * @param string $gradeitem
104 */
105 public function i_hide_the_grade_item($gradeitem) {
106
107 $gradeitem = behat_context_helper::escape($gradeitem);
108
109 if ($this->running_javascript()) {
110 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
111 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
112 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
113 }
114 }
115
8857c715
DW
116 $this->execute("behat_general::i_click_on_in_the", array(get_string('hide'), 'link',
117 "//tr[descendant::*[text() = " . $this->escape($gradeitem) . "]]", 'xpath_element'));
ca2cb90f
EM
118 }
119
887679e5
MD
120 /**
121 * Duplicates a grade item or category.
122 *
123 * Teacher must be on the grade setup page.
124 *
125 * @Given /^I duplicate the grade item "(?P<grade_item_string>(?:[^"]|\\")*)"$/
126 * @param string $gradeitem
127 */
128 public function i_duplicate_the_grade_item($gradeitem) {
129
130 $gradeitem = behat_context_helper::escape($gradeitem);
131
132 if ($this->running_javascript()) {
133 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
134 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
135 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
136 }
137 }
138
139 $this->execute("behat_general::i_click_on_in_the", array(get_string('duplicate'), 'link',
140 "//tr[descendant::*[text() = " . $this->escape($gradeitem) . "]]", 'xpath_element'));
141 }
142
12a4770d 143 /**
53914e44
MN
144 * Sets a calculated manual grade item. Needs a table with item name - idnumber relation.
145 * The step requires you to be in the 'Gradebook setup' page.
12a4770d
DM
146 *
147 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
148 * @param string $calculation The calculation.
149 * @param string $gradeitem The grade item name.
150 * @param TableNode $TableNode The grade item name - idnumbers relation.
12a4770d
DM
151 */
152 public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
153
921faad9 154 $gradeitem = behat_context_helper::escape($gradeitem);
12a4770d
DM
155
156 if ($this->running_javascript()) {
e3652936 157 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]";
12a4770d 158 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
e3652936
MM
159 $this->execute("behat_action_menu::i_open_the_action_menu_in",
160 array("//tr[contains(.,$gradeitem)]",
161 "xpath_element"));
12a4770d
DM
162 }
163 }
164
165 // Going to edit calculation.
166 $savechanges = get_string('savechanges', 'grades');
921faad9 167 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
e3652936
MM
168 $linkxpath = "//a[./*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') " .
169 "and starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848 170 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
12a4770d
DM
171
172 // Mapping names to idnumbers.
173 $datahash = $data->getRowsHash();
174 foreach ($datahash as $gradeitem => $idnumber) {
175 // This xpath looks for course, categories and items with the provided name.
176 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
e3652936
MM
177 $inputxpath = "//input[@class='idnumber'][" .
178 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
179 " or " .
180 "parent::li[@class='categoryitem' or @class='courseitem']" .
181 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
182 "]";
eb9ca848 183 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
77f1778c 184 }
12a4770d 185
eb9ca848
RT
186 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
187 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
188 $this->execute('behat_forms::press_button', $savechanges);
12a4770d 189
12a4770d
DM
190 }
191
f44ffb72
AG
192 /**
193 * Sets a calculated manual grade category total. Needs a table with item name - idnumber relation.
53914e44 194 * The step requires you to be in the 'Gradebook setup' page.
f44ffb72
AG
195 *
196 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade category "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
197 * @param string $calculation The calculation.
198 * @param string $gradeitem The grade item name.
199 * @param TableNode $data The grade item name - idnumbers relation.
f44ffb72
AG
200 */
201 public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
202
921faad9
RT
203 $gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
204 $gradeitem = behat_context_helper::escape($gradeitem);
f44ffb72
AG
205
206 if ($this->running_javascript()) {
e3652936 207 $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]";
f44ffb72 208 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
e3652936
MM
209 $xpath = "//tr[contains(.,$gradecategorytotal)]";
210 $this->execute("behat_action_menu::i_open_the_action_menu_in", array($xpath, "xpath_element"));
f44ffb72
AG
211 }
212 }
213
214 // Going to edit calculation.
215 $savechanges = get_string('savechanges', 'grades');
921faad9 216 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
e3652936
MM
217 $linkxpath = "//a[./*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') " .
218 "and starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848 219 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
f44ffb72
AG
220
221 // Mapping names to idnumbers.
222 $datahash = $data->getRowsHash();
223 foreach ($datahash as $gradeitem => $idnumber) {
224 // This xpath looks for course, categories and items with the provided name.
225 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
226 $inputxpath = "//input[@class='idnumber'][" .
e3652936
MM
227 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
228 " | " .
eed46500 229 "parent::li[@class='categoryitem' or @class='courseitem']" .
e3652936
MM
230 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
231 "]";
eb9ca848 232 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
f44ffb72
AG
233 }
234
eb9ca848 235 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
12a4770d 236
eb9ca848
RT
237 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
238 $this->execute('behat_forms::press_button', $savechanges);
12a4770d
DM
239 }
240
079b2e52
MG
241 /**
242 * Resets the weights for the grade category
243 *
244 * Teacher must be on the grade setup page.
245 *
246 * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
247 * @param $gradeitem
079b2e52
MG
248 */
249 public function i_reset_weights_for_grade_category($gradeitem) {
250
251 $steps = array();
252
253 if ($this->running_javascript()) {
921faad9 254 $gradeitemliteral = behat_context_helper::escape($gradeitem);
e3652936 255 $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]";
079b2e52 256 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
e3652936
MM
257 $xpath = "//tr[contains(.,$gradeitemliteral)]";
258 $this->execute("behat_action_menu::i_open_the_action_menu_in", array($xpath, "xpath_element"));
079b2e52
MG
259 }
260 }
261
262 $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
eb9ca848 263 $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link"));
d06aec10 264 }
deb3d5ed
MG
265
266 /**
267 * Step allowing to test before-the-fix behaviour of the gradebook
268 *
269 * @Given /^gradebook calculations for the course "(?P<coursename_string>(?:[^"]|\\")*)" are frozen at version "(?P<version_string>(?:[^"]|\\")*)"$/
270 * @param string $coursename
271 * @param string $version
deb3d5ed
MG
272 */
273 public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) {
274 global $DB;
275 $courseid = $DB->get_field('course', 'id', array('shortname' => $coursename), MUST_EXIST);
276 set_config('gradebook_calculations_freeze_' . $courseid, $version);
277 }
b0842f61
MG
278
279 /**
ebcff7e2 280 * Select the tab in the gradebook. We must be on one of the gradebook pages already.
b0842f61 281 *
ebcff7e2 282 * @param string $gradepath examples: "View > User report", "Letters > View", "Scales"
b0842f61 283 */
ebcff7e2 284 protected function select_in_gradebook_tabs($gradepath) {
b0842f61
MG
285 $gradepath = preg_split('/\s*>\s*/', trim($gradepath));
286 if (count($gradepath) > 2) {
ebcff7e2 287 throw new coding_exception('Grade path is too long (must have no more than two items separated with ">")');
b0842f61
MG
288 }
289
b0842f61 290 $xpath = '//div[contains(@class,\'grade-navigation\')]';
b0842f61
MG
291
292 // If the first row of the grade-navigation tabs does not have $gradepath[0] as active tab, click on it.
293 $link = '\'' . $this->escape($gradepath[0]) . '\'';
47b03b91 294 $xpathrow1 = $xpath . '//ul[1]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
b0842f61 295 if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow1)) {
1adf21fc
MG
296 $this->find('xpath', $xpath . '//ul[1]/li/a[text()=' . $link . ']')->click();
297 $this->wait_for_pending_js();
b0842f61
MG
298 }
299
300 if (isset($gradepath[1])) {
301 // If the second row of the grade-navigation tabs does not have $gradepath[1] as active tab, click on it.
302 $link = '\'' . $this->escape($gradepath[1]) . '\'';
47b03b91 303 $xpathrow2 = $xpath . '//ul[2]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
b0842f61 304 if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow2)) {
1adf21fc
MG
305 $this->find('xpath', $xpath . '//ul[2]/li/a[text()=' . $link . ']')->click();
306 $this->wait_for_pending_js();
b0842f61
MG
307 }
308 }
309 }
ebcff7e2
MG
310
311 /**
312 * Navigates to the course gradebook and selects a specified item from the grade navigation tabs.
313 *
314 * Examples:
315 * - I navigate to "Setup > Gradebook setup" in the course gradebook
316 * - I navigate to "Scales" in the course gradebook
317 * - I navigate to "Letters > View" in the course gradebook
318 * - I navigate to "View > User report" in the course gradebook // for teachers
319 * - I navigate to "User report" in the course gradebook // for students
320 *
321 * @Given /^I navigate to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
322 * @param string $gradepath
323 */
324 public function i_navigate_to_in_the_course_gradebook($gradepath) {
e3652936 325 // If we are not on one of the gradebook pages already, follow "Grades" link in the navigation drawer.
ebcff7e2
MG
326 $xpath = '//div[contains(@class,\'grade-navigation\')]';
327 if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
e3652936 328 $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', get_string('grades'));
ebcff7e2
MG
329 }
330
331 $this->select_in_gradebook_tabs($gradepath);
332 }
4f5eaa55 333}