Passed
Push — master ( bfbe95...c29b26 )
by Aimeos
06:06
created

StandardTest::testScaleLegacy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 0
dl 0
loc 27
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2021
6
 */
7
8
9
namespace Aimeos\Controller\Common\Media;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
17
18
	protected function setUp() : void
19
	{
20
		\Aimeos\MShop::cache( true );
21
22
		$this->context = \TestHelperCntl::getContext();
23
		$this->object = new \Aimeos\Controller\Common\Media\Standard( $this->context );
24
	}
25
26
27
	protected function tearDown() : void
28
	{
29
		\Aimeos\MShop::cache( false );
30
		unset( $this->object );
31
	}
32
33
34
	public function testAdd()
35
	{
36
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
37
			->setMethods( array( 'checkFileUpload', 'store' ) )
38
			->setConstructorArgs( array( $this->context ) )
39
			->getMock();
40
41
		$object->expects( $this->once() )->method( 'checkFileUpload' );
42
		$object->expects( $this->exactly( 2 ) )->method( 'store' );
43
44
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
45
46
		$file->expects( $this->exactly( 2 ) )->method( 'getClientFilename' )
47
			->will( $this->returnValue( 'test.gif' ) );
48
49
		$file->expects( $this->once() )->method( 'getStream' )
50
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.gif' ) ) );
51
52
53
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create()->setDomain( 'product' );
54
55
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $object->add( $item, $file ) );
56
	}
57
58
59
	public function testAddPreview()
60
	{
61
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
62
			->setConstructorArgs( [$this->context] )
63
			->setMethods( ['checkFileUpload'] )
64
			->getMock();
65
66
		$object->expects( $this->once() )->method( 'checkFileUpload' );
67
68
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
69
70
		$file->expects( $this->exactly( 2 ) )->method( 'getClientFilename' )
71
			->will( $this->returnValue( 'test.gif' ) );
72
73
		$file->expects( $this->once() )->method( 'getStream' )
74
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.gif' ) ) );
75
76
77
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create()->setDomain( 'product' );
78
79
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $object->add( $item, $file ) );
80
	}
81
82
83
	public function testAddBinary()
84
	{
85
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
86
			->setMethods( array( 'checkFileUpload', 'store' ) )
87
			->setConstructorArgs( array( $this->context ) )
88
			->getMock();
89
90
		$object->expects( $this->once() )->method( 'checkFileUpload' );
91
		$object->expects( $this->once() )->method( 'store' );
92
93
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
94
95
		$file->expects( $this->exactly( 2 ) )->method( 'getClientFilename' )
96
			->will( $this->returnValue( 'test.gif' ) );
97
98
		$file->expects( $this->once() )->method( 'getStream' )
99
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.pdf' ) ) );
100
101
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
102
103
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $object->add( $item, $file ) );
104
	}
105
106
107
	public function testCopy()
108
	{
109
		$fsm = $this->getMockBuilder( \Aimeos\MW\Filesystem\Manager\Standard::class )
110
			->setMethods( array( 'get' ) )
111
			->disableOriginalConstructor()
112
			->getMock();
113
114
		$fs = $this->getMockBuilder( \Aimeos\MW\Filesystem\Standard::class )
115
			->setMethods( array( 'has', 'copy' ) )
116
			->disableOriginalConstructor()
117
			->getMock();
118
119
		$fsm->expects( $this->once() )->method( 'get' )
120
			->will( $this->returnValue( $fs ) );
121
122
		$fs->expects( $this->exactly( 2 ) )->method( 'has' )
123
			->will( $this->returnValue( true ) );
124
125
		$fs->expects( $this->exactly( 2 ) )->method( 'copy' );
126
127
		$this->context->setFilesystemManager( $fsm );
128
129
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
130
		$item->setPreview( 'test' )->setUrl( 'test' );
131
132
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->copy( $item ) );
133
	}
134
135
136
	public function testDelete()
137
	{
138
		$fsm = $this->getMockBuilder( \Aimeos\MW\Filesystem\Manager\Standard::class )
139
			->setMethods( array( 'get' ) )
140
			->disableOriginalConstructor()
141
			->getMock();
142
143
		$fs = $this->getMockBuilder( \Aimeos\MW\Filesystem\Standard::class )
144
			->setMethods( array( 'has', 'rm' ) )
145
			->disableOriginalConstructor()
146
			->getMock();
147
148
		$fsm->expects( $this->exactly( 2 ) )->method( 'get' )
149
			->will( $this->returnValue( $fs ) );
150
151
		$fs->expects( $this->exactly( 1 ) )->method( 'has' )
152
			->will( $this->returnValue( true ) );
153
154
		$fs->expects( $this->exactly( 1 ) )->method( 'rm' );
155
156
		$this->context->setFilesystemManager( $fsm );
157
158
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
159
		$item->setPreview( 'test' )->setUrl( 'test' );
160
161
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->delete( $item ) );
162
	}
