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 * Override definitions for the upload repository type.
20 * @copyright 2016 Damyon Wiese
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
26 require_once(__DIR__ . '/../../../../repository/upload/tests/behat/behat_repository_upload.php');
28 use Behat\Mink\Exception\ExpectationException as ExpectationException,
29 Behat\Gherkin\Node\TableNode as TableNode;
32 * Override steps definitions to deal with the upload repository.
34 * @copyright 2016 Damyon Wiese
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class behat_theme_boost_behat_repository_upload extends behat_repository_upload {
39 protected function get_filepicker_node($filepickerelement) {
41 // More info about the problem (in case there is a problem).
42 $exception = new ExpectationException('"' . $filepickerelement . '" filepicker can not be found', $this->getSession());
44 // If no file picker label is mentioned take the first file picker from the page.
45 if (empty($filepickerelement)) {
46 $filepickercontainer = $this->find(
48 "//*[@class=\"form-filemanager\"]",
52 // Gets the ffilemanager node specified by the locator which contains the filepicker container.
53 $filepickerelement = behat_context_helper::escape($filepickerelement);
54 $filepickercontainer = $this->find(
56 "//input[./@id = //label[normalize-space(.)=$filepickerelement]/@for]" .
57 "//ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' felement ')]",
62 return $filepickercontainer;
65 protected function upload_file_to_filemanager($filepath, $filemanagerelement, TableNode $data, $overwriteaction = false) {
68 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
70 // Opening the select repository window and selecting the upload repository.
71 $this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload'));
73 // Ensure all the form is ready.
74 $noformexception = new ExpectationException('The upload file form is not ready', $this->getSession());
77 "//div[contains(concat(' ', normalize-space(@class), ' '), ' container ')]" .
78 "[contains(concat(' ', normalize-space(@class), ' '), ' repository_upload ')]" .
79 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
80 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
81 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-upload-form ')]" .
85 // After this we have the elements we want to interact with.
87 // Form elements to interact with.
88 $file = $this->find_file('repo_upload_file');
90 // Attaching specified file to the node.
91 // Replace 'admin/' if it is in start of path with $CFG->admin .
92 if (substr($filepath, 0, 6) === 'admin/') {
93 $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $CFG->admin .
94 DIRECTORY_SEPARATOR . substr($filepath, 6);
96 $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
97 if (!is_readable($filepath)) {
98 $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
99 if (!is_readable($filepath)) {
100 throw new ExpectationException('The file to be uploaded does not exist.', $this->getSession());
103 $file->attachFile($filepath);
105 // Fill the form in Upload window.
106 $datahash = $data->getRowsHash();
108 // The action depends on the field type.
109 foreach ($datahash as $locator => $value) {
111 $field = behat_field_manager::get_form_field_from_label($locator, $this);
113 // Delegates to the field class.
114 $field->set_value($value);
118 $submit = $this->find_button(get_string('upload', 'repository'));
121 // We wait for all the JS to finish as it is performing an action.
122 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
124 if ($overwriteaction !== false) {
125 $overwritebutton = $this->find_button($overwriteaction);
126 $this->ensure_node_is_visible($overwritebutton);
127 $overwritebutton->click();
129 // We wait for all the JS to finish.
130 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);