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