weekly on-sync release 2.8dev
[moodle.git] / lib / tests / behat / behat_navigation.php
CommitLineData
b1bc8cfc 1<?php
b1bc8cfc
DM
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 * Navigation steps definitions.
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../behat/behat_base.php');
29
c4509f80
DM
30use Behat\Behat\Context\Step\Given as Given,
31 Behat\Mink\Exception\ExpectationException as ExpectationException;
32
b1bc8cfc
DM
33/**
34 * Steps definitions to navigate through the navigation tree nodes.
35 *
36 * @package core
37 * @category test
38 * @copyright 2012 David MonllaĆ³
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class behat_navigation extends behat_base {
42
43 /**
44 * Expands the selected node of the navigation tree that matches the text.
b1bc8cfc 45 * @Given /^I expand "(?P<nodetext_string>(?:[^"]|\\")*)" node$/
1f9ffbdb 46 *
c4509f80 47 * @throws ExpectationException
b1bc8cfc
DM
48 * @param string $nodetext
49 */
50 public function i_expand_node($nodetext) {
51
e6d18d9d
DM
52 // This step is useless with Javascript disabled as Moodle auto expands
53 // all of tree's nodes; adding this because of scenarios that shares the
54 // same steps with and without Javascript enabled.
55 if (!$this->running_javascript()) {
6dfd8325
MG
56 if ($nodetext === get_string('administrationsite')) {
57 // Administration menu is not loaded by default any more. Click the link to expand.
58 return new Given('I click on "'.$nodetext.'" "link" in the "'.get_string('administration').'" "block"');
59 }
fb99ef1d 60 return true;
e6d18d9d
DM
61 }
62
38976081
DM
63 // Avoid problems with quotes.
64 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
65
00ea74cb 66 $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
c4509f80 67 "/child::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ')]" .
38976081
DM
68 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
69 "/child::span[normalize-space(.)=$nodetextliteral]" .
00ea74cb
DM
70 "|" .
71 "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
38976081
DM
72 "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" .
73 "/descendant::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ')]" .
74 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
75 "/child::span[normalize-space(.)=$nodetextliteral]";
b1bc8cfc 76
c4509f80
DM
77 $exception = new ExpectationException('The "' . $nodetext . '" node can not be expanded', $this->getSession());
78 $node = $this->find('xpath', $xpath, $exception);
c1faf86b 79 $this->ensure_node_is_visible($node);
b1bc8cfc
DM
80 $node->click();
81 }
82
c4509f80
DM
83 /**
84 * Collapses the selected node of the navigation tree that matches the text.
85 *
86 * @Given /^I collapse "(?P<nodetext_string>(?:[^"]|\\")*)" node$/
87 * @throws ExpectationException
88 * @param string $nodetext
89 */
90 public function i_collapse_node($nodetext) {
91
92 // No collapsible nodes with non-JS browsers.
93 if (!$this->running_javascript()) {
fb99ef1d 94 return true;
c4509f80
DM
95 }
96
38976081
DM
97 // Avoid problems with quotes.
98 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
99
c4509f80
DM
100 $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
101 "/child::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" .
38976081
DM
102 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
103 "/child::span[normalize-space(.)=$nodetextliteral]" .
c4509f80
DM
104 "|" .
105 "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
38976081
DM
106 "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" .
107 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
108 "/child::span[normalize-space(.)=$nodetextliteral]";
c4509f80
DM
109
110 $exception = new ExpectationException('The "' . $nodetext . '" node can not be collapsed', $this->getSession());
111 $node = $this->find('xpath', $xpath, $exception);
112 $node->click();
113 }
26fb3f5b 114
26fb3f5b 115 /**
1f631c7c 116 * Click link in navigation tree that matches the text in parentnode/s (seperated using greater-than character if more than one)
a542553d 117 *
26fb3f5b
RT
118 * @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" node in "(?P<parentnodes_string>(?:[^"]|\\")*)"$/
119 *
120 * @throws ExpectationException
121 * @param string $nodetext navigation node to click.
122 * @param string $parentnodes comma seperated list of parent nodes.
a542553d 123 * @return void
26fb3f5b
RT
124 */
125 public function i_navigate_to_node_in($nodetext, $parentnodes) {
a542553d
DM
126
127 // Site admin is different and needs special treatment.
128 $siteadminstr = get_string('administrationsite');
129
26fb3f5b 130 // Create array of all parentnodes.
1f631c7c 131 $parentnodes = array_map('trim', explode('>', $parentnodes));
26fb3f5b
RT
132 $countparentnode = count($parentnodes);
133
a542553d
DM
134 // If JS is disabled and Site administration is not expanded we
135 // should follow it, so all the lower-level nodes are available.
136 if (!$this->running_javascript()) {
137 if ($parentnodes[0] === $siteadminstr) {
138 // We don't know if there if Site admin is already expanded so
139 // don't wait, it is non-JS and we already waited for the DOM.
140 if ($siteadminlink = $this->getSession()->getPage()->find('named', array('link', "'" . $siteadminstr . "'"))) {
141 $siteadminlink->click();
142 }
143 }
144 }
145
26fb3f5b
RT
146 // Expand first node, and get it.
147 $node = $this->get_top_navigation_node($parentnodes[0]);
148
149 // Expand parent, sub-parent nodes in navigation if js enabled.
150 if ($node->hasClass('collapsed') || ($node->hasAttribute('data-loaded') && $node->getAttribute('data-loaded') == 0)) {
151 $xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]/span";
152 $nodetoexpand = $node->find('xpath', $xpath);
370e88d4
DM
153
154 if ($this->running_javascript()) {
155 $this->ensure_node_is_visible($nodetoexpand);
156 $nodetoexpand->click();
a542553d
DM
157
158 // Site administration node needs to be expanded.
159 if ($parentnodes[0] === $siteadminstr) {
160 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
161 }
370e88d4 162 }
26fb3f5b
RT
163 }
164
165 // If sub-parent nodes then get to the last one.
166 if ($countparentnode > 1) {
167 for ($i = 1; $i < $countparentnode; $i++) {
168 $node = $this->get_navigation_node($parentnodes[$i], $node);
169
170 // Keep expanding all sub-parents if js enabled.
171 if ($this->running_javascript()) {
172 $xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]";
173 if ($node->hasClass('collapsed')) {
174 $nodetoexpand = $node->find('xpath', $xpath);
370e88d4
DM
175 if ($this->running_javascript()) {
176 $this->ensure_node_is_visible($nodetoexpand);
177 $nodetoexpand->click();
178 }
26fb3f5b
RT
179 }
180 }
181 }
182 }
183
184 // Finally, click on requested node under navigation.
185 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
186 $xpath = "/ul/li/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]" .
187 "/a[normalize-space(.)=" . $nodetextliteral . "]";
188 $node = $node->find('xpath', $xpath);
189
370e88d4 190 // Throw exception if no node found.
26fb3f5b 191 if (!$node) {
a542553d
DM
192 throw new ExpectationException('Navigation node "' . $nodetext . '" not found under "' .
193 $parentnodes . '"', $this->getSession());
26fb3f5b 194 }
370e88d4
DM
195
196 if ($this->running_javascript()) {
197 $this->ensure_node_is_visible($node);
198 }
199
26fb3f5b
RT
200 $node->click();
201 }
a542553d
DM
202
203 /**
204 * Helper function to get top navigation node in tree.
205 *
206 * @throws ExpectationException if note not found.
207 * @param string $nodetext name of top navigation node in tree.
208 * @return NodeElement
209 */
210 protected function get_top_navigation_node($nodetext) {
211
212 // Avoid problems with quotes.
213 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
214 $exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession());
215
216 // First find in navigation block.
217 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]" .
218 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
219 "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
220 "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
221 "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
222 "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
223 "|" .
224 "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
225 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
226 "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
227 "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
228 "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
229 "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
230 "|" .
231 "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
232 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
233 "/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
234 "/span[normalize-space(.)=" . $nodetextliteral ."]]";
235 $node = $this->find('xpath', $xpath, $exception);
236
237 return $node;
238 }
239
240 /**
241 * Helper function to get sub-navigation node.
242 *
243 * @throws ExpectationException if note not found.
244 * @param string $nodetext node to find.
245 * @param NodeElement $parentnode parent navigation node.
246 * @return NodeElement.
247 */
248 protected function get_navigation_node($nodetext, $parentnode = null) {
249
250 // Avoid problems with quotes.
251 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
252
253 $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
254 "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
255 "/child::span[normalize-space(.)=" . $nodetextliteral ."]]";
256 $node = $parentnode->find('xpath', $xpath);
257 if (!$node) {
258 $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
259 "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
260 "/child::a[normalize-space(.)=" . $nodetextliteral ."]]";
261 $node = $parentnode->find('xpath', $xpath);
262 }
263
264 if (!$node) {
265 throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' .
266 $parentnode->getText() . '"', $this->getSession());
267 }
268 return $node;
269 }
b1bc8cfc 270}