MDL-58428 theme: Shift templates ready for Bootstrapbase removal
[moodle.git] / theme / bootstrapbase / tests / behat / behat_theme_bootstrapbase_behat_filepicker.php
CommitLineData
63e4df60
DW
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 * Filemanager and filepicker manipulation steps definitions overrides.
19 *
e3652936
MM
20 * @package theme_bootstrapbase
21 * @category test
63e4df60
DW
22 * @copyright 2016 Damyon Wiese
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
e3652936
MM
28require_once(__DIR__ . '/theme_bootstrapbase_behat_file_helper.php');
29require_once(__DIR__ . '/../../../../repository/tests/behat/behat_filepicker.php');
63e4df60
DW
30
31use Behat\Mink\Exception\ExpectationException as ExpectationException,
32 Behat\Gherkin\Node\TableNode as TableNode;
33
34/**
35 * Steps definitions to deal with the filemanager and filepicker overrides.
36 *
37 * @copyright 2016 Damyon Wiese
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
e3652936 40class behat_theme_bootstrapbase_behat_filepicker extends behat_theme_bootstrapbase_behat_files {
63e4df60
DW
41 public function i_create_folder_in_filemanager($foldername, $filemanagerelement) {
42
43 $fieldnode = $this->get_filepicker_node($filemanagerelement);
44
45 // Looking for the create folder button inside the specified filemanager.
e3652936 46 $exception = new ExpectationException('No folders can be created in "'.$filemanagerelement.'" filemanager',$this->getSession());
63e4df60
DW
47 $newfolder = $this->find('css', 'div.fp-btn-mkdir a', $exception, $fieldnode);
48 $newfolder->click();
49
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);
54
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);
58 $buttonnode->click();
59 }
60
63e4df60
DW
61 public function i_open_folder_from_filemanager($foldername, $filemanagerelement) {
62
63 $fieldnode = $this->get_filepicker_node($filemanagerelement);
64
65 $exception = new ExpectationException(
e3652936
MM
66 'The "'.$foldername.'" folder can not be found in the "'.$filemanagerelement.'" filemanager',
67 $this->getSession()
63e4df60
DW
68 );
69
70 $folderliteral = behat_context_helper::escape($foldername);
71
72 // We look both in the pathbar and in the contents.
73 try {
74
75 // In the current folder workspace.
76 $folder = $this->find(
e3652936
MM
77 'xpath',
78 "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
63e4df60
DW
79 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]" .
80 "[normalize-space(.)=$folderliteral]",
e3652936
MM
81 $exception,
82 $fieldnode
63e4df60
DW
83 );
84 } catch (ExpectationException $e) {
85
86 // And in the pathbar.
87 $folder = $this->find(
e3652936
MM
88 'xpath',
89 "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')]" .
63e4df60 90 "[normalize-space(.)=$folderliteral]",
e3652936
MM
91 $exception,
92 $fieldnode
63e4df60
DW
93 );
94 }
95
96 // It should be a NodeElement, otherwise an exception would have been thrown.
97 $folder->click();
98 }
99
63e4df60
DW
100 public function i_unzip_file_from_filemanager($filename, $filemanagerelement) {
101
102 // Open the contextual menu of the filemanager element.
103 $this->open_element_contextual_menu($filename, $filemanagerelement);
104
105 // Execute the action.
106 $exception = new ExpectationException($filename.' element can not be unzipped', $this->getSession());
107 $this->perform_on_element('unzip', $exception);
108 }
109
63e4df60
DW
110 public function i_zip_folder_from_filemanager($foldername, $filemanagerelement) {
111
112 // Open the contextual menu of the filemanager element.
113 $this->open_element_contextual_menu($foldername, $filemanagerelement);
114
115 // Execute the action.
116 $exception = new ExpectationException($foldername.' element can not be zipped', $this->getSession());
117 $this->perform_on_element('zip', $exception);
118 }
119
63e4df60
DW
120 public function i_delete_file_from_filemanager($name, $filemanagerelement) {
121
122 // Open the contextual menu of the filemanager element.
123 $this->open_element_contextual_menu($name, $filemanagerelement);
124
125 // Execute the action.
126 $exception = new ExpectationException($name.' element can not be deleted', $this->getSession());
127 $this->perform_on_element('delete', $exception);
128
129 // Yes, we are sure.
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');
132 $okbutton->click();
133 }
134
63e4df60
DW
135 public function i_should_see_elements_in_filemanager($elementscount, $filemanagerelement) {
136 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
137
138 // We count .fp-file elements inside a filemanager not being updated.
139 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
e3652936
MM
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 ')]";
63e4df60
DW
143
144 $elements = $this->find_all('xpath', $xpath, false, $filemanagernode);
145 if (count($elements) != $elementscount) {
e3652936 146 throw new ExpectationException('Found '.count($elements).' elements in filemanager instead of expected '.$elementscount, $this->getSession());
63e4df60
DW
147 }
148 }
149
63e4df60
DW
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);
152 }
153
63e4df60
DW
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'));
157 }
158
63e4df60
DW
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);
161 }
162
e3652936 163 public function i_add_and_overwrite_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
63e4df60
DW
164 $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data,
165 get_string('overwrite', 'repository'));
166 }
167
63e4df60
DW
168 protected function add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, TableNode $data,
169 $overwriteaction = false) {
170 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
171
172 // Opening the select repository window and selecting the upload repository.
173 $this->open_add_file_window($filemanagernode, $repository);
174
175 $this->open_element_contextual_menu($filepath);
176
177 // Fill the form in Select window.
178 $datahash = $data->getRowsHash();
179
180 // The action depends on the field type.
181 foreach ($datahash as $locator => $value) {
182
183 $field = behat_field_manager::get_form_field_from_label($locator, $this);
184
185 // Delegates to the field class.
186 $field->set_value($value);
187 }
188
189 $selectfilebutton = $this->find_button(get_string('getfile', 'repository'));
190 $this->ensure_node_is_visible($selectfilebutton);
191 $selectfilebutton->click();
192
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);
195
196 if ($overwriteaction !== false) {
197 $overwritebutton = $this->find_button($overwriteaction);
198 $this->ensure_node_is_visible($overwritebutton);
199 $overwritebutton->click();
200
201 // We wait for all the JS to finish.
202 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
203 }
204
205 }
206
207}