MDL-58428 theme: Shift templates ready for Bootstrapbase removal
[moodle.git] / theme / bootstrapbase / tests / behat / behat_theme_bootstrapbase_behat_repository_upload.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 * Override definitions for the upload repository type.
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
28require_once(__DIR__ . '/../../../../repository/upload/tests/behat/behat_repository_upload.php');
29
30use Behat\Mink\Exception\ExpectationException as ExpectationException,
31 Behat\Gherkin\Node\TableNode as TableNode;
32
33/**
34 * Override steps definitions to deal with the upload repository.
35 *
e3652936
MM
36 * @package theme_bootstrapbase
37 * @category test
63e4df60
DW
38 * @copyright 2016 Damyon Wiese
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
e3652936 41class behat_theme_bootstrapbase_behat_repository_upload extends behat_repository_upload {
63e4df60
DW
42
43 protected function upload_file_to_filemanager($filepath, $filemanagerelement, TableNode $data, $overwriteaction = false) {
44 global $CFG;
45
46 $filemanagernode = $this->get_filepicker_node($filemanagerelement);
47
48 // Opening the select repository window and selecting the upload repository.
49 $this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload'));
50
51 // Ensure all the form is ready.
52 $noformexception = new ExpectationException('The upload file form is not ready', $this->getSession());
53 $this->find(
e3652936
MM
54 'xpath',
55 "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
63e4df60
DW
56 "[contains(concat(' ', normalize-space(@class), ' '), ' repository_upload ')]" .
57 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
58 "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-upload-form ')]" .
59 "/descendant::form",
e3652936 60 $noformexception
63e4df60
DW
61 );
62 // After this we have the elements we want to interact with.
63
64 // Form elements to interact with.
65 $file = $this->find_file('repo_upload_file');
66
67 // Attaching specified file to the node.
68 // Replace 'admin/' if it is in start of path with $CFG->admin .
69 if (substr($filepath, 0, 6) === 'admin/') {
70 $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $CFG->admin .
71 DIRECTORY_SEPARATOR . substr($filepath, 6);
72 }
73 $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
74 if (!is_readable($filepath)) {
75 $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
76 if (!is_readable($filepath)) {
77 throw new ExpectationException('The file to be uploaded does not exist.', $this->getSession());
78 }
79 }
80 $file->attachFile($filepath);
81
82 // Fill the form in Upload window.
83 $datahash = $data->getRowsHash();
84
85 // The action depends on the field type.
86 foreach ($datahash as $locator => $value) {
87
88 $field = behat_field_manager::get_form_field_from_label($locator, $this);
89
90 // Delegates to the field class.
91 $field->set_value($value);
92 }
93
94 // Submit the file.
95 $submit = $this->find_button(get_string('upload', 'repository'));
96 $submit->press();
97
98 // We wait for all the JS to finish as it is performing an action.
99 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
100
101 if ($overwriteaction !== false) {
102 $overwritebutton = $this->find_button($overwriteaction);
103 $this->ensure_node_is_visible($overwritebutton);
104 $overwritebutton->click();
105
106 // We wait for all the JS to finish.
107 $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
108 }
109
110 }
111
112}