Merge branch 'm311_MDL_32226' of https://github.com/danmarsden/moodle into MOODLE_311...
[moodle.git] / repository / tests / repositorylib_test.php
blob00211c9f37f6fc1174a7317cbce6ff7539bef2c2
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.
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/>.
17 /**
18 * Repository API unit tests
20 * @package repository
21 * @category phpunit
22 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once("$CFG->dirroot/repository/lib.php");
31 class core_repositorylib_testcase extends advanced_testcase {
33 /**
34 * Installing repository tests
36 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
38 public function test_install_repository() {
39 global $CFG, $DB;
41 $this->resetAfterTest(true);
43 $syscontext = context_system::instance();
44 $repositorypluginname = 'boxnet';
45 // override repository permission
46 $capability = 'repository/' . $repositorypluginname . ':view';
47 $guestroleid = $DB->get_field('role', 'id', array('shortname' => 'guest'));
48 assign_capability($capability, CAP_ALLOW, $guestroleid, $syscontext->id, true);
50 $plugintype = new repository_type($repositorypluginname);
51 $pluginid = $plugintype->create(false);
52 $this->assertIsInt($pluginid);
53 $args = array();
54 $args['type'] = $repositorypluginname;
55 $repos = repository::get_instances($args);
56 $repository = reset($repos);
57 $this->assertInstanceOf('repository', $repository);
58 $info = $repository->get_meta();
59 $this->assertEquals($repositorypluginname, $info->type);
62 public function test_get_unused_filename() {
63 global $USER;
65 $this->resetAfterTest(true);
67 $this->setAdminUser();
68 $fs = get_file_storage();
70 $draftitemid = null;
71 $context = context_user::instance($USER->id);
72 file_prepare_draft_area($draftitemid, $context->id, 'phpunit', 'test_get_unused_filename', 1);
74 $dummy = array(
75 'contextid' => $context->id,
76 'component' => 'user',
77 'filearea' => 'draft',
78 'itemid' => $draftitemid,
79 'filepath' => '/',
80 'filename' => ''
83 // Create some files.
84 $existingfiles = array(
85 'test',
86 'test.txt',
87 'test (1).txt',
88 'test1.txt',
89 'test1 (1).txt',
90 'test1 (2).txt',
91 'test1 (3).txt',
92 'test1 (My name is Bob).txt',
93 'test2 (555).txt',
94 'test3 (1000).txt',
95 'test3 (1000MB).txt',
97 foreach ($existingfiles as $filename) {
98 $dummy['filename'] = $filename;
99 $file = $fs->create_file_from_string($dummy, 'blah! ' . $filename);
100 $this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
103 // Actual testing.
104 $this->assertEquals('free.txt', repository::get_unused_filename($draftitemid, '/', 'free.txt'));
105 $this->assertEquals('test (1)', repository::get_unused_filename($draftitemid, '/', 'test'));
106 $this->assertEquals('test (2).txt', repository::get_unused_filename($draftitemid, '/', 'test.txt'));
107 $this->assertEquals('test1 (4).txt', repository::get_unused_filename($draftitemid, '/', 'test1.txt'));
108 $this->assertEquals('test1 (8).txt', repository::get_unused_filename($draftitemid, '/', 'test1 (8).txt'));
109 $this->assertEquals('test1 ().txt', repository::get_unused_filename($draftitemid, '/', 'test1 ().txt'));
110 $this->assertEquals('test2 (556).txt', repository::get_unused_filename($draftitemid, '/', 'test2 (555).txt'));
111 $this->assertEquals('test3 (1001).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000).txt'));
112 $this->assertEquals('test3 (1000MB) (1).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000MB).txt'));
113 $this->assertEquals('test4 (1).txt', repository::get_unused_filename($draftitemid, '/', 'test4 (1).txt'));
116 public function test_draftfile_exists() {
117 global $USER;
119 $this->resetAfterTest(true);
121 $this->setAdminUser();
122 $fs = get_file_storage();
124 $draftitemid = file_get_unused_draft_itemid();
125 $context = context_user::instance($USER->id);
127 $dummy = array(
128 'contextid' => $context->id,
129 'component' => 'user',
130 'filearea' => 'draft',
131 'itemid' => $draftitemid,
132 'filepath' => '/',
133 'filename' => ''
136 // Create some files.
137 $existingfiles = array(
138 'The Matrix.movie',
139 'Astalavista.txt',
140 'foobar',
142 foreach ($existingfiles as $filename) {
143 $dummy['filename'] = $filename;
144 $file = $fs->create_file_from_string($dummy, 'Content of ' . $filename);
145 $this->assertInstanceOf('stored_file', $file);
148 // Doing the text.
149 foreach ($existingfiles as $filename) {
150 $this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
152 foreach (array('Terminator.movie', 'Where is Wally?', 'barfoo') as $filename) {
153 $this->assertFalse(repository::draftfile_exists($draftitemid, '/', $filename));
157 public function test_delete_selected_files() {
158 global $USER;
160 $this->resetAfterTest(true);
162 $this->setAdminUser();
163 $fs = get_file_storage();
165 $draftitemid = file_get_unused_draft_itemid();
166 $context = context_user::instance($USER->id);
168 $dummy = [
169 'contextid' => $context->id,
170 'component' => 'user',
171 'filearea' => 'draft',
172 'itemid' => $draftitemid,
173 'filepath' => '/',
174 'filename' => ''
177 // Create some files.
178 $existingfiles = [
179 'The Matrix.movie',
180 'Astalavista.txt',
181 'foobar',
184 $selectedfiles = [
185 'The Matrix.movie' => [],
186 'Astalavista.txt' => []
188 foreach ($existingfiles as $filename) {
189 $dummy['filename'] = $filename;
190 $file = $fs->create_file_from_string($dummy, 'Content of ' . $filename);
191 if (array_key_exists($filename, $selectedfiles)) {
192 $selectedfiles[$filename] = (object)[
193 'filename' => $filename,
194 'filepath' => $file->get_filepath()
199 // Get area files with default options.
200 $areafiles = $fs->get_area_files($context->id, 'user', 'draft', $draftitemid);
201 // Should be the 3 files we added plus the folder.
202 $this->assertEquals(4, count($areafiles));
204 repository_delete_selected_files($context, 'user', 'draft', $draftitemid, $selectedfiles);
206 $areafiles = $fs->get_area_files($context->id, 'user', 'draft', $draftitemid);
207 // Should be the 1 file left plus the folder.
208 $this->assertEquals(2, count($areafiles));
211 public function test_can_be_edited_by_user() {
212 $this->resetAfterTest(true);
214 $syscontext = context_system::instance();
215 $course = $this->getDataGenerator()->create_course();
216 $coursecontext = context_course::instance($course->id);
217 $roleid = create_role('A role', 'arole', 'A role', '');
218 $user = $this->getDataGenerator()->create_user();
219 $this->setUser($user);
221 // Instance on a site level.
222 $this->getDataGenerator()->create_repository_type('flickr_public');
223 $repoid = $this->getDataGenerator()->create_repository('flickr_public')->id;
224 $systemrepo = repository::get_repository_by_id($repoid, $syscontext);
226 role_assign($roleid, $user->id, $syscontext->id);
227 assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext, true);
228 assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
229 accesslib_clear_all_caches_for_unit_testing();
230 $this->assertTrue($systemrepo->can_be_edited_by_user());
232 assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $syscontext, true);
233 assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
234 accesslib_clear_all_caches_for_unit_testing();
235 $this->assertFalse($systemrepo->can_be_edited_by_user());
237 assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
238 assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
239 accesslib_clear_all_caches_for_unit_testing();
240 $this->assertFalse($systemrepo->can_be_edited_by_user());
242 role_unassign($roleid, $user->id, $syscontext->id);
243 accesslib_clear_all_caches_for_unit_testing();
245 // Instance on a course level.
246 $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
248 $params = array('contextid' => $coursecontext->id);
249 $repoid = $this->getDataGenerator()->create_repository('flickr_public', $params)->id;
250 $courserepo = repository::get_repository_by_id($repoid, $coursecontext);
252 assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
253 assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $coursecontext, true);
254 accesslib_clear_all_caches_for_unit_testing();
255 $this->assertTrue($courserepo->can_be_edited_by_user());
257 assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
258 accesslib_clear_all_caches_for_unit_testing();
259 $this->assertFalse($courserepo->can_be_edited_by_user());
261 assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
262 assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
263 accesslib_clear_all_caches_for_unit_testing();
264 $this->assertFalse($courserepo->can_be_edited_by_user());
266 role_unassign($roleid, $user->id, $coursecontext->id);
267 accesslib_clear_all_caches_for_unit_testing();
269 // Instance on a user level.
270 $otheruser = $this->getDataGenerator()->create_user();
271 $otherusercontext = context_user::instance($otheruser->id);
272 role_assign($roleid, $user->id, $syscontext->id);
273 assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
274 accesslib_clear_all_caches_for_unit_testing();
276 // Editing someone else's instance.
277 $record = array('contextid' => $otherusercontext->id);
278 $repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
279 $userrepo = repository::get_repository_by_id($repoid, $syscontext);
280 $this->assertFalse($userrepo->can_be_edited_by_user());
282 // Editing my own instance.
283 $usercontext = context_user::instance($user->id);
284 $record = array('contextid' => $usercontext->id);
285 $repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
286 $userrepo = repository::get_repository_by_id($repoid, $syscontext);
287 $this->assertTrue($userrepo->can_be_edited_by_user());
291 public function test_check_capability() {
292 $this->resetAfterTest(true);
294 $syscontext = context_system::instance();
295 $course1 = $this->getDataGenerator()->create_course();
296 $course1context = context_course::instance($course1->id);
297 $course2 = $this->getDataGenerator()->create_course();
298 $course2context = context_course::instance($course2->id);
300 $forumdata = new stdClass();
301 $forumdata->course = $course1->id;
302 $forumc1 = $this->getDataGenerator()->create_module('forum', $forumdata);
303 $forumc1context = context_module::instance($forumc1->cmid);
304 $forumdata->course = $course2->id;
305 $forumc2 = $this->getDataGenerator()->create_module('forum', $forumdata);
306 $forumc2context = context_module::instance($forumc2->cmid);
308 $blockdata = new stdClass();
309 $blockdata->parentcontextid = $course1context->id;
310 $blockc1 = $this->getDataGenerator()->create_block('online_users', $blockdata);
311 $blockc1context = context_block::instance($blockc1->id);
312 $blockdata->parentcontextid = $course2context->id;
313 $blockc2 = $this->getDataGenerator()->create_block('online_users', $blockdata);
314 $blockc2context = context_block::instance($blockc2->id);
316 $user1 = $this->getDataGenerator()->create_user();
317 $user1context = context_user::instance($user1->id);
318 $user2 = $this->getDataGenerator()->create_user();
319 $user2context = context_user::instance($user2->id);
321 // New role prohibiting Flickr Public access.
322 $roleid = create_role('No Flickr Public', 'noflickrpublic', 'No Flickr Public', '');
323 assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $syscontext, true);
325 // Disallow system access to Flickr Public to user 2.
326 role_assign($roleid, $user2->id, $syscontext->id);
327 accesslib_clear_all_caches_for_unit_testing();
329 // Enable repositories.
330 $this->getDataGenerator()->create_repository_type('flickr_public');
331 $this->getDataGenerator()->create_repository_type('dropbox');
333 // Instance on a site level.
334 $repoid = $this->getDataGenerator()->create_repository('flickr_public')->id;
335 $systemrepo = repository::get_repository_by_id($repoid, $syscontext);
337 // Check that everyone with right capability can view a site-wide repository.
338 $this->setUser($user1);
339 $this->assertTrue($systemrepo->check_capability());
341 // Without the capability, we cannot view a site-wide repository.
342 $this->setUser($user2);
343 $caughtexception = false;
344 try {
345 $systemrepo->check_capability();
346 } catch (repository_exception $e) {
347 $caughtexception = true;
349 $this->assertTrue($caughtexception);
351 // Instance on a course level.
352 $record = new stdClass();
353 $record->contextid = $course1context->id;
354 $courserepoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
356 // Within the course, I can view the repository.
357 $courserepo = repository::get_repository_by_id($courserepoid, $course1context);
358 $this->setUser($user1);
359 $this->assertTrue($courserepo->check_capability());
360 // But not without the capability.
361 $this->setUser($user2);
362 $caughtexception = false;
363 try {
364 $courserepo->check_capability();
365 } catch (repository_exception $e) {
366 $caughtexception = true;
368 $this->assertTrue($caughtexception);
370 // From another course I cannot, with or without the capability.
371 $courserepo = repository::get_repository_by_id($courserepoid, $course2context);
372 $this->setUser($user1);
373 $caughtexception = false;
374 try {
375 $courserepo->check_capability();
376 } catch (repository_exception $e) {
377 $caughtexception = true;
379 $this->assertTrue($caughtexception);
380 $this->setUser($user2);
381 $caughtexception = false;
382 try {
383 $courserepo->check_capability();
384 } catch (repository_exception $e) {
385 $caughtexception = true;
387 $this->assertTrue($caughtexception);
389 // From a module within the course, I can view the repository.
390 $courserepo = repository::get_repository_by_id($courserepoid, $forumc1context);
391 $this->setUser($user1);
392 $this->assertTrue($courserepo->check_capability());
393 // But not without the capability.
394 $this->setUser($user2);
395 $caughtexception = false;
396 try {
397 $courserepo->check_capability();
398 } catch (repository_exception $e) {
399 $caughtexception = true;
401 $this->assertTrue($caughtexception);
403 // From a module in the wrong course, I cannot view the repository.
404 $courserepo = repository::get_repository_by_id($courserepoid, $forumc2context);
405 $this->setUser($user1);
406 $caughtexception = false;
407 try {
408 $courserepo->check_capability();
409 } catch (repository_exception $e) {
410 $caughtexception = true;
412 $this->assertTrue($caughtexception);
414 // From a block within the course, I can view the repository.
415 $courserepo = repository::get_repository_by_id($courserepoid, $blockc1context);
416 $this->setUser($user1);
417 $this->assertTrue($courserepo->check_capability());
418 // But not without the capability.
419 $this->setUser($user2);
420 $caughtexception = false;
421 try {
422 $courserepo->check_capability();
423 } catch (repository_exception $e) {
424 $caughtexception = true;
426 $this->assertTrue($caughtexception);
428 // From a block in the wrong course, I cannot view the repository.
429 $courserepo = repository::get_repository_by_id($courserepoid, $blockc2context);
430 $this->setUser($user1);
431 $caughtexception = false;
432 try {
433 $courserepo->check_capability();
434 } catch (repository_exception $e) {
435 $caughtexception = true;
437 $this->assertTrue($caughtexception);
439 // Instance on a user level.
440 // Instance on a course level.
441 $record = new stdClass();
442 $record->contextid = $user1context->id;
443 $user1repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
444 $record->contextid = $user2context->id;
445 $user2repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
447 // Check that a user can see its own repository.
448 $userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
449 $this->setUser($user1);
450 $this->assertTrue($userrepo->check_capability());
451 // But not without the capability.
452 $userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
453 $this->setUser($user2);
454 $caughtexception = false;
455 try {
456 $userrepo->check_capability();
457 } catch (repository_exception $e) {
458 $caughtexception = true;
460 $this->assertTrue($caughtexception);
462 // Check that a user cannot see someone's repository.
463 $userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
464 $this->setUser($user1);
465 $caughtexception = false;
466 try {
467 $userrepo->check_capability();
468 } catch (repository_exception $e) {
469 $caughtexception = true;
471 $this->assertTrue($caughtexception);
472 // Make sure the repo from user 2 was accessible.
473 role_unassign($roleid, $user2->id, $syscontext->id);
474 accesslib_clear_all_caches_for_unit_testing();
475 $this->setUser($user2);
476 $this->assertTrue($userrepo->check_capability());
477 role_assign($roleid, $user2->id, $syscontext->id);
478 accesslib_clear_all_caches_for_unit_testing();
480 // Check that a user can view SOME repositories when logged in as someone else.
481 $params = new stdClass();
482 $params->name = 'Dropbox';
483 $params->dropbox_key = 'key';
484 $params->dropbox_secret = 'secret';
485 $privaterepoid = $this->getDataGenerator()->create_repository('dropbox')->id;
486 $notprivaterepoid = $this->getDataGenerator()->create_repository('upload')->id;
488 $privaterepo = repository::get_repository_by_id($privaterepoid, $syscontext);
489 $notprivaterepo = repository::get_repository_by_id($notprivaterepoid, $syscontext);
490 $userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
492 $this->setAdminUser();
493 \core\session\manager::loginas($user1->id, $syscontext);
495 // Logged in as, I cannot view a user instance.
496 $caughtexception = false;
497 try {
498 $userrepo->check_capability();
499 } catch (repository_exception $e) {
500 $caughtexception = true;
502 $this->assertTrue($caughtexception);
504 // Logged in as, I cannot view a private instance.
505 $caughtexception = false;
506 try {
507 $privaterepo->check_capability();
508 } catch (repository_exception $e) {
509 $caughtexception = true;
511 $this->assertTrue($caughtexception);
513 // Logged in as, I can view a non-private instance.
514 $this->assertTrue($notprivaterepo->check_capability());
517 function test_delete_all_for_context() {
518 global $DB;
519 $this->resetAfterTest(true);
521 $this->setAdminUser();
522 $course = $this->getDataGenerator()->create_course();
523 $user = $this->getDataGenerator()->create_user();
524 $this->getDataGenerator()->create_repository_type('flickr_public');
525 $this->getDataGenerator()->create_repository_type('filesystem');
526 $coursecontext = context_course::instance($course->id);
527 $usercontext = context_user::instance($user->id);
529 // Creating course instances.
530 $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
531 $courserepo1 = repository::get_repository_by_id($repo->id, $coursecontext);
532 $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
534 $repo = $this->getDataGenerator()->create_repository('filesystem', array('contextid' => $coursecontext->id));
535 $courserepo2 = repository::get_repository_by_id($repo->id, $coursecontext);
536 $this->assertEquals(2, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
538 // Creating user instances.
539 $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
540 $userrepo1 = repository::get_repository_by_id($repo->id, $usercontext);
541 $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
543 $repo = $this->getDataGenerator()->create_repository('filesystem', array('contextid' => $usercontext->id));
544 $userrepo2 = repository::get_repository_by_id($repo->id, $usercontext);
545 $this->assertEquals(2, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
547 // Simulation of course deletion.
548 repository::delete_all_for_context($coursecontext->id);
549 $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
550 $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $courserepo1->id)));
551 $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $courserepo2->id)));
552 $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $courserepo1->id)));
553 $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $courserepo2->id)));
555 // Simulation of user deletion.
556 repository::delete_all_for_context($usercontext->id);
557 $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
558 $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $userrepo1->id)));
559 $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $userrepo2->id)));
560 $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $userrepo1->id)));
561 $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $userrepo2->id)));
563 // Checking deletion upon course context deletion.
564 $course = $this->getDataGenerator()->create_course();
565 $coursecontext = context_course::instance($course->id);
566 $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
567 $courserepo = repository::get_repository_by_id($repo->id, $coursecontext);
568 $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
569 $coursecontext->delete();
570 $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
572 // Checking deletion upon user context deletion.
573 $user = $this->getDataGenerator()->create_user();
574 $usercontext = context_user::instance($user->id);
575 $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
576 $userrepo = repository::get_repository_by_id($repo->id, $usercontext);
577 $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
578 $usercontext->delete();
579 $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
581 // Checking deletion upon course deletion.
582 $course = $this->getDataGenerator()->create_course();
583 $coursecontext = context_course::instance($course->id);
584 $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
585 $courserepo = repository::get_repository_by_id($repo->id, $coursecontext);
586 $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
587 delete_course($course, false);
588 $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
590 // Checking deletion upon user deletion.
591 $user = $this->getDataGenerator()->create_user();
592 $usercontext = context_user::instance($user->id);
593 $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
594 $userrepo = repository::get_repository_by_id($repo->id, $usercontext);
595 $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
596 delete_user($user);
597 $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
601 * Create test file in user private files
603 * @param string $filepath file path
604 * @param string $filename file name
606 private function create_user_private_file(string $filepath, string $filename): void {
607 global $USER;
609 $filerecord = [];
610 $filerecord['contextid'] = context_user::instance($USER->id)->id;
611 $filerecord['component'] = 'user';
612 $filerecord['filearea'] = 'private';
613 $filerecord['itemid'] = 0;
614 $filerecord['filepath'] = $filepath;
615 $filerecord['filename'] = $filename;
616 $filerecord['userid'] = $USER->id;
618 $fs = get_file_storage();
619 $fs->create_file_from_string($filerecord, hash("md5", $filepath . $filename));
622 public function test_listing_and_filter() {
623 $this->resetAfterTest(true);
624 $this->setUser($this->getDataGenerator()->create_user());
625 $repoid = $this->getDataGenerator()->create_repository('user')->id;
626 $this->create_user_private_file('/', 'image1.jpg');
627 $this->create_user_private_file('/', 'file1.txt');
628 $this->create_user_private_file('/folder/', 'image2.jpg');
629 $this->create_user_private_file('/folder/', 'file2.txt');
630 $this->create_user_private_file('/ftexts/', 'file3.txt');
632 // Listing without filters returns 4 records (2 files and 2 directories).
633 $repo = repository::get_repository_by_id($repoid, context_system::instance());
634 $this->assertCount(4, $repo->get_listing()['list']);
636 // Listing with filters returns 3 records (1 files and 2 directories).
637 $_POST['accepted_types'] = ['.jpg'];
638 $this->assertCount(3, $repo->get_listing()['list']);