MDL-68353 test: Update filemanager/filepicker xpath selector
[moodle.git] / lib / behat / core_behat_file_helper.php
CommitLineData
1d823d93
DM
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 * Files interactions with behat.
19 *
4aefaf65
AN
20 * Note that steps definitions files can not extend other steps definitions files, so steps definitions which makes use
21 * of file attachments or filepicker should use this behat_file_helper trait.
1d823d93
DM
22 *
23 * @package core
24 * @category test
25 * @copyright 2013 David MonllaĆ³
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
30
31require_once(__DIR__ . '/behat_base.php');
32
50eeae59 33use Behat\Mink\Exception\ExpectationException as ExpectationException,
fcb6ed47 34 Behat\Mink\Element\NodeElement as NodeElement;
1d823d93
DM
35
36/**
37 * Files-related actions.
38 *
4aefaf65
AN
39 * Steps definitions related with filepicker or repositories should extend use this trait as it provides useful methods
40 * to deal with the common filepicker issues.
1d823d93
DM
41 *
42 * @package core
43 * @category test
44 * @copyright 2013 David MonllaĆ³
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 */
4aefaf65 47trait core_behat_file_helper {
1d823d93
DM
48
49 /**
cde67a83 50 * Gets the NodeElement for filepicker of filemanager moodleform element.
1d823d93 51 *
cde67a83 52 * The filepicker/filemanager element label is pointing to a hidden input which is
1d823d93
DM
53 * not recognized as a named selector, as it is hidden...
54 *
55 * @throws ExpectationException Thrown by behat_base::find
0b297fdc 56 * @param string $filepickerelement The filepicker form field label
1d823d93
DM
57 * @return NodeElement The hidden element node.
58 */
e3652936 59
1d823d93
DM
60 protected function get_filepicker_node($filepickerelement) {
61
62 // More info about the problem (in case there is a problem).
63 $exception = new ExpectationException('"' . $filepickerelement . '" filepicker can not be found', $this->getSession());
64
9e103d6e
JM
65 // If no file picker label is mentioned take the first file picker from the page.
66 if (empty($filepickerelement)) {
67 $filepickercontainer = $this->find(
e3652936
MM
68 'xpath',
69 "//*[@data-fieldtype=\"filemanager\"]",
70 $exception
9e103d6e
JM
71 );
72 } else {
73 // Gets the ffilemanager node specified by the locator which contains the filepicker container.
921faad9 74 $filepickerelement = behat_context_helper::escape($filepickerelement);
9e103d6e 75 $filepickercontainer = $this->find(
e3652936 76 'xpath',
58994fe8 77 "//input[./@id = substring-before(//p[normalize-space(.)=$filepickerelement]/@id, '_label')]" .
e3652936
MM
78 "//ancestor::*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']",
79 $exception
9e103d6e
JM
80 );
81 }
1d823d93
DM
82
83 return $filepickercontainer;
84 }
85
86 /**
cde67a83 87 * Performs $action on a filemanager container element (file or folder).
1d823d93
DM
88 *
89 * It works together with open_element_contextual_menu
90 * as this method needs the contextual menu to be opened.
91 *
92 * @throws ExpectationException Thrown by behat_base::find
93 * @param string $action
94 * @param ExpectationException $exception
95 * @return void
96 */
97 protected function perform_on_element($action, ExpectationException $exception) {
98
99 // Finds the button inside the DOM, is a modal window, so should be unique.
100 $classname = 'fp-file-' . $action;
ae19c68c 101 $button = $this->find('css', '.moodle-dialogue-focused button.' . $classname, $exception);
1d823d93 102
d1e55a47 103 $this->ensure_node_is_visible($button);
1d823d93
DM
104 $button->click();
105 }
106
107 /**
108 * Opens the contextual menu of a folder or a file.
109 *
cde67a83
MG
110 * Works both in filemanager elements and when dealing with repository
111 * elements inside filepicker modal window.
0b297fdc 112 *
1d823d93
DM
113 * @throws ExpectationException Thrown by behat_base::find
114 * @param string $name The name of the folder/file
cde67a83 115 * @param string $filemanagerelement The filemanager form element locator, the repository items are in filepicker modal window if false
1d823d93
DM
116 * @return void
117 */
cde67a83 118 protected function open_element_contextual_menu($name, $filemanagerelement = false) {
0b297fdc 119
cde67a83 120 // If a filemanager is specified we restrict the search to the descendants of this particular filemanager form element.
0b297fdc
DM
121 $containernode = false;
122 $exceptionmsg = '"'.$name.'" element can not be found';
cde67a83
MG
123 if ($filemanagerelement) {
124 $containernode = $this->get_filepicker_node($filemanagerelement);
125 $exceptionmsg = 'The "'.$filemanagerelement.'" filemanager ' . $exceptionmsg;
63e4df60 126 $locatorprefix = "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]";
fcb6ed47 127 } else {
63e4df60
DW
128 $locatorprefix = "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-items ')]" .
129 "//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]";
0b297fdc 130 }
1d823d93 131
0b297fdc 132 $exception = new ExpectationException($exceptionmsg, $this->getSession());
1d823d93 133
38976081 134 // Avoid quote-related problems.
921faad9 135 $name = behat_context_helper::escape($name);
38976081 136
cde67a83 137 // Get a filepicker/filemanager element (folder or file).
1d823d93
DM
138 try {
139
3db1aec5 140 // First we look at the folder as we need to click on the contextual menu otherwise it would be opened.
1d823d93
DM
141 $node = $this->find(
142 'xpath',
fcb6ed47 143 $locatorprefix .
3194dc83 144 "//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
38976081
DM
145 "[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
146 "[normalize-space(.)=$name]" .
3194dc83 147 "//descendant::a[contains(concat(' ', normalize-space(@class), ' '), ' fp-contextmenu ')]",
1d823d93 148 $exception,
0b297fdc 149 $containernode
1d823d93
DM
150 );
151
3db1aec5 152 } catch (ExpectationException $e) {
1d823d93 153
3db1aec5 154 // Here the contextual menu is hidden, we click on the thumbnail.
1d823d93
DM
155 $node = $this->find(
156 'xpath',
fcb6ed47 157 $locatorprefix .
3194dc83 158 "//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
38976081 159 "[normalize-space(.)=$name]" .
d1e55a47 160 "//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename-field ')]",
0b297fdc
DM
161 false,
162 $containernode
1d823d93
DM
163 );
164 }
165
3db1aec5 166 // Click opens the contextual menu when clicking on files.
d1e55a47 167 $this->ensure_node_is_visible($node);
3db1aec5 168 $node->click();
1d823d93
DM
169 }
170
171 /**
cde67a83 172 * Opens the filepicker modal window and selects the repository.
1d823d93
DM
173 *
174 * @throws ExpectationException Thrown by behat_base::find
cde67a83 175 * @param NodeElement $filemanagernode The filemanager or filepicker form element DOM node.
1d823d93
DM
176 * @param mixed $repositoryname The repo name.
177 * @return void
178 */
cde67a83 179 protected function open_add_file_window($filemanagernode, $repositoryname) {
1d823d93 180
cde67a83 181 $exception = new ExpectationException('No files can be added to the specified filemanager', $this->getSession());
1d823d93 182
cde67a83 183 // We should deal with single-file and multiple-file filemanagers,
1d823d93
DM
184 // catching the exception thrown by behat_base::find() in case is not multiple
185 try {
cde67a83
MG
186 // Looking for the add button inside the specified filemanager.
187 $add = $this->find('css', 'div.fp-btn-add a', $exception, $filemanagernode);
1d823d93 188 } catch (Exception $e) {
cde67a83
MG
189 // Otherwise should be a single-file filepicker form element.
190 $add = $this->find('css', 'input.fp-btn-choose', $exception, $filemanagernode);
1d823d93 191 }
d1e55a47 192 $this->ensure_node_is_visible($add);
1d823d93
DM
193 $add->click();
194
c5639c91
MJ
195 // Wait for the default repository (if any) to load. This checks that
196 // the relevant div exists and that it does not include the loading image.
197 $this->ensure_element_exists(
198 "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
199 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
200 "[not(descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content-loading ')])]",
201 'xpath_element');
202
1d823d93
DM
203 // Getting the repository link and opening it.
204 $repoexception = new ExpectationException('The "' . $repositoryname . '" repository has not been found', $this->getSession());
205
38976081 206 // Avoid problems with both double and single quotes in the same string.
921faad9 207 $repositoryname = behat_context_helper::escape($repositoryname);
38976081 208
cde67a83 209 // Here we don't need to look inside the selected element because there can only be one modal window.
1d823d93
DM
210 $repositorylink = $this->find(
211 'xpath',
3194dc83
DM
212 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-area ')]" .
213 "//descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]" .
38976081 214 "[normalize-space(.)=$repositoryname]",
1d823d93
DM
215 $repoexception
216 );
217
218 // Selecting the repo.
d1e55a47 219 $this->ensure_node_is_visible($repositorylink);
c5639c91
MJ
220 if (!$repositorylink->getParent()->getParent()->hasClass('active')) {
221 // If the repository link is active, then the repository is already loaded.
222 // Clicking it while it's active causes issues, so only click it when it isn't (see MDL-51014).
223 $repositorylink->click();
224 }
1d823d93 225 }
50eeae59
DM
226
227 /**
228 * Waits until the file manager modal windows are closed.
229 *
d1e55a47
DM
230 * This method is not used by any of our step definitions,
231 * keeping it here for users already using it.
232 *
50eeae59
DM
233 * @throws ExpectationException
234 * @return void
235 */
236 protected function wait_until_return_to_form() {
237
238 $exception = new ExpectationException('The file manager is taking too much time to finish the current action', $this->getSession());
239
ae19c68c
DW
240 $this->find(
241 'xpath',
242 "//div[contains(concat(' ', @class, ' '), ' moodle-dialogue-lightbox ')][contains(@style, 'display: none;')]",
243 $exception
244 );
50eeae59
DM
245 }
246
247 /**
248 * Checks that the file manager contents are not being updated.
249 *
d1e55a47
DM
250 * This method is not used by any of our step definitions,
251 * keeping it here for users already using it.
252 *
50eeae59
DM
253 * @throws ExpectationException
254 * @param NodeElement $filepickernode The file manager DOM node
255 * @return void
256 */
257 protected function wait_until_contents_are_updated($filepickernode) {
258
259 $exception = new ExpectationException(
260 'The file manager contents are requiring too much time to be updated',
261 $this->getSession()
262 );
263
264 // Looks for the loading image not being displayed. For single-file filepickers is
265 // only used when accessing the filepicker, there is no filemanager-loading after selecting the file.
266 $this->find(
267 'xpath',
38976081
DM
268 "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
269 "[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
50eeae59 270 "|" .
38976081 271 "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager-loading ')]" .
50eeae59
DM
272 "[contains(@style, 'display: none;')]",
273 $exception,
274 $filepickernode
275 );
50eeae59 276 }
1d823d93 277}