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