Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Navigation step definition overrides for the Classic theme.
19
 *
20
 * @package    theme_classic
21
 * @category   test
22
 * @copyright  2019 Michael Hawkins
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
 
28
require_once(__DIR__ . '/../../../../lib/tests/behat/behat_navigation.php');
29
 
30
use Behat\Mink\Exception\ExpectationException as ExpectationException;
31
use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
1441 ariadna 32
use Behat\Mink\Exception\DriverException as DriverException;
1 efrain 33
 
34
/**
35
 * Step definitions and overrides to navigate through the navigation tree nodes in the Classic theme.
36
 *
37
 * @package    theme_classic
38
 * @category   test
39
 * @copyright  2019 Michael Hawkins
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class behat_theme_classic_behat_navigation extends behat_navigation {
43
    /**
44
     * Navigate to an item in a current page administration menu.
45
     *
46
     * @throws ExpectationException
47
     * @param string $nodetext The navigation node/path to follow, eg "Course administration > Edit settings"
48
     * @return void
49
     */
50
    public function i_navigate_to_in_current_page_administration($nodetext) {
51
        $parentnodes = array_map('trim', explode('>', $nodetext));
52
 
53
        // Find the name of the first category of the administration block tree.
54
        $xpath = "//section[contains(@class,'block_settings')]//div[@id='settingsnav']/ul[1]/li[1]/p[1]/span";
55
        $node = $this->find('xpath', $xpath);
56
 
57
        array_unshift($parentnodes, $node->getText());
58
        $lastnode = array_pop($parentnodes);
59
        try {
60
            $this->select_node_in_navigation($lastnode, $parentnodes);
61
        } catch (Exception $e) {
62
            try {
63
                $this->execute("behat_general::click_link", $lastnode);
64
            } catch (Exception $e) {
65
                // We must be in a weird state i.e. Add competencies to course.
66
                $this->execute("behat_general::click_link", array_pop($parentnodes));
67
                $this->execute('behat_forms::press_button', $lastnode);
68
            }
69
        }
70
    }
71
 
72
    /**
73
     * Navigate to an item within the site administration menu.
74
     *
75
     * @throws ExpectationException
76
     * @param string $nodetext The navigation node/path to follow, excluding "Site administration" itself, eg "Grades > Scales"
77
     * @return void
78
     */
79
    public function i_navigate_to_in_site_administration($nodetext) {
80
        $parentnodes = array_map('trim', explode('>', $nodetext));
81
        array_unshift($parentnodes, get_string('administrationsite'));
82
        $lastnode = array_pop($parentnodes);
83
        $this->select_node_in_navigation($lastnode, $parentnodes);
84
    }
85
 
86
    /**
87
     * Helper function to get top navigation node in the tree.
88
     *
89
     * @throws ExpectationException if node not found.
90
     * @param string $nodetext name of top navigation node in tree.
91
     * @return NodeElement
92
     */
93
    protected function get_top_navigation_node($nodetext) {
94
        // Avoid problems with quotes.
95
        $nodetextliteral = behat_context_helper::escape($nodetext);
96
        $exception = new ExpectationException('Top navigation node "' . $nodetext . '" not found', $this->getSession());
97
 
98
        $xpath = // Navigation block.
99
                "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]" .
100
                "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
101
                "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
102
                "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
103
                "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
104
                "[span[normalize-space(.)={$nodetextliteral}] or a[normalize-space(.)={$nodetextliteral}]]]" .
105
                "|" .
106
                // Administration block.
107
                "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
108
                "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
109
                "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
110
                "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
111
                "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
112
                "/span[normalize-space(.)={$nodetextliteral}]]" .
113
                "|" .
114
                "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
115
                "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
116
                "/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
117
                "/span[normalize-space(.)={$nodetextliteral}]]" .
118
                "|" .
119
                "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
120
                "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
121
                "/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
122
                "/a[normalize-space(.)={$nodetextliteral}]]";
123
 
124
        $node = $this->find('xpath', $xpath, $exception);
125
 
126
        return $node;
127
    }
128
 
129
    /**
130
     * Check that current page administration contains an element.
131
     *
132
     * @throws ElementNotFoundException
133
     * @param string $element The locator of the specified selector.
134
     *     This may be a path, for example "Subscription mode > Forced subscription"
135
     * @param string $selectortype The selector type (link or text)
136
     * @return void
137
     */
