2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for /lib/classes/filetypes.php.
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->libdir
. '/filelib.php');
31 * Unit tests for /lib/classes/filetypes.php.
34 * @copyright 2014 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_filetypes_testcase
extends advanced_testcase
{
39 public function test_add_type() {
40 $this->resetAfterTest();
42 // Check the filetypes to be added do not exist yet (basically this
43 // ensures we're testing the cache clear).
44 $types = get_mimetypes_array();
45 $this->assertArrayNotHasKey('frog', $types);
46 $this->assertArrayNotHasKey('zombie', $types);
48 // Add two filetypes (minimal, then all options).
49 core_filetypes
::add_type('frog', 'application/x-frog', 'document');
50 core_filetypes
::add_type('zombie', 'application/x-zombie', 'document',
51 array('document', 'image'), 'image', 'A zombie', true);
53 // Check they now exist, and check data.
54 $types = get_mimetypes_array();
55 $this->assertEquals('application/x-frog', $types['frog']['type']);
56 $this->assertEquals('document', $types['frog']['icon']);
57 $this->assertEquals(array('document', 'image'), $types['zombie']['groups']);
58 $this->assertEquals('image', $types['zombie']['string']);
59 $this->assertEquals(true, $types['zombie']['defaulticon']);
60 $this->assertEquals('A zombie', $types['zombie']['customdescription']);
62 // Test adding again causes exception.
64 core_filetypes
::add_type('frog', 'application/x-frog', 'document');
66 } catch (coding_exception
$e) {
67 $this->assertContains('already exists', $e->getMessage());
68 $this->assertContains('frog', $e->getMessage());
71 // Test bogus extension causes exception.
73 core_filetypes
::add_type('.frog', 'application/x-frog', 'document');
75 } catch (coding_exception
$e) {
76 $this->assertContains('Invalid extension', $e->getMessage());
77 $this->assertContains('..frog', $e->getMessage());
80 core_filetypes
::add_type('', 'application/x-frog', 'document');
82 } catch (coding_exception
$e) {
83 $this->assertContains('Invalid extension', $e->getMessage());
86 // Test there is an exception if you add something with defaulticon when
87 // there is already a type that has it.
89 core_filetypes
::add_type('gecko', 'text/plain', 'document',
90 array(), '', '', true);
92 } catch (coding_exception
$e) {
93 $this->assertContains('default icon set', $e->getMessage());
94 $this->assertContains('text/plain', $e->getMessage());
98 public function test_update_type() {
99 $this->resetAfterTest();
101 // Check previous value for the MIME type of Word documents.
102 $types = get_mimetypes_array();
103 $this->assertEquals('application/msword', $types['doc']['type']);
106 core_filetypes
::update_type('doc', 'doc', 'application/x-frog', 'document');
108 // Check the MIME type is now set and also the other (not specified)
109 // options, like groups, were removed.
110 $types = get_mimetypes_array();
111 $this->assertEquals('application/x-frog', $types['doc']['type']);
112 $this->assertArrayNotHasKey('groups', $types['doc']);
114 // This time change the extension.
115 core_filetypes
::update_type('doc', 'docccc', 'application/x-frog', 'document');
116 $types = get_mimetypes_array();
117 $this->assertEquals('application/x-frog', $types['docccc']['type']);
118 $this->assertArrayNotHasKey('doc', $types);
120 // Test unknown extension.
122 core_filetypes
::update_type('doc', 'doc', 'application/x-frog', 'document');
124 } catch (coding_exception
$e) {
125 $this->assertContains('not found', $e->getMessage());
126 $this->assertContains('doc', $e->getMessage());
129 // Test bogus extension causes exception.
131 core_filetypes
::update_type('docccc', '.frog', 'application/x-frog', 'document');
133 } catch (coding_exception
$e) {
134 $this->assertContains('Invalid extension', $e->getMessage());
135 $this->assertContains('.frog', $e->getMessage());
138 core_filetypes
::update_type('docccc', '', 'application/x-frog', 'document');
140 } catch (coding_exception
$e) {
141 $this->assertContains('Invalid extension', $e->getMessage());
144 // Test defaulticon changes.
146 core_filetypes
::update_type('docccc', 'docccc', 'text/plain', 'document',
147 array(), '', '', true);
149 } catch (coding_exception
$e) {
150 $this->assertContains('default icon set', $e->getMessage());
151 $this->assertContains('text/plain', $e->getMessage());
155 public function test_delete_type() {
156 $this->resetAfterTest();
159 $types = get_mimetypes_array();
160 $this->assertArrayHasKey('doc', $types);
163 core_filetypes
::delete_type('doc');
164 $types = get_mimetypes_array();
165 $this->assertArrayNotHasKey('doc', $types);
167 // Test removing one that doesn't exist causes exception.
169 core_filetypes
::delete_type('doc');
171 } catch (coding_exception
$e) {
172 $this->assertContains('not found', $e->getMessage());
173 $this->assertContains('doc', $e->getMessage());
176 // Try a custom type (slightly different).
177 core_filetypes
::add_type('frog', 'application/x-frog', 'document');
178 $types = get_mimetypes_array();
179 $this->assertArrayHasKey('frog', $types);
180 core_filetypes
::delete_type('frog');
181 $types = get_mimetypes_array();
182 $this->assertArrayNotHasKey('frog', $types);
185 public function test_revert_type_to_default() {
186 $this->resetAfterTest();
188 // Delete and then revert.
189 core_filetypes
::delete_type('doc');
190 $this->assertArrayNotHasKey('doc', get_mimetypes_array());
191 core_filetypes
::revert_type_to_default('doc');
192 $this->assertArrayHasKey('doc', get_mimetypes_array());
194 // Update and then revert.
195 core_filetypes
::update_type('asm', 'asm', 'text/plain', 'sourcecode', array(), '', 'An asm file');
196 $types = get_mimetypes_array();
197 $this->assertEquals('An asm file', $types['asm']['customdescription']);
198 core_filetypes
::revert_type_to_default('asm');
199 $types = get_mimetypes_array();
200 $this->assertArrayNotHasKey('customdescription', $types['asm']);
202 // Test reverting a non-default type causes exception.
204 core_filetypes
::revert_type_to_default('frog');
206 } catch (coding_exception
$e) {
207 $this->assertContains('not a default type', $e->getMessage());
208 $this->assertContains('frog', $e->getMessage());
213 * Check that the logic cleans up the variable by deleting parts that are
216 public function test_cleanup() {
218 $this->resetAfterTest();
220 // The custom filetypes setting is empty to start with.
221 $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
223 // Add a custom filetype, then delete it.
224 core_filetypes
::add_type('frog', 'application/x-frog', 'document');
225 $this->assertObjectHasAttribute('customfiletypes', $CFG);
226 core_filetypes
::delete_type('frog');
227 $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
229 // Change a standard filetype, then change it back.
230 core_filetypes
::update_type('asm', 'asm', 'text/plain', 'document');
231 $this->assertObjectHasAttribute('customfiletypes', $CFG);
232 core_filetypes
::update_type('asm', 'asm', 'text/plain', 'sourcecode');
233 $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
235 // Delete a standard filetype, then add it back (the same).
236 core_filetypes
::delete_type('asm');
237 $this->assertObjectHasAttribute('customfiletypes', $CFG);
238 core_filetypes
::add_type('asm', 'text/plain', 'sourcecode');
239 $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
241 // Revert a changed type.
242 core_filetypes
::update_type('asm', 'asm', 'text/plain', 'document');
243 $this->assertObjectHasAttribute('customfiletypes', $CFG);
244 core_filetypes
::revert_type_to_default('asm');
245 $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
247 // Revert a deleted type.
248 core_filetypes
::delete_type('asm');
249 $this->assertObjectHasAttribute('customfiletypes', $CFG);
250 core_filetypes
::revert_type_to_default('asm');
251 $this->assertObjectNotHasAttribute('customfiletypes', $CFG);