Passed
Push — master ( e7141a...ab4b6a )
by Aimeos
04:16
created

StandardTest::testGetContentHttp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
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-2022
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
46
47
	public function testGetResourceType()
48
	{
49
		$result = $this->object->getResourceType();
50
51
		$this->assertContains( 'media', $result );
52
		$this->assertContains( 'media/lists', $result );
53
	}
54
55
56
	public function testGetSearchAttributes()
57
	{
58
		foreach( $this->object->getSearchAttributes() as $attribute ) {
59
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
60
		}
61
	}
62
63
64
	public function testCreateItem()
65
	{
66
		$item = $this->object->create();
67
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $item );
68
	}
69
70
71
	public function testCreateItemType()
72
	{
73
		$item = $this->object->create( ['media.type' => 'default'] );
74
		$this->assertEquals( 'default', $item->getType() );
75
	}
76
77
78
	public function testCreateSearch()
79
	{
80
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
81
	}
82
83
84
	public function testSearchItem()
85
	{
86
		$search = $this->object->filter();
87
		$search->setConditions( $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' ) );
88
		$item = $this->object->search( $search, ['attribute'] )->first();
89
90
		if( $item && ( $listItem = $item->getListItems( 'attribute', 'option' )->first() ) === null ) {
91
			throw new \RuntimeException( 'No list item found' );
92
		}
93
94
		$search = $this->object->filter();
95
96
		$expr = [];
97
		$expr[] = $search->compare( '!=', 'media.id', null );
98
		$expr[] = $search->compare( '!=', 'media.siteid', null );
99
		$expr[] = $search->compare( '==', 'media.languageid', 'de' );
100
		$expr[] = $search->compare( '==', 'media.type', 'slideshow' );
101
		$expr[] = $search->compare( '==', 'media.domain', 'product' );
102
		$expr[] = $search->compare( '==', 'media.filesystem', 'fs-media' );
103
		$expr[] = $search->compare( '==', 'media.label', 'prod_266x221/198_prod_266x221.jpg' );
104
		$expr[] = $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' );
105
		$expr[] = $search->compare( '=~', 'media.preview', '{' );
106
		$expr[] = $search->compare( '==', 'media.mimetype', 'image/jpeg' );
107
		$expr[] = $search->compare( '==', 'media.status', 1 );
108
		$expr[] = $search->compare( '>=', 'media.mtime', '1970-01-01 00:00:00' );
109
		$expr[] = $search->compare( '>=', 'media.ctime', '1970-01-01 00:00:00' );
110
		$expr[] = $search->compare( '==', 'media.editor', $this->editor );
111
112
		$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...
113
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
114
115
		$param = ['attribute', 'option'];
116
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
117
118
		$param = ['attribute'];
119
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
120
121
		$param = ['copyright', 'de', 'ich, 2019'];
122
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
123
124
		$param = ['copyright', 'de'];
125
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
126
127
		$param = ['copyright'];
128
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
129
130
		$total = 0;
131
		$search->setConditions( $search->and( $expr ) );
132
		$results = $this->object->search( $search, [], $total )->toArray();
133
134
		$this->assertEquals( 1, count( $results ) );
135
		$this->assertEquals( 1, $total );
136
	}
137
138
139
	public function testSearchItemBase()
140
	{
141
		//search with base criteria
142
		$search = $this->object->filter( true );
143
		$conditions = array(
144
			$search->compare( '==', 'media.editor', $this->editor ),
145
			$search->getConditions()
146
		);
147
		$search->setConditions( $search->and( $conditions ) );
148
		$search->slice( 0, 4 );
149
150
		$total = 0;
151
		$results = $this->object->search( $search, [], $total )->toArray();
152
153
		$this->assertEquals( 4, count( $results ) );
154
		$this->assertEquals( 16, $total );
155
156
		foreach( $results as $itemId => $item ) {
157
			$this->assertEquals( $itemId, $item->getId() );
158
		}
159
	}
160
161
	public function testGetItem()
162
	{
163
		$search = $this->object->filter()->slice( 0, 1 );
164
		$conditions = array(
165
			$search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ),
166
			$search->compare( '==', 'media.editor', $this->editor )
167
		);
168
		$search->setConditions( $search->and( $conditions ) );
169
		$item = $this->object->search( $search, ['media/property'] )->first();
170
171
		$this->assertEquals( $item, $this->object->get( $item->getId(), ['media/property'] ) );
172
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
173
	}
174
175
176
	public function testSaveUpdateDeleteItem()
