Passed
Push — master ( 5b5d82...0b20e6 )
by Aimeos
05:27
created

StandardTest::testUploadPreview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 13
rs 9.9666
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2023
7
 */
8
9
10
namespace Aimeos\MShop\Media\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $context;
17
	private $editor = '';
18
19
20
	protected function setUp() : void
21
	{
22
		$this->context = \TestHelper::context();
23
		$this->editor = $this->context->editor();
24
25
		$this->object = new \Aimeos\MShop\Media\Manager\Standard( $this->context );
26
	}
27
28
29
	protected function tearDown() : void
30
	{
31
		unset( $this->object, $this->context );
32
	}
33
34
35
	public function testClear()
36
	{
37
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
38
	}
39
40
41
	public function testDeleteItems()
42
	{
43
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
44
45
		$item = $this->object->create()->setUrl( 'test.jpg' )->setPreviews( [1 => 'test-1.jpg'] )->setId( -1 );
46
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [$item] ) );
47
	}
48
49
50
	public function testGetResourceType()
51
	{
52
		$result = $this->object->getResourceType();
53
54
		$this->assertContains( 'media', $result );
55
		$this->assertContains( 'media/lists', $result );
56
	}
57
58
59
	public function testGetSearchAttributes()
60
	{
61
		foreach( $this->object->getSearchAttributes() as $attribute ) {
62
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
63
		}
64
	}
65
66
67
	public function testCreateItem()
68
	{
69
		$item = $this->object->create();
70
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $item );
71
	}
72
73
74
	public function testCreateItemType()
75
	{
76
		$item = $this->object->create( ['media.type' => 'default'] );
77
		$this->assertEquals( 'default', $item->getType() );
78
	}
79
80
81
	public function testCreateSearch()
82
	{
83
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
84
	}
85
86
87
	public function testCopy()
88
	{
89
		$fsm = $this->getMockBuilder( \Aimeos\Base\Filesystem\Manager\Standard::class )
90
			->onlyMethods( array( 'get' ) )
91
			->disableOriginalConstructor()
92
			->getMock();
93
94
		$fs = $this->getMockBuilder( \Aimeos\Base\Filesystem\Standard::class )
95
			->onlyMethods( array( 'has', 'copy' ) )
96
			->disableOriginalConstructor()
97
			->getMock();
98
99
		$fsm->expects( $this->once() )->method( 'get' )
100
			->will( $this->returnValue( $fs ) );
101
102
		$fs->expects( $this->exactly( 2 ) )->method( 'has' )
103
			->will( $this->returnValue( true ) );
104
105
		$fs->expects( $this->exactly( 2 ) )->method( 'copy' );
106
107
		$this->context->setFilesystemManager( $fsm );
108
109
		$item = $this->object->create()->setPreview( 'test' )->setUrl( 'test' );
110
111
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->copy( $item ) );
112
	}
113
114
115
	public function testSearchItem()
116
	{
117
		$search = $this->object->filter();
118
		$search->setConditions( $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' ) );
119
		$item = $this->object->search( $search, ['attribute'] )->first();
120
121
		if( $item && ( $listItem = $item->getListItems( 'attribute', 'option' )->first() ) === null ) {
122
			throw new \RuntimeException( 'No list item found' );
123
		}
124
125
		$search = $this->object->filter();
126
127
		$expr = [];
128
		$expr[] = $search->compare( '!=', 'media.id', null );
129
		$expr[] = $search->compare( '!=', 'media.siteid', null );
130
		$expr[] = $search->compare( '==', 'media.languageid', 'de' );
131
		$expr[] = $search->compare( '==', 'media.type', 'slideshow' );
132
		$expr[] = $search->compare( '==', 'media.domain', 'product' );
133
		$expr[] = $search->compare( '==', 'media.filesystem', 'fs-media' );
134
		$expr[] = $search->compare( '==', 'media.label', 'prod_266x221/198_prod_266x221.jpg' );
135
		$expr[] = $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' );
136
		$expr[] = $search->compare( '=~', 'media.preview', '{' );
137
		$expr[] = $search->compare( '==', 'media.mimetype', 'image/jpeg' );
138
		$expr[] = $search->compare( '==', 'media.status', 1 );
139
		$expr[] = $search->compare( '>=', 'media.mtime', '1970-01-01 00:00:00' );
140
		$expr[] = $search->compare( '>=', 'media.ctime', '1970-01-01 00:00:00' );
141
		$expr[] = $search->compare( '==', 'media.editor', $this->editor );
142
143
		$param = ['attribute', 'option', $listItem->getRefId()];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $listItem does not seem to be defined for all execution paths leading up to this point.
Loading history...
144
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
145
146
		$param = ['attribute', 'option'];
147
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
148
149
		$param = ['attribute'];
150
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
151
152
		$param = ['copyright', 'de', 'ich, 2019'];
153
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
154
155
		$param = ['copyright', 'de'];
156
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
157
158
		$param = ['copyright'];
159
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
160
161
		$total = 0;
162
		$search->setConditions( $search->and( $expr ) );
163
		$results = $this->object->search( $search, [], $total )->toArray();
164
165
		$this->assertEquals( 1, count( $results ) );
166
		$this->assertEquals( 1, $total );
167
	}
168
169
170
	public function testSearchItemBase()
171
	{
172
		//search with base criteria
173
		$search = $this->object->filter( true );
174
		$conditions = array(
175
			$search->compare( '==', 'media.editor', $this->editor ),
176
			$search->getConditions()
177
		);
178
		$search->setConditions( $search->and( $conditions ) );
179
		$search->slice( 0, 4 );
180
181
		$total = 0;
182
		$results = $this->object->search( $search, [], $total )->toArray();
183
184
		$this->assertEquals( 4, count( $results ) );
185
		$this->assertEquals( 16, $total );
186
187
		foreach( $results as $itemId => $item ) {
188
			$this->assertEquals( $itemId, $item->getId() );
189
		}
190
	}
191
192
	public function testGetItem()
193
	{
194
		$search = $this->object->filter()->slice( 0, 1 );
195
		$conditions = array(
196
			$search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ),
197
			$search->compare( '==', 'media.editor', $this->editor )
198
		);
199
		$search->setConditions( $search->and( $conditions ) );
200
		$item = $this->object->search( $search, ['media/property'] )->first();
201
202
		$this->assertEquals( $item, $this->object->get( $item->getId(), ['media/property'] ) );
203
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
204
	}
