weekly release 3.2dev
[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
12a4770d 94 /**
53914e44
MN
95 * Sets a calculated manual grade item. Needs a table with item name - idnumber relation.
96 * The step requires you to be in the 'Gradebook setup' page.
12a4770d
DM
97 *
98 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
99 * @param string $calculation The calculation.
100 * @param string $gradeitem The grade item name.
101 * @param TableNode $TableNode The grade item name - idnumbers relation.
12a4770d
DM
102 */
103 public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
104
921faad9 105 $gradeitem = behat_context_helper::escape($gradeitem);
12a4770d
DM
106
107 if ($this->running_javascript()) {
108 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
109 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
eb9ca848 110 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
12a4770d
DM
111 }
112 }
113
114 // Going to edit calculation.
115 $savechanges = get_string('savechanges', 'grades');
921faad9 116 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
12a4770d 117 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848 118 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
12a4770d
DM
119
120 // Mapping names to idnumbers.
121 $datahash = $data->getRowsHash();
122 foreach ($datahash as $gradeitem => $idnumber) {
123 // This xpath looks for course, categories and items with the provided name.
124 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
125 $inputxpath ="//input[@class='idnumber'][" .
126 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
77f1778c
RT
127 " or " .
128 "parent::li[@class='categoryitem' or @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
12a4770d 129 "]";
eb9ca848 130 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
77f1778c 131 }
12a4770d 132
eb9ca848
RT
133 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
134 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
135 $this->execute('behat_forms::press_button', $savechanges);
12a4770d 136
12a4770d
DM
137 }
138
f44ffb72
AG
139 /**
140 * Sets a calculated manual grade category total. Needs a table with item name - idnumber relation.
53914e44 141 * The step requires you to be in the 'Gradebook setup' page.
f44ffb72
AG
142 *
143 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade category "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
144 * @param string $calculation The calculation.
145 * @param string $gradeitem The grade item name.
146 * @param TableNode $data The grade item name - idnumbers relation.
f44ffb72
AG
147 */
148 public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
149
921faad9
RT
150 $gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
151 $gradeitem = behat_context_helper::escape($gradeitem);
f44ffb72
AG
152
153 if ($this->running_javascript()) {
154 $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]" .
155 "//a[contains(@class,'toggle-display')]";
156 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
eb9ca848 157 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
f44ffb72
AG
158 }
159 }
160
161 // Going to edit calculation.
162 $savechanges = get_string('savechanges', 'grades');
921faad9 163 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
f44ffb72 164 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
eb9ca848 165 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
f44ffb72
AG
166
167 // Mapping names to idnumbers.
168 $datahash = $data->getRowsHash();
169 foreach ($datahash as $gradeitem => $idnumber) {
170 // This xpath looks for course, categories and items with the provided name.
171 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
172 $inputxpath = "//input[@class='idnumber'][" .
173 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
174 " | " .
175 "parent::li[@class='categoryitem' | @class='courseitem']" .
176 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
177 "]";
eb9ca848 178 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
f44ffb72
AG
179 }
180
eb9ca848 181 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
12a4770d 182
eb9ca848
RT
183 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
184 $this->execute('behat_forms::press_button', $savechanges);
12a4770d
DM
185 }
186
079b2e52
MG
187 /**
188 * Resets the weights for the grade category
189 *
190 * Teacher must be on the grade setup page.
191 *
192 * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
193 * @param $gradeitem
079b2e52
MG
194 */
195 public function i_reset_weights_for_grade_category($gradeitem) {
196
197 $steps = array();
198
199 if ($this->running_javascript()) {
921faad9 200 $gradeitemliteral = behat_context_helper::escape($gradeitem);
079b2e52
MG
201 $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
202 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
eb9ca848 203 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
079b2e52
MG
204 }
205 }
206
207 $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
eb9ca848 208 $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link"));
d06aec10 209 }
deb3d5ed
MG
210
211 /**
212 * Step allowing to test before-the-fix behaviour of the gradebook
213 *
214 * @Given /^gradebook calculations for the course "(?P<coursename_string>(?:[^"]|\\")*)" are frozen at version "(?P<version_string>(?:[^"]|\\")*)"$/
215 * @param string $coursename
216 * @param string $version
deb3d5ed
MG
217 */
218 public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) {
219 global $DB;
220 $courseid = $DB->get_field('course', 'id', array('shortname' => $coursename), MUST_EXIST);
221 set_config('gradebook_calculations_freeze_' . $courseid, $version);
222 }
4f5eaa55 223}