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
* File manager form element.
22
* @copyright 2014 David Monllaó
23
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28
require_once(__DIR__ . '/behat_form_field.php');
31
* File manager form field.
33
* Simple filemanager field manager to allow
34
* forms to be filled using TableNodes. It only
35
* adds files and checks the field contents in the
36
* root directory. If you want to run complex actions
37
* that involves subdirectories or other repositories
38
* than 'Upload a file' you should use steps related with
39
* behat_filepicker::i_add_file_from_repository_to_filemanager
40
* this is intended to be used with multi-field
42
* This field manager allows you to:
43
* - Get: A comma-separated list of the root directory
44
* file names, including folders.
45
* - Set: Add a file, in case you want to add more than
46
* one file you can always set two table rows using
48
* - Match: A comma-separated list of file names.
52
* @copyright 2014 David Monllaó
53
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
55
class behat_form_filemanager extends behat_form_field {
60
* @return string A comma-separated list of the root directory file names.
62
public function get_value() {
64
// Wait until DOM and JS is ready.
65
$this->session->wait(behat_base::TIMEOUT, behat_base::PAGE_READY_JS);
67
// Get the label to restrict the files to this single form field.
68
$fieldlabel = $this->get_field_locator();
70
// Get the name of the current directory elements.
71
$xpath = "//label[contains(., '" . $fieldlabel . "')]" .
72
"/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' fitemtitle ')]" .
73
"/following-sibling::div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ')]" .
74
"/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]";
76
// We don't need to wait here, also we don't have access to protected
77
// contexts find* methods.
78
$files = $this->session->getPage()->findAll('xpath', $xpath);
85
foreach ($files as $filenode) {
86
$filenames[] = $filenode->getText();
89
return implode(',', $filenames);
93
* Sets the field value.
95
* @param string $value
98
public function set_value($value) {
100
// Getting the filemanager label from the DOM.
101
$fieldlabel = $this->get_field_locator();
103
// Getting the filepicker context and using the step definition
104
// to upload the requested file.
105
$uploadcontext = behat_context_helper::get('behat_repository_upload');
106
$uploadcontext->i_upload_file_to_filemanager($value, $fieldlabel);
110
* Matches the provided filename/s against the current field value.
112
* If the filemanager contains more than one file the $expectedvalue
113
* value should include all the file names separating them by comma.
115
* @param string $expectedvalue
116
* @return bool The provided value matches the field value?
118
public function matches($expectedvalue) {
119
return $this->text_matches($expectedvalue);