2 // This file is part of Stack - http://stack.bham.ac.uk/
4 // Stack 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 // Stack 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 Stack. If not, see <http://www.gnu.org/licenses/>.
18 * Behat steps definitions for drag and drop onto image.
20 * @package gradereport_grader
22 * @copyright 2015 Oakland University
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,
31 Behat\Behat\Context\Step\Then,
32 Behat\Mink\Exception\ExpectationException as ExpectationException,
33 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
36 * Steps definitions related with the drag and drop onto image question type.
38 * @copyright 2015 Oakland University
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class behat_gradereport_grader extends behat_base {
43 * Click a given user grade cell.
45 * @Given /^I click on student "([^"]*)" for grade item "([^"]*)"$/
46 * @param string $student
47 * @param string $itemname
50 public function i_click_on_student_and_grade_item($student, $itemname) {
51 $xpath = $this->get_student_and_grade_cell_selector($student, $itemname);
53 return new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
57 * Remove focus for a grade value cell.
59 * @Given /^I click away from student "([^"]*)" and grade item "([^"]*)" value$/
60 * @param string $student
61 * @param string $itemname
64 public function i_click_away_from_student_and_grade_value($student, $itemname) {
65 $xpath = $this->get_student_and_grade_value_selector($student, $itemname);
67 return new Given('I take focus off "' . $this->escape($xpath) . '" "xpath_element"');
71 * Remove focus for a grade value cell.
73 * @Given /^I click away from student "([^"]*)" and grade item "([^"]*)" feedback$/
74 * @param string $student
75 * @param string $itemname
78 public function i_click_away_from_student_and_grade_feedback($student, $itemname) {
79 $xpath = $this->get_student_and_grade_feedback_selector($student, $itemname);
81 return new Given('I take focus off "' . $this->escape($xpath) . '" "xpath_element"');
85 * Checks grade values with or without a edit box.
87 * @Then /^the grade for "([^"]*)" in grade item "([^"]*)" should match "([^"]*)"$/
89 * @throws ElementNotFoundException
90 * @param string $student
91 * @param string $itemname
92 * @param string $value
95 public function the_grade_should_match($student, $itemname, $value) {
96 $xpath = $this->get_student_and_grade_value_selector($student, $itemname);
98 $gradefield = $this->getSession()->getPage()->find('xpath', $xpath);
99 if (!empty($gradefield)) {
101 $fieldtype = behat_field_manager::guess_field_type($gradefield, $this->getSession());
103 throw new Exception('Could not get field type for grade field "' . $itemname . '"');
105 $field = behat_field_manager::get_field_instance($fieldtype, $gradefield, $this->getSession());
106 if (!$field->matches($value)) {
107 $fieldvalue = $field->get_value();
108 throw new ExpectationException(
109 'The "' . $student . '" and "' . $itemname . '" grade is "' . $fieldvalue . '", "' . $value . '" expected' ,
114 // If there isn't a form field, just search for contents.
115 $valueliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($value);
117 $xpath = $this->get_student_and_grade_cell_selector($student, $itemname);
118 $xpath .= "[contains(normalize-space(.)," . $valueliteral . ")]";
120 $node = $this->getSession()->getDriver()->find($xpath);
122 $locatorexceptionmsg = 'Cell for "' . $student . '" and "' . $itemname . '" with value "' . $value . '"';
123 throw new ElementNotFoundException($this->getSession(), $locatorexceptionmsg, null, $xpath);
129 * Look for a grade editing field.
131 * @Then /^I should see a grade field for "([^"]*)" and grade item "([^"]*)"$/
132 * @param string $student
133 * @param string $itemname
136 public function i_should_see_grade_field($student, $itemname) {
137 $xpath = $this->get_student_and_grade_value_selector($student, $itemname);
139 return new Then('"' . $this->escape($xpath) . '" "xpath_element" should be visible');
143 * Look for a feedback editing field.
145 * @Then /^I should see a feedback field for "([^"]*)" and grade item "([^"]*)"$/
146 * @param string $student
147 * @param string $itemname
150 public function i_should_see_feedback_field($student, $itemname) {
151 $xpath = $this->get_student_and_grade_feedback_selector($student, $itemname);
153 return new Then('"' . $this->escape($xpath) . '" "xpath_element" should be visible');
157 * Look for a lack of the grade editing field.
159 * @Then /^I should not see a grade field for "([^"]*)" and grade item "([^"]*)"$/
160 * @param string $student
161 * @param string $itemname
164 public function i_should_not_see_grade_field($student, $itemname) {
165 $xpath = $this->get_student_and_grade_value_selector($student, $itemname);
167 return new Then('"' . $this->escape($xpath) . '" "xpath_element" should not exist');
171 * Look for a lack of the feedback editing field.
173 * @Then /^I should not see a feedback field for "([^"]*)" and grade item "([^"]*)"$/
174 * @param string $student
175 * @param string $itemname
178 public function i_should_not_see_feedback_field($student, $itemname) {
179 $xpath = $this->get_student_and_grade_feedback_selector($student, $itemname);
181 return new Then('"' . $this->escape($xpath) . '" "xpath_element" should not exist');
185 * Gets the user id from its name.
188 * @param string $name
191 protected function get_user_id($name) {
193 $names = explode(' ', $name);
195 if (!$id = $DB->get_field('user', 'id', array('firstname' => $names[0], 'lastname' => $names[1]))) {
196 throw new Exception('The specified user with username "' . $name . '" does not exist');
202 * Gets the grade item id from its name.
205 * @param string $itemname
208 protected function get_grade_item_id($itemname) {
211 if ($id = $DB->get_field('grade_items', 'id', array('itemname' => $itemname))) {
215 // The course total is a special case.
216 if ($itemname === "Course total") {
217 if (!$id = $DB->get_field('grade_items', 'id', array('itemtype' => 'course'))) {
218 throw new Exception('The specified grade_item with name "' . $itemname . '" does not exist');
223 // Find a category with the name.
224 if ($catid = $DB->get_field('grade_categories', 'id', array('fullname' => $itemname))) {
225 if ($id = $DB->get_field('grade_items', 'id', array('iteminstance' => $catid))) {
230 throw new Exception('The specified grade_item with name "' . $itemname . '" does not exist');
234 * Gets unique xpath selector for a student/grade item combo.
237 * @param string $student
238 * @param string $itemname
241 protected function get_student_and_grade_cell_selector($student, $itemname) {
242 $itemid = 'u' . $this->get_user_id($student) . 'i' . $this->get_grade_item_id($itemname);
243 return "//table[@id='user-grades']//td[@id='" . $itemid . "']";
247 * Gets xpath for a particular student/grade item grade value cell.
250 * @param string $student
251 * @param string $itemname
254 protected function get_student_and_grade_value_selector($student, $itemname) {
255 $cell = $this->get_student_and_grade_cell_selector($student, $itemname);
256 return $cell . "//*[contains(@id, 'grade_') or @name='ajaxgrade']";
260 * Gets xpath for a particular student/grade item feedback cell.
263 * @param string $student
264 * @param string $itemname
267 protected function get_student_and_grade_feedback_selector($student, $itemname) {
268 $cell = $this->get_student_and_grade_cell_selector($student, $itemname);
269 return $cell . "//input[contains(@id, 'feedback_') or @name='ajaxfeedback']";