|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00004 // // 00005 // NOTICE OF COPYRIGHT // 00006 // // 00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00008 // http://moodle.org // 00009 // // 00010 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com // 00011 // // 00012 // This program is free software; you can redistribute it and/or modify // 00013 // it under the terms of the GNU General Public License as published by // 00014 // the Free Software Foundation; either version 2 of the License, or // 00015 // (at your option) any later version. // 00016 // // 00017 // This program is distributed in the hope that it will be useful, // 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00020 // GNU General Public License for more details: // 00021 // // 00022 // http://www.gnu.org/copyleft/gpl.html // 00023 // // 00025 00026 if (!defined('MOODLE_INTERNAL')) { 00027 die('Direct access to this script is forbidden.'); 00028 } 00029 00030 require_once($CFG->libdir.'/filelib.php'); 00031 00032 require_once($CFG->dirroot.'/user/lib.php'); 00033 require_once($CFG->dirroot.'/mod/forum/lib.php'); 00034 00038 class filelib_test extends UnitTestCaseUsingDatabase { 00039 protected $course; 00040 protected $section; 00041 protected $coursecat; 00042 protected $user; 00043 protected $module; 00044 public static $includecoverage = array('lib/filelib.php'); 00045 00049 public function setup() { 00050 parent::setUp(); 00051 $tables = array('block_instances', 'cache_flags', 'capabilities', 'context', 'context_temp', 00052 'course', 'course_modules', 'course_categories', 'course_sections','files', 00053 'grade_items', 'grade_categories', 'groups', 'groups_members', 00054 'modules', 'role', 'role_names', 'role_context_levels', 'role_assignments', 00055 'role_capabilities', 'user'); 00056 $this->create_test_tables($tables, 'lib'); 00057 $this->create_test_table('forum', 'mod/forum'); 00058 $this->switch_to_test_db(); 00059 00060 global $DB, $CFG; 00061 // Insert needed capabilities 00062 $DB->insert_record('capabilities', 00063 array('id' => 45, 'name' => 'moodle/course:update', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 4)); 00064 $DB->insert_record('capabilities', 00065 array('id' => 14, 'name' => 'moodle/backup:backupcourse', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 28)); 00066 $DB->insert_record('capabilities', 00067 array('id' => 17, 'name' => 'moodle/restore:restorecourse', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 28)); 00068 $DB->insert_record('capabilities', 00069 array('id' => 52, 'name' => 'moodle/course:managefiles', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 4)); 00070 $DB->insert_record('capabilities', 00071 array('id' => 73, 'name' => 'moodle/user:editownprofile', 'cattype' => 'write', 'contextlevel' => 10, 'component' => 'moodle', 'riskbitmask' => 16)); 00072 00073 // Insert system context 00074 $DB->insert_record('context', array('id' => 1, 'contextlevel' => 10, 'instanceid' => 0, 'path' => '/1', 'depth' => 1)); 00075 $DB->insert_record('context', array('id' => 2, 'contextlevel' => 50, 'instanceid' => 1, 'path' => '/1/2', 'depth' => 2)); 00076 00077 // Insert site course 00078 $DB->insert_record('course', array('category' => 0, 'sortorder' => 1, 'fullname' => 'Test site', 'shortname' => 'test', 'format' => 'site', 'modinfo' => 'a:0:{}')); 00079 00080 // User and capability stuff (stolen from testaccesslib.php) 00081 $syscontext = get_system_context(false); 00082 00084 update_capabilities('moodle'); 00085 update_capabilities('mod_forum'); 00086 00087 $contexts = $this->load_test_data('context', 00088 array('contextlevel', 'instanceid', 'path', 'depth'), array( 00089 1 => array(40, 666, '', 2))); 00090 $contexts[0] = $syscontext; 00091 $contexts[1]->path = $contexts[0]->path . '/' . $contexts[1]->id; 00092 $this->testdb->set_field('context', 'path', $contexts[1]->path, array('id' => $contexts[1]->id)); 00093 $users = $this->load_test_data('user', 00094 array('username', 'confirmed', 'deleted'), array( 00095 'a' => array('a', 1, 0))); 00096 $admin = $this->testdb->get_record('role', array('shortname' => 'admin')); 00097 $ras = $this->load_test_data('role_assignments', array('userid', 'roleid', 'contextid'), array( 'a' => array($users['a']->id, $admin->id, $contexts[0]->id))); 00098 00099 $this->switch_global_user_id(1); 00100 accesslib_clear_all_caches_for_unit_testing(); 00101 00102 // Create a coursecat 00103 $newcategory = new stdClass(); 00104 $newcategory->name = 'test category'; 00105 $newcategory->sortorder = 999; 00106 $newcategory->id = $DB->insert_record('course_categories', $newcategory); 00107 00108 $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id); 00109 mark_context_dirty($newcategory->context->path); 00110 fix_course_sortorder(); // Required to build course_categories.depth and .path. 00111 $this->coursecat = $DB->get_record('course_categories', array('id' => $newcategory->id)); 00112 00113 // Create a course 00114 $coursedata = new stdClass(); 00115 $coursedata->category = $newcategory->id; 00116 $coursedata->shortname = 'testcourse'; 00117 $coursedata->fullname = 'Test Course'; 00118 00119 try { 00120 $this->course = create_course($coursedata); 00121 } catch (moodle_exception $e) { 00122 // Most likely the result of an aborted unit test: the test course was not correctly deleted 00123 $this->course = $DB->get_record('course', array('shortname' => $coursedata->shortname)); 00124 } 00125 00126 // Create a user 00127 $this->user = new stdClass(); 00128 $this->user->username = 'testuser09987654321'; 00129 $this->user->password = 'password'; 00130 $this->user->firstname = 'TestUser'; 00131 $this->user->lastname = 'TestUser'; 00132 $this->user->email = 'fakeemail@fake.org'; 00133 $this->user->id = create_user($this->user); 00134 00135 // Assign user to course 00136 // role_assign(5, $this->user->id, get_context_instance(CONTEXT_COURSE, $this->course->id)->id); 00137 00138 // Create a module 00139 $module = new stdClass(); 00140 $module->intro = 'Forum used for testing filelib API'; 00141 $module->type = 'general'; 00142 $module->forcesubscribe = 1; 00143 $module->format = 1; 00144 $module->name = 'Test Forum'; 00145 $module->module = $DB->get_field('modules', 'id', array('name' => 'forum')); 00146 $module->modulename = 'forum'; 00147 $module->add = 'forum'; 00148 $module->cmidnumber = ''; 00149 $module->course = $this->course->id; 00150 00151 $module->instance = forum_add_instance($module, ''); 00152 00153 $this->section = get_course_section(1, $this->course->id); 00154 $module->section = $this->section->id; 00155 $module->coursemodule = add_course_module($module); 00156 00157 add_mod_to_section($module); 00158 00159 $module->cmidnumber = set_coursemodule_idnumber($module->coursemodule, ''); 00160 00161 rebuild_course_cache($this->course->id); 00162 $this->module= $DB->get_record('forum', array('id' => $module->instance)); 00163 $this->module->instance = $module->instance; 00164 00165 // Update local copy of course 00166 $this->course = $DB->get_record('course', array('id' => $this->course->id)); 00167 } 00168 00169 public function teardown() { 00170 parent::tearDown(); 00171 } 00172 00173 public function createFiles() { 00174 00175 } 00176 } 00177 00182 class file_browser_test extends filelib_test { 00183 00184 public function test_encodepath() { 00185 global $CFG; 00186 $fb = new file_browser(); 00187 00188 $CFG->slasharguments = true; 00189 $this->assertEqual('http://test.url.com/path/to/page.php', file_encode_url('http://test.url.com', '/path/to/page.php')); 00190 $this->assertEqual('http://test.url.com/path/to/page.php?forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true)); 00191 $this->assertEqual('https://test.url.com/path/to/page.php?forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true, true)); 00192 00193 // TODO add error checking for malformed path (does method support get variables?) 00194 $this->assertEqual('http://test.url.com/path/to/page.php?var1=value1&var2=value2', file_encode_url('http://test.url.com', '/path/to/page.php?var1=value1&var2=value2')); 00195 $this->assertEqual('http://test.url.com/path/to/page.php?var1=value1&var2=value2&forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php?var1=value1&var2=value2', true)); 00196 00197 $CFG->slasharguments = false; 00198 $this->assertEqual('http://test.url.com?file=%2Fpath%2Fto%2Fpage.php', file_encode_url('http://test.url.com', '/path/to/page.php')); 00199 $this->assertEqual('http://test.url.com?file=%2Fpath%2Fto%2Fpage.php&forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true)); 00200 $this->assertEqual('https://test.url.com?file=%2Fpath%2Fto%2Fpage.php&forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true, true)); 00201 } 00202 } 00203 00204 class test_file_info_context_system extends filelib_test { 00205 public function test_get_children() { 00206 $context = get_context_instance(CONTEXT_SYSTEM); 00207 00208 $fis = new file_info_context_system(new file_browser(), $context); 00209 $children = $fis->get_children(); 00210 00211 $found_coursecat = false; 00212 $context_coursecat = get_context_instance(CONTEXT_COURSECAT, $this->coursecat->id); 00213 $file_info_context_coursecat = new file_info_context_coursecat(new file_browser(), $context_coursecat, $this->coursecat); 00214 00215 foreach ($children as $child) { 00216 if ($child == $file_info_context_coursecat) { 00217 $found_coursecat = true; 00218 } 00219 } 00220 $this->assertTrue($found_coursecat); 00221 } 00222 } 00223 00224 class test_file_info_context_coursecat extends filelib_test { 00225 private $fileinfo; 00226 00227 public function setup() { 00228 parent::setup(); 00229 $context = get_context_instance(CONTEXT_COURSECAT, $this->coursecat->id); 00230 $this->fileinfo = new file_info_context_coursecat(new file_browser(), $context, $this->coursecat); 00231 } 00232 00233 public function test_get_children() { 00234 $children = $this->fileinfo->get_children(); 00235 $this->assertEqual(2, count($children)); 00236 00237 // Not sure but I think there should be two children: a file_info_stored object and a file_info_course object. 00238 $this->assertEqual('Category introduction', $children[0]->get_visible_name()); 00239 $this->assertEqual('', $children[0]->get_url()); 00240 $this->assertEqual('file_info_stored', get_class($children[0])); 00241 00242 $context_course = get_context_instance(CONTEXT_COURSE, $this->course->id); 00243 $fic = new file_info_context_course(new file_browser(), $context_course, $this->course); 00244 $this->assertEqual($fic, $children[1]); 00245 } 00246 00247 public function test_get_parent() { 00248 $context = get_context_instance(CONTEXT_SYSTEM); 00249 $fis = new file_info_context_system(new file_browser(), $context); 00250 $parent = $this->fileinfo->get_parent(); 00251 $this->assertEqual($parent, $fis); 00252 } 00253 } 00254 00255 class test_file_info_context_course extends filelib_test { 00256 private $fileinfo; 00257 00258 public function setup() { 00259 parent::setup(); 00260 $context = get_context_instance(CONTEXT_COURSE, $this->course->id); 00261 $this->fileinfo = new file_info_context_course(new file_browser(), $context, $this->course); 00262 } 00263 00264 public function test_get_children() { 00265 global $DB; 00266 00267 $children = $this->fileinfo->get_children(); 00268 $this->assertEqual(4, count($children)); 00269 00270 $this->assertEqual('Course introduction', $children[0]->get_visible_name()); 00271 $this->assertEqual('', $children[0]->get_url()); 00272 $this->assertEqual('file_info_stored', get_class($children[0])); 00273 00274 $context_course = get_context_instance(CONTEXT_COURSE, $this->course->id); 00275 $fics = new file_info_area_course_section(new file_browser(), $context_course, $this->course); 00276 $this->assertEqual($fics, $children[1]); 00277 00278 $this->assertEqual('Backups', $children[2]->get_visible_name()); 00279 $this->assertEqual('', $children[2]->get_url()); 00280 $this->assertEqual('file_info_stored', get_class($children[2])); 00281 00282 $this->assertEqual('Course files', $children[3]->get_visible_name()); 00283 $this->assertEqual('', $children[3]->get_url()); 00284 $this->assertEqual('file_info_area_course_legacy', get_class($children[3])); 00285 00286 } 00287 00288 public function test_get_parent() { 00289 $context = get_context_instance(CONTEXT_COURSECAT, $this->coursecat->id); 00290 $fic = new file_info_context_coursecat(new file_browser(), $context, $this->coursecat); 00291 $parent = $this->fileinfo->get_parent(); 00292 $this->assertEqual($parent, $fic); 00293 } 00294 } 00295 00296 class test_file_info_context_user extends filelib_test { 00297 private $fileinfo; 00298 00299 public function setup() { 00300 parent::setup(); 00301 $context = get_context_instance(CONTEXT_USER, $this->user->id); 00302 $this->fileinfo = new file_info_context_user(new file_browser(), $context, $this->user); 00303 } 00304 00305 public function test_get_parent() { 00306 $context = get_context_instance(CONTEXT_SYSTEM); 00307 $fic = new file_info_context_system(new file_browser(), $context); 00308 $parent = $this->fileinfo->get_parent(); 00309 $this->assertEqual($parent, $fic); 00310 } 00311 00312 public function test_get_children() { 00313 $children = $this->fileinfo->get_children(); 00314 $this->assertEqual(2, count($children)); 00315 00316 $this->assertEqual('Personal', $children[0]->get_visible_name()); 00317 $this->assertEqual('', $children[0]->get_url()); 00318 $this->assertEqual('file_info_stored', get_class($children[0])); 00319 00320 $this->assertEqual('Profile', $children[1]->get_visible_name()); 00321 $this->assertEqual('', $children[1]->get_url()); 00322 $this->assertEqual('file_info_stored', get_class($children[1])); 00323 } 00324 } 00325 00326 class test_file_info_context_module extends filelib_test { 00327 private $fileinfo; 00328 00329 public function setup() { 00330 global $DB; 00331 parent::setup(); 00332 $context = get_context_instance(CONTEXT_MODULE, $DB->get_field('course_modules', 'id', array('instance' => $this->module->instance))); 00333 $this->fileinfo = new file_info_context_module(new file_browser(), $this->course, $this->module->instance, $context, array()); 00334 } 00335 00336 public function test_get_parent() { 00337 $context = get_context_instance(CONTEXT_COURSE, $this->course->id); 00338 $fic = new file_info_context_course(new file_browser(), $context, $this->course); 00339 $parent = $this->fileinfo->get_parent(); 00340 $this->assertEqual($parent, $fic); 00341 } 00342 00343 public function test_get_children() { 00344 $children = $this->fileinfo->get_children(); 00345 $this->assertEqual(0, count($children)); 00346 } 00347 }