163
164
165
	public function testDeleteMimeicon()
166
	{
167
		$this->context->getConfig()->set( 'controller/common/media/mimeicon/directory', 'path/to/mimeicons' );
168
169
		$fsm = $this->getMockBuilder( \Aimeos\MW\Filesystem\Manager\Standard::class )
170
			->setMethods( array( 'get' ) )
171
			->disableOriginalConstructor()
172
			->getMock();
173
174
		$fs = $this->getMockBuilder( \Aimeos\MW\Filesystem\Standard::class )
175
			->setMethods( array( 'has', 'rm' ) )
176
			->disableOriginalConstructor()
177
			->getMock();
178
179
		$fsm->expects( $this->exactly( 2 ) )->method( 'get' )
180
			->will( $this->returnValue( $fs ) );
181
182
		$fs->expects( $this->exactly( 1 ) )->method( 'has' )
183
			->will( $this->returnValue( true ) );
184
185
		$fs->expects( $this->exactly( 1 ) )->method( 'rm' );
186
187
		$this->context->setFilesystemManager( $fsm );
188
189
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
190
		$item->setPreview( 'path/to/mimeicons/application/test.png' )->setUrl( 'test' );
191
192
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->delete( $item ) );
193
	}
194
195
196
	public function testScale()
197
	{
198
		$this->context->getConfig()->set( 'controller/common/media/files/scale', true );
199
200
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/';
201
		if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); }
202
		copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' );
203
204
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
205
			->setMethods( array( 'getFileContent', 'store' ) )
206
			->setConstructorArgs( array( $this->context ) )
207
			->getMock();
208
209
		$object->expects( $this->once() )->method( 'getFileContent' )
210
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.png' ) ) );
211
212
		$object->expects( $this->exactly( 1 ) )->method( 'store' );
213
214
215
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
216
		$item->setPreview( 'preview.gif' )->setUrl( 'test.gif' )->setDomain( 'product' );
217
218
		$result = $object->scale( $item, 'fs-media', 1 );
219
220
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
221
		$this->assertEquals( 'test.gif', $result->getUrl() );
222
		$this->assertNotEquals( 'preview.gif', $result->getPreview() );
223
	}
224
225
226
	public function testScaleLegacy()
227
	{
228
		$this->context->getConfig()->set( 'controller/common/media/files/scale', true );
229
230
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/';
231
		if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); }
232
		copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' );
233
234
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
235
			->setMethods( array( 'getFileContent', 'store' ) )
236
			->setConstructorArgs( array( $this->context ) )
237
			->getMock();
238
239
		$object->expects( $this->once() )->method( 'getFileContent' )
240
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.png' ) ) );
241
242
		$object->expects( $this->exactly( 1 ) )->method( 'store' );
243
244
245
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
246
		$item->setPreview( 'preview.gif' )->setUrl( 'test.gif' )->setDomain( 'product' );
247
248
		$result = $object->scale( $item, 'fs-media', true );
249
250
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
251
		$this->assertEquals( 'test.gif', $result->getUrl() );
252
		$this->assertNotEquals( 'preview.gif', $result->getPreview() );
253
	}
254
255
256
	public function testCheckFileUploadOK()
257
	{
258
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
259
		$file->expects( $this->once() )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_OK ) );
260
261
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
262
	}
263
264
265
	public function testCheckFileUploadSize()
266
	{
267
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
268
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_INI_SIZE ) );
269
270
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
271
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
272
	}
273
274
275
	public function testCheckFileUploadPartitial()
276
	{
277
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
278
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_PARTIAL ) );
279
280
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
281
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
282
	}
283
284
285
	public function testCheckFileUploadNoFile()
286
	{
287
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
288
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_FILE ) );
289
290
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
291
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
292
	}
293
294
295
	public function testCheckFileUploadNoTmpdir()
296
	{
297
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
298
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_TMP_DIR ) );
299
300
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
301
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
302
	}
