MDL-58428 theme: Shift templates ready for Bootstrapbase removal
[moodle.git] / repository / tests / behat / behat_filepicker.php
CommitLineData
d87bbb3d
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/**
cde67a83 18 * Filemanager and filepicker manipulation steps definitions.
d87bbb3d 19 *
abe572e3 20 * @package core_filepicker
d87bbb3d
DM
21 * @category test
22 * @copyright 2013 David MonllaĆ³
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
28require_once(__DIR__ . '/../../../lib/behat/behat_files.php');
29
fcb6ed47
MG
30use Behat\Mink\Exception\ExpectationException as ExpectationException,
31 Behat\Gherkin\Node\TableNode as TableNode;
d87bbb3d
DM
32
33/**
cde67a83 34 * Steps definitions to deal with the filemanager and filepicker.
d87bbb3d
DM
35 *
36 * Extends behat_files rather than behat_base as is file-related.
37 *
abe572e3 38 * @package core_filepicker
d87bbb3d
DM
39 * @category test
40 * @copyright 2013 David MonllaĆ³
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class behat_filepicker extends behat_files {
44
45 /**
cde67a83 46 * Creates a folder with specified name in the current folder and in the specified filemanager field.
d87bbb3d 47 *
cde67a83 48 * @Given /^I create "(?P<foldername_string>(?:[^"]|\\")*)" folder in "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
d87bbb3d
DM
49 * @throws ExpectationException Thrown by behat_base::find
50 * @param string $foldername
cde67a83 51 * @param string $filemanagerelement
d87bbb3d 52 */
cde67a83 53 public function i_create_folder_in_filemanager($foldername, $filemanagerelement) {
d87bbb3d 54
cde67a83 55 $fieldnode = $this->get_filepicker_node($filemanagerelement);
d87bbb3d 56
cde67a83 57 // Looking for the create folder button inside the specified filemanager.
e3652936
MM
58 $exception = new ExpectationException('No folders can be created in "'.$filemanagerelement.'" filemanager',
59 $this->getSession());
d87bbb3d
DM
60 $newfolder = $this->find('css', 'div.fp-btn-mkdir a', $exception, $fieldnode);
61 $newfolder->click();
62
63 // Setting the folder name in the modal window.
64 $exception = new ExpectationException('The dialog to enter the folder name does not appear', $this->getSession());
199c9375 65 $dialoginput = $this->find('css', '.fp-mkdir-dlg-text input', $exception);
d87bbb3d
DM
66 $dialoginput->setValue($foldername);
67
199c9375
JF
68 $exception = new ExpectationException('The button for the create folder dialog can not be located', $this->getSession());
69 $dialognode = $this->find('css', '.moodle-dialogue-focused');
70 $buttonnode = $this->find('css', '.fp-dlg-butcreate', $exception, $dialognode);
71 $buttonnode->click();
d87bbb3d
DM
72 }
73
74 /**
cde67a83 75 * Opens the contents of a filemanager folder. It looks for the folder in the current folder and in the path bar.
d87bbb3d 76 *
cde67a83 77 * @Given /^I open "(?P<foldername_string>(?:[^"]|\\")*)" folder from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
d87bbb3d
DM
78 * @throws ExpectationException Thrown by behat_base::find
79 * @param string $foldername
cde67a83 80 * @param string $filemanagerelement
d87bbb3d 81 */
cde67a83 82 public function i_open_folder_from_filemanager($foldername, $filemanagerelement) {
d87bbb3d 83
cde67a83 84 $fieldnode = $this->get_filepicker_node($filemanagerelement);
d87bbb3d
DM
85
86 $exception = new ExpectationException(
e3652936
MM
87 'The "'.$foldername.'" folder can not be found in the "'.$filemanagerelement.'" filemanager',
88 $this->getSession()
d87bbb3d
DM
89 );
90
921faad9 91 $folderliteral = behat_context_helper::escape($foldername);
38976081 92
d87bbb3d
DM
93 // We look both in the pathbar and in the contents.
94 try {
95
96 // In the current folder workspace.
97 $folder = $this->find(
e3652936
MM
98 'xpath',
99 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
38976081
DM
100 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]" .
101 "[normalize-space(.)=$folderliteral]",
e3652936
MM
102 $exception,
103 $fieldnode
d87bbb3d
DM
104 );
105 } catch (ExpectationException $e) {
106
107 // And in the pathbar.
108 $folder = $this->find(
e3652936
MM
109 'xpath',
110 "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')]" .
38976081 111 "[normalize-space(.)=$folderliteral]",
e3652936
MM
112 $exception,
113 $fieldnode
d87bbb3d
DM
114 );
115 }
116
117 // It should be a NodeElement, otherwise an exception would have been thrown.
118 $folder->click();
d87bbb3d
DM
119 }
120
121 /**
cde67a83 122 * Unzips the specified file from the specified filemanager field. The zip file has to be visible in the current folder.
d87bbb3d 123 *
cde67a83 124 * @Given /^I unzip "(?P<filename_string>(?:[^"]|\\")*)" file from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
d87bbb3d
DM
125 * @throws ExpectationException Thrown by behat_base::find
126 * @param string $filename
cde67a83 127 * @param string $filemanagerelement
d87bbb3d 128 */
cde67a83 129 public function i_unzip_file_from_filemanager($filename, $filemanagerelement) {
d87bbb3d 130
cde67a83
MG
131 // Open the contextual menu of the filemanager element.
132 $this->open_element_contextual_menu($filename, $filemanagerelement);
d87bbb3d
DM
133
134 // Execute the action.
135 $exception = new ExpectationException($filename.' element can not be unzipped', $this->getSession());
136 $this->perform_on_element('unzip', $exception);
d87bbb3d
DM
137 }
138
139 /**
cde67a83 140 * Zips the specified folder from the specified filemanager field. The folder has to be in the current folder.
d87bbb3d 141 *
cde67a83 142 * @Given /^I zip "(?P<filename_string>(?:[^"]|\\")*)" folder from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
d87bbb3d
DM
143 * @throws ExpectationException Thrown by behat_base::find
144 * @param string $foldername
cde67a83 145 * @param string $filemanagerelement
d87bbb3d 146 */
cde67a83 147 public function i_zip_folder_from_filemanager($foldername, $filemanagerelement) {
d87bbb3d 148
cde67a83
MG
149 // Open the contextual menu of the filemanager element.
150 $this->open_element_contextual_menu($foldername, $filemanagerelement);
d87bbb3d
DM
151
152 // Execute the action.
153 $exception = new ExpectationException($foldername.' element can not be zipped', $this->getSession());
154 $this->perform_on_element('zip', $exception);
d87bbb3d
DM
155 }
156
157 /**
cde67a83 158 * Deletes the specified file or folder from the specified filemanager field.
d87bbb3d 159 *
cde67a83 160 * @Given /^I delete "(?P<file_or_folder_name_string>(?:[^"]|\\")*)" from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
d87bbb3d 161 * @throws ExpectationException Thrown by behat_base::find
cde67a83
MG
162 * @param string $name
163 * @param string $filemanagerelement
d87bbb3d 164 */
cde67a83 165 public function i_delete_file_from_filemanager($name, $filemanagerelement) {
d87bbb3d 166
cde67a83
MG
167 // Open the contextual menu of the filemanager element.
168 $this->open_element_contextual_menu($name, $filemanagerelement);
d87bbb3d
DM
169
170 // Execute the action.
171 $exception = new ExpectationException($name.' element can not be deleted', $this->getSession());
172 $this->perform_on_element('delete', $exception);
173
174 // Yes, we are sure.
175 // Using xpath + click instead of pressButton as 'Ok' it is a common string.
176 $okbutton = $this->find('css', 'div.fp-dlg button.fp-dlg-butconfirm');
177 $okbutton->click();
d87bbb3d
DM
178 }
179
fcb6ed47
MG
180 /**
181 * Makes sure user can see the exact number of elements (files in folders) in the filemanager.
182 *
183 * @Then /^I should see "(?P<elementscount_number>\d+)" elements in "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
184 * @throws ExpectationException Thrown by behat_base::find
185 * @param int $elementscount
186 * @param string $filemanagerelement
187 */
188 public function i_should_see_elements_in_filemanager($elementscount, $filemanagerelement) {
189 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
c5a10153
DM
190
191 // We count .fp-file elements inside a filemanager not being updated.
192 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
e3652936
MM
193 "[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
194 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
195 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]";
c5a10153
DM
196
197 $elements = $this->find_all('xpath', $xpath, false, $filemanagernode);
fcb6ed47 198 if (count($elements) != $elementscount) {
e3652936
MM
199 throw new ExpectationException('Found '.count($elements).' elements in filemanager. Expected '.$elementscount,
200 $this->getSession());
fcb6ed47
MG
201 }
202 }
203
204 /**
205 * Picks the file from repository leaving default values in select file dialogue.
206 *
207 * @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
208 * @throws ExpectationException Thrown by behat_base::find
209 * @param string $filepath
50900389 210 * @param string $repository
fcb6ed47
MG
211 * @param string $filemanagerelement
212 */
213 public function i_add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
42ad096f 214 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()), false);
fcb6ed47
MG
215 }
216
217 /**
218 * Picks the file from repository leaving default values in select file dialogue and confirming to overwrite an existing file.
219 *
220 * @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
221 * @throws ExpectationException Thrown by behat_base::find
222 * @param string $filepath
50900389 223 * @param string $repository
fcb6ed47
MG
224 * @param string $filemanagerelement
225 */
226 public function i_add_and_overwrite_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
42ad096f 227 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()),
fcb6ed47
MG
228 get_string('overwrite', 'repository'));
229 }
230
231 /**
232 * Picks the file from repository filling the form in Select file dialogue.
233 *
234 * @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
235 * @throws ExpectationException Thrown by behat_base::find
236 * @param string $filepath
50900389 237 * @param string $repository
fcb6ed47
MG
238 * @param string $filemanagerelement
239 * @param TableNode $data Data to fill the form in Select file dialogue
240 */
241 public function i_add_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
242 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data, false);
243 }
244
245 /**
246 * Picks the file from repository confirming to overwrite an existing file
247 *
248 * @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
249 * @throws ExpectationException Thrown by behat_base::find
250 * @param string $filepath
50900389 251 * @param string $repository
fcb6ed47
MG
252 * @param string $filemanagerelement
253 * @param TableNode $data Data to fill the form in Select file dialogue
254 */
e3652936
MM
255 public function i_add_and_overwrite_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement,
256 TableNode $data) {
fcb6ed47
MG
257 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data,
258 get_string('overwrite', 'repository'));
259 }
260
261 /**
262 * Picks the file from private files repository
263 *
264 * @throws ExpectationException Thrown by behat_base::find
265 * @param string $filepath
50900389 266 * @param string $repository
fcb6ed47
MG
267 * @param string $filemanagerelement
268 * @param TableNode $data Data to fill the form in Select file dialogue
269 * @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
270 * or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
271 */
272 protected function add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, TableNode $data,
273 $overwriteaction = false) {
274 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
275
fcb6ed47
MG
276 // Opening the select repository window and selecting the upload repository.
277 $this->open_add_file_window($filemanagernode, $repository);
278
279 $this->open_element_contextual_menu($filepath);
280
281 // Fill the form in Select window.
282 $datahash = $data->getRowsHash();
283
284 // The action depends on the field type.
285 foreach ($datahash as $locator => $value) {
fcb6ed47 286
af4830a2 287 $field = behat_field_manager::get_form_field_from_label($locator, $this);
fcb6ed47
MG
288
289 // Delegates to the field class.
290 $field->set_value($value);
291 }
292
e01012e9
RT
293 $selectfilebutton = $this->find_button(get_string('getfile', 'repository'));
294 $this->ensure_node_is_visible($selectfilebutton);
295 $selectfilebutton->click();
fcb6ed47 296
c1faf86b
DM
297 // We wait for all the JS to finish as it is performing an action.
298 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
299
fcb6ed47 300 if ($overwriteaction !== false) {
c1faf86b
DM
301 $overwritebutton = $this->find_button($overwriteaction);
302 $this->ensure_node_is_visible($overwritebutton);
303 $overwritebutton->click();
304
305 // We wait for all the JS to finish.
306 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
fcb6ed47 307 }
c1faf86b 308
fcb6ed47
MG
309 }
310
d87bbb3d 311}