Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * File manager form element.
19
 *
20
 * @package    core_form
21
 * @category   test
22
 * @copyright  2014 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
 
28
require_once(__DIR__  . '/behat_form_field.php');
29
 
30
/**
31
 * File manager form field.
32
 *
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
41
 *
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
47
 *     the same locator.
48
 * - Match: A comma-separated list of file names.
49
 *
50
 * @package    core_form
51
 * @category   test
52
 * @copyright  2014 David Monllaó
53
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
54
 */
55
class behat_form_filemanager extends behat_form_field {
56
 
57
    /**
58
     * Gets the value.
59
     *
60
     * @return string A comma-separated list of the root directory file names.
61
     */
62
    public function get_value() {
63
 
64
        // Wait until DOM and JS is ready.
65
        $this->session->wait(behat_base::get_timeout(), behat_base::PAGE_READY_JS);
66
 
67
        // Get the label to restrict the files to this single form field.
68
        $fieldlabel = $this->get_field_locator();
69
 
70
        // Get the name of the current directory elements.
71
        $xpath = "//p[normalize-space(.)='$fieldlabel']" .
72
            "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' fitem ')]" .
73
            "/descendant::div[@data-fieldtype = 'filemanager']" .
74
            "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]";
75
 
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);
79
 
80
        if (!$files) {
81
            return '';
82
        }
83
 
84
        $filenames = array();
85
        foreach ($files as $filenode) {
86
            $filenames[] = $filenode->getText();
87
        }
88
 
89
        return implode(',', $filenames);
90
    }
91
 
92
    /**
93
     * Sets the field value.
94
     *
95
     * @param string $value
96
     * @return void
97
     */
98
    public function set_value($value) {
99
 
100
        // Getting the filemanager label from the DOM.
101
        $fieldlabel = $this->get_field_locator();
102
 
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);
107
    }
108
 
109
    /**
110
     * Matches the provided filename/s against the current field value.
111
     *
112
     * If the filemanager contains more than one file the $expectedvalue
113
     * value should include all the file names separating them by comma.
114
     *
115
     * @param string $expectedvalue
116
     * @return bool The provided value matches the field value?
117
     */
118
    public function matches($expectedvalue) {
119
        return $this->text_matches($expectedvalue);
120
    }
121
 
122
}