Merge branch 'MDL-75553-311' of https://github.com/junpataleta/moodle into MOODLE_311...
[moodle.git] / lib / tests / filetypes_test.php
blob478de231f163c3214dee1ec67b3c5b8771fa3091
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 namespace core;
19 use core_filetypes;
21 defined('MOODLE_INTERNAL') || die();
23 global $CFG;
24 require_once($CFG->libdir . '/filelib.php');
26 /**
27 * Unit tests for /lib/classes/filetypes.php.
29 * @package core
30 * @copyright 2014 The Open University
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class filetypes_test extends \advanced_testcase {
35 public function test_add_type() {
36 $this->resetAfterTest();
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);
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);
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']);
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());
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());
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());
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());
94 public function test_update_type() {
95 $this->resetAfterTest();
97 // Check previous value for the MIME type of Word documents.
98 $types = get_mimetypes_array();
99 $this->assertEquals('application/msword', $types['doc']['type']);
101 // Change it.
102 core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document');
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']);
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);
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());
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());
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());
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());
151 public function test_delete_type() {
152 $this->resetAfterTest();
154 // Filetype exists.
155 $types = get_mimetypes_array();
156 $this->assertArrayHasKey('doc', $types);
158 // Remove it.
159 core_filetypes::delete_type('doc');
160 $types = get_mimetypes_array();
161 $this->assertArrayNotHasKey('doc', $types);
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());
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);
181 public function test_revert_type_to_default() {
182 $this->resetAfterTest();
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());
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']);
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());
209 * Check that the logic cleans up the variable by deleting parts that are
210 * no longer needed.
212 public function test_cleanup() {
213 global $CFG;
214 $this->resetAfterTest();
216 // The custom filetypes setting is empty to start with.
217 $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
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);
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);
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);
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);
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);