Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 core_filetypes; 20 21 defined('MOODLE_INTERNAL') || die(); 22 23 global $CFG; 24 require_once($CFG->libdir . '/filelib.php'); 25 26 /** 27 * Unit tests for /lib/classes/filetypes.php. 28 * 29 * @package core 30 * @copyright 2014 The Open University 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class filetypes_test extends \advanced_testcase { 34 35 public function test_add_type() { 36 $this->resetAfterTest(); 37 38 // Check the filetypes to be added do not exist yet (basically this 39 // ensures we're testing the cache clear). 40 $types = get_mimetypes_array(); 41 $this->assertArrayNotHasKey('frog', $types); 42 $this->assertArrayNotHasKey('zombie', $types); 43 44 // Add two filetypes (minimal, then all options). 45 core_filetypes::add_type('frog', 'application/x-frog', 'document'); 46 core_filetypes::add_type('zombie', 'application/x-zombie', 'document', 47 array('document', 'image'), 'image', 'A zombie', true); 48 49 // Check they now exist, and check data. 50 $types = get_mimetypes_array(); 51 $this->assertEquals('application/x-frog', $types['frog']['type']); 52 $this->assertEquals('document', $types['frog']['icon']); 53 $this->assertEquals(array('document', 'image'), $types['zombie']['groups']); 54 $this->assertEquals('image', $types['zombie']['string']); 55 $this->assertEquals(true, $types['zombie']['defaulticon']); 56 $this->assertEquals('A zombie', $types['zombie']['customdescription']); 57 58 // Test adding again causes exception. 59 try { 60 core_filetypes::add_type('frog', 'application/x-frog', 'document'); 61 $this->fail(); 62 } catch (\coding_exception $e) { 63 $this->assertStringContainsString('already exists', $e->getMessage()); 64 $this->assertStringContainsString('frog', $e->getMessage()); 65 } 66 67 // Test bogus extension causes exception. 68 try { 69 core_filetypes::add_type('.frog', 'application/x-frog', 'document'); 70 $this->fail(); 71 } catch (\coding_exception $e) { 72 $this->assertStringContainsString('Invalid extension', $e->getMessage()); 73 $this->assertStringContainsString('..frog', $e->getMessage()); 74 } 75 try { 76 core_filetypes::add_type('', 'application/x-frog', 'document'); 77 $this->fail(); 78 } catch (\coding_exception $e) { 79 $this->assertStringContainsString('Invalid extension', $e->getMessage()); 80 } 81 82 // Test there is an exception if you add something with defaulticon when 83 // there is already a type that has it. 84 try { 85 core_filetypes::add_type('gecko', 'text/plain', 'document', 86 array(), '', '', true); 87 $this->fail(); 88 } catch (\coding_exception $e) { 89 $this->assertStringContainsString('default icon set', $e->getMessage()); 90 $this->assertStringContainsString('text/plain', $e->getMessage()); 91 } 92 } 93 94 public function test_update_type() { 95 $this->resetAfterTest(); 96 97 // Check previous value for the MIME type of Word documents. 98 $types = get_mimetypes_array(); 99 $this->assertEquals('application/msword', $types['doc']['type']); 100 101 // Change it. 102 core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document'); 103 104 // Check the MIME type is now set and also the other (not specified) 105 // options, like groups, were removed. 106 $types = get_mimetypes_array(); 107 $this->assertEquals('application/x-frog', $types['doc']['type']); 108 $this->assertArrayNotHasKey('groups', $types['doc']); 109 110 // This time change the extension. 111 core_filetypes::update_type('doc', 'docccc', 'application/x-frog', 'document'); 112 $types = get_mimetypes_array(); 113 $this->assertEquals('application/x-frog', $types['docccc']['type']); 114 $this->assertArrayNotHasKey('doc', $types); 115 116 // Test unknown extension. 117 try { 118 core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document'); 119 $this->fail(); 120 } catch (\coding_exception $e) { 121 $this->assertStringContainsString('not found', $e->getMessage()); 122 $this->assertStringContainsString('doc', $e->getMessage()); 123 } 124 125 // Test bogus extension causes exception. 126 try { 127 core_filetypes::update_type('docccc', '.frog', 'application/x-frog', 'document'); 128 $this->fail(); 129 } catch (\coding_exception $e) { 130 $this->assertStringContainsString('Invalid extension', $e->getMessage()); 131 $this->assertStringContainsString('.frog', $e->getMessage()); 132 } 133 try { 134 core_filetypes::update_type('docccc', '', 'application/x-frog', 'document'); 135 $this->fail(); 136 } catch (\coding_exception $e) { 137 $this->assertStringContainsString('Invalid extension', $e->getMessage()); 138 } 139 140 // Test defaulticon changes. 141 try { 142 core_filetypes::update_type('docccc', 'docccc', 'text/plain', 'document', 143 array(), '', '', true); 144 $this->fail(); 145 } catch (\coding_exception $e) { 146 $this->assertStringContainsString('default icon set', $e->getMessage()); 147 $this->assertStringContainsString('text/plain', $e->getMessage()); 148 } 149 } 150 151 public function test_delete_type() { 152 $this->resetAfterTest(); 153 154 // Filetype exists. 155 $types = get_mimetypes_array(); 156 $this->assertArrayHasKey('doc', $types); 157 158 // Remove it. 159 core_filetypes::delete_type('doc'); 160 $types = get_mimetypes_array(); 161 $this->assertArrayNotHasKey('doc', $types); 162 163 // Test removing one that doesn't exist causes exception. 164 try { 165 core_filetypes::delete_type('doc'); 166 $this->fail(); 167 } catch (\coding_exception $e) { 168 $this->assertStringContainsString('not found', $e->getMessage()); 169 $this->assertStringContainsString('doc', $e->getMessage()); 170 } 171 172 // Try a custom type (slightly different). 173 core_filetypes::add_type('frog', 'application/x-frog', 'document'); 174 $types = get_mimetypes_array(); 175 $this->assertArrayHasKey('frog', $types); 176 core_filetypes::delete_type('frog'); 177 $types = get_mimetypes_array(); 178 $this->assertArrayNotHasKey('frog', $types); 179 } 180 181 public function test_revert_type_to_default() { 182 $this->resetAfterTest(); 183 184 // Delete and then revert. 185 core_filetypes::delete_type('doc'); 186 $this->assertArrayNotHasKey('doc', get_mimetypes_array()); 187 core_filetypes::revert_type_to_default('doc'); 188 $this->assertArrayHasKey('doc', get_mimetypes_array()); 189 190 // Update and then revert. 191 core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode', array(), '', 'An asm file'); 192 $types = get_mimetypes_array(); 193 $this->assertEquals('An asm file', $types['asm']['customdescription']); 194 core_filetypes::revert_type_to_default('asm'); 195 $types = get_mimetypes_array(); 196 $this->assertArrayNotHasKey('customdescription', $types['asm']); 197 198 // Test reverting a non-default type causes exception. 199 try { 200 core_filetypes::revert_type_to_default('frog'); 201 $this->fail(); 202 } catch (\coding_exception $e) { 203 $this->assertStringContainsString('not a default type', $e->getMessage()); 204 $this->assertStringContainsString('frog', $e->getMessage()); 205 } 206 } 207 208 /** 209 * Check that the logic cleans up the variable by deleting parts that are 210 * no longer needed. 211 */ 212 public function test_cleanup() { 213 global $CFG; 214 $this->resetAfterTest(); 215 216 // The custom filetypes setting is empty to start with. 217 $this->assertObjectNotHasAttribute('customfiletypes', $CFG); 218 219 // Add a custom filetype, then delete it. 220 core_filetypes::add_type('frog', 'application/x-frog', 'document'); 221 $this->assertObjectHasAttribute('customfiletypes', $CFG); 222 core_filetypes::delete_type('frog'); 223 $this->assertObjectNotHasAttribute('customfiletypes', $CFG); 224 225 // Change a standard filetype, then change it back. 226 core_filetypes::update_type('asm', 'asm', 'text/plain', 'document'); 227 $this->assertObjectHasAttribute('customfiletypes', $CFG); 228 core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode'); 229 $this->assertObjectNotHasAttribute('customfiletypes', $CFG); 230 231 // Delete a standard filetype, then add it back (the same). 232 core_filetypes::delete_type('asm'); 233 $this->assertObjectHasAttribute('customfiletypes', $CFG); 234 core_filetypes::add_type('asm', 'text/plain', 'sourcecode'); 235 $this->assertObjectNotHasAttribute('customfiletypes', $CFG); 236 237 // Revert a changed type. 238 core_filetypes::update_type('asm', 'asm', 'text/plain', 'document'); 239 $this->assertObjectHasAttribute('customfiletypes', $CFG); 240 core_filetypes::revert_type_to_default('asm'); 241 $this->assertObjectNotHasAttribute('customfiletypes', $CFG); 242 243 // Revert a deleted type. 244 core_filetypes::delete_type('asm'); 245 $this->assertObjectHasAttribute('customfiletypes', $CFG); 246 core_filetypes::revert_type_to_default('asm'); 247 $this->assertObjectNotHasAttribute('customfiletypes', $CFG); 248 } 249 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body