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 steps definitions for block site main menu
19 *
20 * @package    block_site_main_menu
21 * @category   test
22 * @copyright  2016 Marina Glancy
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__ . '/../../../../lib/behat/behat_base.php');
29
30use Behat\Mink\Exception\ExpectationException as ExpectationException,
31    Behat\Mink\Exception\DriverException as DriverException,
32    Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
33
34/**
35 * Behat steps definitions for block site main menu
36 *
37 * @package    block_site_main_menu
38 * @category   test
39 * @copyright  2016 Marina Glancy
40 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class behat_block_site_main_menu extends behat_base {
43
44    /**
45     * Returns the DOM node of the activity in the site menu block
46     *
47     * @throws ElementNotFoundException Thrown by behat_base::find
48     * @param string $activityname The activity name
49     * @return NodeElement
50     */
51    protected function get_site_menu_activity_node($activityname) {
52        $activityname = behat_context_helper::escape($activityname);
53        $xpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]//li[contains(., $activityname)]";
54
55        return $this->find('xpath', $xpath);
56    }
57
58    /**
59     * Checks that the specified activity's action menu contains an item.
60     *
61     * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should have "(?P<icon_name_string>(?:[^"]|\\")*)" editing icon$/
62     * @param string $activityname
63     * @param string $iconname
64     */
65    public function activity_in_site_main_menu_block_should_have_editing_icon($activityname, $iconname) {
66        $activitynode = $this->get_site_menu_activity_node($activityname);
67
68        $notfoundexception = new ExpectationException('"' . $activityname . '" doesn\'t have a "' .
69            $iconname . '" editing icon', $this->getSession());
70        $this->find('named_partial', array('link', $iconname), $notfoundexception, $activitynode);
71    }
72
73    /**
74     * Checks that the specified activity's action menu contains an item.
75     *
76     * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should not have "(?P<icon_name_string>(?:[^"]|\\")*)" editing icon$/
77     * @param string $activityname
78     * @param string $iconname
79     */
80    public function activity_in_site_main_menu_block_should_not_have_editing_icon($activityname, $iconname) {
81        $activitynode = $this->get_site_menu_activity_node($activityname);
82
83        try {
84            $this->find('named_partial', array('link', $iconname), false, $activitynode);
85            throw new ExpectationException('"' . $activityname . '" has a "' . $iconname .
86                '" editing icon when it should not', $this->getSession());
87        } catch (ElementNotFoundException $e) {
88            // This is good, the menu item should not be there.
89        }
90    }
91
92    /**
93     * Clicks on the specified element of the activity. You should be in the course page with editing mode turned on.
94     *
95     * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block$/
96     * @param string $element
97     * @param string $selectortype
98     * @param string $activityname
99     */
100    public function i_click_on_in_the_activity_in_site_main_menu_block($element, $selectortype, $activityname) {
101        $element = $this->get_site_menu_activity_element($element, $selectortype, $activityname);
102        $element->click();
103    }
104
105    /**
106     * Clicks on the specified element inside the activity container.
107     *
108     * @throws ElementNotFoundException
109     * @param string $element
110     * @param string $selectortype
111     * @param string $activityname
112     * @return NodeElement
113     */
114    protected function get_site_menu_activity_element($element, $selectortype, $activityname) {
115        $activitynode = $this->get_site_menu_activity_node($activityname);
116
117        $exception = new ElementNotFoundException($this->getSession(), "'{$element}' '{$selectortype}' in '${activityname}'");
118        return $this->find($selectortype, $element, $exception, $activitynode);
119    }
120
121    /**
122     * Checks that the specified activity is hidden.
123     *
124     * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should be hidden$/
125     * @param string $activityname
126     */
127    public function activity_in_site_main_menu_block_should_be_hidden($activityname) {
128        $this->get_site_menu_activity_element("a.dimmed", "css_element", $activityname);
129    }
130
131    /**
132     * Checks that the specified activity is hidden.
133     *
134     * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should be available but hidden from course page$/
135     * @param string $activityname
136     */
137    public function activity_in_site_main_menu_block_should_be_available_but_hidden_from_course_page($activityname) {
138        $this->get_site_menu_activity_element("a.stealth", "css_element", $activityname);
139    }
140
141    /**
142     * Opens an activity actions menu if it is not already opened.
143     *
144     * @Given /^I open "(?P<activity_name_string>(?:[^"]|\\")*)" actions menu in site main menu block$/
145     * @throws DriverException The step is not available when Javascript is disabled
146     * @param string $activityname
147     */
148    public function i_open_actions_menu_in_site_main_menu_block($activityname) {
149        $activityname = behat_context_helper::escape($activityname);
150        $xpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]//li[contains(., $activityname)]";
151        $this->execute('behat_action_menu::i_open_the_action_menu_in', [$xpath, 'xpath_element']);
152    }
153
154    /**
155     * Return the list of partial named selectors.
156     *
157     * @return array
158     */
159    public static function get_partial_named_selectors(): array {
160        return [
161            new behat_component_named_selector('Activity', [
162                "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]//li[contains(., %locator%)]"
163            ]),
164        ];
165    }
166}
167