2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Filemanager and filepicker manipulation steps definitions overrides.
20 * @package theme_bootstrapbase
22 * @copyright 2016 Damyon Wiese
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__ . '/theme_bootstrapbase_behat_file_helper.php');
29 require_once(__DIR__ . '/../../../../repository/tests/behat/behat_filepicker.php');
31 use Behat\Mink\Exception\ExpectationException as ExpectationException,
32 Behat\Gherkin\Node\TableNode as TableNode;
35 * Steps definitions to deal with the filemanager and filepicker overrides.
37 * @copyright 2016 Damyon Wiese
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class behat_theme_bootstrapbase_behat_filepicker extends behat_theme_bootstrapbase_behat_files {
41 public function i_create_folder_in_filemanager($foldername, $filemanagerelement) {
43 $fieldnode = $this->get_filepicker_node($filemanagerelement);
45 // Looking for the create folder button inside the specified filemanager.
46 $exception = new ExpectationException('No folders can be created in "'.$filemanagerelement.'" filemanager',$this->getSession());
47 $newfolder = $this->find('css', 'div.fp-btn-mkdir a', $exception, $fieldnode);
50 // Setting the folder name in the modal window.
51 $exception = new ExpectationException('The dialog to enter the folder name does not appear', $this->getSession());
52 $dialoginput = $this->find('css', '.fp-mkdir-dlg-text input', $exception);
53 $dialoginput->setValue($foldername);
55 $exception = new ExpectationException('The button for the create folder dialog can not be located', $this->getSession());
56 $dialognode = $this->find('css', '.moodle-dialogue-focused');
57 $buttonnode = $this->find('css', '.fp-dlg-butcreate', $exception, $dialognode);
61 public function i_open_folder_from_filemanager($foldername, $filemanagerelement) {
63 $fieldnode = $this->get_filepicker_node($filemanagerelement);
65 $exception = new ExpectationException(
66 'The "'.$foldername.'" folder can not be found in the "'.$filemanagerelement.'" filemanager',
70 $folderliteral = behat_context_helper::escape($foldername);
72 // We look both in the pathbar and in the contents.
75 // In the current folder workspace.
76 $folder = $this->find(
78 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
79 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]" .
80 "[normalize-space(.)=$folderliteral]",
84 } catch (ExpectationException $e) {
86 // And in the pathbar.
87 $folder = $this->find(
89 "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')]" .
90 "[normalize-space(.)=$folderliteral]",
96 // It should be a NodeElement, otherwise an exception would have been thrown.
100 public function i_unzip_file_from_filemanager($filename, $filemanagerelement) {
102 // Open the contextual menu of the filemanager element.
103 $this->open_element_contextual_menu($filename, $filemanagerelement);
105 // Execute the action.
106 $exception = new ExpectationException($filename.' element can not be unzipped', $this->getSession());
107 $this->perform_on_element('unzip', $exception);
110 public function i_zip_folder_from_filemanager($foldername, $filemanagerelement) {
112 // Open the contextual menu of the filemanager element.
113 $this->open_element_contextual_menu($foldername, $filemanagerelement);
115 // Execute the action.
116 $exception = new ExpectationException($foldername.' element can not be zipped', $this->getSession());
117 $this->perform_on_element('zip', $exception);
120 public function i_delete_file_from_filemanager($name, $filemanagerelement) {
122 // Open the contextual menu of the filemanager element.
123 $this->open_element_contextual_menu($name, $filemanagerelement);
125 // Execute the action.
126 $exception = new ExpectationException($name.' element can not be deleted', $this->getSession());
127 $this->perform_on_element('delete', $exception);
130 // Using xpath + click instead of pressButton as 'Ok' it is a common string.
131 $okbutton = $this->find('css', 'div.fp-dlg button.fp-dlg-butconfirm');
135 public function i_should_see_elements_in_filemanager($elementscount, $filemanagerelement) {
136 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
138 // We count .fp-file elements inside a filemanager not being updated.
139 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
140 "[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
141 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
142 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]";
144 $elements = $this->find_all('xpath', $xpath, false, $filemanagernode);
145 if (count($elements) != $elementscount) {
146 throw new ExpectationException('Found '.count($elements).' elements in filemanager instead of expected '.$elementscount, $this->getSession());
150 public function i_add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
151 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()), false);
154 public function i_add_and_overwrite_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
155 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()),
156 get_string('overwrite', 'repository'));
159 public function i_add_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
160 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data, false);
163 public function i_add_and_overwrite_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
164 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data,
165 get_string('overwrite', 'repository'));
168 protected function add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, TableNode $data,
169 $overwriteaction = false) {
170 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
172 // Opening the select repository window and selecting the upload repository.
173 $this->open_add_file_window($filemanagernode, $repository);
175 $this->open_element_contextual_menu($filepath);
177 // Fill the form in Select window.
178 $datahash = $data->getRowsHash();
180 // The action depends on the field type.
181 foreach ($datahash as $locator => $value) {
183 $field = behat_field_manager::get_form_field_from_label($locator, $this);
185 // Delegates to the field class.
186 $field->set_value($value);
189 $selectfilebutton = $this->find_button(get_string('getfile', 'repository'));
190 $this->ensure_node_is_visible($selectfilebutton);
191 $selectfilebutton->click();
193 // We wait for all the JS to finish as it is performing an action.
194 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
196 if ($overwriteaction !== false) {
197 $overwritebutton = $this->find_button($overwriteaction);
198 $this->ensure_node_is_visible($overwritebutton);
199 $overwritebutton->click();
201 // We wait for all the JS to finish.
202 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);