Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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_form;
18
 
19
/**
20
 * Test cases for the {@link core_form\filetypes_util} class.
21
 *
1441 ariadna 22
 * @package   core_form
1 efrain 23
 * @copyright 2017 David Mudrak <david@moodle.com>
24
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 25
 * @covers    \core_form\filetypes_util
1 efrain 26
 */
1441 ariadna 27
final class filetypes_util_test extends \advanced_testcase {
1 efrain 28
    /**
29
     * Test normalizing list of extensions.
30
     */
11 efrain 31
    public function test_normalize_file_types(): void {
1 efrain 32
        $this->resetAfterTest(true);
33
        $util = new filetypes_util();
34
 
35
        $this->assertSame(['.odt'], $util->normalize_file_types('.odt'));
36
        $this->assertSame(['.odt'], $util->normalize_file_types('odt'));
37
        $this->assertSame(['.odt'], $util->normalize_file_types('.ODT'));
38
        $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types('doc, jpg, mp3'));
39
        $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types(['.doc', '.jpg', '.mp3']));
40
        $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types('doc, *.jpg, mp3'));
41
        $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types(['doc ', ' JPG ', '.mp3']));
42
        $this->assertSame(['.rtf', '.pdf', '.docx'],
43
            $util->normalize_file_types("RTF,.pdf\n...DocX,,,;\rPDF\trtf ...Rtf"));
44
        $this->assertSame(['.tgz', '.tar.gz'], $util->normalize_file_types('tgz,TAR.GZ tar.gz .tar.gz tgz TGZ'));
45
        $this->assertSame(['.notebook'], $util->normalize_file_types('"Notebook":notebook;NOTEBOOK;,\'NoTeBook\''));
46
        $this->assertSame([], $util->normalize_file_types(''));
47
        $this->assertSame([], $util->normalize_file_types([]));
48
        $this->assertSame(['.0'], $util->normalize_file_types(0));
49
        $this->assertSame(['.0'], $util->normalize_file_types('0'));
50
        $this->assertSame(['.odt'], $util->normalize_file_types('*.odt'));
51
        $this->assertSame([], $util->normalize_file_types('.'));
52
        $this->assertSame(['.foo'], $util->normalize_file_types('. foo'));
53
        $this->assertSame(['*'], $util->normalize_file_types('*'));
54
        $this->assertSame([], $util->normalize_file_types('*~'));
55
        $this->assertSame(['.pdf', '.ps'], $util->normalize_file_types('pdf *.ps foo* *bar .r??'));
56
        $this->assertSame(['*'], $util->normalize_file_types('pdf *.ps foo* * *bar .r??'));
57
    }
58
 
59
    /**
60
     * Test MIME type formal recognition.
61
     */
11 efrain 62
    public function test_looks_like_mimetype(): void {
1 efrain 63
        $this->resetAfterTest(true);
64
        $util = new filetypes_util();
65
 
66
        $this->assertTrue($util->looks_like_mimetype('type/subtype'));
67
        $this->assertTrue($util->looks_like_mimetype('type/x-subtype'));
68
        $this->assertTrue($util->looks_like_mimetype('type/x-subtype+xml'));
69
        $this->assertTrue($util->looks_like_mimetype('type/vnd.subtype.xml'));
70
        $this->assertTrue($util->looks_like_mimetype('type/vnd.subtype+xml'));
71
 
72
        $this->assertFalse($util->looks_like_mimetype('.gif'));
73
        $this->assertFalse($util->looks_like_mimetype('audio'));
74
        $this->assertFalse($util->looks_like_mimetype('foo/bar/baz'));
75
    }
76
 
77
    /**
78
     * Test getting/checking group.
79
     */
11 efrain 80
    public function test_is_filetype_group(): void {
1 efrain 81
        $this->resetAfterTest(true);
82
        $util = new filetypes_util();
83
 
84
        $audio = $util->is_filetype_group('audio');
85
        $this->assertNotFalse($audio);
86
        $this->assertIsArray($audio->extensions);
87
        $this->assertIsArray($audio->mimetypes);
88
 
89
        $this->assertFalse($util->is_filetype_group('.gif'));
90
        $this->assertFalse($util->is_filetype_group('somethingveryunlikelytoeverexist'));
91
    }
