MDL-68353 test: Update filemanager/filepicker xpath selector
[moodle.git] / repository / upload / tests / behat / behat_repository_upload.php
CommitLineData
aa988a9a
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 * Steps definitions for the upload repository type.
19 *
20 * @package repository_upload
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
4aefaf65 28require_once(__DIR__ . '/../../../../lib/behat/core_behat_file_helper.php');
aa988a9a 29
e8edca25
MG
30use Behat\Mink\Exception\DriverException as DriverException,
31 Behat\Mink\Exception\ExpectationException as ExpectationException,
fcb6ed47 32 Behat\Gherkin\Node\TableNode as TableNode;
aa988a9a
DM
33
34/**
fcb6ed47 35 * Steps definitions to deal with the upload repository.
aa988a9a 36 *
aa988a9a
DM
37 * @package repository_upload
38 * @category test
39 * @copyright 2013 David MonllaĆ³
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
4aefaf65
AN
42class behat_repository_upload extends behat_base {
43
44 use core_behat_file_helper;
aa988a9a
DM
45
46 /**
fcb6ed47 47 * Uploads a file to the specified filemanager leaving other fields in upload form default. The paths should be relative to moodle codebase.
fcb6ed47
MG
48 *
49 * @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
e8edca25 50 * @throws DriverException
fcb6ed47
MG
51 * @throws ExpectationException Thrown by behat_base::find
52 * @param string $filepath
53 * @param string $filemanagerelement
54 */
55 public function i_upload_file_to_filemanager($filepath, $filemanagerelement) {
42ad096f 56 $this->upload_file_to_filemanager($filepath, $filemanagerelement, new TableNode(array()), false);
fcb6ed47
MG
57 }
58
59 /**
60 * Uploads a file to the specified filemanager leaving other fields in upload form default and confirms to overwrite an existing file. The paths should be relative to moodle codebase.
61 *
62 * @When /^I upload and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
e8edca25 63 * @throws DriverException
fcb6ed47
MG
64 * @throws ExpectationException Thrown by behat_base::find
65 * @param string $filepath
66 * @param string $filemanagerelement
67 */
68 public function i_upload_and_overwrite_file_to_filemanager($filepath, $filemanagerelement) {
42ad096f 69 $this->upload_file_to_filemanager($filepath, $filemanagerelement, new TableNode(array()),
fcb6ed47
MG
70 get_string('overwrite', 'repository'));
71 }
72
73 /**
74 * Uploads a file to the specified filemanager and confirms to overwrite an existing file. The paths should be relative to moodle codebase.
75 *
76 * @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
e8edca25 77 * @throws DriverException
fcb6ed47
MG
78 * @throws ExpectationException Thrown by behat_base::find
79 * @param string $filepath
80 * @param string $filemanagerelement
81 * @param TableNode $data Data to fill in upload form
82 */
83 public function i_upload_file_to_filemanager_as($filepath, $filemanagerelement, TableNode $data) {
84 $this->upload_file_to_filemanager($filepath, $filemanagerelement, $data, false);
85 }
86
87 /**
88 * Uploads a file to the specified filemanager. The paths should be relative to moodle codebase.
89 *
90 * @When /^I upload and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
e8edca25 91 * @throws DriverException
fcb6ed47
MG
92 * @throws ExpectationException Thrown by behat_base::find
93 * @param string $filepath
94 * @param string $filemanagerelement
95 * @param TableNode $data Data to fill in upload form
96 */
97 public function i_upload_and_overwrite_file_to_filemanager_as($filepath, $filemanagerelement, TableNode $data) {
98 $this->upload_file_to_filemanager($filepath, $filemanagerelement, $data,
99 get_string('overwrite', 'repository'));
100 }
101
102 /**
103 * Uploads a file to filemanager
104 *
e8edca25 105 * @throws DriverException
fcb6ed47 106 * @throws ExpectationException Thrown by behat_base::find
16282275 107 * @param string $filepath Normally a path relative to $CFG->dirroot, but can be an absolute path too.
fcb6ed47
MG
108 * @param string $filemanagerelement
109 * @param TableNode $data Data to fill in upload form
110 * @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
111 * or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
112 */
113 protected function upload_file_to_filemanager($filepath, $filemanagerelement, TableNode $data, $overwriteaction = false) {
aa988a9a
DM
114 global $CFG;
115
e8edca25
MG
116 if (!$this->has_tag('_file_upload')) {
117 throw new DriverException('File upload tests must have the @_file_upload tag on either the scenario or feature.');
118 }
119
fcb6ed47 120 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
aa988a9a
DM
121
122 // Opening the select repository window and selecting the upload repository.
fcb6ed47 123 $this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload'));
aa988a9a 124
50eeae59 125 // Ensure all the form is ready.
50eeae59
DM
126 $noformexception = new ExpectationException('The upload file form is not ready', $this->getSession());
127 $this->find(
e3652936
MM
128 'xpath',
129 "//div[contains(concat(' ', normalize-space(@class), ' '), ' container ')]" .
38976081 130 "[contains(concat(' ', normalize-space(@class), ' '), ' repository_upload ')]" .
e3652936 131 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
63e4df60 132 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
38976081 133 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-upload-form ')]" .
50eeae59 134 "/descendant::form",
e3652936 135 $noformexception
50eeae59
DM
136 );
137 // After this we have the elements we want to interact with.
138
139 // Form elements to interact with.
140 $file = $this->find_file('repo_upload_file');
50eeae59 141
aa988a9a 142 // Attaching specified file to the node.
93fe4d2a 143 // Replace 'admin/' if it is in start of path with $CFG->admin .
16282275
TH
144 if (substr($filepath, 0, 6) === 'admin/') {
145 $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $CFG->admin .
146 DIRECTORY_SEPARATOR . substr($filepath, 6);
93fe4d2a 147 }
e533d14f 148 $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
16282275
TH
149 if (!is_readable($filepath)) {
150 $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
151 if (!is_readable($filepath)) {
152 throw new ExpectationException('The file to be uploaded does not exist.', $this->getSession());
153 }
154 }
155 $file->attachFile($filepath);
aa988a9a 156
fcb6ed47
MG
157 // Fill the form in Upload window.
158 $datahash = $data->getRowsHash();
159
160 // The action depends on the field type.
161 foreach ($datahash as $locator => $value) {
fcb6ed47 162
af4830a2 163 $field = behat_field_manager::get_form_field_from_label($locator, $this);
fcb6ed47
MG
164
165 // Delegates to the field class.
166 $field->set_value($value);
167 }
168
aa988a9a 169 // Submit the file.
fcb6ed47 170 $submit = $this->find_button(get_string('upload', 'repository'));
50eeae59
DM
171 $submit->press();
172
c1faf86b 173 // We wait for all the JS to finish as it is performing an action.
05a5d547 174 $this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS);
c1faf86b 175
fcb6ed47 176 if ($overwriteaction !== false) {
c1faf86b
DM
177 $overwritebutton = $this->find_button($overwriteaction);
178 $this->ensure_node_is_visible($overwritebutton);
179 $overwritebutton->click();
180
181 // We wait for all the JS to finish.
05a5d547 182 $this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS);
fcb6ed47
MG
183 }
184
aa988a9a
DM
185 }
186
e3652936
MM
187 /**
188 * Try to get the filemanager node specified by the element
189 *
190 * @param string $filepickerelement
191 * @return \Behat\Mink\Element\NodeElement
192 * @throws ExpectationException
193 */
194 protected function get_filepicker_node($filepickerelement) {
195
196 // More info about the problem (in case there is a problem).
197 $exception = new ExpectationException('"' . $filepickerelement . '" filepicker can not be found', $this->getSession());
198
199 // If no file picker label is mentioned take the first file picker from the page.
200 if (empty($filepickerelement)) {
201 $filepickercontainer = $this->find(
202 'xpath',
203 "//*[@class=\"form-filemanager\"]",
204 $exception
205 );
206 } else {
4805a5b9
KO
207 // Gets the filemanager node specified by the locator which contains the filepicker container
208 // either for filepickers created by mform or by admin config.
e3652936
MM
209 $filepickerelement = behat_context_helper::escape($filepickerelement);
210 $filepickercontainer = $this->find(
211 'xpath',
58994fe8
SR
212 "//input[./@id = substring-before(//p[normalize-space(.)=$filepickerelement]/@id, '_label')]" .
213 "//ancestor::*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']",
e3652936
MM
214 $exception
215 );
216 }
217
218 return $filepickercontainer;
219 }
220
aa988a9a 221}