weekly back-to-dev release 3.3dev
[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()) {
79 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
80 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
eb9ca848 81 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
079b2e52
MG
82 }
83 }
84
d06aec10 85 $savechanges = get_string('savechanges', 'grades');
921faad9 86 $edit = behat_context_helper::escape(get_string('edit') . ' ');
d06aec10 87 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848
RT
88
89 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
90 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
91 $this->execute('behat_forms::press_button', $this->escape($savechanges));
079b2e52
MG
92 }
93
ca2cb90f
EM
94 /**
95 * Hids a grade item or category.
96 *
97 * Teacher must be either on the grade setup page or on the Grader report page with editing mode turned on.
98 *
99 * @Given /^I hide the grade item "(?P<grade_item_string>(?:[^"]|\\")*)"$/
100 * @param string $gradeitem
101 */
102 public function i_hide_the_grade_item($gradeitem) {
103
104 $gradeitem = behat_context_helper::escape($gradeitem);
105
106 if ($this->running_javascript()) {
107 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
108 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
109 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
110 }
111 }
112
113 $hide = behat_context_helper::escape(get_string('hide') . ' ');
114 $linkxpath = "//a[./img[starts-with(@title,$hide) and contains(@title,$gradeitem)]]";
115
116 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
117 }
118
12a4770d 119 /**
53914e44
MN
120 * Sets a calculated manual grade item. Needs a table with item name - idnumber relation.
121 * The step requires you to be in the 'Gradebook setup' page.
12a4770d
DM
122 *
123 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
124 * @param string $calculation The calculation.
125 * @param string $gradeitem The grade item name.
126 * @param TableNode $TableNode The grade item name - idnumbers relation.
12a4770d
DM
127 */
128 public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
129
921faad9 130 $gradeitem = behat_context_helper::escape($gradeitem);
12a4770d
DM
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)) {
eb9ca848 135 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
12a4770d
DM
136 }
137 }
138
139 // Going to edit calculation.
140 $savechanges = get_string('savechanges', 'grades');
921faad9 141 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
12a4770d 142 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848 143 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
12a4770d
DM
144
145 // Mapping names to idnumbers.
146 $datahash = $data->getRowsHash();
147 foreach ($datahash as $gradeitem => $idnumber) {
148 // This xpath looks for course, categories and items with the provided name.
149 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
150 $inputxpath ="//input[@class='idnumber'][" .
151 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
77f1778c
RT
152 " or " .
153 "parent::li[@class='categoryitem' or @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
12a4770d 154 "]";
eb9ca848 155 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
77f1778c 156 }
12a4770d 157
eb9ca848
RT
158 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
159 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
160 $this->execute('behat_forms::press_button', $savechanges);
12a4770d 161
12a4770d
DM
162 }
163
f44ffb72
AG
164 /**
165 * Sets a calculated manual grade category total. Needs a table with item name - idnumber relation.
53914e44 166 * The step requires you to be in the 'Gradebook setup' page.
f44ffb72
AG
167 *
168 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade category "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
169 * @param string $calculation The calculation.
170 * @param string $gradeitem The grade item name.
171 * @param TableNode $data The grade item name - idnumbers relation.
f44ffb72
AG
172 */
173 public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
174
921faad9
RT
175 $gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
176 $gradeitem = behat_context_helper::escape($gradeitem);
f44ffb72
AG
177
178 if ($this->running_javascript()) {
179 $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]" .
180 "//a[contains(@class,'toggle-display')]";
181 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
eb9ca848 182 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
f44ffb72
AG
183 }
184 }
185
186 // Going to edit calculation.
187 $savechanges = get_string('savechanges', 'grades');
921faad9 188 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
f44ffb72 189 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848 190 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
f44ffb72
AG
191
192 // Mapping names to idnumbers.
193 $datahash = $data->getRowsHash();
194 foreach ($datahash as $gradeitem => $idnumber) {
195 // This xpath looks for course, categories and items with the provided name.
196 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
197 $inputxpath = "//input[@class='idnumber'][" .
198 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
199 " | " .
200 "parent::li[@class='categoryitem' | @class='courseitem']" .
201 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
202 "]";
eb9ca848 203 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
f44ffb72
AG
204 }
205
eb9ca848 206 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
12a4770d 207
eb9ca848
RT
208 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
209 $this->execute('behat_forms::press_button', $savechanges);
12a4770d
DM
210 }
211
079b2e52
MG
212 /**
213 * Resets the weights for the grade category
214 *
215 * Teacher must be on the grade setup page.
216 *
217 * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
218 * @param $gradeitem
079b2e52
MG
219 */
220 public function i_reset_weights_for_grade_category($gradeitem) {
221
222 $steps = array();
223
224 if ($this->running_javascript()) {
921faad9 225 $gradeitemliteral = behat_context_helper::escape($gradeitem);
079b2e52
MG
226 $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
227 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
eb9ca848 228 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
079b2e52
MG
229 }
230 }
231
232 $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
eb9ca848 233 $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link"));
d06aec10 234 }
deb3d5ed
MG
235
236 /**
237 * Step allowing to test before-the-fix behaviour of the gradebook
238 *
239 * @Given /^gradebook calculations for the course "(?P<coursename_string>(?:[^"]|\\")*)" are frozen at version "(?P<version_string>(?:[^"]|\\")*)"$/
240 * @param string $coursename
241 * @param string $version
deb3d5ed
MG
242 */
243 public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) {
244 global $DB;
245 $courseid = $DB->get_field('course', 'id', array('shortname' => $coursename), MUST_EXIST);
246 set_config('gradebook_calculations_freeze_' . $courseid, $version);
247 }
b0842f61
MG
248
249 /**
250 * Navigates to the course gradebook and selects a specified item from the grade navigation tabs.
251 *
252 * Examples:
253 * - I go to "Setup > Gradebook setup" in the course gradebook
254 * - I go to "Scales" in the course gradebook
255 * - I go to "Letters > View" in the course gradebook
256 * - I go to "View > User report" in the course gradebook // for teachers
257 * - I go to "User report" in the course gradebook // for students
258 *
259 * @Given /^I go to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
260 * @param string $gradepath
261 */
262 public function i_go_to_in_the_course_gradebook($gradepath) {
263 $gradepath = preg_split('/\s*>\s*/', trim($gradepath));
264 if (count($gradepath) > 2) {
265 throw new DriverException('Grade path is too long (must have no more than two items separated with ">")');
266 }
267
268 // If we are not on one of the gradebook pages already, follow "Grades" link in the navigation block.
269 $xpath = '//div[contains(@class,\'grade-navigation\')]';
270 if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
271 $this->execute("behat_general::i_click_on_in_the", array(get_string('grades'), 'link',
272 get_string('pluginname', 'block_navigation'), 'block'));
273 }
274
275 // If the first row of the grade-navigation tabs does not have $gradepath[0] as active tab, click on it.
276 $link = '\'' . $this->escape($gradepath[0]) . '\'';
47b03b91 277 $xpathrow1 = $xpath . '//ul[1]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
b0842f61 278 if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow1)) {
1adf21fc
MG
279 $this->find('xpath', $xpath . '//ul[1]/li/a[text()=' . $link . ']')->click();
280 $this->wait_for_pending_js();
b0842f61
MG
281 }
282
283 if (isset($gradepath[1])) {
284 // If the second row of the grade-navigation tabs does not have $gradepath[1] as active tab, click on it.
285 $link = '\'' . $this->escape($gradepath[1]) . '\'';
47b03b91 286 $xpathrow2 = $xpath . '//ul[2]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
b0842f61 287 if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow2)) {
1adf21fc
MG
288 $this->find('xpath', $xpath . '//ul[2]/li/a[text()=' . $link . ']')->click();
289 $this->wait_for_pending_js();
b0842f61
MG
290 }
291 }
292 }
4f5eaa55 293}