MDL-63050 cachestore_redis: Update hExists to check empty
[moodle.git] / lib / behat / behat_files.php
blob43ed54f7ff6ff49d7a72b413a9f15d399c345242
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.
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/>.
17 /**
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.
24 * @package core
25 * @category test
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;
37 /**
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.
44 * @package core
45 * @category test
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 {
51 /**
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(
69 'xpath',
70 "//*[@data-fieldtype=\"filemanager\"]",
71 $exception
73 } else {
74 // Gets the ffilemanager node specified by the locator which contains the filepicker container.
75 $filepickerelement = behat_context_helper::escape($filepickerelement);
76 $filepickercontainer = $this->find(
77 'xpath',
78 "//input[./@id = //label[normalize-space(.)=$filepickerelement]/@for]" .
79 '//ancestor::div[@data-fieldtype="filemanager" or @data-fieldtype="filepicker"]',
80 $exception
84 return $filepickercontainer;
87 /**
88 * Performs $action on a filemanager container element (file or folder).
90 * It works together with open_element_contextual_menu
91 * as this method needs the contextual menu to be opened.
93 * @throws ExpectationException Thrown by behat_base::find
94 * @param string $action
95 * @param ExpectationException $exception
96 * @return void
98 protected function perform_on_element($action, ExpectationException $exception) {
100 // Finds the button inside the DOM, is a modal window, so should be unique.
101 $classname = 'fp-file-' . $action;
102 $button = $this->find('css', '.moodle-dialogue-focused button.' . $classname, $exception);
104 $this->ensure_node_is_visible($button);
105 $button->click();
109 * Opens the contextual menu of a folder or a file.
111 * Works both in filemanager elements and when dealing with repository
112 * elements inside filepicker modal window.
114 * @throws ExpectationException Thrown by behat_base::find
115 * @param string $name The name of the folder/file
116 * @param string $filemanagerelement The filemanager form element locator, the repository items are in filepicker modal window if false
117 * @return void
119 protected function open_element_contextual_menu($name, $filemanagerelement = false) {
121 // If a filemanager is specified we restrict the search to the descendants of this particular filemanager form element.
122 $containernode = false;
123 $exceptionmsg = '"'.$name.'" element can not be found';
124 if ($filemanagerelement) {
125 $containernode = $this->get_filepicker_node($filemanagerelement);
126 $exceptionmsg = 'The "'.$filemanagerelement.'" filemanager ' . $exceptionmsg;
127 $locatorprefix = "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]";
128 } else {
129 $locatorprefix = "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-items ')]" .
130 "//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]";
133 $exception = new ExpectationException($exceptionmsg, $this->getSession());
135 // Avoid quote-related problems.
136 $name = behat_context_helper::escape($name);
138 // Get a filepicker/filemanager element (folder or file).
139 try {
141 // First we look at the folder as we need to click on the contextual menu otherwise it would be opened.
142 $node = $this->find(
143 'xpath',
144 $locatorprefix .
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 ')]",
149 $exception,
150 $containernode
153 } catch (ExpectationException $e) {
155 // Here the contextual menu is hidden, we click on the thumbnail.
156 $node = $this->find(
157 'xpath',
158 $locatorprefix .
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 ')]",
162 false,
163 $containernode
167 // Click opens the contextual menu when clicking on files.
168 $this->ensure_node_is_visible($node);
169 $node->click();
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.
178 * @return void
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
186 try {
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);
194 $add->click();
196 // Wait for the default repository (if any) to load. This checks that
197 // the relevant div exists and that it does not include the loading image.
198 $this->ensure_element_exists(
199 "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
200 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
201 "[not(descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content-loading ')])]",
202 'xpath_element');
204 // Getting the repository link and opening it.
205 $repoexception = new ExpectationException('The "' . $repositoryname . '" repository has not been found', $this->getSession());
207 // Avoid problems with both double and single quotes in the same string.
208 $repositoryname = behat_context_helper::escape($repositoryname);
210 // Here we don't need to look inside the selected element because there can only be one modal window.
211 $repositorylink = $this->find(
212 'xpath',
213 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-area ')]" .
214 "//descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]" .
215 "[normalize-space(.)=$repositoryname]",
216 $repoexception
219 // Selecting the repo.
220 $this->ensure_node_is_visible($repositorylink);
221 if (!$repositorylink->getParent()->getParent()->hasClass('active')) {
222 // If the repository link is active, then the repository is already loaded.
223 // Clicking it while it's active causes issues, so only click it when it isn't (see MDL-51014).
224 $repositorylink->click();
229 * Waits until the file manager modal windows are closed.
231 * This method is not used by any of our step definitions,
232 * keeping it here for users already using it.
234 * @throws ExpectationException
235 * @return void
237 protected function wait_until_return_to_form() {
239 $exception = new ExpectationException('The file manager is taking too much time to finish the current action', $this->getSession());
241 $this->find(
242 'xpath',
243 "//div[contains(concat(' ', @class, ' '), ' moodle-dialogue-lightbox ')][contains(@style, 'display: none;')]",
244 $exception
249 * Checks that the file manager contents are not being updated.
251 * This method is not used by any of our step definitions,
252 * keeping it here for users already using it.
254 * @throws ExpectationException
255 * @param NodeElement $filepickernode The file manager DOM node
256 * @return void
258 protected function wait_until_contents_are_updated($filepickernode) {
260 $exception = new ExpectationException(
261 'The file manager contents are requiring too much time to be updated',
262 $this->getSession()
265 // Looks for the loading image not being displayed. For single-file filepickers is
266 // only used when accessing the filepicker, there is no filemanager-loading after selecting the file.
267 $this->find(
268 'xpath',
269 "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
270 "[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
271 "|" .
272 "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager-loading ')]" .
273 "[contains(@style, 'display: none;')]",
274 $exception,
275 $filepickernode