3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2017-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
37 namespace Galette\Core\test\units
;
39 use PHPUnit\Framework\TestCase
;
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
53 class Picture
extends TestCase
55 private \Galette\Core\Db
$zdb;
56 private \Galette\Core\Picture
$picture;
57 private array $expected_badchars = [
77 public function setUp(): void
79 $this->zdb
= new \Galette\Core\
Db();
80 $this->picture
= new \Galette\Core\
Picture();
88 public function tearDown(): void
90 if (TYPE_DB
=== 'mysql') {
91 $this->assertSame([], $this->zdb
->getWarnings());
96 * Test defaults after initialization
100 public function testDefaults()
102 $picture = new \Galette\Core\
Picture();
103 $this->assertNull($picture->getDestDir());
104 $this->assertNull($picture->getFileName());
106 $expected_exts = ['jpeg', 'jpg', 'png', 'gif', 'webp'];
107 $this->assertSame(implode(', ', $expected_exts), $picture->getAllowedExts());
110 'jpg' => 'image/jpeg',
111 'png' => 'image/png',
112 'gif' => 'image/gif',
113 'webp' => 'image/webp'
115 $this->assertSame($expected_mimes, $picture->getAllowedMimeTypes());
118 '`' . implode('`, `', $this->expected_badchars
) . '`',
119 $this->picture
->getBadChars()
128 public function testSetters()
130 $this->assertNull($this->picture
->getDestDir());
131 $this->assertNull($this->picture
->getFileName());
133 $this->picture
->setDestDir(__DIR__
);
134 $this->assertSame(__DIR__
, $this->picture
->getDestDir());
136 $this->picture
->setFileName('myfile.png');
137 $this->assertSame('myfile.png', $this->picture
->getFileName());
141 * Test mimetype guess
142 * FileInfo installed.
146 public function testFileInfoMimeType()
148 $url = realpath(GALETTE_ROOT
. '../tests/fake_image.jpg');
149 $this->assertNotFalse($url);
150 $this->assertSame('image/jpeg', $this->picture
->getMimeType($url));
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));
156 $url = realpath(GALETTE_ROOT
. '../tests/test.gif');
157 $this->assertNotFalse($url);
158 $this->assertSame('image/gif', $this->picture
->getMimeType($url));
160 $this->assertSame('text/x-php', $this->picture
->getMimeType(__DIR__
. '/Picture.php'));
164 * Test mimetype guess
165 * FileInfo not installed, back to mime_content_type call
167 * Does not actually works :/
171 /*public function testMimeContentTypeMimeType()
173 $url = realpath(GALETTE_ROOT . '../tests/fake_image.jpg');
174 $this->assertNotFalse($url);
176 $this->assert('FileInfo extension missing')
177 ->given($picture = new \Galette\Core\Picture())
178 ->if($this->function->function_exists = false)
180 ->variable($picture->getMimeType($url))->isIdenticalTo('image/jpeg');
188 public function testStore()
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
;
197 'name' => 'file-with-' . $badchar . '-char.jpg',
200 $this->assertSame($expected, $this->picture
->store($file));
206 'accentued-éè-file.gif',
212 foreach ($files as $file) {
215 'tmp_name' => 'none',
216 'size' => \Galette\Core\Picture
::MAX_FILE_SIZE
* 1024 * 100
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));
224 * Test error messages
228 public function testErrorMessages()
231 'File name is invalid, it should not contain any special character or space.',
232 $this->picture
->getErrorMessage(\Galette\Core\Picture
::INVALID_FILENAME
)
235 'File extension is not allowed, only jpeg, jpg, png, gif, webp files are.',
236 $this->picture
->getErrorMessage(\Galette\Core\Picture
::INVALID_EXTENSION
)
239 'File is too big. Maximum allowed size is 2048Ko',
240 $this->picture
->getErrorMessage(\Galette\Core\Picture
::FILE_TOO_BIG
)
243 'Mime-Type not allowed',
244 $this->picture
->getErrorMessage(\Galette\Core\Picture
::MIME_NOT_ALLOWED
)
247 'File does not comply with requirements.',
248 $this->picture
->getErrorMessage(\Galette\Core\Picture
::INVALID_FILE
)
251 'Unable to write file or temporary file',
252 $this->picture
->getErrorMessage(\Galette\Core\Picture
::CANT_WRITE
)
255 'An SQL error has occurred.',
256 $this->picture
->getErrorMessage(\Galette\Core\Picture
::SQL_ERROR
)
259 'An SQL error has occurred.',
260 $this->picture
->getErrorMessage(\Galette\Core\Picture
::SQL_BLOB_ERROR
)