138
    public function should_exist_in_current_page_administration($element, $selectortype) {
139
        $nodes = array_map('trim', explode('>', $element));
140
        $nodetext = end($nodes);
141
 
142
        // Find administration menu.
143
        $rootxpath = $this->find_header_administration_menu() ?: $this->find_page_administration_menu(true);
144
        $menuxpath = $rootxpath . '/p/../ul[1]';
145
 
146
        for ($i = 0; $i < (count($nodes) - 1); $i++) {
147
            $menuxpath .= "/li/p/span[contains(text(), '{$nodes[$i]}')]/../../ul[1]";
148
        }
149
 
150
        if ($selectortype == 'link') {
151
            $menuxpath .= "/li/p[a[contains(text(), '{$nodetext}')]";
152
            $menuxpath .= "|a/span[contains(text(), '{$nodetext}')]]";
153
        } else {
154
            $menuxpath .= "/li/p/span[contains(text(), '{$nodes[$i]}')]";
155
        }
156
 
157
        $exception = new ElementNotFoundException($this->getSession(), "\"{$element}\" \"{$selectortype}\"");
158
        try {
159
            $this->find('xpath', $menuxpath, $exception);
160
        } catch (Exception $e) {
161
            // For question bank a different approach.
162
            $menuxpath = $rootxpath . "//div[contains(@class, 'dropdown-menu')]";
163
            if ($selectortype === 'link') {
164
                $menuxpath .= "//a[contains(text(), 'Categories')]";
165
            }
166
            $this->find('xpath', $menuxpath, $e);
167
        }
168
    }
169
 
170
    /**
171
     * Check that current page administration does not contains an element.
172
     *
173
     * @throws ExpectationException
174
     * @param string $element The locator of the specified selector.
175
     *     This may be a path, for example "Subscription mode > Forced subscription"
176
     * @param string $selectortype The selector type (link or text)
177
     * @return void
178
     */
179
    public function should_not_exist_in_current_page_administration($element, $selectortype) {
180
        try {
181
            $menuxpath = $this->find_header_administration_menu() ?: $this->find_page_administration_menu(true);
182
        } catch (Exception $e) {
183
            // If an exception was thrown, it means the root note does not exist, so we can conclude the test is a success.
184
            return;
185
        }
186
 
187
        // Test if the element exists.
188
        try {
189
            $this->should_exist_in_current_page_administration($element, $selectortype);
190
        } catch (ElementNotFoundException $e) {
191
 
192
            // If an exception was thrown, it means the element does not exist, so the test is successful.
193
            return;
194
        }
195
 
196
        // If the try block passed, the element exists, so throw an exception.
197
        $exception = 'The "' . $element . '" "' . $selectortype . '" was found, but should not exist';
198
        throw new ExpectationException($exception, $this->getSession());
199
    }
200
 
201
    /**
202
     * Check that the page administration menu exists on the page.
203
     *
204
     * This confirms the existence of the menu, which authorised users should have access to.
205
     * @Given /^I should see the page administration menu$/
206
     *
207
     * @throws ExpectationException
208
     * @return void
209
     */
210
    public function page_administration_exists() {
211
        $menuxpath = "//section[contains(@class,'block_settings')]//div[@id='settingsnav']";
212
        $this->ensure_element_exists($menuxpath, 'xpath_element');
213
    }
214
 
215
    /**
216
     * Check that the page administration menu does not exist on the page.
217
     *
218
     * This confirms the absence of the menu, which unauthorised users should not have access to.
219
     * @Given /^I should not see the page administration menu$/
220
     *
221
     * @throws ExpectationException
222
     * @return void
223
     */
224
    public function page_administration_does_not_exist() {
225
        $menuxpath = "//section[contains(@class,'block_settings')]//div[@id='settingsnav']";
226
        $this->ensure_element_does_not_exist($menuxpath, 'xpath_element');
227
    }
228
 
229
    /**
230
     * Locate the administration menu on the page (but not in the header) and return its xpath.
231
     *
232
     * @throws ElementNotFoundException
233
     * @param bool $mustexist If true, throws an exception if menu is not found
234
     * @return null|string
235
     */
236
    protected function find_page_administration_menu($mustexist = false) {
237
        $menuxpath = "//section[contains(@class,'block_settings')]//div[@id='settingsnav']/ul[1]/li[1]";
238
 
239
        if ($mustexist) {
240
            $exception = new ElementNotFoundException($this->getSession(), 'Page administration menu');
241
            $this->find('xpath', $menuxpath, $exception);
242
 
243
        } else if (!$this->getSession()->getPage()->find('xpath', $menuxpath)) {
244
            return null;
245
        }
246
 
247
        return $menuxpath;
248
    }
249
 
250
    /**
251
     * Turns editing mode off.
252
     */
