fe803eae3f69dfe602a73219632302ddf1e10bde
[moodle.git] / theme / boost / tests / behat / behat_theme_boost_behat_grade.php
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/>.
17 /**
18  * Behat grade related steps definitions overrides.
19  *
20  * @copyright  2016 Damyon Wiese
21  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
26 require_once(__DIR__ . '/../../../../grade/tests/behat/behat_grade.php');
28 use Behat\Gherkin\Node\TableNode as TableNode;
30 class behat_theme_boost_behat_grade extends behat_grade {
32     public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {
34         $gradeitem = behat_context_helper::escape($gradeitem);
36         if ($this->running_javascript()) {
37             $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]";
38             if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
39                 $this->execute("behat_action_menu::i_open_the_action_menu_in",
40                     array("//tr[contains(.,$gradeitem)]",
41                     "xpath_element"));
42             }
43         }
45         $savechanges = get_string('savechanges', 'grades');
46         $edit = behat_context_helper::escape(get_string('edit') . '  ');
47         $linkxpath = "//a[./*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') " .
48             "and starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
50         $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
51         $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
52         $this->execute('behat_forms::press_button', $this->escape($savechanges));
53     }
55     public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
57         $gradeitem = behat_context_helper::escape($gradeitem);
59         if ($this->running_javascript()) {
60             $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]";
61             if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
62                 $this->execute("behat_action_menu::i_open_the_action_menu_in",
63                     array("//tr[contains(.,$gradeitem)]",
64                     "xpath_element"));
65             }
66         }
68         // Going to edit calculation.
69         $savechanges = get_string('savechanges', 'grades');
70         $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
71         $linkxpath = "//a[./*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') " .
72             "and starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
73         $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
75         // Mapping names to idnumbers.
76         $datahash = $data->getRowsHash();
77         foreach ($datahash as $gradeitem => $idnumber) {
78             // This xpath looks for course, categories and items with the provided name.
79             // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
80             $inputxpath = "//input[@class='idnumber'][" .
81                 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
82                 " or " .
83                 "parent::li[@class='categoryitem' or @class='courseitem']" .
84                 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
85             "]";
86             $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
87         }
89         $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
90         $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
91         $this->execute('behat_forms::press_button', $savechanges);
93     }
95     public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
97         $gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
98         $gradeitem = behat_context_helper::escape($gradeitem);
100         if ($this->running_javascript()) {
101             $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]";
102             if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
103                 $xpath = "//tr[contains(.,$gradecategorytotal)]";
104                 $this->execute("behat_action_menu::i_open_the_action_menu_in", array($xpath, "xpath_element"));
105             }
106         }
108         // Going to edit calculation.
109         $savechanges = get_string('savechanges', 'grades');
110         $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
111         $linkxpath = "//a[./*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') " .
112             "and starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
113         $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
115         // Mapping names to idnumbers.
116         $datahash = $data->getRowsHash();
117         foreach ($datahash as $gradeitem => $idnumber) {
118             // This xpath looks for course, categories and items with the provided name.
119             // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
120             $inputxpath = "//input[@class='idnumber'][" .
121                 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
122                 " | " .
123                 "parent::li[@class='categoryitem' | @class='courseitem']" .
124                 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
125             "]";
126             $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
127         }
129         $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
131         $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
132         $this->execute('behat_forms::press_button', $savechanges);
133     }
135     public function i_reset_weights_for_grade_category($gradeitem) {
137         $steps = array();
139         if ($this->running_javascript()) {
140             $gradeitemliteral = behat_context_helper::escape($gradeitem);
141             $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]";
142             if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
143                 $xpath = "//tr[contains(.,$gradeitemliteral)]";
144                 $this->execute("behat_action_menu::i_open_the_action_menu_in", array($xpath, "xpath_element"));
145             }
146         }
148         $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
149         $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link"));
150     }
152     public function i_navigate_to_in_the_course_gradebook($gradepath) {
153         // If we are not on one of the gradebook pages already, follow "Grades" link in the navigation drawer.
154         $xpath = '//div[contains(@class,\'grade-navigation\')]';
155         if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
156             $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', get_string('grades'));
157         }
159         $this->select_in_gradebook_tabs($gradepath);
160     }