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