92
 
93
 
94
    /**
95
     * Test describing list of extensions.
96
     */
11 efrain 97
    public function test_describe_file_types(): void {
1 efrain 98
        $this->resetAfterTest(true);
99
        $util = new filetypes_util();
100
 
101
        force_current_language('en');
102
 
103
        // Check that it is able to describe individual file extensions.
104
        $desc = $util->describe_file_types('jpg .jpeg *.jpe PNG;.gif,  mudrd8mz');
105
        $this->assertTrue($desc->hasdescriptions);
106
 
107
        $desc = $desc->descriptions;
108
        $this->assertEquals(4, count($desc));
109
 
110
        $this->assertEquals('File', $desc[0]->description);
111
        $this->assertEquals('.mudrd8mz', $desc[0]->extensions);
112
 
113
        $this->assertEquals('Image (JPEG)', $desc[2]->description);
114
        $this->assertStringContainsString('.jpg', $desc[2]->extensions);
115
        $this->assertStringContainsString('.jpeg', $desc[2]->extensions);
116
        $this->assertStringContainsString('.jpe', $desc[2]->extensions);
117
 
118
        // Check that it can describe groups and mimetypes too.
119
        $desc = $util->describe_file_types('audio text/plain');
120
        $this->assertTrue($desc->hasdescriptions);
121
 
122
        $desc = $desc->descriptions;
123
        $this->assertEquals(2, count($desc));
124
 
125
        $this->assertEquals('Audio files', $desc[0]->description);
126
        $this->assertStringContainsString('.mp3', $desc[0]->extensions);
127
        $this->assertStringContainsString('.wav', $desc[0]->extensions);
128
        $this->assertStringContainsString('.ogg', $desc[0]->extensions);
129
 
130
        $this->assertEquals('Text file', $desc[1]->description);
131
        $this->assertStringContainsString('.txt', $desc[1]->extensions);
132
 
133
        // Empty.
134
        $desc = $util->describe_file_types('');
135
        $this->assertFalse($desc->hasdescriptions);
136
        $this->assertEmpty($desc->descriptions);
137
 
138
        // Any.
139
        $desc = $util->describe_file_types('*');
140
        $this->assertTrue($desc->hasdescriptions);
141
        $this->assertNotEmpty($desc->descriptions[0]->description);
142
        $this->assertEmpty($desc->descriptions[0]->extensions);
143
 
144
        // Unknown mimetype.
145
        $desc = $util->describe_file_types('application/x-something-really-unlikely-ever-exist');
146
        $this->assertTrue($desc->hasdescriptions);
147
        $this->assertEquals('application/x-something-really-unlikely-ever-exist', $desc->descriptions[0]->description);
148
        $this->assertEmpty($desc->descriptions[0]->extensions);
149
    }
150
 
151
    /**
152
     * Test expanding mime types into extensions.
153
     */
11 efrain 154
    public function test_expand(): void {
1 efrain 155
        $this->resetAfterTest(true);
156
        $util = new filetypes_util();
157
 
158
        $this->assertSame([], $util->expand(''));
159
 
160
        $expanded = $util->expand('document .cdr text/plain');
161
        $this->assertNotContains('document', $expanded);
162
        $this->assertNotContains('text/plain', $expanded);
163
        $this->assertContains('.doc', $expanded);
164
        $this->assertContains('.odt', $expanded);
165
        $this->assertContains('.txt', $expanded);
166
        $this->assertContains('.cdr', $expanded);
167
 
168
        $expanded = $util->expand('document .cdr text/plain', true, false);
169
        $this->assertContains('document', $expanded);
170
        $this->assertNotContains('text/plain', $expanded);
171
        $this->assertContains('.doc', $expanded);
172
        $this->assertContains('.odt', $expanded);
173
        $this->assertContains('.txt', $expanded);
174
        $this->assertContains('.cdr', $expanded);
175
 
176
        $expanded = $util->expand('document .cdr text/plain', false, true);
177
        $this->assertNotContains('document', $expanded);
178
        $this->assertContains('text/plain', $expanded);
179
        $this->assertContains('.doc', $expanded);
180
        $this->assertContains('.odt', $expanded);
181
        $this->assertContains('.txt', $expanded);
182
        $this->assertContains('.cdr', $expanded);
183
 
184
        $this->assertSame([], $util->expand('foo/bar', true, false));
185
        $this->assertSame(['foo/bar'], $util->expand('foo/bar', true, true));
186
    }
