Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
namespace core;
18
 
19
use file_info_context_course;
20
use file_info_context_coursecat;
21
use file_info_context_module;
22
use file_info_stored;
23
use stdClass;
24
 
25
/**
26
 * Unit tests for file browser
27
 *
28
 * @package    core
29
 * @copyright  2017 Marina Glancy
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
1441 ariadna 32
final class file_browser_test extends \advanced_testcase {
1 efrain 33
 
34
    /** @var int */
35
    protected $initialnonempty;
36
    /** @var int */
37
    protected $initialcategories;
38
    /** @var int */
39
    protected $initialcourses;
40
    /** @var int */
41
    protected $initialjpg;
42
    /** @var stdClass */
43
    protected $course1;
44
    /** @var stdClass */
45
    protected $course2;
46
    /** @var stdClass */
47
    protected $module1;
48
    /** @var stdClass */
49
    protected $module2;
50
    /** @var array */
51
    protected $course1filerecord;
52
    /** @var stdClass */
53
    protected $teacher;
54
    /** @var stdClass */
55
    protected $teacherrole;
56
 
57
    /**
58
     * Set up
59
     */
60
    public function setUp(): void {
61
        global $DB;
1441 ariadna 62
        parent::setUp();
1 efrain 63
        $this->resetAfterTest();
64
 
65
        $this->setAdminUser();
66
 
67
        $browser = get_file_browser();
68
        $fileinfo = $browser->get_file_info(\context_system::instance());
69
        $this->initialnonempty = $fileinfo->count_non_empty_children();
70
        $this->initialcategories = count(array_filter($fileinfo->get_children(), function($a) {
71
            return $a instanceof file_info_context_coursecat;
72
        }));
73
        $this->initialcourses = count(array_filter($fileinfo->get_children(), function($a) {
74
            return $a instanceof file_info_context_course;
75
        }));
76
        $this->initialcourses -= 1; // This includes the site course by default.
77
        $this->initialjpg = $fileinfo->count_non_empty_children(['.jpg']);
78
 
79
        $this->getDataGenerator()->create_category(); // Empty category.
80
        $this->course1 = $this->getDataGenerator()->create_course(); // Empty course.
81
 
82
        $this->course2 = $this->getDataGenerator()->create_course();
83
 
84
        // Add a file to course1 summary.
85
        $coursecontext1 = \context_course::instance($this->course1->id);
86
        $this->course1filerecord = array('contextid' => $coursecontext1->id,
87
            'component' => 'course',
88
            'filearea' => 'summary',
89
            'itemid' => '0',
90
            'filepath' => '/',
91
            'filename' => 'summaryfile.jpg');
92
        $fs = get_file_storage();
93
        $fs->create_file_from_string($this->course1filerecord, 'IMG');
94
 
95
        $this->module1 = $this->getDataGenerator()->create_module('resource', ['course' => $this->course2->id]); // Contains 1 file.
96
        $this->module2 = $this->getDataGenerator()->create_module('assign', ['course' => $this->course2->id]); // Contains no files.
97
 
98
        $this->teacher = $this->getDataGenerator()->create_user();
99
        $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
100
 
101
        // Make sure we're testing what should be the default capabilities.
102
        assign_capability('moodle/restore:viewautomatedfilearea', CAP_ALLOW, $this->teacherrole->id, $coursecontext1);
103
 
104
        $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course1->id, $this->teacherrole->id);
105
        $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course2->id, $this->teacherrole->id);
106
 
107
        $this->setUser($this->teacher);
108
    }
109
 
110
    /**
111
     * Test "Server files" from the system context
112
     */
11 efrain 113
    public function test_file_info_context_system(): void {
1 efrain 114
 
115
        // There is one non-empty category child and two category children.
116
 
117
        $browser = get_file_browser();
118
        $fileinfo = $browser->get_file_info(\context_system::instance());
119
        $this->assertNotEmpty($fileinfo->count_non_empty_children());
120
        $this->assertEquals($this->initialnonempty + 1, count($fileinfo->get_non_empty_children()));
121
        $categorychildren = array_filter($fileinfo->get_children(), function($a) {
122
            return $a instanceof file_info_context_coursecat;
123
        });
124
        $this->assertEquals($this->initialcategories + 1, count($categorychildren));
125
    }
126
 
127
    /**
128
     * Test "Server files" from the system context, hide Misc category
129
     */
11 efrain 130
    public function test_file_info_context_system_hidden(): void {
1 efrain 131
 
132
        // Hide the course category that contains our two courses. Teacher does not have cap to view hidden categories.
133
        \core_course_category::get($this->course1->category)->update(['visible' => 0]);
134
 
135
        // We should have two non-empty children in system context (courses).
136
        $browser = get_file_browser();
137
        $fileinfo = $browser->get_file_info(\context_system::instance());
138
        $this->assertNotEmpty($fileinfo->count_non_empty_children());
139
        $this->assertEquals($this->initialnonempty + 2, count($fileinfo->get_non_empty_children()));
140
 
141
        // Should be 1 category children (empty category).
142
        $categorychildren = array_filter($fileinfo->get_children(), function($a) {
143
            return $a instanceof file_info_context_coursecat;
144
        });
145
        $this->assertEquals($this->initialcategories, count($categorychildren));
146
 
147
        // Should be 2 course children - courses that belonged to hidden subcategory are now direct children of "System".
148
        $coursechildren = array_filter($fileinfo->get_children(), function($a) {
149
            return $a instanceof file_info_context_course;
150
        });
151
        $this->assertEquals($this->initialcourses + 2, count($coursechildren));
152
    }
