Passed
Push — master ( fb66a4...bfbe95 )
by Aimeos
05:37
created

StandardTest::testAddPreview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 21
rs 9.8666
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', true );
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 testCheckFileUploadOK()
227
	{
228
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
229
		$file->expects( $this->once() )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_OK ) );
230
231
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
232
	}
233
234
235
	public function testCheckFileUploadSize()
236
	{
237
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
238
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_INI_SIZE ) );
239
240
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
241
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
242
	}
243
244
245
	public function testCheckFileUploadPartitial()
246
	{
247
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
248
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_PARTIAL ) );
249
250
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
251
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
252
	}
253
254
255
	public function testCheckFileUploadNoFile()
256
	{
257
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
258
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_FILE ) );
259
260
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
261
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
262
	}
263
264
265
	public function testCheckFileUploadNoTmpdir()
266
	{
267
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
268
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_TMP_DIR ) );
269
270
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
271
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
272
	}
273
274
275
	public function testCheckFileUploadCantWrite()
276
	{
277
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
278
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_CANT_WRITE ) );
279
280
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
281
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
282
	}
283
284
285
	public function testCheckFileUploadExtension()
286
	{
287
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
288
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_EXTENSION ) );
289
290
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
291
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
292
	}
293
294
295
	public function testCheckFileUploadException()
296
	{
297
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
298
299
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
300
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
301
	}
302
303
304
	public function testGetFileContent()
305
	{
306
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/';
307
		if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); }
308
		copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' );
309
310
		$result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( 'test.gif', 'fs-media' ) );
311
312
		$this->assertNotEquals( '', $result );
313
	}
314
315
316
	public function testGetFileContentHttp()
317
	{
318
		$url = 'https://aimeos.org/fileadmin/logos/favicon.png';
319
		$result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( $url, 'fs-media' ) );
320
321
		$this->assertNotEquals( '', $result );
322
	}
323
324
325
	public function testGetFileContentException()
326
	{
327
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
328
		$this->access( 'getFileContent' )->invokeArgs( $this->object, array( '', 'fs-media' ) );
329
	}
330
331
332
	public function testGetFilePathOctet()
333
	{
334
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/octet-stream' ) );
335
		$this->assertNotEquals( '.', substr( $result, -4, 1 ) );
336
	}
337
338
339
	public function testGetFilePathPDF()
340
	{
341
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/pdf' ) );
342
		$this->assertEquals( '.pdf', substr( $result, -4 ) );
343
	}
344
345
346
	public function testGetFilePathGIF()
347
	{
348
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/gif' ) );
349
		$this->assertEquals( '.gif', substr( $result, -4 ) );
350
	}
351
352
353
	public function testGetFilePathJPEG()
354
	{
355
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/jpeg' ) );
356
		$this->assertEquals( '.jpg', substr( $result, -4 ) );
357
	}
358
359
360
	public function testGetFilePathPNG()
361
	{
362
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/png' ) );
363
		$this->assertEquals( '.png', substr( $result, -4 ) );
364
	}
365
366
367
	public function testGetFilePathTIFF()
368
	{
369
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/tiff' ) );
370
		$this->assertEquals( '.tif', substr( $result, -4 ) );
371
	}
372
373
374
	public function testGetMediaFile()
375
	{
376
		$result = $this->access( 'getMediaFile' )->invokeArgs( $this->object, array( __FILE__ ) );
377
		$this->assertInstanceOf( \Aimeos\MW\Media\Iface::class, $result );
378
	}
379
380
381
	public function testGetMimeIcon()
382
	{
383
		file_exists( 'tmp/image' ) ?: mkdir( 'tmp/image' );
384
		touch( 'tmp/image/jpeg.png' );
385
386
		$this->context->getConfig()->set( 'controller/common/media/mimeicon/directory', 'tmp' );
387
		$result = $this->access( 'getMimeIcon' )->invokeArgs( $this->object, array( 'image/jpeg' ) );
388
		$this->assertStringContainsString( 'tmp/image/jpeg.png', $result );
389
	}
390
391
392
	public function testGetMimeIconNoConfig()
393
	{
394
		$this->context->getConfig()->set( 'controller/common/media/mimeicon/directory', '' );
395
		$result = $this->access( 'getMimeIcon' )->invokeArgs( $this->object, array( 'image/jpeg' ) );
396
		$this->assertEquals( '', $result );
397
	}
398
399
400
	public function testGetMimeType()
401
	{
402
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.png' );
403
404
		$result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
405
		$this->assertEquals( 'image/png', $result );
406
	}
407
408
409
	public function testGetMimeTypeNotAllowed()
410
	{
411
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' );
412
		$this->context->getConfig()->set( 'controller/common/media/files/allowedtypes', array( 'image/jpeg' ) );
413
414
		$result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
415
		$this->assertEquals( 'image/jpeg', $result );
416
	}
417
418
419
	public function testGetMimeTypeNoTypes()
420
	{
421
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' );
422
		$this->context->getConfig()->set( 'controller/common/media/files/allowedtypes', [] );
423
424
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
425
		$this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
426
	}
427
428
429
	protected function access( $name )
430
	{
431
		$class = new \ReflectionClass( \Aimeos\Controller\Common\Media\Standard::class );
432
		$method = $class->getMethod( $name );
433
		$method->setAccessible( true );
434
435
		return $method;
436
	}
437
}
438