]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Picture.php
Set default max filesize to 2Mio
[galette.git] / tests / Galette / Core / tests / units / Picture.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Picture tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category IO
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2017-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since 2017-05-14
35 */
36
37 namespace Galette\Core\test\units;
38
39 use PHPUnit\Framework\TestCase;
40
41 /**
42 * Picture tests class
43 *
44 * @category Core
45 * @name Picture
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2017-2023 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
51 * @since 2017-05-14
52 */
53 class Picture extends TestCase
54 {
55 private \Galette\Core\Db $zdb;
56 private \Galette\Core\Picture $picture;
57 private array $expected_badchars = [
58 '.',
59 '\\',
60 "'",
61 ' ',
62 '/',
63 ':',
64 '*',
65 '?',
66 '"',
67 '<',
68 '>',
69 '|'
70 ];
71
72 /**
73 * Set up tests
74 *
75 * @return void
76 */
77 public function setUp(): void
78 {
79 $this->zdb = new \Galette\Core\Db();
80 $this->picture = new \Galette\Core\Picture();
81 }
82
83 /**
84 * Tear down tests
85 *
86 * @return void
87 */
88 public function tearDown(): void
89 {
90 if (TYPE_DB === 'mysql') {
91 $this->assertSame([], $this->zdb->getWarnings());
92 }
93 }
94
95 /**
96 * Test defaults after initialization
97 *
98 * @return void
99 */
100 public function testDefaults()
101 {
102 $picture = new \Galette\Core\Picture();
103 $this->assertNull($picture->getDestDir());
104 $this->assertNull($picture->getFileName());
105
106 $expected_exts = ['jpeg', 'jpg', 'png', 'gif', 'webp'];
107 $this->assertSame(implode(', ', $expected_exts), $picture->getAllowedExts());
108
109 $expected_mimes = [
110 'jpg' => 'image/jpeg',
111 'png' => 'image/png',
112 'gif' => 'image/gif',
113 'webp' => 'image/webp'
114 ];
115 $this->assertSame($expected_mimes, $picture->getAllowedMimeTypes());
116
117 $this->assertSame(
118 '`' . implode('`, `', $this->expected_badchars) . '`',
119 $this->picture->getBadChars()
120 );
121 }
122
123 /**
124 * Test setters
125 *
126 * @return void
127 */
128 public function testSetters()
129 {
130 $this->assertNull($this->picture->getDestDir());
131 $this->assertNull($this->picture->getFileName());
132
133 $this->picture->setDestDir(__DIR__);
134 $this->assertSame(__DIR__, $this->picture->getDestDir());
135
136 $this->picture->setFileName('myfile.png');
137 $this->assertSame('myfile.png', $this->picture->getFileName());
138 }
139
140 /**
141 * Test mimetype guess
142 * FileInfo installed.
143 *
144 * @return void
145 */
146 public function testFileInfoMimeType()
147 {
148 $url = realpath(GALETTE_ROOT . '../tests/fake_image.jpg');
149 $this->assertNotFalse($url);
150 $this->assertSame('image/jpeg', $this->picture->getMimeType($url));
151
152 $url = realpath(GALETTE_ROOT . '../galette/webroot/themes/default/images/galette.png');
153 $this->assertNotFalse($url);
154 $this->assertSame('image/png', $this->picture->getMimeType($url));
155
156 $url = realpath(GALETTE_ROOT . '../tests/test.gif');
157 $this->assertNotFalse($url);
158 $this->assertSame('image/gif', $this->picture->getMimeType($url));
159
160 $this->assertSame('text/x-php', $this->picture->getMimeType(__DIR__ . '/Picture.php'));
161 }
162
163 /**
164 * Test mimetype guess
165 * FileInfo not installed, back to mime_content_type call
166 *
167 * Does not actually works :/
168 *
169 * @return void
170 */
171 /*public function testMimeContentTypeMimeType()
172 {
173 $url = realpath(GALETTE_ROOT . '../tests/fake_image.jpg');
174 $this->assertNotFalse($url);
175
176 $this->assert('FileInfo extension missing')
177 ->given($picture = new \Galette\Core\Picture())
178 ->if($this->function->function_exists = false)
179 ->then
180 ->variable($picture->getMimeType($url))->isIdenticalTo('image/jpeg');
181 }*/
182
183 /**
184 * Test storage
185 *
186 * @return void
187 */
188 public function testStore()
189 {
190 foreach ($this->expected_badchars as $badchar) {
191 $expected = \Galette\Core\Picture::INVALID_FILENAME;
192 if ($badchar == '.') {
193 //will give an invalid extension
194 $expected = \Galette\Core\Picture::INVALID_EXTENSION;
195 }
196 $file = [
197 'name' => 'file-with-' . $badchar . '-char.jpg',
198 'tmp_name' => 'none'
199 ];
200 $this->assertSame($expected, $this->picture->store($file));
201 }
202
203 $files = [
204 'myfile.png',
205 'another-file.jpg',
206 'accentued-éè-file.gif',
207 'a3.jpg',
208 'a.jpg',
209 '3.jpg'
210 ];
211
212 foreach ($files as $file) {
213 $file = [
214 'name' => $file,
215 'tmp_name' => 'none',
216 'size' => \Galette\Core\Picture::MAX_FILE_SIZE * 1024 * 100
217 ];
218 //Will fail on filesize, but this is OK, filenames and extensions have been checked :)
219 $this->assertSame(\Galette\Core\Picture::FILE_TOO_BIG, $this->picture->store($file));
220 }
221 }
222
223 /**
224 * Test error messages
225 *
226 * @return void
227 */
228 public function testErrorMessages()
229 {
230 $this->assertSame(
231 'File name is invalid, it should not contain any special character or space.',
232 $this->picture->getErrorMessage(\Galette\Core\Picture::INVALID_FILENAME)
233 );
234 $this->assertSame(
235 'File extension is not allowed, only jpeg, jpg, png, gif, webp files are.',
236 $this->picture->getErrorMessage(\Galette\Core\Picture::INVALID_EXTENSION)
237 );
238 $this->assertSame(
239 'File is too big. Maximum allowed size is 2048Ko',
240 $this->picture->getErrorMessage(\Galette\Core\Picture::FILE_TOO_BIG)
241 );
242 $this->assertSame(
243 'Mime-Type not allowed',
244 $this->picture->getErrorMessage(\Galette\Core\Picture::MIME_NOT_ALLOWED)
245 );
246 $this->assertSame(
247 'File does not comply with requirements.',
248 $this->picture->getErrorMessage(\Galette\Core\Picture::INVALID_FILE)
249 );
250 $this->assertSame(
251 'Unable to write file or temporary file',
252 $this->picture->getErrorMessage(\Galette\Core\Picture::CANT_WRITE)
253 );
254 $this->assertSame(
255 'An SQL error has occurred.',
256 $this->picture->getErrorMessage(\Galette\Core\Picture::SQL_ERROR)
257 );
258 $this->assertSame(
259 'An SQL error has occurred.',
260 $this->picture->getErrorMessage(\Galette\Core\Picture::SQL_BLOB_ERROR)
261 );
262 }
263 }