2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Behat grade related steps definitions.
20 * @package core_grades
22 * @copyright 2014 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
30 use Behat\Behat\Context\Step\Given as Given,
31 Behat\Gherkin\Node\TableNode as TableNode;
33 class behat_grade extends behat_base {
36 * Enters a grade via the gradebook for a specific grade item and user when viewing the 'Grader report' with editing mode turned on.
38 * @Given /^I give the grade "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
40 * @param string $userfullname the user's fullname as returned by fullname()
41 * @param string $itemname
44 public function i_give_the_grade($grade, $userfullname, $itemname) {
45 $gradelabel = $userfullname . ' ' . $itemname;
46 $fieldstr = get_string('useractivitygrade', 'gradereport_grader', $gradelabel);
48 return new Given('I set the field "' . $this->escape($fieldstr) . '" to "' . $grade . '"');
52 * Changes the settings of a grade item or category or the course.
54 * Teacher must be either on the grade setup page or on the Grader report page with editing mode turned on.
56 * @Given /^I set the following settings for grade item "(?P<grade_item_string>(?:[^"]|\\")*)":$/
57 * @param string $gradeitem
58 * @param TableNode $data
61 public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {
64 $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
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"');
73 $savechanges = get_string('savechanges', 'grades');
74 $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('edit') . ' ');
75 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
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) . '"');
83 * Sets a calculated manual grade item. Needs a table with item name - idnumber relation. The step requires you to be in categories and items page.
85 * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
86 * @param string $calculation The calculation.
87 * @param string $gradeitem The grade item name.
88 * @param TableNode $TableNode The grade item name - idnumbers relation.
91 public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
94 $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
96 if ($this->running_javascript()) {
97 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
98 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
99 $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
103 // Going to edit calculation.
104 $savechanges = get_string('savechanges', 'grades');
105 $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('editcalculation', 'grades'));
106 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
107 $steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"');
109 // After adding id numbers we should wait until the page is reloaded.
110 $steps[] = new Given('I wait until the page is ready');
112 // Mapping names to idnumbers.
113 $datahash = $data->getRowsHash();
114 foreach ($datahash as $gradeitem => $idnumber) {
115 // This xpath looks for course, categories and items with the provided name.
116 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
117 $inputxpath ="//input[@class='idnumber'][" .
118 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
120 "parent::li[@class='categoryitem' | @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
122 $steps[] = new Given('I set the field with xpath "' . $inputxpath . '" to "' . $idnumber . '"');
125 $steps[] = new Given('I press "' . get_string('addidnumbers', 'grades') . '"');
127 // After adding id numbers we should wait until the page is reloaded.
128 $steps[] = new Given('I wait until the page is ready');
130 $steps[] = new Given('I set the field "' . get_string('calculation', 'grades') . '" to "' . $calculation . '"');
131 $steps[] = new Given('I press "' . $savechanges . '"');
137 * Resets the weights for the grade category
139 * Teacher must be on the grade setup page.
141 * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
145 public function i_reset_weights_for_grade_category($gradeitem) {
149 if ($this->running_javascript()) {
150 $gradeitemliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
151 $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
152 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
153 $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
157 $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
158 $steps[] = new Given('I click on "' . $this->escape($linktext) . '" "link"');