1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace ImageUploader\Test\TestCase\Lib; |
14
|
|
|
|
15
|
|
|
use Cake\TestSuite\TestCase; |
16
|
|
|
use ImageUploader\Lib\UploaderConfig; |
17
|
|
|
|
18
|
|
|
class UploaderConfigTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testMaxResize() |
21
|
|
|
{ |
22
|
|
|
$config = new UploaderConfig(); |
23
|
|
|
|
24
|
|
|
$this->assertEquals(650000, $config->getMaxResize()); |
25
|
|
|
|
26
|
|
|
$newDefaultSize = random_int(1, 1000); |
27
|
|
|
$config->setDefaultMaxResize($newDefaultSize); |
28
|
|
|
$this->assertEquals($newDefaultSize, $config->getMaxResize()); |
29
|
|
|
|
30
|
|
|
$config->setDefaultMaxResize('1 MB'); |
31
|
|
|
$this->assertEquals(1048576, $config->getMaxResize()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testMaxResizeNotValid() |
35
|
|
|
{ |
36
|
|
|
$config = new UploaderConfig(); |
37
|
|
|
|
38
|
|
|
$this->expectException(\InvalidArgumentException::class); |
39
|
|
|
$this->expectExceptionCode(1596199482); |
40
|
|
|
|
41
|
|
|
$config->setDefaultMaxResize(true); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testSetImageCompressionQuality() |
45
|
|
|
{ |
46
|
|
|
$config = new UploaderConfig(); |
47
|
|
|
|
48
|
|
|
$this->assertEquals(92, $config->getJpegCompressionFactor()); |
49
|
|
|
$new = random_int(0, 100); |
50
|
|
|
$config->setImageCompressionQuality($new); |
51
|
|
|
$this->assertEquals($new, $config->getJpegCompressionFactor()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testSetImageCompressionQualityNotValid() |
55
|
|
|
{ |
56
|
|
|
$config = new UploaderConfig(); |
57
|
|
|
|
58
|
|
|
$this->expectException(\InvalidArgumentException::class); |
59
|
|
|
$this->expectExceptionCode(1596204082); |
60
|
|
|
|
61
|
|
|
$config->setImageCompressionQuality(101); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testDefaultSize() |
65
|
|
|
{ |
66
|
|
|
$config = new UploaderConfig(); |
67
|
|
|
$config->addType('video/webm'); |
68
|
|
|
$this->assertEquals(2000000, $config->getSize('video/webm')); |
69
|
|
|
|
70
|
|
|
$config->setDefaultMaxFileSize(5); |
71
|
|
|
$config->addType('text/plain'); |
72
|
|
|
$this->assertEquals(5, $config->getSize('text/plain')); |
73
|
|
|
|
74
|
|
|
$config->setDefaultMaxFileSize('3KB'); |
75
|
|
|
$config->addType('text/plain'); |
76
|
|
|
$this->assertEquals(3 * 1024, $config->getSize('text/plain')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testCustomSize() |
80
|
|
|
{ |
81
|
|
|
$config = new UploaderConfig(); |
82
|
|
|
|
83
|
|
|
$config->addType('text/plain', 10); |
84
|
|
|
$this->assertEquals(10, $config->getSize('text/plain')); |
85
|
|
|
|
86
|
|
|
$config->addType('text/plain', '10KB'); |
87
|
|
|
$this->assertEquals(10 * 1024, $config->getSize('text/plain')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testSizeNotFound() |
91
|
|
|
{ |
92
|
|
|
$config = new UploaderConfig(); |
93
|
|
|
|
94
|
|
|
$this->expectException(\RuntimeException::class); |
95
|
|
|
$this->expectExceptionCode(1561357996); |
96
|
|
|
|
97
|
|
|
$config->getSize('foo/bar'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetCacheKey() |
101
|
|
|
{ |
102
|
|
|
$config = new UploaderConfig(); |
103
|
|
|
|
104
|
|
|
$this->assertEquals('uploadsThumbnails', $config->getCacheKey()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetAllTypes() |
108
|
|
|
{ |
109
|
|
|
$config = new UploaderConfig(); |
110
|
|
|
$types = ['text/plain', 'video/webm']; |
111
|
|
|
foreach ($types as $type) { |
112
|
|
|
$config->addType($type); |
113
|
|
|
} |
114
|
|
|
$this->assertEquals($types, $config->getAllTypes()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testMaxNumberOfUploadsPerUser() |
118
|
|
|
{ |
119
|
|
|
$config = new UploaderConfig(); |
120
|
|
|
$this->assertEquals(10, $config->getMaxNumberOfUploadsPerUser()); |
121
|
|
|
|
122
|
|
|
$config->setMaxNumberOfUploadsPerUser(20); |
123
|
|
|
$this->assertEquals(20, $config->getMaxNumberOfUploadsPerUser()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testHasType() |
127
|
|
|
{ |
128
|
|
|
$config = new UploaderConfig(); |
129
|
|
|
$this->assertFalse($config->hasType('text/plain')); |
130
|
|
|
|
131
|
|
|
$config->addType('text/plain'); |
132
|
|
|
$this->assertTrue($config->hasType('text/plain')); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|