MDL-53240 filetypes: Introduce the filetypes_util helper class
[moodle.git] / lib / form / tests / filetypes_util_test.php
CommitLineData
6c4a5fdf
DM
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/**
18 * Provides the {@link core_form\filetypes_util_testcase} class.
19 *
20 * @package core_form
21 * @category test
22 * @copyright 2017 David Mudrák <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core_form;
27
28use advanced_testcase;
29
30defined('MOODLE_INTERNAL') || die();
31
32global $CFG;
33
34/**
35 * Test cases for the {@link core_form\filetypes_util} class.
36 *
37 * @copyright 2017 David Mudrak <david@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class filetypes_util_testcase extends advanced_testcase {
41
42 /**
43 * Test normalizing list of extensions.
44 */
45 public function test_normalize_file_types() {
46
47 $this->resetAfterTest(true);
48 $util = new filetypes_util();
49
50 $this->assertSame(['.odt'], $util->normalize_file_types('.odt'));
51 $this->assertSame(['.odt'], $util->normalize_file_types('odt'));
52 $this->assertSame(['.odt'], $util->normalize_file_types('.ODT'));
53 $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types('doc, jpg, mp3'));
54 $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types(['.doc', '.jpg', '.mp3']));
55 $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types('doc, *.jpg, mp3'));
56 $this->assertSame(['.doc', '.jpg', '.mp3'], $util->normalize_file_types(['doc ', ' JPG ', '.mp3']));
57 $this->assertSame(['.rtf', '.pdf', '.docx'],
58 $util->normalize_file_types("RTF,.pdf\n...DocX,,,;\rPDF\trtf ...Rtf"));
59 $this->assertSame(['.tgz', '.tar.gz'], $util->normalize_file_types('tgz,TAR.GZ tar.gz .tar.gz tgz TGZ'));
60 $this->assertSame(['.notebook'], $util->normalize_file_types('"Notebook":notebook;NOTEBOOK;,\'NoTeBook\''));
61 $this->assertSame([], $util->normalize_file_types(''));
62 $this->assertSame([], $util->normalize_file_types([]));
63 $this->assertSame(['.0'], $util->normalize_file_types(0));
64 $this->assertSame(['.0'], $util->normalize_file_types('0'));
65 $this->assertSame(['.odt'], $util->normalize_file_types('*.odt'));
66 $this->assertSame([], $util->normalize_file_types('.'));
67 $this->assertSame(['.foo'], $util->normalize_file_types('. foo'));
68 $this->assertSame(['*'], $util->normalize_file_types('*'));
69 $this->assertSame([], $util->normalize_file_types('*~'));
70 $this->assertSame(['.pdf', '.ps'], $util->normalize_file_types('pdf *.ps foo* *bar .r??'));
71 $this->assertSame(['*'], $util->normalize_file_types('pdf *.ps foo* * *bar .r??'));
72 }
73
74 /**
75 * Test MIME type formal recognition.
76 */
77 public function test_looks_like_mimetype() {
78
79 $this->resetAfterTest(true);
80 $util = new filetypes_util();
81
82 $this->assertTrue($util->looks_like_mimetype('type/subtype'));
83 $this->assertTrue($util->looks_like_mimetype('type/x-subtype'));
84 $this->assertTrue($util->looks_like_mimetype('type/x-subtype+xml'));
85 $this->assertTrue($util->looks_like_mimetype('type/vnd.subtype.xml'));
86 $this->assertTrue($util->looks_like_mimetype('type/vnd.subtype+xml'));
87
88 $this->assertFalse($util->looks_like_mimetype('.gif'));
89 $this->assertFalse($util->looks_like_mimetype('audio'));
90 $this->assertFalse($util->looks_like_mimetype('foo/bar/baz'));
91 }
92
93 /**
94 * Test getting/checking group.
95 */
96 public function test_is_filetype_group() {
97
98 $this->resetAfterTest(true);
99 $util = new filetypes_util();
100
101 $audio = $util->is_filetype_group('audio');
102 $this->assertNotFalse($audio);
103 $this->assertInternalType('array', $audio->extensions);
104 $this->assertInternalType('array', $audio->mimetypes);
105
106 $this->assertFalse($util->is_filetype_group('.gif'));
107 $this->assertFalse($util->is_filetype_group('somethingveryunlikelytoeverexist'));
108 }
109
110
111 /**
112 * Test describing list of extensions.
113 */
114 public function test_describe_file_types() {
115
116 $this->resetAfterTest(true);
117 $util = new filetypes_util();
118
119 force_current_language('en');
120
121 // Check that it is able to describe individual file extensions.
122 $desc = $util->describe_file_types('jpg .jpeg *.jpe PNG;.gif, mudrd8mz');
123 $this->assertTrue($desc->hasdescriptions);
124
125 $desc = $desc->descriptions;
126 $this->assertEquals(4, count($desc));
127
128 $this->assertEquals('File', $desc[0]->description);
129 $this->assertEquals('.mudrd8mz', $desc[0]->extensions);
130
131 $this->assertEquals('Image (JPEG)', $desc[2]->description);
132 $this->assertContains('.jpg', $desc[2]->extensions);
133 $this->assertContains('.jpeg', $desc[2]->extensions);
134 $this->assertContains('.jpe', $desc[2]->extensions);
135
136 // Check that it can describe groups and mimetypes too.
137 $desc = $util->describe_file_types('audio text/plain');
138 $this->assertTrue($desc->hasdescriptions);
139
140 $desc = $desc->descriptions;
141 $this->assertEquals(2, count($desc));
142
143 $this->assertEquals('Audio files', $desc[0]->description);
144 $this->assertContains('.mp3', $desc[0]->extensions);
145 $this->assertContains('.wav', $desc[0]->extensions);
146 $this->assertContains('.ogg', $desc[0]->extensions);
147
148 $this->assertEquals('Text file', $desc[1]->description);
149 $this->assertContains('.txt', $desc[1]->extensions);
150
151 // Empty.
152 $desc = $util->describe_file_types('');
153 $this->assertFalse($desc->hasdescriptions);
154 $this->assertEmpty($desc->descriptions);
155
156 // Any.
157 $desc = $util->describe_file_types('*');
158 $this->assertTrue($desc->hasdescriptions);
159 $this->assertNotEmpty($desc->descriptions[0]->description);
160 $this->assertEmpty($desc->descriptions[0]->extensions);
161
162 // Unknown mimetype.
163 $desc = $util->describe_file_types('application/x-something-really-unlikely-ever-exist');
164 $this->assertTrue($desc->hasdescriptions);
165 $this->assertEquals('application/x-something-really-unlikely-ever-exist', $desc->descriptions[0]->description);
166 $this->assertEmpty($desc->descriptions[0]->extensions);
167 }
168}