187
 
188
    /**
189
     * Test checking that a type is among others.
190
     */
11 efrain 191
    public function test_is_listed(): void {
1 efrain 192
        $this->resetAfterTest(true);
193
        $util = new filetypes_util();
194
 
195
        // These should be intuitively true.
196
        $this->assertTrue($util->is_listed('txt', 'text/plain'));
197
        $this->assertTrue($util->is_listed('txt', 'doc txt rtf'));
198
        $this->assertTrue($util->is_listed('.txt', '.doc;.txt;.rtf'));
199
        $this->assertTrue($util->is_listed('audio', 'text/plain audio video'));
200
        $this->assertTrue($util->is_listed('text/plain', 'text/plain audio video'));
201
        $this->assertTrue($util->is_listed('jpg jpe jpeg', 'image/jpeg'));
202
        $this->assertTrue($util->is_listed(['jpg', 'jpe', '.png'], 'image'));
203
 
204
        // These should be intuitively false.
205
        $this->assertFalse($util->is_listed('.gif', 'text/plain'));
206
 
207
        // Not all text/plain formats are in the document group.
208
        $this->assertFalse($util->is_listed('text/plain', 'document'));
209
 
210
        // Not all documents (and also the group itself) is not a plain text.
211
        $this->assertFalse($util->is_listed('document', 'text/plain'));
212
 
213
        // This may look wrong at the first sight as you might expect that the
214
        // mimetype should simply map to an extension ...
215
        $this->assertFalse($util->is_listed('image/jpeg', '.jpg'));
216
 
217
        // But it is principally same situation as this (there is no 1:1 mapping).
218
        $this->assertFalse($util->is_listed('.c', '.txt'));
219
        $this->assertTrue($util->is_listed('.txt .c', 'text/plain'));
220
        $this->assertFalse($util->is_listed('text/plain', '.c'));
221
 
222
        // Any type is included if the filter is empty.
223
        $this->assertTrue($util->is_listed('txt', ''));
224
        $this->assertTrue($util->is_listed('txt', '*'));
225
 
226
        // Empty value is part of any list.
227
        $this->assertTrue($util->is_listed('', '.txt'));
228
    }
229
 
230
    /**
231
     * Test getting types not present in a list.
232
     */
11 efrain 233
    public function test_get_not_listed(): void {
1 efrain 234
        $this->resetAfterTest(true);
235
        $util = new filetypes_util();
236
 
237
        $this->assertEmpty($util->get_not_listed('txt', 'text/plain'));
238
        $this->assertEmpty($util->get_not_listed('txt', '.doc .txt .rtf'));
239
        $this->assertEmpty($util->get_not_listed('txt', 'text/plain'));
240
        $this->assertEmpty($util->get_not_listed(['jpg', 'jpe', 'jpeg'], 'image/jpeg'));
241
        $this->assertEmpty($util->get_not_listed('', 'foo/bar'));
242
        $this->assertEmpty($util->get_not_listed('.foobar', ''));
243
        $this->assertEmpty($util->get_not_listed('.foobar', '*'));
244
 
245
        // Returned list is normalized so extensions have the dot added.
246
        $this->assertContains('.exe', $util->get_not_listed('exe', '.c .h'));
247
 
248
        // If this looks wrong to you, see {@see self::test_is_listed()} for more details on this behaviour.
249
        $this->assertContains('image/jpeg', $util->get_not_listed('image/jpeg', '.jpg .jpeg'));
250
    }