253
    public function i_turn_editing_mode_off(): void {
254
        $buttonnames = [get_string('turneditingoff'), get_string('updatemymoodleoff'), get_string('blockseditoff')];
255
        foreach ($buttonnames as $buttonname) {
256
            if ($editbutton = $this->getSession()->getPage()->findButton($buttonname)) {
257
                $this->execute('behat_general::i_click_on', [$editbutton, 'NodeElement']);
258
                return;
259
            }
260
        }
261
        // Click the turneditingoff link in the Site Administration block.
262
        if ($this->is_editing_on()) {
263
            $this->execute('behat_general::i_click_on', [get_string('turneditingoff'), "link"]);
264
        }
265
    }
266
 
267
    /**
268
     * Turns editing mode on.
269
     */
270
    public function i_turn_editing_mode_on(): void {
271
        $buttonnames = [get_string('turneditingon'), get_string('updatemymoodleon'), get_string('blocksediton')];
272
        foreach ($buttonnames as $buttonname) {
273
            if ($editbutton = $this->getSession()->getPage()->findButton($buttonname)) {
274
                $this->execute('behat_general::i_click_on', [$editbutton, 'NodeElement']);
275
                return;
276
            }
277
        }
278
 
279
        if (!$this->is_editing_on()) {
280
            $this->execute('behat_general::i_click_on', [get_string('turneditingon'), "link"]);
281
        }
282
    }
283
 
284
    /**
285
     * Finds and clicks a link on the admin page (site administration or course administration)
286
     *
287
     * @param array $nodelist
288
     */
289
    protected function select_on_administration_page($nodelist) {
290
        $parentnodes = $nodelist;
291
        $lastnode = array_pop($parentnodes);
1441 ariadna 292
        $xpath = '//div[@id=\'region-main\']';
1 efrain 293
 
294
        // Check if there is a separate tab for this submenu of the page. If found go to it.
295
        if ($parentnodes) {
296
            $tabname = behat_context_helper::escape($parentnodes[0]);
297
            $tabxpath = '//ul[@role=\'tablist\']/li/a[contains(normalize-space(.), ' . $tabname . ')]';
298
            $menubarxpath = '//ul[@role=\'menubar\']/li/a[contains(normalize-space(.), ' . $tabname . ')]';
299
            $linkname = behat_context_helper::escape(get_string('moremenu'));
300
            $menubarmorexpath = '//ul[@role=\'menubar\']/li/a[contains(normalize-space(.), ' . $linkname . ')]';
301
            $tabnode = $this->getSession()->getPage()->find('xpath', $tabxpath);
302
            $menunode = $this->getSession()->getPage()->find('xpath', $menubarxpath);
303
            $menubuttons = $this->getSession()->getPage()->findAll('xpath', $menubarmorexpath);
304
            if ($tabnode || $menunode) {
305
                $node = is_object($tabnode) ? $tabnode : $menunode;
306
                if ($this->running_javascript()) {
307
                    $this->execute('behat_general::i_click_on', [$node, 'NodeElement']);
308
                    // Click on the tab and add 'active' tab to the xpath.
309
                    $xpath .= '//div[contains(@class,\'active\')]';
310
                } else {
311
                    // Add the tab content selector to the xpath.
312
                    $tabid = behat_context_helper::escape(ltrim($node->getAttribute('href'), '#'));
313
                    $xpath .= '//div[@id = ' . $tabid . ']';
314
                }
315
                array_shift($parentnodes);
316
            } else if (count($menubuttons) > 0) {
317
                try {
318
                    $menubuttons[0]->isVisible();
319
                    try {
320
                        $this->execute('behat_general::i_click_on', [$menubuttons[1], 'NodeElement']);
321
                    } catch (Exception $e) {
322
                        $this->execute('behat_general::i_click_on', [$menubuttons[0], 'NodeElement']);
323
                    }
324
                    $moreitemxpath = '//ul[@data-region=\'moredropdown\']/li/a[contains(normalize-space(.), ' . $tabname . ')]';
325
                    if ($morenode = $this->getSession()->getPage()->find('xpath', $moreitemxpath)) {
326
                        $this->execute('behat_general::i_click_on', [$morenode, 'NodeElement']);
327
                        $xpath .= '//div[contains(@class,\'active\')]';
328
                        array_shift($parentnodes);
329
                    }
330
                } catch (Exception $e) {
331
                    return;
332
                }
333
            }
334
        }
335
 
336
        // Find a section with the parent name in it.
337
        if ($parentnodes) {
338
            // Find the section on the page (links may be repeating in different sections).
339
            $section = behat_context_helper::escape($parentnodes[0]);
340
            $xpath .= '//div[@class=\'row\' and contains(.,'.$section.')]';
341
        }
342
 
343
        // Find a link and click on it.
344
        $linkname = behat_context_helper::escape($lastnode);
345
        $xpath .= '//a[contains(normalize-space(.), ' . $linkname . ')]';
346
        if (!$node = $this->getSession()->getPage()->find('xpath', $xpath)) {
347
            throw new ElementNotFoundException($this->getSession(), 'Link "' . join(' > ', $nodelist) . '"');
348
        }
349
        $this->execute('behat_general::i_click_on', [$node, 'NodeElement']);
350
    }
