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