MDL-36606 gradereport: Correctly handle ungraded assessment ajax grading
[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
d06aec10
MG
30use Behat\Behat\Context\Step\Given as Given,
31 Behat\Gherkin\Node\TableNode as TableNode;
4f5eaa55
MN
32
33class behat_grade extends behat_base {
34
35 /**
36 * Enters a grade via the gradebook for a specific grade item and user when viewing the 'Grader report' with editing mode turned on.
37 *
38 * @Given /^I give the grade "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
39 * @param int $grade
40 * @param string $userfullname the user's fullname as returned by fullname()
41 * @param string $itemname
42 * @return Given
43 */
44 public function i_give_the_grade($grade, $userfullname, $itemname) {
45 $gradelabel = $userfullname . ' ' . $itemname;
46 $fieldstr = get_string('useractivitygrade', 'gradereport_grader', $gradelabel);
47
f083d614 48 return new Given('I set the field "' . $this->escape($fieldstr) . '" to "' . $grade . '"');
4f5eaa55 49 }
d06aec10
MG
50
51 /**
52 * Changes the settings of a grade item or category or the course.
53 *
54 * Teacher must be either on the grade setup page or on the Grader report page with editing mode turned on.
55 *
56 * @Given /^I set the following settings for grade item "(?P<grade_item_string>(?:[^"]|\\")*)":$/
57 * @param string $gradeitem
58 * @param TableNode $data
59 * @return Given[]
60 */
61 public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {
079b2e52
MG
62
63 $steps = array();
64 $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
65
66 if ($this->running_javascript()) {
67 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
68 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
69 $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
70 }
71 }
72
d06aec10
MG
73 $savechanges = get_string('savechanges', 'grades');
74 $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('edit') . ' ');
d06aec10 75 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
079b2e52
MG
76 $steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"');
77 $steps[] = new Given('I set the following fields to these values:', $data);
78 $steps[] = new Given('I press "' . $this->escape($savechanges) . '"');
79 return $steps;
80 }
81
12a4770d 82 /**
53914e44
MN
83 * Sets a calculated manual grade item. Needs a table with item name - idnumber relation.
84 * The step requires you to be in the 'Gradebook setup' page.
12a4770d
DM
85 *
86 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
87 * @param string $calculation The calculation.
88 * @param string $gradeitem The grade item name.
89 * @param TableNode $TableNode The grade item name - idnumbers relation.
90 * @return Given[]
91 */
92 public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
93
94 $steps = array();
95 $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
96
97 if ($this->running_javascript()) {
98 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
99 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
100 $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
101 }
102 }
103
104 // Going to edit calculation.
105 $savechanges = get_string('savechanges', 'grades');
106 $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('editcalculation', 'grades'));
107 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
108 $steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"');
109
110 // After adding id numbers we should wait until the page is reloaded.
77f1778c
RT
111 if ($this->running_javascript()) {
112 $steps[] = new Given('I wait until the page is ready');
113 }
12a4770d
DM
114
115 // Mapping names to idnumbers.
116 $datahash = $data->getRowsHash();
117 foreach ($datahash as $gradeitem => $idnumber) {
118 // This xpath looks for course, categories and items with the provided name.
119 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
120 $inputxpath ="//input[@class='idnumber'][" .
121 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
77f1778c
RT
122 " or " .
123 "parent::li[@class='categoryitem' or @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
12a4770d
DM
124 "]";
125 $steps[] = new Given('I set the field with xpath "' . $inputxpath . '" to "' . $idnumber . '"');
126 }
127
128 $steps[] = new Given('I press "' . get_string('addidnumbers', 'grades') . '"');
129
130 // After adding id numbers we should wait until the page is reloaded.
77f1778c
RT
131 if ($this->running_javascript()) {
132 $steps[] = new Given('I wait until the page is ready');
133 }
12a4770d
DM
134
135 $steps[] = new Given('I set the field "' . get_string('calculation', 'grades') . '" to "' . $calculation . '"');
136 $steps[] = new Given('I press "' . $savechanges . '"');
137
138 return $steps;
139 }
140
f44ffb72
AG
141 /**
142 * Sets a calculated manual grade category total. Needs a table with item name - idnumber relation.
53914e44 143 * The step requires you to be in the 'Gradebook setup' page.
f44ffb72
AG
144 *
145 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade category "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
146 * @param string $calculation The calculation.
147 * @param string $gradeitem The grade item name.
148 * @param TableNode $data The grade item name - idnumbers relation.
149 * @return Given[]
150 */
151 public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
152
153 $steps = array();
154 $gradecategorytotal = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem . ' total');
155 $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
156
157 if ($this->running_javascript()) {
158 $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]" .
159 "//a[contains(@class,'toggle-display')]";
160 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
161 $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
162 }
163 }
164
165 // Going to edit calculation.
166 $savechanges = get_string('savechanges', 'grades');
167 $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('editcalculation', 'grades'));
168 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
169 $steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"');
170
171 // After adding id numbers we should wait until the page is reloaded.
172 $steps[] = new Given('I wait until the page is ready');
173
174 // Mapping names to idnumbers.
175 $datahash = $data->getRowsHash();
176 foreach ($datahash as $gradeitem => $idnumber) {
177 // This xpath looks for course, categories and items with the provided name.
178 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
179 $inputxpath = "//input[@class='idnumber'][" .
180 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
181 " | " .
182 "parent::li[@class='categoryitem' | @class='courseitem']" .
183 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
184 "]";
185 $steps[] = new Given('I set the field with xpath "' . $inputxpath . '" to "' . $idnumber . '"');
186 }
187
188 $steps[] = new Given('I press "' . get_string('addidnumbers', 'grades') . '"');
189
190 // After adding id numbers we should wait until the page is reloaded.
12a4770d
DM
191 $steps[] = new Given('I wait until the page is ready');
192
193 $steps[] = new Given('I set the field "' . get_string('calculation', 'grades') . '" to "' . $calculation . '"');
194 $steps[] = new Given('I press "' . $savechanges . '"');
195
196 return $steps;
197 }
198
079b2e52
MG
199 /**
200 * Resets the weights for the grade category
201 *
202 * Teacher must be on the grade setup page.
203 *
204 * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
205 * @param $gradeitem
206 * @return array
207 */
208 public function i_reset_weights_for_grade_category($gradeitem) {
209
210 $steps = array();
211
212 if ($this->running_javascript()) {
213 $gradeitemliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
214 $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
215 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
216 $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
217 }
218 }
219
220 $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
221 $steps[] = new Given('I click on "' . $this->escape($linktext) . '" "link"');
222 return $steps;
d06aec10 223 }
deb3d5ed
MG
224
225 /**
226 * Step allowing to test before-the-fix behaviour of the gradebook
227 *
228 * @Given /^gradebook calculations for the course "(?P<coursename_string>(?:[^"]|\\")*)" are frozen at version "(?P<version_string>(?:[^"]|\\")*)"$/
229 * @param string $coursename
230 * @param string $version
231 * @return Given
232 */
233 public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) {
234 global $DB;
235 $courseid = $DB->get_field('course', 'id', array('shortname' => $coursename), MUST_EXIST);
236 set_config('gradebook_calculations_freeze_' . $courseid, $version);
237 }
4f5eaa55 238}