351
 
352
    /**
353
     * Locates the administration menu in the <header> element and returns its xpath
354
     *
355
     * @param bool $mustexist if specified throws an exception if menu is not found
356
     * @return null|string
357
     */
358
    protected function find_header_administration_menu($mustexist = false) {
359
        $menuxpath = '//header[@id=\'page-header\']//div[contains(@class,\'moodle-actionmenu\')]';
360
        if ($mustexist) {
361
            $exception = new ElementNotFoundException($this->getSession(), 'Page header administration menu');
362
            $this->find('xpath', $menuxpath, $exception);
363
        } else if (!$this->getSession()->getPage()->find('xpath', $menuxpath)) {
364
            return null;
365
        }
366
        return $menuxpath;
367
    }
368
 
369
    /**
370
     * Toggles administration menu
371
     *
372
     * @param string $menuxpath (optional) xpath to the page administration menu if already known
373
     */
374
    protected function toggle_page_administration_menu($menuxpath = null) {
375
        if (!$menuxpath) {
376
            $menuxpath = $this->find_header_administration_menu() ?: $this->find_page_administration_menu();
377
        }
378
        if ($menuxpath && $this->running_javascript()) {
1441 ariadna 379
            $node = $this->find('xpath', $menuxpath . '//a[@data-bs-toggle=\'dropdown\']');
1 efrain 380
            $this->execute('behat_general::i_click_on', [$node, 'NodeElement']);
381
        }
382
    }
383
 
384
    /**
385
     * Finds a page edit cog and select an item from it
386
     *
387
     * If the page edit cog is in the page header and the item is not found there, click "More..." link
388
     * and find the item on the course/frontpage administration page
389
     *
390
     * @param array $nodelist
391
     * @throws ElementNotFoundException
392
     */
393
    protected function select_from_administration_menu($nodelist) {
394
        // Find administration menu.
395
        if ($menuxpath = $this->find_header_administration_menu()) {
396
            $isheader = true;
397
        } else {
398
            $menuxpath = $this->find_page_administration_menu(true);
399
            $isheader = false;
400
        }
401
 
402
        $this->execute('behat_navigation::toggle_page_administration_menu', [$menuxpath]);
403
 
404
        if (!$isheader || count($nodelist) == 1) {
405
            $lastnode = end($nodelist);
406
            $linkname = behat_context_helper::escape($lastnode);
407
            $link = $this->getSession()->getPage()->find('xpath', $menuxpath . '//a[contains(normalize-space(.), ' .
408
                $linkname . ')]'
409
            );
410
            if ($link) {
411
                $this->execute('behat_general::i_click_on', [$link, 'NodeElement']);
412
                return;
413
            }
414
        }
415
 
416
        if ($isheader) {
417
            // Course administration and Front page administration will have subnodes under "More...".
418
            $linkname = behat_context_helper::escape(get_string('morenavigationlinks'));
419
            $link = $this->getSession()->getPage()->find('xpath', $menuxpath . '//a[contains(normalize-space(.), ' .
420
                $linkname . ')]'
421
            );
422
            if ($link) {
423
                $this->execute('behat_general::i_click_on', [$link, 'NodeElement']);
424
                $this->select_on_administration_page($nodelist);
425
                return;
426
            }
427
        }
428
 
429
        throw new ElementNotFoundException($this->getSession(),
430
            'Link "' . join(' > ', $nodelist) . '" in the current page edit menu"');
431
    }
1441 ariadna 432
 
433
    #[\Override]
434
    public function menu_item_should_be_active(string $navigationmenuitem): void {
435
        throw new DriverException(
436
            'The Classic theme does not implement navigation in a way that allows this step to be used.',
437
        );
438
    }
439
 
440
    #[\Override]
441
    public function menu_item_should_not_be_active(string $navigationmenuitem): void {
442
        throw new DriverException(
443
            'The Classic theme does not implement the navigation in a way that allows this step to be used.',
444
        );
445
    }
1 efrain 446
}