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