303
304
305
	public function testCheckFileUploadCantWrite()
306
	{
307
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
308
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_CANT_WRITE ) );
309
310
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
311
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
312
	}
313
314
315
	public function testCheckFileUploadExtension()
316
	{
317
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
318
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_EXTENSION ) );
319
320
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
321
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
322
	}
323
324
325
	public function testCheckFileUploadException()
326
	{
327
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
328
329
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
330
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
331
	}
332
333
334
	public function testGetFileContent()
335
	{
336
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/';
337
		if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); }
338
		copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' );
339
340
		$result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( 'test.gif', 'fs-media' ) );
341
342
		$this->assertNotEquals( '', $result );
343
	}
344
345
346
	public function testGetFileContentHttp()
347
	{
348
		$url = 'https://aimeos.org/fileadmin/logos/favicon.png';
349
		$result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( $url, 'fs-media' ) );
350
351
		$this->assertNotEquals( '', $result );
352
	}
353
354
355
	public function testGetFileContentException()
356
	{
357
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
358
		$this->access( 'getFileContent' )->invokeArgs( $this->object, array( '', 'fs-media' ) );
359
	}
360
361
362
	public function testGetFilePathOctet()
363
	{
364
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/octet-stream' ) );
365
		$this->assertNotEquals( '.', substr( $result, -4, 1 ) );
366
	}
367
368
369
	public function testGetFilePathPDF()
370
	{
371
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/pdf' ) );
372
		$this->assertEquals( '.pdf', substr( $result, -4 ) );
373
	}
374
375
376
	public function testGetFilePathGIF()
377
	{
378
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/gif' ) );
379
		$this->assertEquals( '.gif', substr( $result, -4 ) );
380
	}
381
382
383
	public function testGetFilePathJPEG()
384
	{
385
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/jpeg' ) );
386
		$this->assertEquals( '.jpg', substr( $result, -4 ) );
387
	}
388
389
390
	public function testGetFilePathPNG()
391
	{
392
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/png' ) );
393
		$this->assertEquals( '.png', substr( $result, -4 ) );
394
	}
395
396
397
	public function testGetFilePathTIFF()
398
	{
399
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/tiff' ) );
400
		$this->assertEquals( '.tif', substr( $result, -4 ) );
401
	}
402
403
404
	public function testGetMediaFile()
405
	{
406
		$result = $this->access( 'getMediaFile' )->invokeArgs( $this->object, array( __FILE__ ) );
407
		$this->assertInstanceOf( \Aimeos\MW\Media\Iface::class, $result );
408
	}
409
410
411
	public function testGetMimeIcon()
412
	{
413
		file_exists( 'tmp/image' ) ?: mkdir( 'tmp/image' );
414
		touch( 'tmp/image/jpeg.png' );
415
416
		$this->context->getConfig()->set( 'controller/common/media/mimeicon/directory', 'tmp' );
417
		$result = $this->access( 'getMimeIcon' )->invokeArgs( $this->object, array( 'image/jpeg' ) );
418
		$this->assertStringContainsString( 'tmp/image/jpeg.png', $result );
419
	}
420
421
422
	public function testGetMimeIconNoConfig()
423
	{
424
		$this->context->getConfig()->set( 'controller/common/media/mimeicon/directory', '' );
425
		$result = $this->access( 'getMimeIcon' )->invokeArgs( $this->object, array( 'image/jpeg' ) );
426
		$this->assertEquals( '', $result );
427
	}
428
429
430
	public function testGetMimeType()
431
	{
432
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.png' );
433
434
		$result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
435
		$this->assertEquals( 'image/png', $result );
436
	}
437
438
439
	public function testGetMimeTypeNotAllowed()
440
	{
441
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' );
442
		$this->context->getConfig()->set( 'controller/common/media/files/allowedtypes', array( 'image/jpeg' ) );
443
444
		$result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
445
		$this->assertEquals( 'image/jpeg', $result );
446
	}
447
448
449
	public function testGetMimeTypeNoTypes()
450
	{
451
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' );
452
		$this->context->getConfig()->set( 'controller/common/media/files/allowedtypes', [] );
453
454
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
455
		$this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
456
	}
457
458
459
	protected function access( $name )
460
	{
461
		$class = new \ReflectionClass( \Aimeos\Controller\Common\Media\Standard::class );
462
		$method = $class->getMethod( $name );
463
		$method->setAccessible( true );
464
465
		return $method;
466
	}
467
}
468