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