153
 
154
    /**
155
     * Test "Server files" from the course category context
156
     */
11 efrain 157
    public function test_file_info_context_coursecat(): void {
1 efrain 158
 
159
        // There are two non-empty courses.
160
 
161
        $browser = get_file_browser();
162
        $fileinfo = $browser->get_file_info(\context_coursecat::instance($this->course2->category));
163
        $this->assertNotEmpty($fileinfo->count_non_empty_children());
164
        $this->assertEquals(2, count($fileinfo->get_non_empty_children()));
165
        $coursechildren = array_filter($fileinfo->get_children(), function($a) {
166
            return $a instanceof file_info_context_course;
167
        });
168
        $this->assertEquals($this->initialcourses + 2, count($coursechildren));
169
    }
170
 
171
    /**
172
     * Test "Server files" from the course category context, only look for .jpg
173
     */
11 efrain 174
    public function test_file_info_context_coursecat_jpg(): void {
1 efrain 175
 
176
        // There is one non-empty category child and two category children.
177
 
178
        $browser = get_file_browser();
179
        $fileinfo = $browser->get_file_info(\context_system::instance());
180
        $this->assertNotEmpty($fileinfo->count_non_empty_children(['.jpg']));
181
        $this->assertEquals($this->initialjpg + 1, count($fileinfo->get_non_empty_children(['.jpg'])));
182
    }
183
 
184
    /**
185
     * Test "Server files" from the course context (course1)
186
     */
11 efrain 187
    public function test_file_info_context_course_1(): void {
1 efrain 188
 
189
        $browser = get_file_browser();
190
        $fileinfo = $browser->get_file_info(\context_course::instance($this->course1->id));
191
        // Fileinfo element has only one non-empty child - "Course summary" file area.
192
        $this->assertNotEmpty($fileinfo->count_non_empty_children());
193
        $nonemptychildren = $fileinfo->get_non_empty_children();
194
        $this->assertEquals(1, count($nonemptychildren));
195
        $child = reset($nonemptychildren);
196
        $this->assertTrue($child instanceof file_info_stored);
197
        $this->assertEquals(['filename' => '.'] + $this->course1filerecord, $child->get_params());
198
        // Filearea "Course summary" has a child that is the actual image file.
199
        $this->assertEquals($this->course1filerecord, $child->get_children()[0]->get_params());
200
 
201
        // There are seven course-level file areas available to teachers with default caps and no modules in this course.
202
        $allchildren = $fileinfo->get_children();
203
        $this->assertEquals(7, count($allchildren));
204
        $modulechildren = array_filter($allchildren, function($a) {
205
            return $a instanceof file_info_context_module;
206
        });
207
        $this->assertEquals(0, count($modulechildren));
208
 
209
        // Admin can see seven course-level file areas.
210
        $this->setAdminUser();
211
        $fileinfo = $browser->get_file_info(\context_course::instance($this->course1->id));
212
        $this->assertEquals(7, count($fileinfo->get_children()));
213
    }
214
 
215
    /**
216
     * Test "Server files" from the course context (course1)
217
     */
11 efrain 218
    public function test_file_info_context_course_2(): void {
1 efrain 219
 
220
        // 2. Start from the course level.
221
        $browser = get_file_browser();
222
        $fileinfo = $browser->get_file_info(\context_course::instance($this->course2->id));
223
        $this->assertNotEmpty($fileinfo->count_non_empty_children());
224
        $nonemptychildren = $fileinfo->get_non_empty_children();
225
        $this->assertEquals(1, count($nonemptychildren));
226
        $child = reset($nonemptychildren);
227
        $this->assertTrue($child instanceof file_info_context_module);
228
        $this->assertEquals($this->module1->name.' (File)', $child->get_visible_name());
229
        $this->assertEquals(1, count($child->get_non_empty_children()));
230
        $this->assertEquals(1, $child->count_non_empty_children());
231
        $modulechildren = array_filter($fileinfo->get_children(), function($a) {
232
            return $a instanceof file_info_context_module;
233
        });
234
        $this->assertEquals(2, count($modulechildren));
235
    }
236
 
237
    /**
238
     * Test "Server files" from the course context (module1)
239
     */
11 efrain 240
    public function test_file_info_context_module_1(): void {
1 efrain 241
 
242
        $module1context = \context_module::instance($this->module1->cmid);
243
        $browser = get_file_browser();
244
        $fileinfo = $browser->get_file_info($module1context);
245
        $this->assertEquals($this->module1->name . ' (File)', $fileinfo->get_visible_name());
246
        $this->assertNotEmpty($fileinfo->count_non_empty_children());
247
        $nonemptychildren = $fileinfo->get_non_empty_children();
248
        $this->assertEquals(1, count($nonemptychildren));
249
        $child = reset($nonemptychildren);
250
        $this->assertTrue($child instanceof file_info_stored);
251
    }
252
 
253
    /**
254
     * Test "Server files" from the course context (module1)
255
     */
11 efrain 256
    public function test_file_info_context_module_2(): void {
1 efrain 257
 
258
        $module2context = \context_module::instance($this->module2->cmid);
259
        $browser = get_file_browser();
260
        $fileinfo = $browser->get_file_info($module2context);
261
        $this->assertEquals($this->module2->name.' (Assignment)', $fileinfo->get_visible_name());
262
        $this->assertEmpty($fileinfo->count_non_empty_children());
263
        $nonemptychildren = $fileinfo->get_non_empty_children();
264
        $this->assertEquals(0, count($nonemptychildren));
265
 
266
    }
267
}