251
 
252
    /**
253
     * Test populating the tree for the browser.
254
     */
11 efrain 255
    public function test_data_for_browser(): void {
1 efrain 256
        $this->resetAfterTest(true);
257
        $util = new filetypes_util();
258
 
259
        $data = $util->data_for_browser();
260
        $this->assertContainsOnly('object', $data);
261
        foreach ($data as $group) {
262
            $this->assertObjectHasProperty('key', $group);
263
            $this->assertObjectHasProperty('types', $group);
264
            if ($group->key !== '') {
265
                $this->assertTrue($group->selectable);
266
            }
267
        }
268
 
269
        // Confirm that the reserved type '.xxx' isn't present in the 'Other files' section.
270
        $types = array_reduce($data, function($carry, $group) {
271
            if ($group->name === 'Other files') {
272
                return $group->types;
273
            }
274
        });
275
        $typekeys = array_map(function($type) {
276
            return $type->key;
277
        }, $types);
278
        $this->assertNotContains('.xxx', $typekeys);
279
 
280
        // All these three files are in both "image" and also "web_image"
281
        // groups. We display both groups.
282
        $data = $util->data_for_browser('jpg png gif', true, '.gif');
283
        $this->assertEquals(3, count($data));
284
        $this->assertTrue($data[0]->key !== $data[1]->key);
285
        foreach ($data as $group) {
286
            $this->assertTrue(($group->key === 'image' || $group->key === 'web_image' || $group->key === 'optimised_image'));
287
            $this->assertEquals(3, count($group->types));
288
            $this->assertFalse($group->selectable);
289
            foreach ($group->types as $ext) {
290
                if ($ext->key === '.gif') {
291
                    $this->assertTrue($ext->selected);
292
                } else {
293
                    $this->assertFalse($ext->selected);
294
                }
295
            }
296
        }
297
 
298
        // The groups web_image and optimised_image are a subset of the group image. The
299
        // file extensions that fall into these groups will be displayed thrice.
300
        $data = $util->data_for_browser('web_image');
301
        foreach ($data as $group) {
302
            $this->assertTrue(($group->key === 'image' || $group->key === 'web_image' || $group->key === 'optimised_image'));
303
        }
304
 
305
        // Check that "All file types" are displayed first.
306
        $data = $util->data_for_browser();
307
        $group = array_shift($data);
308
        $this->assertEquals('*', $group->key);
309
 
310
        // Check that "All file types" is not displayed if should not.
311
        $data = $util->data_for_browser(null, false);
312
        $group = array_shift($data);
313
        $this->assertNotEquals('*', $group->key);
314
 
315
        // Groups with an extension selected start expanded. The "Other files"
316
        // starts expanded. The rest start collapsed.
317
        $data = $util->data_for_browser(null, false, '.png');
318
        foreach ($data as $group) {
319
            if ($group->key === 'document') {
320
                $this->assertfalse($group->expanded);
321
            } else if ($group->key === '') {
322
                $this->assertTrue($group->expanded);
323
            }
324
            foreach ($group->types as $ext) {
325
                foreach ($group->types as $ext) {
326
                    if ($ext->key === '.png') {
327
                        $this->assertTrue($ext->selected);
328
                        $this->assertTrue($group->expanded);
329
                    }
330
                }
331
            }
332
        }
333
    }
334
 
335
    /**
336
     * Data provider for testing test_is_allowed_file_type.
337
     *
338
     * @return array
339
     */
