PageRenderTime 42ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/tests/behat/behat_grade.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 302 lines | 140 code | 43 blank | 119 comment | 15 complexity | 4ffbe992e8b42f8bcc23245f0f191772 MD5 | raw file
  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. * Behat grade related steps definitions.
  18. *
  19. * @package core_grades
  20. * @category test
  21. * @copyright 2014 Mark Nelson <markn@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  25. require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
  26. use Behat\Gherkin\Node\TableNode as TableNode;
  27. class behat_grade extends behat_base {
  28. /**
  29. * Enters a grade via the gradebook for a specific grade item and user when viewing the 'Grader report' with editing mode turned on.
  30. *
  31. * @Given /^I give the grade "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
  32. * @param int $grade
  33. * @param string $userfullname the user's fullname as returned by fullname()
  34. * @param string $itemname
  35. */
  36. public function i_give_the_grade($grade, $userfullname, $itemname) {
  37. $gradelabel = $userfullname . ' ' . $itemname;
  38. $fieldstr = get_string('useractivitygrade', 'gradereport_grader', $gradelabel);
  39. $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $grade));
  40. }
  41. /**
  42. * Enters a quick feedback via the gradebook for a specific grade item and user when viewing
  43. * the 'Grader report' with editing mode turned on.
  44. *
  45. * @Given /^I give the feedback "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
  46. * @param string $feedback
  47. * @param string $userfullname the user's fullname as returned by fullname()
  48. * @param string $itemname
  49. */
  50. public function i_give_the_feedback($feedback, $userfullname, $itemname) {
  51. $gradelabel = $userfullname . ' ' . $itemname;
  52. $fieldstr = get_string('useractivityfeedback', 'gradereport_grader', $gradelabel);
  53. $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $this->escape($feedback)));
  54. }
  55. /**
  56. * Changes the settings of a grade item or category or the course.
  57. *
  58. * Teacher must be either on the grade setup page or on the Grader report page with editing mode turned on.
  59. *
  60. * @Given /^I set the following settings for grade item "(?P<grade_item_string>(?:[^"]|\\")*)":$/
  61. * @param string $gradeitem
  62. * @param TableNode $data
  63. */
  64. public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {
  65. $gradeitem = behat_context_helper::escape($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. $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
  70. }
  71. }
  72. $savechanges = get_string('savechanges', 'grades');
  73. $edit = behat_context_helper::escape(get_string('edit') . ' ');
  74. $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
  75. $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
  76. $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
  77. $this->execute('behat_forms::press_button', $this->escape($savechanges));
  78. }
  79. /**
  80. * Hids a grade item or category.
  81. *
  82. * Teacher must be on the grade setup page.
  83. *
  84. * @Given /^I hide the grade item "(?P<grade_item_string>(?:[^"]|\\")*)"$/
  85. * @param string $gradeitem
  86. */
  87. public function i_hide_the_grade_item($gradeitem) {
  88. $gradeitem = behat_context_helper::escape($gradeitem);
  89. if ($this->running_javascript()) {
  90. $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
  91. if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
  92. $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
  93. }
  94. }
  95. $this->execute("behat_general::i_click_on_in_the", array(get_string('hide'), 'link',
  96. "//tr[descendant::*[text() = " . $this->escape($gradeitem) . "]]", 'xpath_element'));
  97. }
  98. /**
  99. * Sets a calculated manual grade item. Needs a table with item name - idnumber relation.
  100. * The step requires you to be in the 'Gradebook setup' page.
  101. *
  102. * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
  103. * @param string $calculation The calculation.
  104. * @param string $gradeitem The grade item name.
  105. * @param TableNode $TableNode The grade item name - idnumbers relation.
  106. */
  107. public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
  108. $gradeitem = behat_context_helper::escape($gradeitem);
  109. if ($this->running_javascript()) {
  110. $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
  111. if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
  112. $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
  113. }
  114. }
  115. // Going to edit calculation.
  116. $savechanges = get_string('savechanges', 'grades');
  117. $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
  118. $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
  119. $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
  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 . "']" .
  127. " or " .
  128. "parent::li[@class='categoryitem' or @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
  129. "]";
  130. $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
  131. }
  132. $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
  133. $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
  134. $this->execute('behat_forms::press_button', $savechanges);
  135. }
  136. /**
  137. * Sets a calculated manual grade category total. Needs a table with item name - idnumber relation.
  138. * The step requires you to be in the 'Gradebook setup' page.
  139. *
  140. * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade category "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
  141. * @param string $calculation The calculation.
  142. * @param string $gradeitem The grade item name.
  143. * @param TableNode $data The grade item name - idnumbers relation.
  144. */
  145. public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
  146. $gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
  147. $gradeitem = behat_context_helper::escape($gradeitem);
  148. if ($this->running_javascript()) {
  149. $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]" .
  150. "//a[contains(@class,'toggle-display')]";
  151. if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
  152. $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
  153. }
  154. }
  155. // Going to edit calculation.
  156. $savechanges = get_string('savechanges', 'grades');
  157. $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
  158. $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
  159. $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
  160. // Mapping names to idnumbers.
  161. $datahash = $data->getRowsHash();
  162. foreach ($datahash as $gradeitem => $idnumber) {
  163. // This xpath looks for course, categories and items with the provided name.
  164. // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
  165. $inputxpath = "//input[@class='idnumber'][" .
  166. "parent::li[@class='item'][text()='" . $gradeitem . "']" .
  167. " | " .
  168. "parent::li[@class='categoryitem' | @class='courseitem']" .
  169. "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
  170. "]";
  171. $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
  172. }
  173. $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
  174. $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
  175. $this->execute('behat_forms::press_button', $savechanges);
  176. }
  177. /**
  178. * Resets the weights for the grade category
  179. *
  180. * Teacher must be on the grade setup page.
  181. *
  182. * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
  183. * @param $gradeitem
  184. */
  185. public function i_reset_weights_for_grade_category($gradeitem) {
  186. $steps = array();
  187. if ($this->running_javascript()) {
  188. $gradeitemliteral = behat_context_helper::escape($gradeitem);
  189. $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
  190. if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
  191. $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
  192. }
  193. }
  194. $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
  195. $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link"));
  196. }
  197. /**
  198. * Step allowing to test before-the-fix behaviour of the gradebook
  199. *
  200. * @Given /^gradebook calculations for the course "(?P<coursename_string>(?:[^"]|\\")*)" are frozen at version "(?P<version_string>(?:[^"]|\\")*)"$/
  201. * @param string $coursename
  202. * @param string $version
  203. */
  204. public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) {
  205. global $DB;
  206. $courseid = $DB->get_field('course', 'id', array('shortname' => $coursename), MUST_EXIST);
  207. set_config('gradebook_calculations_freeze_' . $courseid, $version);
  208. }
  209. /**
  210. * Select the tab in the gradebook. We must be on one of the gradebook pages already.
  211. *
  212. * @param string $gradepath examples: "View > User report", "Letters > View", "Scales"
  213. */
  214. protected function select_in_gradebook_tabs($gradepath) {
  215. $gradepath = preg_split('/\s*>\s*/', trim($gradepath));
  216. if (count($gradepath) > 2) {
  217. throw new coding_exception('Grade path is too long (must have no more than two items separated with ">")');
  218. }
  219. $xpath = '//div[contains(@class,\'grade-navigation\')]';
  220. // If the first row of the grade-navigation tabs does not have $gradepath[0] as active tab, click on it.
  221. $link = '\'' . $this->escape($gradepath[0]) . '\'';
  222. $xpathrow1 = $xpath . '//ul[1]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
  223. if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow1)) {
  224. $this->find('xpath', $xpath . '//ul[1]/li/a[text()=' . $link . ']')->click();
  225. $this->wait_for_pending_js();
  226. }
  227. if (isset($gradepath[1])) {
  228. // If the second row of the grade-navigation tabs does not have $gradepath[1] as active tab, click on it.
  229. $link = '\'' . $this->escape($gradepath[1]) . '\'';
  230. $xpathrow2 = $xpath . '//ul[2]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
  231. if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow2)) {
  232. $this->find('xpath', $xpath . '//ul[2]/li/a[text()=' . $link . ']')->click();
  233. $this->wait_for_pending_js();
  234. }
  235. }
  236. }
  237. /**
  238. * Navigates to the course gradebook and selects a specified item from the grade navigation tabs.
  239. *
  240. * Examples:
  241. * - I navigate to "Setup > Gradebook setup" in the course gradebook
  242. * - I navigate to "Scales" in the course gradebook
  243. * - I navigate to "Letters > View" in the course gradebook
  244. * - I navigate to "View > User report" in the course gradebook // for teachers
  245. * - I navigate to "User report" in the course gradebook // for students
  246. *
  247. * @Given /^I navigate to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
  248. * @param string $gradepath
  249. */
  250. public function i_navigate_to_in_the_course_gradebook($gradepath) {
  251. // If we are not on one of the gradebook pages already, follow "Grades" link in the navigation block.
  252. $xpath = '//div[contains(@class,\'grade-navigation\')]';
  253. if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
  254. $this->execute("behat_general::i_click_on_in_the", array(get_string('grades'), 'link',
  255. get_string('pluginname', 'block_navigation'), 'block'));
  256. }
  257. $this->select_in_gradebook_tabs($gradepath);
  258. }
  259. }