MDL-44440 behat: Log out compatible with clean
[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 }
b1bc8cfc 114}