MDL-50163 behat: Return subcontext from session
[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
7b05a0fe
DW
30use Behat\Behat\Context\Step\Given as Given;
31use Behat\Mink\Exception\ExpectationException as ExpectationException;
32use Behat\Mink\Exception\DriverException as DriverException;
33use Behat\Behat\Context\Step\When as When;
c4509f80 34
b1bc8cfc
DM
35/**
36 * Steps definitions to navigate through the navigation tree nodes.
37 *
38 * @package core
39 * @category test
40 * @copyright 2012 David MonllaĆ³
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class behat_navigation extends behat_base {
44
817d39b8
SH
45 /**
46 * Helper function to get a navigation nodes text element given its text from within the navigation block.
47 *
48 * This function finds the node with the given text from within the navigation block.
49 * It checks to make sure the node is visible, and then returns it.
50 *
51 * @param string $text
52 * @param bool $branch Set this true if you're only interested in the node if its a branch.
53 * @param null|bool $collapsed Set this to true or false if you want the node to either be collapsed or not.
54 * If its left as null then we don't worry about it.
55 * @param null|string|Exception|false $exception The exception to throw if the node is not found.
56 * @return \Behat\Mink\Element\NodeElement
57 */
58 protected function get_node_text_node($text, $branch = false, $collapsed = null, $exception = null) {
59 if ($exception === null) {
60 $exception = new ExpectationException('The "' . $text . '" node could not be found', $this->getSession());
61 } else if (is_string($exception)) {
62 $exception = new ExpectationException($exception, $this->getSession());
63 }
64
65 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
66 $hasblocktree = "[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]";
67 $hasbranch = "[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]";
37d39fe3 68 $hascollapsed = "li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ') or @data-expandable='1']";
487e68b4 69 $notcollapsed = "li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]";
817d39b8
SH
70 $match = "[normalize-space(.)={$nodetextliteral}]";
71
72 // Avoid problems with quotes.
73 $isbranch = ($branch) ? $hasbranch : '';
74 if ($collapsed === true) {
75 $iscollapsed = $hascollapsed;
76 } else if ($collapsed === false) {
77 $iscollapsed = $notcollapsed;
78 } else {
79 $iscollapsed = 'li';
80 }
81
487e68b4
SH
82 // First check root nodes.
83 $xpath = "//ul{$hasblocktree}/$hascollapsed/p{$isbranch}/span{$match}|";
84 // Next search for the node containing the text within a link.
85 $xpath .= "//ul{$hasblocktree}//{$notcollapsed}/ul/{$iscollapsed}/p{$isbranch}/a{$match}|";
86 // Finally search for the node containing the text within a span.
87 $xpath .= "//ul{$hasblocktree}//{$notcollapsed}/ul/{$iscollapsed}/p{$isbranch}/span{$match}";
817d39b8
SH
88
89 $node = $this->find('xpath', $xpath, $exception);
90 $this->ensure_node_is_visible($node);
91 return $node;
92 }
93
94 /**
95 * Returns true if the navigation node with the given text is expandable.
96 *
97 * @Given /^navigation node "([^"]*)" should be expandable$/
98 *
99 * @throws ExpectationException
100 * @param string $nodetext
101 * @return bool
102 */
103 public function navigation_node_should_be_expandable($nodetext) {
104 if (!$this->running_javascript()) {
105 // Nodes are only expandable when JavaScript is enabled.
106 return false;
107 }
108
109 $node = $this->get_node_text_node($nodetext, true);
110 $node = $node->getParent();
111 if ($node->hasAttribute('data-expandable') && $node->getAttribute('data-expandable')) {
112 return true;
113 }
114 throw new ExpectationException('The "' . $nodetext . '" node is not expandable', $this->getSession());
115 }
116
117 /**
118 * Returns true if the navigation node with the given text is not expandable.
119 *
120 * @Given /^navigation node "([^"]*)" should not be expandable$/
121 *
122 * @throws ExpectationException
123 * @param string $nodetext
124 * @return bool
125 */
126 public function navigation_node_should_not_be_expandable($nodetext) {
127 if (!$this->running_javascript()) {
128 // Nodes are only expandable when JavaScript is enabled.
129 return false;
130 }
131
132 $node = $this->get_node_text_node($nodetext);
133 $node = $node->getParent();
134 if ($node->hasAttribute('data-expandable') && $node->getAttribute('data-expandable')) {
135 throw new ExpectationException('The "' . $nodetext . '" node is expandable', $this->getSession());
136 }
137 return true;
138 }
139
7b05a0fe
DW
140 /**
141 * Click on an entry in the user menu.
142 * @Given /^I follow "(?P<nodetext_string>(?:[^"]|\\")*)" in the user menu$/
143 *
7b05a0fe
DW
144 * @param string $nodetext
145 * @return bool|void
146 */
147 public function i_follow_in_the_user_menu($nodetext) {
317fd6fe 148 $steps = array();
7b05a0fe 149
317fd6fe
AN
150 if ($this->running_javascript()) {
151 // The user menu must be expanded when JS is enabled.
152 $xpath = "//div[@class='usermenu']//a[contains(concat(' ', @class, ' '), ' toggle-display ')]";
153 $steps[] = new When('I click on "'.$xpath.'" "xpath_element"');
7b05a0fe
DW
154 }
155
317fd6fe
AN
156 // Now select the link.
157 // The CSS path is always present, with or without JS.
7b05a0fe 158 $csspath = ".usermenu [data-rel='menu-content']";
7b05a0fe
DW
159 $steps[] = new When('I click on "'.$nodetext.'" "link" in the "'.$csspath.'" "css_element"');
160
161 return $steps;
7b05a0fe
DW
162 }
163
b1bc8cfc
DM
164 /**
165 * Expands the selected node of the navigation tree that matches the text.
b1bc8cfc 166 * @Given /^I expand "(?P<nodetext_string>(?:[^"]|\\")*)" node$/
1f9ffbdb 167 *
c4509f80 168 * @throws ExpectationException
b1bc8cfc 169 * @param string $nodetext
817d39b8 170 * @return bool|void
b1bc8cfc
DM
171 */
172 public function i_expand_node($nodetext) {
173
e6d18d9d
DM
174 // This step is useless with Javascript disabled as Moodle auto expands
175 // all of tree's nodes; adding this because of scenarios that shares the
176 // same steps with and without Javascript enabled.
177 if (!$this->running_javascript()) {
6dfd8325
MG
178 if ($nodetext === get_string('administrationsite')) {
179 // Administration menu is not loaded by default any more. Click the link to expand.
180 return new Given('I click on "'.$nodetext.'" "link" in the "'.get_string('administration').'" "block"');
181 }
fb99ef1d 182 return true;
e6d18d9d
DM
183 }
184
817d39b8
SH
185 $node = $this->get_node_text_node($nodetext, true, true, 'The "' . $nodetext . '" node can not be expanded');
186 // Check if the node is a link AND a branch.
187 if (strtolower($node->getTagName()) === 'a') {
188 // We just want to expand the node, we don't want to follow it.
189 $node = $node->getParent();
190 }
b1bc8cfc
DM
191 $node->click();
192 }
193
c4509f80
DM
194 /**
195 * Collapses the selected node of the navigation tree that matches the text.
196 *
197 * @Given /^I collapse "(?P<nodetext_string>(?:[^"]|\\")*)" node$/
198 * @throws ExpectationException
199 * @param string $nodetext
817d39b8 200 * @return bool|void
c4509f80
DM
201 */
202 public function i_collapse_node($nodetext) {
203
204 // No collapsible nodes with non-JS browsers.
205 if (!$this->running_javascript()) {
fb99ef1d 206 return true;
c4509f80
DM
207 }
208
817d39b8
SH
209 $node = $this->get_node_text_node($nodetext, true, false, 'The "' . $nodetext . '" node can not be collapsed');
210 // Check if the node is a link AND a branch.
211 if (strtolower($node->getTagName()) === 'a') {
212 // We just want to expand the node, we don't want to follow it.
213 $node = $node->getParent();
214 }
c4509f80
DM
215 $node->click();
216 }
26fb3f5b 217
26fb3f5b 218 /**
1f631c7c 219 * Click link in navigation tree that matches the text in parentnode/s (seperated using greater-than character if more than one)
a542553d 220 *
26fb3f5b
RT
221 * @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" node in "(?P<parentnodes_string>(?:[^"]|\\")*)"$/
222 *
223 * @throws ExpectationException
224 * @param string $nodetext navigation node to click.
225 * @param string $parentnodes comma seperated list of parent nodes.
a542553d 226 * @return void
26fb3f5b
RT
227 */
228 public function i_navigate_to_node_in($nodetext, $parentnodes) {
a542553d
DM
229
230 // Site admin is different and needs special treatment.
231 $siteadminstr = get_string('administrationsite');
232
26fb3f5b 233 // Create array of all parentnodes.
1f631c7c 234 $parentnodes = array_map('trim', explode('>', $parentnodes));
26fb3f5b
RT
235 $countparentnode = count($parentnodes);
236
a542553d
DM
237 // If JS is disabled and Site administration is not expanded we
238 // should follow it, so all the lower-level nodes are available.
239 if (!$this->running_javascript()) {
240 if ($parentnodes[0] === $siteadminstr) {
241 // We don't know if there if Site admin is already expanded so
242 // don't wait, it is non-JS and we already waited for the DOM.
243 if ($siteadminlink = $this->getSession()->getPage()->find('named', array('link', "'" . $siteadminstr . "'"))) {
244 $siteadminlink->click();
245 }
246 }
247 }
248
26fb3f5b
RT
249 // Expand first node, and get it.
250 $node = $this->get_top_navigation_node($parentnodes[0]);
251
252 // Expand parent, sub-parent nodes in navigation if js enabled.
253 if ($node->hasClass('collapsed') || ($node->hasAttribute('data-loaded') && $node->getAttribute('data-loaded') == 0)) {
254 $xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]/span";
255 $nodetoexpand = $node->find('xpath', $xpath);
370e88d4
DM
256
257 if ($this->running_javascript()) {
258 $this->ensure_node_is_visible($nodetoexpand);
259 $nodetoexpand->click();
a542553d
DM
260
261 // Site administration node needs to be expanded.
262 if ($parentnodes[0] === $siteadminstr) {
263 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
264 }
370e88d4 265 }
26fb3f5b
RT
266 }
267
268 // If sub-parent nodes then get to the last one.
269 if ($countparentnode > 1) {
270 for ($i = 1; $i < $countparentnode; $i++) {
271 $node = $this->get_navigation_node($parentnodes[$i], $node);
272
273 // Keep expanding all sub-parents if js enabled.
274 if ($this->running_javascript()) {
275 $xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]";
276 if ($node->hasClass('collapsed')) {
277 $nodetoexpand = $node->find('xpath', $xpath);
370e88d4
DM
278 if ($this->running_javascript()) {
279 $this->ensure_node_is_visible($nodetoexpand);
280 $nodetoexpand->click();
281 }
26fb3f5b
RT
282 }
283 }
284 }
285 }
286
287 // Finally, click on requested node under navigation.
288 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
289 $xpath = "/ul/li/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]" .
290 "/a[normalize-space(.)=" . $nodetextliteral . "]";
291 $node = $node->find('xpath', $xpath);
292
370e88d4 293 // Throw exception if no node found.
26fb3f5b 294 if (!$node) {
a542553d 295 throw new ExpectationException('Navigation node "' . $nodetext . '" not found under "' .
ab7fa556 296 implode($parentnodes, ' > ') . '"', $this->getSession());
26fb3f5b 297 }
370e88d4
DM
298
299 if ($this->running_javascript()) {
300 $this->ensure_node_is_visible($node);
301 }
302
26fb3f5b
RT
303 $node->click();
304 }
a542553d
DM
305
306 /**
307 * Helper function to get top navigation node in tree.
308 *
309 * @throws ExpectationException if note not found.
310 * @param string $nodetext name of top navigation node in tree.
311 * @return NodeElement
312 */
313 protected function get_top_navigation_node($nodetext) {
314
315 // Avoid problems with quotes.
316 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
317 $exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession());
318
319 // First find in navigation block.
320 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]" .
321 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
322 "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
323 "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
324 "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
325 "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
326 "|" .
327 "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
328 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
329 "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
330 "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
331 "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
332 "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
333 "|" .
334 "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
335 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
336 "/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
337 "/span[normalize-space(.)=" . $nodetextliteral ."]]";
338 $node = $this->find('xpath', $xpath, $exception);
339
340 return $node;
341 }
342
343 /**
344 * Helper function to get sub-navigation node.
345 *
346 * @throws ExpectationException if note not found.
347 * @param string $nodetext node to find.
348 * @param NodeElement $parentnode parent navigation node.
349 * @return NodeElement.
350 */
351 protected function get_navigation_node($nodetext, $parentnode = null) {
352
353 // Avoid problems with quotes.
354 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
355
356 $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
357 "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
358 "/child::span[normalize-space(.)=" . $nodetextliteral ."]]";
359 $node = $parentnode->find('xpath', $xpath);
360 if (!$node) {
361 $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
362 "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
363 "/child::a[normalize-space(.)=" . $nodetextliteral ."]]";
364 $node = $parentnode->find('xpath', $xpath);
365 }
366
367 if (!$node) {
368 throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' .
369 $parentnode->getText() . '"', $this->getSession());
370 }
371 return $node;
372 }
b1bc8cfc 373}