weekly release 3.7dev
[moodle.git] / theme / bootstrapbase / tests / behat / behat_theme_bootstrapbase_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  * @package    theme_bootstrapbase
21  * @category   test
22  * @copyright  2016 Damyon Wiese
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../../../grade/tests/behat/behat_grade.php');
30 use Behat\Gherkin\Node\TableNode as TableNode;
32 /**
33  * Behat grade overrides.
34  *
35  * @package    theme_bootstrapbase
36  * @category   test
37  * @copyright  2016 Damyon Wiese
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class behat_theme_bootstrapbase_behat_grade extends behat_grade {
42     public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {
44         $gradeitem = behat_context_helper::escape($gradeitem);
46         if ($this->running_javascript()) {
47             $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
48             if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
49                 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
50             }
51         }
53         $savechanges = get_string('savechanges', 'grades');
54         $edit = behat_context_helper::escape(get_string('edit') . '  ');
55         $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
57         $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
58         $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
59         $this->execute('behat_forms::press_button', $this->escape($savechanges));
60     }
62     public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
64         $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         }
73         // Going to edit calculation.
74         $savechanges = get_string('savechanges', 'grades');
75         $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
76         $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
77         $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
79         // Mapping names to idnumbers.
80         $datahash = $data->getRowsHash();
81         foreach ($datahash as $gradeitem => $idnumber) {
82             // This xpath looks for course, categories and items with the provided name.
83             // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
84             $inputxpath = "//input[@class='idnumber'][" .
85                     "parent::li[@class='item'][text()='" . $gradeitem . "']" .
86                     " or " .
87                     "parent::li[@class='categoryitem' or @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" .
88                     $gradeitem . "')]]";
89             $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
90         }
92         $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
93         $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
94         $this->execute('behat_forms::press_button', $savechanges);
96     }
98     public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
100         $gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
101         $gradeitem = behat_context_helper::escape($gradeitem);
103         if ($this->running_javascript()) {
104             $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]" .
105                     "//a[contains(@class,'toggle-display')]";
106             if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
107                 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
108             }
109         }
111         // Going to edit calculation.
112         $savechanges = get_string('savechanges', 'grades');
113         $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
114         $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
115         $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
117         // Mapping names to idnumbers.
118         $datahash = $data->getRowsHash();
119         foreach ($datahash as $gradeitem => $idnumber) {
120             // This xpath looks for course, categories and items with the provided name.
121             // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
122             $inputxpath = "//input[@class='idnumber'][" .
123                     "parent::li[@class='item'][text()='" . $gradeitem . "']" .
124                     " | " .
125                     "parent::li[@class='categoryitem' | @class='courseitem']" .
126                     "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
127                     "]";
128             $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
129         }
131         $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     }
137     public function i_reset_weights_for_grade_category($gradeitem) {
139         $steps = array();
141         if ($this->running_javascript()) {
142             $gradeitemliteral = behat_context_helper::escape($gradeitem);
143             $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]" .
144                     "//a[contains(@class,'toggle-display')]";
145             if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
146                 $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
147             }
148         }
150         $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
151         $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link"));
152     }
154     public function i_navigate_to_in_the_course_gradebook($gradepath) {
155         // If we are not on one of the gradebook pages already, follow "Grades" link in the navigation block.
156         $xpath = '//div[contains(@class,\'grade-navigation\')]';
157         if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
158             $this->execute("behat_general::i_click_on_in_the", array(get_string('grades'), 'link',
159                     get_string('pluginname', 'block_navigation'), 'block'));
160         }
162         $this->select_in_gradebook_tabs($gradepath);
163     }