|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\FilePond\Test; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
|
6
|
|
|
use SilverStripe\Assets\File; |
|
7
|
|
|
use SilverStripe\Assets\Image; |
|
8
|
|
|
use SilverStripe\Security\Member; |
|
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
10
|
|
|
use LeKoala\FilePond\FilePondField; |
|
11
|
|
|
use SilverStripe\Control\Controller; |
|
12
|
|
|
use SilverStripe\Core\Config\Config; |
|
13
|
|
|
use SilverStripe\Assets\Upload_Validator; |
|
14
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Tests for FilePond module |
|
18
|
|
|
*/ |
|
19
|
|
|
class FilePondTest extends SapphireTest |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Defines the fixture file to use for this test class |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $fixture_file = 'FilePondTest.yml'; |
|
26
|
|
|
|
|
27
|
|
|
protected static $extra_dataobjects = array( |
|
28
|
|
|
Test_FilePondModel::class, |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
public function setUp() |
|
32
|
|
|
{ |
|
33
|
|
|
parent::setUp(); |
|
34
|
|
|
Upload_Validator::config()->set('default_max_file_size', '5MB'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function tearDown() |
|
38
|
|
|
{ |
|
39
|
|
|
parent::tearDown(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getPond() |
|
43
|
|
|
{ |
|
44
|
|
|
$controller = Controller::curr(); |
|
45
|
|
|
$controller->config()->set('url_segment', 'test_controller'); |
|
46
|
|
|
$form = new Form($controller); |
|
47
|
|
|
|
|
48
|
|
|
$uploader = new FilePondField('TestUpload'); |
|
49
|
|
|
$uploader->setRecord($this->getTestModel()); |
|
50
|
|
|
$uploader->setForm($form); |
|
51
|
|
|
return $uploader; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getTestModel() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->objFromFixture(Test_FilePondModel::class, 'demo'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getAdminMember() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->objFromFixture(Member::class, 'admin'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getTempFile() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->objFromFixture(File::class, 'temp'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getRegularFile() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->objFromFixture(File::class, 'regular'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGetMaxFileSize() |
|
75
|
|
|
{ |
|
76
|
|
|
$pond = new FilePondField('TestUpload'); |
|
77
|
|
|
$this->assertEquals('5MB', $pond->getMaxFileSize()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testDefaultDescription() |
|
81
|
|
|
{ |
|
82
|
|
|
// uploaders without records don't have a description |
|
83
|
|
|
$pond = new FilePondField('TestUpload'); |
|
84
|
|
|
$pond->Field(); // mock call to trigger default |
|
85
|
|
|
$this->assertEmpty($pond->getDescription()); |
|
86
|
|
|
|
|
87
|
|
|
// uploaders with a record have a default description |
|
88
|
|
|
$pond = $this->getPond(); |
|
89
|
|
|
$pond->Field(); // mock call to trigger default |
|
90
|
|
|
$this->assertNotEmpty($pond->getDescription()); |
|
91
|
|
|
|
|
92
|
|
|
// we can still set our own |
|
93
|
|
|
$pond->setDescription("my description"); |
|
94
|
|
|
$this->assertEquals("my description", $pond->getDescription()); |
|
95
|
|
|
|
|
96
|
|
|
// custom images sizes can be recommended |
|
97
|
|
|
$pond = $this->getPond(); |
|
98
|
|
|
$pond->setName("Image"); |
|
99
|
|
|
$pond->Field(); // mock call to trigger default |
|
100
|
|
|
$this->assertContains("1080x1080px", $pond->getDescription()); |
|
101
|
|
|
$this->assertContains("min", strtolower($pond->getDescription())); |
|
102
|
|
|
|
|
103
|
|
|
// can set a max res |
|
104
|
|
|
$pond = $this->getPond(); |
|
105
|
|
|
$pond->setName("SmallImage"); |
|
106
|
|
|
$pond->Field(); // mock call to trigger default |
|
107
|
|
|
$this->assertContains("512x512px", $pond->getDescription()); |
|
108
|
|
|
$this->assertContains("max", strtolower($pond->getDescription())); |
|
109
|
|
|
|
|
110
|
|
|
// we don't specify extensions by default |
|
111
|
|
|
$pond = $this->getPond(); |
|
112
|
|
|
$pond->Field(); // mock call to trigger default |
|
113
|
|
|
$this->assertNotContains("extensions", (string)$pond->getDescription()); |
|
114
|
|
|
|
|
115
|
|
|
// image have default type jpg, jpeg, png |
|
116
|
|
|
$pond = $this->getPond(); |
|
117
|
|
|
$pond->setName("Image"); |
|
118
|
|
|
$pond->Field(); // mock call to trigger default |
|
119
|
|
|
$this->assertContains("extensions", $pond->getDescription()); |
|
120
|
|
|
|
|
121
|
|
|
// but we do if we have a small list |
|
122
|
|
|
$pond = $this->getPond(); |
|
123
|
|
|
$pond->setName("Image"); |
|
124
|
|
|
$pond->setAllowedExtensions(['jpg', 'jpeg']); |
|
125
|
|
|
$pond->Field(); // mock call to trigger default |
|
126
|
|
|
$this->assertContains("jpg", $pond->getDescription()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testGetAcceptedFileTypes() |
|
130
|
|
|
{ |
|
131
|
|
|
$pond = $this->getPond(); |
|
132
|
|
|
$pond->setAllowedExtensions(['jpg', 'jpeg']); |
|
133
|
|
|
$this->assertContains('image/jpeg', $pond->getAcceptedFileTypes()); |
|
134
|
|
|
$this->assertCount(1, $pond->getAcceptedFileTypes()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testGetDefaultFolderName() |
|
138
|
|
|
{ |
|
139
|
|
|
$pond = $this->getPond(); |
|
140
|
|
|
$this->assertEquals("Test_FilePondModel/TestUpload", $pond->getFolderName()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testRenamePattern() |
|
144
|
|
|
{ |
|
145
|
|
|
$pond = $this->getPond(); |
|
146
|
|
|
$pond->setRenamePattern("{field}_{date}.{extension}"); |
|
147
|
|
|
|
|
148
|
|
|
$filename = 'mytestfile.jpg'; |
|
149
|
|
|
$expected = 'TestUpload_' . date('Ymd') . '.jpg'; |
|
150
|
|
|
|
|
151
|
|
|
$postVars = [ |
|
152
|
|
|
$pond->getName() => [ |
|
153
|
|
|
'name' => $filename, |
|
154
|
|
|
'error' => 0, |
|
155
|
|
|
'test' => true, |
|
156
|
|
|
] |
|
157
|
|
|
]; |
|
158
|
|
|
$opts = $pond->getServerOptions(); |
|
159
|
|
|
$request = new HTTPRequest('POST', $opts['process']['url'], [], $postVars); |
|
160
|
|
|
foreach ($opts['process']['headers'] as $k => $v) { |
|
161
|
|
|
$request->addHeader($k, $v); |
|
162
|
|
|
} |
|
163
|
|
|
$response = $pond->prepareUpload($request); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertEquals($expected, $response['name']); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function testClearTempFiles() |
|
169
|
|
|
{ |
|
170
|
|
|
// create a temp file |
|
171
|
|
|
$tempFile = new File(); |
|
172
|
|
|
$tempFile->IsTemporary = 1; |
|
173
|
|
|
$tempFile->write(); |
|
174
|
|
|
|
|
175
|
|
|
$result = FilePondField::clearTemporaryUploads(); |
|
176
|
|
|
$this->assertCount(1, $result); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertNotEquals($tempFile->ID, $result[0]->ID, "It should not delete a recent temporary file"); |
|
179
|
|
|
$this->assertEquals($this->getTempFile()->ID, $result[0]->ID, "It should delete an old temporary file"); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function testCustomConfig() |
|
183
|
|
|
{ |
|
184
|
|
|
$pond = $this->getPond(); |
|
185
|
|
|
$pond->addFilePondConfig('allowDrop', false); |
|
186
|
|
|
$this->assertArrayHasKey('allowDrop', $pond->getFilePondConfig()); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function testRequirements() |
|
190
|
|
|
{ |
|
191
|
|
|
FilePondField::config()->use_cdn = true; |
|
192
|
|
|
FilePondField::Requirements(); |
|
193
|
|
|
FilePondField::config()->use_cdn = false; |
|
194
|
|
|
FilePondField::Requirements(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function testImageSizes() |
|
198
|
|
|
{ |
|
199
|
|
|
$pond = $this->getPond(); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertEquals(FilePondField::DEFAULT_POSTER_WIDTH, $pond->getPosterWidth()); |
|
202
|
|
|
|
|
203
|
|
|
$pond->setImageSize(10, 10); |
|
204
|
|
|
$this->assertEquals(10, $pond->getPosterWidth()); |
|
205
|
|
|
|
|
206
|
|
|
$pond->setImageSize(100, 300); |
|
207
|
|
|
|
|
208
|
|
|
$ratio = 300 / FilePondField::DEFAULT_POSTER_WIDTH; // 300/264 = 1,13 |
|
209
|
|
|
$targetWidth = 100 / $ratio; // 100/1,13 = 88 |
|
210
|
|
|
$this->assertEquals($targetWidth, $pond->getPosterWidth()); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|