177
	{
178
		$search = $this->object->filter();
179
		$conditions = array(
180
			$search->compare( '~=', 'media.label', 'example' ),
181
			$search->compare( '==', 'media.editor', $this->editor )
182
		);
183
		$search->setConditions( $search->and( $conditions ) );
184
		$item = $this->object->search( $search )->first();
185
186
		$item->setId( null );
187
		$item->setLanguageId( 'de' );
188
		$item->setDomain( 'test_dom' );
189
		$item->setLabel( 'test' );
190
		$item->setMimeType( 'image/jpeg' );
191
		$item->setUrl( 'test.jpg' );
192
		$item->setPreview( 'xxxtest-preview.jpg' );
193
194
		$resultSaved = $this->object->save( $item );
195
		$itemSaved = $this->object->get( $item->getId() );
196
197
		$itemExp = clone $itemSaved;
198
		$itemExp->setPreview( 'test-preview.jpg' );
199
		$resultUpd = $this->object->save( $itemExp );
200
		$itemUpd = $this->object->get( $itemExp->getId() );
201
202
		$this->object->delete( $item->getId() );
203
204
205
		$this->assertTrue( $item->getId() !== null );
206
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
207
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
208
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
209
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
210
		$this->assertEquals( $item->getFileSystem(), $itemSaved->getFileSystem() );
211
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
212
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
213
		$this->assertEquals( $item->getMimeType(), $itemSaved->getMimeType() );
214
		$this->assertEquals( $item->getUrl(), $itemSaved->getUrl() );
215
		$this->assertEquals( $item->getPreview(), $itemSaved->getPreview() );
216
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
217
218
		$this->assertEquals( $this->editor, $itemSaved->editor() );
219
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
220
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
221
222
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
223
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
224
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
225
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
226
		$this->assertEquals( $itemExp->getFileSystem(), $itemUpd->getFileSystem() );
227
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
228
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
229
		$this->assertEquals( $itemExp->getMimeType(), $itemUpd->getMimeType() );
230
		$this->assertEquals( $itemExp->getUrl(), $itemUpd->getUrl() );
231
		$this->assertEquals( $itemExp->getPreview(), $itemUpd->getPreview() );
232
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
233
234
		$this->assertEquals( $this->editor, $itemUpd->editor() );
235
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
236
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
237
238
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
239
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
240
241
		$this->expectException( \Aimeos\MShop\Exception::class );
242
		$this->object->get( $item->getId() );
243
	}
244
245
246
	public function testGetSavePropertyItems()
247
	{
248
		$search = $this->object->filter();
249
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ) );
250
		$item = $this->object->search( $search, ['media/property'] )->first();
251
252
		$item->setId( null )->setLabel( 'path/to/folder/example1-1.jpg' );
253
		$this->object->save( $item );
254
255
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1-1.jpg' ) );
256
		$item2 = $this->object->search( $search, ['media/property'] )->first();
257
258
		$this->object->delete( $item->getId() );
259
260
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
261
		$this->assertEquals( 2, count( $item2->getPropertyItems() ) );
262
	}
263
264
265
	public function testGetSubManager()
266
	{
267
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type' ) );
268
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type', 'Standard' ) );
269
270
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists' ) );
271
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists', 'Standard' ) );
272
273
		$this->expectException( \Aimeos\MShop\Exception::class );
274
		$this->object->getSubManager( 'unknown' );
275
	}
276
277
278
	public function testGetSubManagerInvalidName()
279
	{
280
		$this->expectException( \Aimeos\MShop\Exception::class );
281
		$this->object->getSubManager( 'lists', 'unknown' );
282
	}
283
284
285
	public function testScale()
286
	{
287
		$object = $this->getMockBuilder( \Aimeos\MShop\Media\Manager\Standard::class )
288
			->setConstructorArgs( [$this->context] )
289
			->setMethods( ['getContent', 'store'] )
290
			->getMock();
291
292
		$object->expects( $this->once() )->method( 'getContent' )
293
			->will( $this->returnValue( file_get_contents( __DIR__ . '/_testfiles/test.gif' ) ) );
294
295
		$object->expects( $this->any() )->method( 'store' );
296
297
		$item = $this->object->create()->setUrl( 'test.gif' )->setPreview( 'preview.gif' )
298
			->setMimeType( 'image/gif' )->setDomain( 'product' );
299
300
		$result = $object->scale( $item, true );
301
302
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
303
		$this->assertEquals( 'test.gif', $result->getUrl() );
304
		$this->assertNotEquals( 'preview.gif', $result->getPreview() );
305
	}
306
307
308
	public function testGetContent()
309
	{
310
		$dest = dirname( __DIR__, 3 ) . '/tmp/';
311
		@mkdir( $dest, 0755, true );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

311
		/** @scrutinizer ignore-unhandled */ @mkdir( $dest, 0755, true );

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
312
		copy( __DIR__ . '/_testfiles/test.gif', $dest . 'test.gif' );
313
314
		$result = $this->access( 'getContent' )->invokeArgs( $this->object, ['test.gif'] );
315
316
		$this->assertNotEquals( '', $result );
317
	}
318
319
320
	public function testGetContentHttp()
321
	{
322
		$url = 'https://aimeos.org/fileadmin/logos/favicon.png';
323
		$result = $this->access( 'getContent' )->invokeArgs( $this->object, [$url] );
324
325
		$this->assertNotEquals( '', $result );
326
	}
327
328
329
	public function testGetContentException()
330
	{
331
		$this->expectException( \Aimeos\MShop\Media\Exception::class );
332
		$this->access( 'getContent' )->invokeArgs( $this->object, [''] );
333
	}
334
335
336
	public function testGetFile()
337
	{
338
		$dest = dirname( __DIR__, 3 ) . '/tmp/';
339
		@mkdir( $dest, 0755, true );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

339
		/** @scrutinizer ignore-unhandled */ @mkdir( $dest, 0755, true );

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
340
		copy( __DIR__ . '/_testfiles/test.gif', $dest . 'test.gif' );
341
342
		$result = $this->access( 'getFile' )->invokeArgs( $this->object, ['test.gif'] );
343
		$this->assertInstanceOf( \Aimeos\MW\Media\Iface::class, $result );
344
	}
345
346
347
	public function testGetMime()
348
	{
349
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/_testfiles/test.png' );
350
351
		$result = $this->access( 'getMime' )->invokeArgs( $this->object, array( $file, 'files' ) );
352
		$this->assertEquals( true, in_array( $result, ['image/webp', 'image/png'] ) );
353
	}
354
355
356
	protected function access( $name )
357
	{
358
		$class = new \ReflectionClass( \Aimeos\MShop\Media\Manager\Standard::class );
359
		$method = $class->getMethod( $name );
360
		$method->setAccessible( true );
361
362
		return $method;
363
	}
364
}
365