Passed
Push — master ( c7bfcd...219f2b )
by Aimeos
06:02
created

StandardTest::testDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 9.7
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( 3 ) )->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( 3 ) )->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
		touch( 'tmp/unknown.png' );
86
87
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
88
			->setMethods( array( 'checkFileUpload', 'store' ) )
89
			->setConstructorArgs( array( $this->context ) )
90
			->getMock();
91
92
		$object->expects( $this->once() )->method( 'checkFileUpload' );
93
		$object->expects( $this->once() )->method( 'store' );
94
95
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
96
97
		$file->expects( $this->exactly( 2 ) )->method( 'getClientFilename' )
98
			->will( $this->returnValue( 'test.pdf' ) );
99
100
		$file->expects( $this->once() )->method( 'getStream' )
101
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.pdf' ) ) );
102
103
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
104
105
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $object->add( $item, $file ) );
106
	}
107
108
109
	public function testCopy()
110
	{
111
		$fsm = $this->getMockBuilder( \Aimeos\MW\Filesystem\Manager\Standard::class )
112
			->setMethods( array( 'get' ) )
113
			->disableOriginalConstructor()
114
			->getMock();
115
116
		$fs = $this->getMockBuilder( \Aimeos\MW\Filesystem\Standard::class )
117
			->setMethods( array( 'has', 'copy' ) )
118
			->disableOriginalConstructor()
119
			->getMock();
120
121
		$fsm->expects( $this->once() )->method( 'get' )
122
			->will( $this->returnValue( $fs ) );
123
124
		$fs->expects( $this->exactly( 2 ) )->method( 'has' )
125
			->will( $this->returnValue( true ) );
126
127
		$fs->expects( $this->exactly( 2 ) )->method( 'copy' );
128
129
		$this->context->setFilesystemManager( $fsm );
130
131
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
132
		$item->setPreview( 'test' )->setUrl( 'test' );
133
134
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->copy( $item ) );
135
	}
136
137
138
	public function testDelete()
139
	{
140
		$fsm = $this->getMockBuilder( \Aimeos\MW\Filesystem\Manager\Standard::class )
141
			->setMethods( array( 'get' ) )
142
			->disableOriginalConstructor()
143
			->getMock();
144
145
		$fs = $this->getMockBuilder( \Aimeos\MW\Filesystem\Standard::class )
146
			->setMethods( array( 'has', 'rm' ) )
147
			->disableOriginalConstructor()
148
			->getMock();
149
150
		$fsm->expects( $this->exactly( 2 ) )->method( 'get' )
151
			->will( $this->returnValue( $fs ) );
152
153
		$fs->expects( $this->exactly( 2 ) )->method( 'has' )
154
			->will( $this->returnValue( true ) );
155
156
		$fs->expects( $this->exactly( 2 ) )->method( 'rm' );
157
158
		$this->context->setFilesystemManager( $fsm );
159
160
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
161
		$item->setPreview( 'test' )->setUrl( 'test' );
162
163
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->delete( $item ) );
164
	}
165
166
167
	public function testScale()
168
	{
169
		$this->context->getConfig()->set( 'controller/common/media/files/scale', true );
170
171
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/';
172
		if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); }
173
		copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' );
174
175
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
176
			->setMethods( array( 'getFileContent', 'store' ) )
177
			->setConstructorArgs( array( $this->context ) )
178
			->getMock();
179
180
		$object->expects( $this->once() )->method( 'getFileContent' )
181
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.png' ) ) );
182
183
		$object->expects( $this->exactly( 1 ) )->method( 'store' );
184
185
186
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
187
		$item->setPreview( 'preview.gif' )->setUrl( 'test.gif' )->setDomain( 'product' );
188
189
		$result = $object->scale( $item, 'fs-media', 1 );
190
191
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
192
		$this->assertEquals( 'test.gif', $result->getUrl() );
193
		$this->assertNotEquals( 'preview.gif', $result->getPreview() );
194
	}
195
196
197
	public function testScaleLegacy()
198
	{
199
		$this->context->getConfig()->set( 'controller/common/media/files/scale', true );
200
201
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/';
202
		if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); }
203
		copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' );
204
205
		$object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class )
206
			->setMethods( array( 'getFileContent', 'store' ) )
207
			->setConstructorArgs( array( $this->context ) )
208
			->getMock();
209
210
		$object->expects( $this->once() )->method( 'getFileContent' )
211
			->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.png' ) ) );
212
213
		$object->expects( $this->exactly( 1 ) )->method( 'store' );
214
215
216
		$item = \Aimeos\MShop::create( $this->context, 'media' )->create();
217
		$item->setPreview( 'preview.gif' )->setUrl( 'test.gif' )->setDomain( 'product' );
218
219
		$result = $object->scale( $item, 'fs-media', true );
220
221
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
222
		$this->assertEquals( 'test.gif', $result->getUrl() );
223
		$this->assertNotEquals( 'preview.gif', $result->getPreview() );