1441 ariadna 340
    public static function is_allowed_file_type_provider(): array {
1 efrain 341
        return [
342
            'Filetype not in extension list' => [
343
                'filename' => 'test.xml',
344
                'list' => '.png .jpg',
345
                'expected' => false
346
            ],
347
            'Filetype not in mimetype list' => [
348
                'filename' => 'test.xml',
349
                'list' => 'image/png',
350
                'expected' => false
351
            ],
352
            'Filetype not in group list' => [
353
                'filename' => 'test.xml',
354
                'list' => 'web_file',
355
                'expected' => false
356
            ],
357
            'Filetype in list as extension' => [
358
                'filename' => 'test.xml',
359
                'list' => 'xml',
360
                'expected' => true
361
            ],
362
            'Empty list should allow all' => [
363
                'filename' => 'test.xml',
364
                'list' => '',
365
                'expected' => true
366
            ],
367
            'Filetype in list but later on' => [
368
                'filename' => 'test.xml',
369
                'list' => 'gif;jpeg,image/png xml xlsx',
370
                'expected' => true
371
            ],
372
            'Filetype in list as mimetype' => [
373
                'filename' => 'test.xml',
374
                'list' => 'image/png application/xml',
375
                'expected' => true
376
            ],
377
            'Filetype in list as group' => [
378
                'filename' => 'test.html',
379
                'list' => 'video,web_file',
380
                'expected' => true
381
            ],
382
        ];
383
    }
384
 
385
    /**
386
     * Test is_allowed_file_type().
387
     * @dataProvider is_allowed_file_type_provider
388
     * @param string $filename The filename to check
389
     * @param string $list The space , or ; separated list of types supported
390
     * @param boolean $expected The expected result. True if the file is allowed, false if not.
391
     */
11 efrain 392
    public function test_is_allowed_file_type($filename, $list, $expected): void {
1 efrain 393
        $util = new filetypes_util();
394
        $this->assertSame($expected, $util->is_allowed_file_type($filename, $list));
395
    }
396
 
397
    /**
398
     * Data provider for testing test_get_unknown_file_types.
399
     *
400
     * @return array
401
     */
1441 ariadna 402
    public static function get_unknown_file_types_provider(): array {
1 efrain 403
        return [
404
            'Empty list' => [
405
                'filetypes' => '',
406
                'expected' => [],
407
            ],
408
            'Any file type' => [
409
                'filetypes' => '*',
410
                'expected' => [],
411
            ],
412
            'Unknown extension' => [
413
                'filetypes' => '.rat',
414
                'expected' => ['.rat']
415
            ],
416
            'Multiple unknown extensions' => [
417
                'filetypes' => '.ricefield .rat',
418
                'expected' => ['.ricefield', '.rat']
419
            ],
420
            'Existant extension' => [
421
                'filetypes' => '.xml',
422
                'expected' => []
423
            ],
424
            'Existant group' => [
425
                'filetypes' => 'web_file',
426
                'expected' => []
427
            ],
428
            'Nonexistant mimetypes' => [
429
                'filetypes' => 'ricefield/rat',
430
                'expected' => ['ricefield/rat']
431
            ],
432
            'Existant mimetype' => [
433
                'filetypes' => 'application/xml',
434
                'expected' => []
435
            ],
436
            'Multiple unknown mimetypes' => [
437
                'filetypes' => 'ricefield/rat cam/ball',
438
                'expected' => ['ricefield/rat', 'cam/ball']
439
            ],
440
            'Strange characters in unknown extension/group' => [
441
                'filetypes' => '©ç√√ß∂å√©åß©√',
442
                'expected' => ['.©ç√√ß∂å√©åß©√']
443
            ],
444
            'Some existant some not' => [
445
                'filetypes' => '.txt application/xml web_file ©ç√√ß∂å√©åß©√ .png ricefield/rat document',
446
                'expected' => ['.©ç√√ß∂å√©åß©√', 'ricefield/rat']
447
            ],
448
            'Reserved file type xxx included' => [
449
                'filetypes' => '.xxx .html .jpg',
450
                'expected' => ['.xxx']
451
            ]
452
        ];
453
    }
454
 
455
    /**
456
     * Test get_unknown_file_types().
457
     * @dataProvider get_unknown_file_types_provider
458
     * @param string $filetypes The filetypes to check
459
     * @param array $expected The expected result. The list of non existant file types.
460
     */
11 efrain 461
    public function test_get_unknown_file_types($filetypes, $expected): void {
1 efrain 462
        $util = new filetypes_util();
463
        $this->assertSame($expected, $util->get_unknown_file_types($filetypes));
464
    }
465
}