1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Tests\Provider; |
15
|
|
|
|
16
|
|
|
use Gaufrette\File as GaufretteFile; |
17
|
|
|
use Gaufrette\Filesystem; |
18
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
19
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
20
|
|
|
use Sonata\Form\Validator\ErrorElement; |
21
|
|
|
use Sonata\MediaBundle\CDN\Server; |
22
|
|
|
use Sonata\MediaBundle\Filesystem\Local; |
23
|
|
|
use Sonata\MediaBundle\Generator\IdGenerator; |
24
|
|
|
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface; |
25
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
26
|
|
|
use Sonata\MediaBundle\Provider\FileProvider; |
27
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
28
|
|
|
use Sonata\MediaBundle\Resizer\ResizerInterface; |
29
|
|
|
use Sonata\MediaBundle\Tests\Entity\Media; |
30
|
|
|
use Sonata\MediaBundle\Thumbnail\FormatThumbnail; |
31
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
32
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
33
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
34
|
|
|
use Symfony\Component\HttpFoundation\Request; |
35
|
|
|
|
36
|
|
|
class FileProviderTest extends AbstractProviderTest |
37
|
|
|
{ |
38
|
|
|
public function getProvider(): MediaProviderInterface |
39
|
|
|
{ |
40
|
|
|
$resizer = $this->createMock(ResizerInterface::class); |
41
|
|
|
$resizer->method('resize')->willReturn(true); |
42
|
|
|
|
43
|
|
|
$adapter = $this->createMock(Local::class); |
44
|
|
|
$adapter->method('getDirectory')->willReturn(realpath(__DIR__).'/../fixtures'); |
45
|
|
|
|
46
|
|
|
$filesystem = $this->getMockBuilder(Filesystem::class) |
47
|
|
|
->onlyMethods(['get']) |
48
|
|
|
->setConstructorArgs([$adapter]) |
49
|
|
|
->getMock(); |
50
|
|
|
$file = $this->getMockBuilder(GaufretteFile::class) |
51
|
|
|
->setConstructorArgs(['foo', $filesystem]) |
52
|
|
|
->getMock(); |
53
|
|
|
$filesystem->method('get')->willReturn($file); |
54
|
|
|
|
55
|
|
|
$cdn = new Server('/uploads/media'); |
56
|
|
|
|
57
|
|
|
$generator = new IdGenerator(); |
58
|
|
|
|
59
|
|
|
$thumbnail = new FormatThumbnail('jpg'); |
60
|
|
|
|
61
|
|
|
$metadata = $this->createMock(MetadataBuilderInterface::class); |
62
|
|
|
|
63
|
|
|
$provider = new FileProvider('file', $filesystem, $cdn, $generator, $thumbnail, ['txt'], ['foo/bar'], $metadata); |
64
|
|
|
$provider->setResizer($resizer); |
65
|
|
|
|
66
|
|
|
return $provider; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testProvider(): void |
70
|
|
|
{ |
71
|
|
|
$provider = $this->getProvider(); |
72
|
|
|
|
73
|
|
|
$media = new Media(); |
74
|
|
|
$media->setName('test.txt'); |
75
|
|
|
$media->setProviderReference('ASDASD.txt'); |
76
|
|
|
$media->setContext('default'); |
77
|
|
|
|
78
|
|
|
$media->setId(1023456); |
79
|
|
|
$this->assertSame('default/0011/24/ASDASD.txt', $provider->getReferenceImage($media)); |
80
|
|
|
|
81
|
|
|
$this->assertSame('default/0011/24', $provider->generatePath($media)); |
82
|
|
|
|
83
|
|
|
// default icon image |
84
|
|
|
$this->assertSame('/uploads/media/sonatamedia/files/big/file.png', $provider->generatePublicUrl($media, 'big')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testHelperProperties(): void |
88
|
|
|
{ |
89
|
|
|
$provider = $this->getProvider(); |
90
|
|
|
|
91
|
|
|
$provider->addFormat('admin', ['width' => 100]); |
92
|
|
|
$media = new Media(); |
93
|
|
|
$media->setName('test.png'); |
94
|
|
|
$media->setProviderReference('ASDASDAS.png'); |
95
|
|
|
$media->setId(10); |
96
|
|
|
$media->setHeight(100); |
97
|
|
|
|
98
|
|
|
$properties = $provider->getHelperProperties($media, 'admin'); |
99
|
|
|
|
100
|
|
|
$this->assertIsArray($properties); |
101
|
|
|
$this->assertSame('test.png', $properties['title']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testForm(): void |
105
|
|
|
{ |
106
|
|
|
$provider = $this->getProvider(); |
107
|
|
|
|
108
|
|
|
$admin = $this->createMock(AdminInterface::class); |
109
|
|
|
$admin |
110
|
|
|
->method('trans') |
111
|
|
|
->willReturn('message'); |
112
|
|
|
|
113
|
|
|
$formMapper = $this->createMock(FormMapper::class); |
114
|
|
|
$formMapper->expects($this->exactly(8)) |
115
|
|
|
->method('add') |
116
|
|
|
->willReturn(null); |
117
|
|
|
|
118
|
|
|
$provider->buildCreateForm($formMapper); |
119
|
|
|
$provider->buildEditForm($formMapper); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @doesNotPerformAssertions |
124
|
|
|
*/ |
125
|
|
|
public function testThumbnail(): void |
126
|
|
|
{ |
127
|
|
|
$provider = $this->getProvider(); |
128
|
|
|
|
129
|
|
|
$media = new Media(); |
130
|
|
|
$media->setName('test.png'); |
131
|
|
|
$media->setId(1023456); |
132
|
|
|
|
133
|
|
|
$provider->generateThumbnails($media); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testEvent(): void |
137
|
|
|
{ |
138
|
|
|
$provider = $this->getProvider(); |
139
|
|
|
|
140
|
|
|
$provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
141
|
|
|
|
142
|
|
|
$file = __DIR__.'/../fixtures/file.txt'; |
143
|
|
|
|
144
|
|
|
$media = new Media(); |
145
|
|
|
$provider->preUpdate($media); |
146
|
|
|
$this->assertNull($media->getProviderReference()); |
147
|
|
|
|
148
|
|
|
$media->setBinaryContent($file); |
149
|
|
|
$provider->transform($media); |
150
|
|
|
|
151
|
|
|
$this->assertInstanceOf(\DateTime::class, $media->getUpdatedAt()); |
152
|
|
|
$this->assertNotNull($media->getProviderReference()); |
153
|
|
|
|
154
|
|
|
$provider->postUpdate($media); |
155
|
|
|
|
156
|
|
|
$file = new File(realpath(__DIR__.'/../fixtures/file.txt')); |
157
|
|
|
|
158
|
|
|
$media = new Media(); |
159
|
|
|
$media->setContext('default'); |
160
|
|
|
$media->setBinaryContent($file); |
161
|
|
|
$media->setId(1023456); |
162
|
|
|
|
163
|
|
|
// pre persist the media |
164
|
|
|
$provider->transform($media); |
165
|
|
|
|
166
|
|
|
$this->assertSame('file.txt', $media->getName(), '::getName() return the file name'); |
167
|
|
|
$this->assertNotNull($media->getProviderReference(), '::getProviderReference() is set'); |
168
|
|
|
|
169
|
|
|
$this->assertFalse($provider->generatePrivateUrl($media, 'big'), '::generatePrivateUrl() return false on non reference formate'); |
170
|
|
|
$this->assertNotNull($provider->generatePrivateUrl($media, 'reference'), '::generatePrivateUrl() return path for reference formate'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testDownload(): void |
174
|
|
|
{ |
175
|
|
|
$provider = $this->getProvider(); |
176
|
|
|
|
177
|
|
|
$file = new File(realpath(__DIR__.'/../fixtures/FileProviderTest/0011/24/file.txt')); |
178
|
|
|
|
179
|
|
|
$media = new Media(); |
180
|
|
|
$media->setBinaryContent($file); |
181
|
|
|
$media->setProviderReference('file.txt'); |
182
|
|
|
$media->setContext('FileProviderTest'); |
183
|
|
|
$media->setId(1023456); |
184
|
|
|
|
185
|
|
|
$response = $provider->getDownloadResponse($media, 'reference', 'X-Accel-Redirect'); |
186
|
|
|
|
187
|
|
|
$this->assertInstanceOf(BinaryFileResponse::class, $response); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @dataProvider mediaProvider |
192
|
|
|
*/ |
193
|
|
|
public function testTransform(string $expected, Media $media): void |
194
|
|
|
{ |
195
|
|
|
$closure = function () use ($expected, $media): void { |
196
|
|
|
$provider = $this->getProvider(); |
197
|
|
|
|
198
|
|
|
$provider->transform($media); |
199
|
|
|
|
200
|
|
|
$this->assertInstanceOf($expected, $media->getBinaryContent()); |
201
|
|
|
}; |
202
|
|
|
|
203
|
|
|
$closure(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function mediaProvider(): array |
207
|
|
|
{ |
208
|
|
|
$file = new File(realpath(__DIR__.'/../fixtures/file.txt')); |
209
|
|
|
$content = file_get_contents(realpath(__DIR__.'/../fixtures/file.txt')); |
210
|
|
|
$request = new Request([], [], [], [], [], [], $content); |
211
|
|
|
|
212
|
|
|
$media1 = new Media(); |
213
|
|
|
$media1->setBinaryContent($file); |
214
|
|
|
$media1->setContentType('foo'); |
215
|
|
|
$media1->setId(1023456); |
216
|
|
|
|
217
|
|
|
$media2 = new Media(); |
218
|
|
|
$media2->setBinaryContent($request); |
219
|
|
|
$media2->setContentType('text/plain'); |
220
|
|
|
$media2->setId(1023456); |
221
|
|
|
|
222
|
|
|
return [ |
223
|
|
|
[File::class, $media1], |
224
|
|
|
[File::class, $media1], |
225
|
|
|
[File::class, $media2], |
226
|
|
|
]; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function testBinaryContentWithRealPath(): void |
230
|
|
|
{ |
231
|
|
|
$media = $this->createMock(MediaInterface::class); |
232
|
|
|
|
233
|
|
|
$media |
234
|
|
|
->method('getProviderReference') |
235
|
|
|
->willReturn('provider'); |
236
|
|
|
|
237
|
|
|
$media |
238
|
|
|
->method('getId') |
239
|
|
|
->willReturn(10000); |
240
|
|
|
|
241
|
|
|
$media |
242
|
|
|
->method('getContext') |
243
|
|
|
->willReturn('context'); |
244
|
|
|
|
245
|
|
|
$binaryContent = $this->createMock(File::class); |
246
|
|
|
|
247
|
|
|
$binaryContent->expects($this->atLeastOnce()) |
248
|
|
|
->method('getRealPath') |
249
|
|
|
->willReturn(__DIR__.'/../fixtures/file.txt'); |
250
|
|
|
|
251
|
|
|
$binaryContent->expects($this->never()) |
252
|
|
|
->method('getPathname'); |
253
|
|
|
|
254
|
|
|
$media |
255
|
|
|
->method('getBinaryContent') |
256
|
|
|
->willReturn($binaryContent); |
257
|
|
|
|
258
|
|
|
$provider = $this->getProvider(); |
259
|
|
|
|
260
|
|
|
$setFileContents = new \ReflectionMethod(FileProvider::class, 'setFileContents'); |
261
|
|
|
$setFileContents->setAccessible(true); |
262
|
|
|
|
263
|
|
|
$setFileContents->invoke($provider, $media); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function testBinaryContentStreamWrapped(): void |
267
|
|
|
{ |
268
|
|
|
$media = $this->createMock(MediaInterface::class); |
269
|
|
|
|
270
|
|
|
$media |
271
|
|
|
->method('getProviderReference') |
272
|
|
|
->willReturn('provider'); |
273
|
|
|
|
274
|
|
|
$media |
275
|
|
|
->method('getId') |
276
|
|
|
->willReturn(10000); |
277
|
|
|
|
278
|
|
|
$media |
279
|
|
|
->method('getContext') |
280
|
|
|
->willReturn('context'); |
281
|
|
|
|
282
|
|
|
$binaryContent = $this->createMock(File::class); |
283
|
|
|
|
284
|
|
|
$binaryContent->expects($this->atLeastOnce()) |
285
|
|
|
->method('getRealPath') |
286
|
|
|
->willReturn(false); |
287
|
|
|
|
288
|
|
|
$binaryContent->expects($this->atLeastOnce()) |
289
|
|
|
->method('getPathname') |
290
|
|
|
->willReturn(__DIR__.'/../fixtures/file.txt'); |
291
|
|
|
|
292
|
|
|
$media |
293
|
|
|
->method('getBinaryContent') |
294
|
|
|
->willReturn($binaryContent); |
295
|
|
|
|
296
|
|
|
$provider = $this->getProvider(); |
297
|
|
|
|
298
|
|
|
$setFileContents = new \ReflectionMethod(FileProvider::class, 'setFileContents'); |
299
|
|
|
$setFileContents->setAccessible(true); |
300
|
|
|
|
301
|
|
|
$setFileContents->invoke($provider, $media); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @doesNotPerformAssertions |
306
|
|
|
*/ |
307
|
|
|
public function testValidate(): void |
308
|
|
|
{ |
309
|
|
|
$errorElement = $this->createMock(ErrorElement::class); |
310
|
|
|
|
311
|
|
|
$media = new Media(); |
312
|
|
|
|
313
|
|
|
$provider = $this->getProvider(); |
314
|
|
|
$provider->validate($errorElement, $media); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function testValidateUploadSize(): void |
318
|
|
|
{ |
319
|
|
|
$errorElement = $this->createMock(ErrorElement::class); |
320
|
|
|
$errorElement->expects($this->once())->method('with') |
321
|
|
|
->willReturnSelf(); |
322
|
|
|
$errorElement->expects($this->once())->method('addViolation') |
323
|
|
|
->with($this->stringContains('The file is too big, max size:')) |
324
|
|
|
->willReturnSelf(); |
325
|
|
|
$errorElement->expects($this->once())->method('end') |
326
|
|
|
->willReturnSelf(); |
327
|
|
|
|
328
|
|
|
$upload = $this->getMockBuilder(UploadedFile::class) |
329
|
|
|
->setConstructorArgs([tempnam(sys_get_temp_dir(), ''), 'dummy']) |
330
|
|
|
->getMock(); |
331
|
|
|
$upload->method('getSize') |
332
|
|
|
->willReturn(0); |
333
|
|
|
$upload->method('getFilename') |
334
|
|
|
->willReturn('test.txt'); |
335
|
|
|
$upload->method('getClientOriginalName') |
336
|
|
|
->willReturn('test.txt'); |
337
|
|
|
$upload->method('getMimeType') |
338
|
|
|
->willReturn('foo/bar'); |
339
|
|
|
|
340
|
|
|
$media = new Media(); |
341
|
|
|
$media->setBinaryContent($upload); |
342
|
|
|
|
343
|
|
|
$provider = $this->getProvider(); |
344
|
|
|
$provider->validate($errorElement, $media); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public function testValidateUploadNullSize(): void |
348
|
|
|
{ |
349
|
|
|
$errorElement = $this->createMock(ErrorElement::class); |
350
|
|
|
$errorElement->expects($this->once())->method('with') |
351
|
|
|
->willReturnSelf(); |
352
|
|
|
$errorElement->expects($this->once())->method('addViolation') |
353
|
|
|
->with($this->stringContains('The file is too big, max size:')) |
354
|
|
|
->willReturnSelf(); |
355
|
|
|
$errorElement->expects($this->once())->method('end') |
356
|
|
|
->willReturnSelf(); |
357
|
|
|
|
358
|
|
|
$upload = $this->getMockBuilder(UploadedFile::class) |
359
|
|
|
->setConstructorArgs([tempnam(sys_get_temp_dir(), ''), 'dummy']) |
360
|
|
|
->getMock(); |
361
|
|
|
$upload->method('getSize') |
362
|
|
|
->willReturn(null); |
363
|
|
|
$upload->method('getFilename') |
364
|
|
|
->willReturn('test.txt'); |
365
|
|
|
$upload->method('getClientOriginalName') |
366
|
|
|
->willReturn('test.txt'); |
367
|
|
|
$upload->method('getMimeType') |
368
|
|
|
->willReturn('foo/bar'); |
369
|
|
|
|
370
|
|
|
$media = new Media(); |
371
|
|
|
$media->setBinaryContent($upload); |
372
|
|
|
|
373
|
|
|
$provider = $this->getProvider(); |
374
|
|
|
$provider->validate($errorElement, $media); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function testValidateUploadSizeOK(): void |
378
|
|
|
{ |
379
|
|
|
$errorElement = $this->createMock(ErrorElement::class); |
380
|
|
|
$errorElement->expects($this->never())->method('with') |
381
|
|
|
->willReturnSelf(); |
382
|
|
|
$errorElement->expects($this->never())->method('addViolation') |
383
|
|
|
->with($this->stringContains('The file is too big, max size:')) |
384
|
|
|
->willReturnSelf(); |
385
|
|
|
$errorElement->expects($this->never())->method('end') |
386
|
|
|
->willReturnSelf(); |
387
|
|
|
|
388
|
|
|
$upload = $this->getMockBuilder(UploadedFile::class) |
389
|
|
|
->setConstructorArgs([tempnam(sys_get_temp_dir(), ''), 'dummy']) |
390
|
|
|
->getMock(); |
391
|
|
|
$upload->method('getSize') |
392
|
|
|
->willReturn(1); |
393
|
|
|
$upload->method('getFilename') |
394
|
|
|
->willReturn('test.txt'); |
395
|
|
|
$upload->method('getClientOriginalName') |
396
|
|
|
->willReturn('test.txt'); |
397
|
|
|
$upload->method('getMimeType') |
398
|
|
|
->willReturn('foo/bar'); |
399
|
|
|
|
400
|
|
|
$media = new Media(); |
401
|
|
|
$media->setBinaryContent($upload); |
402
|
|
|
|
403
|
|
|
$provider = $this->getProvider(); |
404
|
|
|
$provider->validate($errorElement, $media); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function testValidateUploadType(): void |
408
|
|
|
{ |
409
|
|
|
$errorElement = $this->createMock(ErrorElement::class); |
410
|
|
|
$errorElement->expects($this->once())->method('with') |
411
|
|
|
->willReturnSelf(); |
412
|
|
|
$errorElement->expects($this->once())->method('addViolation') |
413
|
|
|
->with('Invalid mime type : %type%', ['%type%' => 'bar/baz']) |
414
|
|
|
->willReturnSelf(); |
415
|
|
|
$errorElement->expects($this->once())->method('end') |
416
|
|
|
->willReturnSelf(); |
417
|
|
|
|
418
|
|
|
$upload = $this->getMockBuilder(UploadedFile::class) |
419
|
|
|
->setConstructorArgs([tempnam(sys_get_temp_dir(), ''), 'dummy']) |
420
|
|
|
->getMock(); |
421
|
|
|
$upload->method('getSize') |
422
|
|
|
->willReturn(23); |
423
|
|
|
$upload->method('getFilename') |
424
|
|
|
->willReturn('test.txt'); |
425
|
|
|
$upload->method('getClientOriginalName') |
426
|
|
|
->willReturn('test.txt'); |
427
|
|
|
$upload->method('getMimeType') |
428
|
|
|
->willReturn('bar/baz'); |
429
|
|
|
|
430
|
|
|
$media = new Media(); |
431
|
|
|
$media->setBinaryContent($upload); |
432
|
|
|
|
433
|
|
|
$provider = $this->getProvider(); |
434
|
|
|
$provider->validate($errorElement, $media); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
public function testMetadata(): void |
438
|
|
|
{ |
439
|
|
|
$provider = $this->getProvider(); |
440
|
|
|
|
441
|
|
|
$this->assertSame('file', $provider->getProviderMetadata()->getTitle()); |
442
|
|
|
$this->assertSame('file.description', $provider->getProviderMetadata()->getDescription()); |
443
|
|
|
$this->assertNotNull($provider->getProviderMetadata()->getImage()); |
444
|
|
|
$this->assertSame('fa fa-file-text-o', $provider->getProviderMetadata()->getOption('class')); |
445
|
|
|
$this->assertSame('SonataMediaBundle', $provider->getProviderMetadata()->getDomain()); |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|