MDL-55839 behat: Behat fixes for theme_noname
[moodle.git] / theme / noname / tests / behat / behat_theme_noname_behat_grade.php
CommitLineData
63e4df60
DW
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/**
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 */
23
24// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
25
26require_once(__DIR__ . '/../../../../grade/tests/behat/behat_grade.php');
27
28use Behat\Gherkin\Node\TableNode as TableNode;
29
30class behat_theme_noname_behat_grade extends behat_grade {
31
32 public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {
33
34 $gradeitem = behat_context_helper::escape($gradeitem);
35
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", array("//tr[contains(.,$gradeitem)]", "xpath_element"));
40 }
41 }
42
43 $savechanges = get_string('savechanges', 'grades');
44 $edit = behat_context_helper::escape(get_string('edit') . ' ');
45 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
46
47 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
48 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
49 $this->execute('behat_forms::press_button', $this->escape($savechanges));
50 }
51
52 public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
53
54 $gradeitem = behat_context_helper::escape($gradeitem);
55
56 if ($this->running_javascript()) {
57 $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]";
58 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
59 $this->execute("behat_action_menu::i_open_the_action_menu_in", array("//tr[contains(.,$gradeitem)]", "xpath_element"));
60 }
61 }
62
63 // Going to edit calculation.
64 $savechanges = get_string('savechanges', 'grades');
65 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
66 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
67 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
68
69 // Mapping names to idnumbers.
70 $datahash = $data->getRowsHash();
71 foreach ($datahash as $gradeitem => $idnumber) {
72 // This xpath looks for course, categories and items with the provided name.
73 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
74 $inputxpath ="//input[@class='idnumber'][" .
75 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
76 " or " .
77 "parent::li[@class='categoryitem' or @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
78 "]";
79 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
80 }
81
82 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
83 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
84 $this->execute('behat_forms::press_button', $savechanges);
85
86 }
87
88 public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {
89
90 $gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
91 $gradeitem = behat_context_helper::escape($gradeitem);
92
93 if ($this->running_javascript()) {
94 $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]";
95 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
96 $xpath = "//tr[contains(.,$gradecategorytotal)]";
97 $this->execute("behat_action_menu::i_open_the_action_menu_in", array($xpath, "xpath_element"));
98 }
99 }
100
101 // Going to edit calculation.
102 $savechanges = get_string('savechanges', 'grades');
103 $edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
104 $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
105 $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
106
107 // Mapping names to idnumbers.
108 $datahash = $data->getRowsHash();
109 foreach ($datahash as $gradeitem => $idnumber) {
110 // This xpath looks for course, categories and items with the provided name.
111 // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
112 $inputxpath = "//input[@class='idnumber'][" .
113 "parent::li[@class='item'][text()='" . $gradeitem . "']" .
114 " | " .
115 "parent::li[@class='categoryitem' | @class='courseitem']" .
116 "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
117 "]";
118 $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
119 }
120
121 $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
122
123 $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation));
124 $this->execute('behat_forms::press_button', $savechanges);
125 }
126
127 public function i_reset_weights_for_grade_category($gradeitem) {
128
129 $steps = array();
130
131 if ($this->running_javascript()) {
132 $gradeitemliteral = behat_context_helper::escape($gradeitem);
133 $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]";
134 if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
135 $xpath = "//tr[contains(.,$gradeitemliteral)]";
136 $this->execute("behat_action_menu::i_open_the_action_menu_in", array($xpath, "xpath_element"));
137 }
138 }
139
140 $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
141 $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link"));
142 }
143}