2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Files interactions with behat.
20 * Note that steps definitions files can not extend other steps definitions files, so
21 * steps definitions which makes use of file attachments or filepicker should
22 * extend behat_files instead of behat_base.
26 * @copyright 2013 David MonllaĆ³
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
32 require_once(__DIR__
. '/behat_base.php');
34 use Behat\Mink\Exception\ExpectationException
as ExpectationException
,
35 Behat\Mink\Element\NodeElement
as NodeElement
;
38 * Files-related actions.
40 * Steps definitions related with filepicker or repositories should extend
41 * this class instead of behat_base as it provides useful methods to deal
42 * with the common filepicker issues.
46 * @copyright 2013 David MonllaĆ³
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 class behat_files
extends behat_base
{
52 * Gets the NodeElement for filepicker of filemanager moodleform element.
54 * The filepicker/filemanager element label is pointing to a hidden input which is
55 * not recognized as a named selector, as it is hidden...
57 * @throws ExpectationException Thrown by behat_base::find
58 * @param string $filepickerelement The filepicker form field label
59 * @return NodeElement The hidden element node.
61 protected function get_filepicker_node($filepickerelement) {
63 // More info about the problem (in case there is a problem).
64 $exception = new ExpectationException('"' . $filepickerelement . '" filepicker can not be found', $this->getSession());
66 // If no file picker label is mentioned take the first file picker from the page.
67 if (empty($filepickerelement)) {
68 $filepickercontainer = $this->find(
70 "//*[@class=\"form-filemanager\"]",
74 // Gets the ffilemanager node specified by the locator which contains the filepicker container.
75 $filepickerelement = $this->getSession()->getSelectorsHandler()->xpathLiteral($filepickerelement);
76 $filepickercontainer = $this->find(
78 "//input[./@id = //label[normalize-space(.)=$filepickerelement]/@for]" .
79 "//ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ') or " .
80 "contains(concat(' ', normalize-space(@class), ' '), ' ffilepicker ')]",
85 return $filepickercontainer;
89 * Performs $action on a filemanager container element (file or folder).
91 * It works together with open_element_contextual_menu
92 * as this method needs the contextual menu to be opened.
94 * @throws ExpectationException Thrown by behat_base::find
95 * @param string $action
96 * @param ExpectationException $exception
99 protected function perform_on_element($action, ExpectationException
$exception) {
101 // Finds the button inside the DOM, is a modal window, so should be unique.
102 $classname = 'fp-file-' . $action;
103 $button = $this->find('css', '.moodle-dialogue-focused button.' . $classname, $exception);
105 $this->ensure_node_is_visible($button);
110 * Opens the contextual menu of a folder or a file.
112 * Works both in filemanager elements and when dealing with repository
113 * elements inside filepicker modal window.
115 * @throws ExpectationException Thrown by behat_base::find
116 * @param string $name The name of the folder/file
117 * @param string $filemanagerelement The filemanager form element locator, the repository items are in filepicker modal window if false
120 protected function open_element_contextual_menu($name, $filemanagerelement = false) {
122 // If a filemanager is specified we restrict the search to the descendants of this particular filemanager form element.
123 $containernode = false;
124 $exceptionmsg = '"'.$name.'" element can not be found';
125 if ($filemanagerelement) {
126 $containernode = $this->get_filepicker_node($filemanagerelement);
127 $exceptionmsg = 'The "'.$filemanagerelement.'" filemanager ' . $exceptionmsg;
128 $locatorprefix = "//div[@class='fp-content']";
130 $locatorprefix = "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-items ')]//descendant::div[@class='fp-content']";
133 $exception = new ExpectationException($exceptionmsg, $this->getSession());
135 // Avoid quote-related problems.
136 $name = $this->getSession()->getSelectorsHandler()->xpathLiteral($name);
138 // Get a filepicker/filemanager element (folder or file).
141 // First we look at the folder as we need to click on the contextual menu otherwise it would be opened.
145 "//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
146 "[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
147 "[normalize-space(.)=$name]" .
148 "//descendant::a[contains(concat(' ', normalize-space(@class), ' '), ' fp-contextmenu ')]",
153 } catch (ExpectationException
$e) {
155 // Here the contextual menu is hidden, we click on the thumbnail.
159 "//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
160 "[normalize-space(.)=$name]" .
161 "//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename-field ')]",
167 // Click opens the contextual menu when clicking on files.
168 $this->ensure_node_is_visible($node);
173 * Opens the filepicker modal window and selects the repository.
175 * @throws ExpectationException Thrown by behat_base::find
176 * @param NodeElement $filemanagernode The filemanager or filepicker form element DOM node.
177 * @param mixed $repositoryname The repo name.
180 protected function open_add_file_window($filemanagernode, $repositoryname) {
182 $exception = new ExpectationException('No files can be added to the specified filemanager', $this->getSession());
184 // We should deal with single-file and multiple-file filemanagers,
185 // catching the exception thrown by behat_base::find() in case is not multiple
187 // Looking for the add button inside the specified filemanager.
188 $add = $this->find('css', 'div.fp-btn-add a', $exception, $filemanagernode);
189 } catch (Exception
$e) {
190 // Otherwise should be a single-file filepicker form element.
191 $add = $this->find('css', 'input.fp-btn-choose', $exception, $filemanagernode);
193 $this->ensure_node_is_visible($add);
196 // Getting the repository link and opening it.
197 $repoexception = new ExpectationException('The "' . $repositoryname . '" repository has not been found', $this->getSession());
199 // Avoid problems with both double and single quotes in the same string.
200 $repositoryname = $this->getSession()->getSelectorsHandler()->xpathLiteral($repositoryname);
202 // Here we don't need to look inside the selected element because there can only be one modal window.
203 $repositorylink = $this->find(
205 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-area ')]" .
206 "//descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]" .
207 "[normalize-space(.)=$repositoryname]",
211 // Selecting the repo.
212 $this->ensure_node_is_visible($repositorylink);
213 $repositorylink->click();
217 * Waits until the file manager modal windows are closed.
219 * This method is not used by any of our step definitions,
220 * keeping it here for users already using it.
222 * @throws ExpectationException
225 protected function wait_until_return_to_form() {
227 $exception = new ExpectationException('The file manager is taking too much time to finish the current action', $this->getSession());
231 "//div[contains(concat(' ', @class, ' '), ' moodle-dialogue-lightbox ')][contains(@style, 'display: none;')]",
237 * Checks that the file manager contents are not being updated.
239 * This method is not used by any of our step definitions,
240 * keeping it here for users already using it.
242 * @throws ExpectationException
243 * @param NodeElement $filepickernode The file manager DOM node
246 protected function wait_until_contents_are_updated($filepickernode) {
248 $exception = new ExpectationException(
249 'The file manager contents are requiring too much time to be updated',
253 // Looks for the loading image not being displayed. For single-file filepickers is
254 // only used when accessing the filepicker, there is no filemanager-loading after selecting the file.
257 "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
258 "[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
260 "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager-loading ')]" .
261 "[contains(@style, 'display: none;')]",