205
206
207
	public function testSaveUpdateDeleteItem()
208
	{
209
		$search = $this->object->filter();
210
		$conditions = array(
211
			$search->compare( '~=', 'media.label', 'example' ),
212
			$search->compare( '==', 'media.editor', $this->editor )
213
		);
214
		$search->setConditions( $search->and( $conditions ) );
215
		$item = $this->object->search( $search )->first();
216
217
		$item->setId( null );
218
		$item->setLanguageId( 'de' );
219
		$item->setDomain( 'test_dom' );
220
		$item->setLabel( 'test' );
221
		$item->setMimeType( 'image/jpeg' );
222
		$item->setUrl( 'test.jpg' );
223
		$item->setPreview( 'xxxtest-preview.jpg' );
224
		$item->setFileSystem( 'fs-media' );
225
226
		$resultSaved = $this->object->save( $item );
227
		$itemSaved = $this->object->get( $item->getId() );
228
229
		$itemExp = clone $itemSaved;
230
		$itemExp->setPreview( 'test-preview.jpg' );
231
		$resultUpd = $this->object->save( $itemExp );
232
		$itemUpd = $this->object->get( $itemExp->getId() );
233
234
		$this->object->delete( $item );
235
236
237
		$this->assertTrue( $item->getId() !== null );
238
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
239
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
240
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
241
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
242
		$this->assertEquals( $item->getFileSystem(), $itemSaved->getFileSystem() );
243
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
244
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
245
		$this->assertEquals( $item->getMimeType(), $itemSaved->getMimeType() );
246
		$this->assertEquals( $item->getUrl(), $itemSaved->getUrl() );
247
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
248
		$this->assertEquals( 0, strncmp( $item->getPreview(), $itemSaved->getPreview(), 19 ) );
249
250
		$this->assertEquals( $this->editor, $itemSaved->editor() );
251
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
252
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
253
254
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
255
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
256
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
257
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
258
		$this->assertEquals( $itemExp->getFileSystem(), $itemUpd->getFileSystem() );
259
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
260
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
261
		$this->assertEquals( $itemExp->getMimeType(), $itemUpd->getMimeType() );
262
		$this->assertEquals( $itemExp->getUrl(), $itemUpd->getUrl() );
263
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
264
		$this->assertEquals( 0, strncmp( $itemExp->getPreview(), $itemUpd->getPreview(), 16 ) );
265
266
		$this->assertEquals( $this->editor, $itemUpd->editor() );
267
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
268
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
269
270
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
271
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
272
273
		$this->expectException( \Aimeos\MShop\Exception::class );
274
		$this->object->get( $item->getId() );
275
	}
276
277
278
	public function testGetSavePropertyItems()
279
	{
280
		$search = $this->object->filter();
281
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ) );
282
		$item = $this->object->search( $search, ['media/property'] )->first();
283
284
		$item->setId( null )->setLabel( 'path/to/folder/example1-1.jpg' );
285
		$this->object->save( $item );
286
287
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1-1.jpg' ) );
288
		$item2 = $this->object->search( $search, ['media/property'] )->first();
289
290
		$this->object->delete( $item->getId() );
291
292
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
293
		$this->assertEquals( 2, count( $item2->getPropertyItems() ) );
294
	}
295
296
297
	public function testGetSubManager()
298
	{
299
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type' ) );
300
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type', 'Standard' ) );
301
302
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists' ) );
303
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists', 'Standard' ) );
304
305
		$this->expectException( \LogicException::class );
306
		$this->object->getSubManager( 'unknown' );
307
	}
