MDL-39348 behat: Chaining strings instead of splitting strings in lines
[moodle.git] / repository / tests / behat / behat_filepicker.php
blob8b83ae3ecaf97422028f328771d81e96b10e4ee9
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 and filepicker manipulation steps definitions.
20 * @package core
21 * @category test
22 * @copyright 2013 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../../lib/behat/behat_files.php');
30 use Behat\Mink\Exception\ExpectationException as ExpectationException;
32 /**
33 * Steps definitions to deal with the filepicker.
35 * Extends behat_files rather than behat_base as is file-related.
37 * @package core
38 * @category test
39 * @copyright 2013 David MonllaĆ³
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class behat_filepicker extends behat_files {
44 /**
45 * Creates a folder with specified name in the current folder and in the specified filepicker field.
47 * @Given /^I create "(?P<foldername_string>(?:[^"]|\\")*)" folder in "(?P<filepicker_field_string>(?:[^"]|\\")*)" filepicker$/
48 * @throws ExpectationException Thrown by behat_base::find
49 * @param string $foldername
50 * @param string $filepickerelement
52 public function i_create_folder_in_filepicker($foldername, $filepickerelement) {
54 $fieldnode = $this->get_filepicker_node($filepickerelement);
56 // Looking for the create folder button inside the specified filepicker.
57 $exception = new ExpectationException('No folders can be created in "'.$filepickerelement.'" filepicker', $this->getSession());
58 $newfolder = $this->find('css', 'div.fp-btn-mkdir a', $exception, $fieldnode);
59 $newfolder->click();
61 // Setting the folder name in the modal window.
62 $exception = new ExpectationException('The dialog to enter the folder name does not appear', $this->getSession());
63 $dialoginput = $this->find('css', '.fp-mkdir-dlg-text input');
64 $dialoginput->setValue($foldername);
66 $this->getSession()->getPage()->pressButton('Create folder');
68 // Wait few seconds for the folder to be created and filepicker contents reloaded.
69 $this->getSession()->wait(4 * 1000, false);
72 /**
73 * Opens the contents of a filepicker folder. It looks for the folder in the current folder and in the path bar.
75 * @Given /^I open "(?P<foldername_string>(?:[^"]|\\")*)" folder from "(?P<filepicker_field_string>(?:[^"]|\\")*)" filepicker$/
76 * @throws ExpectationException Thrown by behat_base::find
77 * @param string $foldername
78 * @param string $filepickerelement
80 public function i_open_folder_from_filepicker($foldername, $filepickerelement) {
82 $fieldnode = $this->get_filepicker_node($filepickerelement);
84 $exception = new ExpectationException(
85 'The "'.$foldername.'" folder can not be found in the "'.$filepickerelement.'" filepicker',
86 $this->getSession()
89 // We look both in the pathbar and in the contents.
90 try {
92 // In the current folder workspace.
93 $folder = $this->find(
94 'xpath',
95 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
96 "//descendant::div[contains(concat(' ', normalize-space(.), ' '), '" . $foldername . "')]",
97 $exception,
98 $fieldnode
100 } catch (ExpectationException $e) {
102 // And in the pathbar.
103 $folder = $this->find(
104 'xpath',
105 "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')]" .
106 "[contains(concat(' ', normalize-space(.), ' '), '" . $foldername . "')]",
107 $exception,
108 $fieldnode
112 // It should be a NodeElement, otherwise an exception would have been thrown.
113 $folder->click();
115 // Wait few seconds for the filepicker contents to be updated.
116 $this->getSession()->wait(4 * 1000, false);
120 * Unzips the specified file from the specified filepicker field. The zip file has to be visible in the current folder.
122 * @Given /^I unzip "(?P<filename_string>(?:[^"]|\\")*)" file from "(?P<filepicker_field_string>(?:[^"]|\\")*)" filepicker$/
123 * @throws ExpectationException Thrown by behat_base::find
124 * @param string $filename
125 * @param string $filepickerelement
127 public function i_unzip_file_from_filepicker($filename, $filepickerelement) {
129 // Open the contextual menu of the filepicker element.
130 $this->open_element_contextual_menu($filename, $filepickerelement);
132 // Execute the action.
133 $exception = new ExpectationException($filename.' element can not be unzipped', $this->getSession());
134 $this->perform_on_element('unzip', $exception);
136 // Wait few seconds.
137 // Here the time will depend on the zip contents and the server load, so it better to be conservative.
138 $this->getSession()->wait(8 * 1000, false);
142 * Zips the specified folder from the specified filepicker field. The folder has to be in the current folder.
144 * @Given /^I zip "(?P<filename_string>(?:[^"]|\\")*)" folder from "(?P<filepicker_field_string>(?:[^"]|\\")*)" filepicker$/
145 * @throws ExpectationException Thrown by behat_base::find
146 * @param string $foldername
147 * @param string $filepickerelement
149 public function i_zip_folder_from_filepicker($foldername, $filepickerelement) {
151 // Open the contextual menu of the filepicker element.
152 $this->open_element_contextual_menu($foldername, $filepickerelement);
154 // Execute the action.
155 $exception = new ExpectationException($foldername.' element can not be zipped', $this->getSession());
156 $this->perform_on_element('zip', $exception);
158 // Wait few seconds.
159 // Here the time will depend on the folder contents and the server load, so it better to be conservative.
160 $this->getSession()->wait(8 * 1000, false);
164 * Deletes the specified file or folder from the specified filepicker field.
166 * @Given /^I delete "(?P<file_or_folder_name_string>(?:[^"]|\\")*)" from "(?P<filepicker_field_string>(?:[^"]|\\")*)" filepicker$/
167 * @throws ExpectationException Thrown by behat_base::find
168 * @param string $foldername
169 * @param string $filepickerelement
171 public function i_delete_file_from_filepicker($name, $filepickerelement) {
173 // Open the contextual menu of the filepicker element.
174 $this->open_element_contextual_menu($name, $filepickerelement);
176 // Execute the action.
177 $exception = new ExpectationException($name.' element can not be deleted', $this->getSession());
178 $this->perform_on_element('delete', $exception);
180 // Yes, we are sure.
181 // Using xpath + click instead of pressButton as 'Ok' it is a common string.
182 $okbutton = $this->find('css', 'div.fp-dlg button.fp-dlg-butconfirm');
183 $okbutton->click();
185 // Wait few seconds until filepicker contents are reloaded.
186 $this->getSession()->wait(4 * 1000, false);