224
	}
225
226
227
	public function testCheckFileUploadOK()
228
	{
229
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
230
		$file->expects( $this->once() )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_OK ) );
231
232
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
233
	}
234
235
236
	public function testCheckFileUploadSize()
237
	{
238
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
239
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_INI_SIZE ) );
240
241
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
242
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
243
	}
244
245
246
	public function testCheckFileUploadPartitial()
247
	{
248
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
249
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_PARTIAL ) );
250
251
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
252
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
253
	}
254
255
256
	public function testCheckFileUploadNoFile()
257
	{
258
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
259
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_FILE ) );
260
261
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
262
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
263
	}
264
265
266
	public function testCheckFileUploadNoTmpdir()
267
	{
268
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
269
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_TMP_DIR ) );
270
271
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
272
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
273
	}
274
275
276
	public function testCheckFileUploadCantWrite()
277
	{
278
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
279
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_CANT_WRITE ) );
280
281
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
282
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
283
	}
284
285
286
	public function testCheckFileUploadExtension()
287
	{
288
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
289
		$file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_EXTENSION ) );
290
291
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
292
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
293
	}
294
295
296
	public function testCheckFileUploadException()
297
	{
298
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
299
300
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
301
		$this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) );
302
	}
303
304
305
	public function testGetFileContent()
306
	{
307
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/';
308
		if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); }
309
		copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' );
310
311
		$result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( 'test.gif', 'fs-media' ) );
312
313
		$this->assertNotEquals( '', $result );
314
	}
315
316
317
	public function testGetFileContentHttp()
318
	{
319
		$url = 'https://aimeos.org/fileadmin/logos/favicon.png';
320
		$result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( $url, 'fs-media' ) );
321
322
		$this->assertNotEquals( '', $result );
323
	}
324
325
326
	public function testGetFileContentException()
327
	{
328
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
329
		$this->access( 'getFileContent' )->invokeArgs( $this->object, array( '', 'fs-media' ) );
330
	}
331
332
333
	public function testGetFilePathOctet()
334
	{
335
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/octet-stream' ) );
336
		$this->assertNotEquals( '.', substr( $result, -4, 1 ) );
337
	}
338
339
340
	public function testGetFilePathPDF()
341
	{
342
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/pdf' ) );
343
		$this->assertEquals( '.pdf', substr( $result, -4 ) );
344
	}
345
346
347
	public function testGetFilePathGIF()
348
	{
349
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/gif' ) );
350
		$this->assertEquals( '.gif', substr( $result, -4 ) );
351
	}
352
353
354
	public function testGetFilePathJPEG()
355
	{
356
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/jpeg' ) );
357
		$this->assertEquals( '.jpg', substr( $result, -4 ) );
358
	}
359
360
361
	public function testGetFilePathPNG()
362
	{
363
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/png' ) );
364
		$this->assertEquals( '.png', substr( $result, -4 ) );
365
	}
366
367
368
	public function testGetFilePathTIFF()
369
	{
370
		$result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/tiff' ) );
371
		$this->assertEquals( '.tif', substr( $result, -4 ) );
372
	}
373
374
375
	public function testGetMediaFile()
376
	{
377
		$result = $this->access( 'getMediaFile' )->invokeArgs( $this->object, array( __FILE__ ) );
378
		$this->assertInstanceOf( \Aimeos\MW\Media\Iface::class, $result );
379
	}
380
381
382
	public function testGetMimeIcon()
383
	{
384
		file_exists( 'tmp/image' ) ?: mkdir( 'tmp/image' );
385
		touch( 'tmp/image/jpeg.png' );
386
387
		$result = $this->access( 'getMimeIcon' )->invokeArgs( $this->object, ['image/jpeg', 'fs-media'] );
388
		$this->assertStringContainsString( 'image-jpeg.png', $result );
389
	}
390
391
392
	public function testGetMimeType()
393
	{
394
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.png' );
395
396
		$result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
397
		$this->assertEquals( 'image/png', $result );
398
	}
399
400
401
	public function testGetMimeTypeNotAllowed()
402
	{
403
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' );
404
		$this->context->getConfig()->set( 'controller/common/media/files/allowedtypes', array( 'image/jpeg' ) );
405
406
		$result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
407
		$this->assertEquals( 'image/jpeg', $result );
408
	}
409
410
411
	public function testGetMimeTypeNoTypes()
412
	{
413
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' );
414
		$this->context->getConfig()->set( 'controller/common/media/files/allowedtypes', [] );
415
416
		$this->expectException( \Aimeos\Controller\Common\Exception::class );
417
		$this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) );
418
	}
419
420
421
	protected function access( $name )
422
	{
423
		$class = new \ReflectionClass( \Aimeos\Controller\Common\Media\Standard::class );
424
		$method = $class->getMethod( $name );
425
		$method->setAccessible( true );
426
427
		return $method;
428
	}
429
}
430