308
309
310
	public function testGetSubManagerInvalidName()
311
	{
312
		$this->expectException( \LogicException::class );
313
		$this->object->getSubManager( 'lists', 'unknown' );
314
	}
315
316
317
	public function testScale()
318
	{
319
		copy( __DIR__ . '/_testfiles/test.gif', 'tmp/test.gif' );
320
321
		$object = $this->getMockBuilder( \Aimeos\MShop\Media\Manager\Standard::class )
322
			->setConstructorArgs( [$this->context] )
323
			->onlyMethods( ['getContent', 'store'] )
324
			->getMock();
325
326
		$object->expects( $this->once() )->method( 'getContent' )
327
			->will( $this->returnValue( file_get_contents( __DIR__ . '/_testfiles/test.gif' ) ) );
328
329
		$object->expects( $this->any() )->method( 'store' );
330
331
		$item = $this->object->create()->setUrl( 'test.gif' )
332
			->setMimeType( 'image/gif' )->setDomain( 'product' );
333
334
		$result = $object->scale( $item, true );
335
336
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
337
		$this->assertEquals( 'test.gif', $result->getUrl() );
338
		$this->assertNotEquals( '', $result->getPreview() );
339
	}
340
341
342
	public function testUpload()
343
	{
344
		$content = file_get_contents( __DIR__ . '/_testfiles/test.gif' );
345
		$file = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), UPLOAD_ERR_OK, 'test.gif' );
346
347
		$result = $this->object->upload( $this->object->create(), $file );
348
349
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
350
		$this->assertStringEndsWith( '_test.gif', $result->getUrl() );
351
		$this->assertEquals( 'image/gif', $result->getMimetype() );
352
		$this->assertEquals( 'test.gif', $result->getLabel() );
353
	}
354
355
356
	public function testUploadPreview()
357
	{
358
		$content = file_get_contents( __DIR__ . '/_testfiles/test.gif' );
359
		$file = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), UPLOAD_ERR_OK, 'test.gif' );
360
		$preview = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), UPLOAD_ERR_OK, 'test.gif' );
361
362
		$result = $this->object->upload( $this->object->create(), $file, $preview );
363
364
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
365
		$this->assertStringEndsWith( '_test.gif', $result->getPreview() );
366
		$this->assertStringEndsWith( '_test.gif', $result->getUrl() );
367
		$this->assertEquals( 'image/gif', $result->getMimetype() );
368
		$this->assertEquals( 'test.gif', $result->getLabel() );
369
	}
370
371
372
	public function testUploadException()
373
	{
374
		$content = file_get_contents( __DIR__ . '/_testfiles/test.gif' );
375
		$file = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), 1, 'test.gif' );
376
377
		$this->expectException( \RuntimeException::class );
378
		$result = $this->object->upload( $this->object->create(), $file );
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
379
	}
380
381
382
	public function testGetContent()
383
	{
384
		$dest = dirname( __DIR__, 3 ) . '/tmp/';
385
		is_dir( $dest ) ?: mkdir( $dest, 0755, true );
386
		copy( __DIR__ . '/_testfiles/test.gif', $dest . 'test.gif' );
387
388
		$result = $this->access( 'getContent' )->invokeArgs( $this->object, ['test.gif'] );
389
390
		$this->assertNotEquals( '', $result );
391
	}
392
393
394
	public function testGetContentHttp()
395
	{
396
		$url = 'https://aimeos.org/fileadmin/logos/favicon.png';
397
		$result = $this->access( 'getContent' )->invokeArgs( $this->object, [$url] );
398
399
		$this->assertNotEquals( '', $result );
400
	}
401
402
403
	public function testGetContentException()
404
	{
405
		$this->expectException( \Aimeos\MShop\Media\Exception::class );
406
		$this->access( 'getContent' )->invokeArgs( $this->object, [''] );
407
	}
408
409
410
	public function testGetFile()
411
	{
412
		$dest = dirname( __DIR__, 3 ) . '/tmp/';
413
		is_dir( $dest ) ?: mkdir( $dest, 0755, true );
414
		copy( __DIR__ . '/_testfiles/test.gif', $dest . 'test.gif' );
415
416
		$result = $this->access( 'getFile' )->invokeArgs( $this->object, ['test.gif'] );
417
		$this->assertInstanceOf( \Aimeos\MW\Media\Iface::class, $result );
418
	}
419
420
421
	public function testGetMime()
422
	{
423
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/_testfiles/test.png' );
424
425
		$result = $this->access( 'getMime' )->invokeArgs( $this->object, array( $file, 'files' ) );
426
		$this->assertEquals( true, in_array( $result, ['image/webp', 'image/png'] ) );
427
	}
428
429
430
	protected function access( $name )
431
	{
432
		$class = new \ReflectionClass( \Aimeos\MShop\Media\Manager\Standard::class );
433
		$method = $class->getMethod( $name );
434
		$method->setAccessible( true );
435
436
		return $method;
437
	}
438
}
439