Completed
Push — master ( 0b39e0...ec78c4 )
by Aimeos
09:03
created

StandardTest::testSearchWildcards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2016
7
 */
8
9
10
namespace Aimeos\MShop\Product\Manager;
11
12
13
class StandardTest extends \PHPUnit_Framework_TestCase
14
{
15
	private $context;
16
	private $object;
17
	private $editor = '';
18
19
20
	protected function setUp()
21
	{
22
		$this->context = \TestHelperMShop::getContext();
23
		$this->editor = $this->context->getEditor();
24
25
		$this->object = new \Aimeos\MShop\Product\Manager\Standard( $this->context );
26
	}
27
28
	protected function tearDown()
29
	{
30
		$this->object = null;
31
	}
32
33
34
	public function testCleanup()
35
	{
36
		$this->object->cleanup( array( -1 ) );
37
	}
38
39
40
	public function testCreateItem()
41
	{
42
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Product\\Item\\Iface', $this->object->createItem() );
43
	}
44
45
46
	public function testCreateSearch()
47
	{
48
		$search = $this->object->createSearch();
49
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\SQL', $search );
50
	}
51
52
53
	public function testGetResourceType()
54
	{
55
		$result = $this->object->getResourceType();
56
57
		$this->assertContains( 'product', $result );
58
		$this->assertContains( 'product/type', $result );
59
		$this->assertContains( 'product/lists', $result );
60
		$this->assertContains( 'product/lists/type', $result );
61
		$this->assertContains( 'product/property', $result );
62
		$this->assertContains( 'product/property/type', $result );
63
	}
64
65
66
	public function testGetSearchAttributes()
67
	{
68
		foreach( $this->object->getSearchAttributes() as $attribute ) {
69
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
70
		}
71
	}
72
73
74
	public function testFindItem()
75
	{
76
		$item = $this->object->findItem( 'CNC' );
77
78
		$this->assertEquals( 'CNC', $item->getCode() );
79
	}
80
81
82
	public function testGetItem()
83
	{
84
		$domains = array( 'text', 'product', 'price', 'media', 'attribute', 'product/property' );
85
86
		$search = $this->object->createSearch();
87
		$conditions = array(
88
				$search->compare( '==', 'product.code', 'CNC' ),
89
				$search->compare( '==', 'product.editor', $this->editor )
90
		);
91
		$search->setConditions( $search->combine( '&&', $conditions ) );
92
		$products = $this->object->searchItems( $search, $domains );
93
94
		if( ( $product = reset( $products ) ) === false ) {
95
			throw new \RuntimeException( sprintf( 'Found no Productitem with text "%1$s"', 'Cafe Noire Cappuccino' ) );
96
		}
97
98
		$this->assertEquals( $product, $this->object->getItem( $product->getId(), $domains ) );
99
		$this->assertEquals( 6, count( $product->getRefItems( 'text' ) ) );
100
		$this->assertEquals( 4, count( $product->getPropertyItems() ) );
101
		$this->assertNotEquals( '', $product->getTypeName() );
102
	}
103
104
105
	public function testSaveUpdateDeleteItem()
106
	{
107
		$search = $this->object->createSearch();
108
		$conditions = array(
109
				$search->compare( '==', 'product.code', 'CNC' ),
110
				$search->compare( '==', 'product.editor', $this->editor )
111
		);
112
		$search->setConditions( $search->combine( '&&', $conditions ) );
113
		$items = $this->object->searchItems( $search );
114
115
		if( ( $item = reset( $items ) ) === false ) {
116
			throw new \RuntimeException( 'No product item found' );
117
		}
118
119
		$item->setId( null );
120
		$item->setCode( 'CNC unit test' );
121
		$this->object->saveItem( $item );
122
		$itemSaved = $this->object->getItem( $item->getId() );
123
124
		$itemExp = clone $itemSaved;
125
		$itemExp->setCode( 'unit save test' );
126
		$this->object->saveItem( $itemExp );
127
		$itemUpd = $this->object->getItem( $itemExp->getId() );
128
129
		$this->object->deleteItem( $itemSaved->getId() );
130
131
132
		$this->assertTrue( $item->getId() !== null );
133
		$this->assertTrue( $itemSaved->getType() !== null );
134
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
135
		$this->assertEquals( $item->getSiteid(), $itemSaved->getSiteId() );
136
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
137
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
138
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
139
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
140
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
141
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
142
		$this->assertEquals( $item->getConfig(), $itemSaved->getConfig() );
143
144
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
145
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
146
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
147
148
		$this->assertTrue( $itemUpd->getType() !== null );
149
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
150
		$this->assertEquals( $itemExp->getSiteid(), $itemUpd->getSiteId() );
151
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
152
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
153
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
154
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
155
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
156
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
157
		$this->assertEquals( $itemExp->getConfig(), $itemUpd->getConfig() );
158
159
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
160
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
161
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
162
163
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
164
		$this->object->getItem( $itemSaved->getId() );
165
	}
166
167
168
	public function testSaveItemSitecheck()
169
	{
170
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperMShop::getContext() );
171
172
		$search = $manager->createSearch();
173
		$search->setConditions( $search->compare( '==', 'product.editor', $this->editor ) );
174
		$search->setSlice( 0, 1 );
175
		$products = $manager->searchItems( $search );
176
177
		if( ( $item = reset( $products ) ) === false ) {
178
			throw new \RuntimeException( 'No product found' );
179
		}
180
181
		$item->setId( null );
182
		$item->setCode( 'unittest' );
183
184
		$manager->saveItem( $item );
185
		$manager->getItem( $item->getId() );
186
		$manager->deleteItem( $item->getId() );
187
188
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
189
		$manager->getItem( $item->getId() );
190
	}
191
192
193
	public function testUpdateListItems()
194
	{
195
		$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( $this->context );
196
		$attrId = $attrManager->findItem( 's', array(), 'product', 'size' )->getId();
197
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
198
199
		// create new list item
200
		$map = array( $attrId => array( 'product.lists.datestart' => '2000-01-01 00:00:00' ) );
201
		$this->object->updateListItems( $item, $map, 'attribute', 'hidden' );
202
203
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
204
		$listItems = $item->getListItems( 'attribute', 'hidden' );
205
206
		$this->assertEquals( 1, count( $listItems ) );
207
		$this->assertEquals( '2000-01-01 00:00:00', reset( $listItems )->getDateStart() );
208
209
210
		// update existing list item
211
		$map = array( $attrId => array( 'product.lists.config' => array( 'key' => 'value' ) ) );
212
		$this->object->updateListItems( $item, $map, 'attribute', 'hidden' );
213
214
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
215
		$listItems = $item->getListItems( 'attribute', 'hidden' );
216
217
		$this->assertEquals( 1, count( $listItems ) );
218
		$this->assertEquals( '2000-01-01 00:00:00', reset( $listItems )->getDateStart() );
219
		$this->assertEquals( array( 'key' => 'value' ), reset( $listItems )->getConfig() );
220
221
222
		// delete existing list item
223
		$this->object->updateListItems( $item, array(), 'attribute', 'hidden' );
224
225
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
226
		$this->assertEquals( 0, count( $item->getListItems( 'attribute', 'hidden' ) ) );
227
	}
228
229
230
	public function testSearchItems()
231
	{
232
		$total = 0;
233
		$listManager = $this->object->getSubManager( 'lists' );
234
235
		$search = $listManager->createSearch();
236
		$expr = array(
237
			$search->compare( '==', 'product.lists.type.domain', 'product' ),
238
			$search->compare( '==', 'product.lists.type.code', 'suggestion' ),
239
			$search->compare( '==', 'product.lists.datestart', null ),
240
			$search->compare( '==', 'product.lists.dateend', null ),
241
			$search->compare( '!=', 'product.lists.config', null ),
242
			$search->compare( '==', 'product.lists.position', 0 ),
243
			$search->compare( '==', 'product.lists.status', 1 ),
244
			$search->compare( '==', 'product.lists.editor', $this->editor ),
245
		);
246
		$search->setConditions( $search->combine( '&&', $expr ) );
247
248
		$results = $listManager->searchItems( $search );
249
		if( ( $listItem = reset( $results ) ) === false ) {
250
			throw new \RuntimeException( 'No list item found' );
251
		}
252
253
		$search = $this->object->createSearch();
254
255
		$expr = array();
256
		$expr[] = $search->compare( '!=', 'product.id', null );
257
		$expr[] = $search->compare( '!=', 'product.siteid', null );
258
		$expr[] = $search->compare( '!=', 'product.typeid', null );
259
		$expr[] = $search->compare( '==', 'product.code', 'CNE' );
260
		$expr[] = $search->compare( '==', 'product.label', 'Cafe Noire Expresso' );
261
		$expr[] = $search->compare( '~=', 'product.config', 'css-class' );
262
		$expr[] = $search->compare( '==', 'product.datestart', null );
263
		$expr[] = $search->compare( '==', 'product.dateend', null );
264
		$expr[] = $search->compare( '==', 'product.status', 1 );
265
		$expr[] = $search->compare( '>=', 'product.ctime', '1970-01-01 00:00:00' );
266
		$expr[] = $search->compare( '>=', 'product.mtime', '1970-01-01 00:00:00' );
267
		$expr[] = $search->compare( '==', 'product.editor', $this->editor );
268
269
		$param = array( 'product', $listItem->getTypeId(), array( $listItem->getRefId() ) );
270
		$expr[] = $search->compare( '>', $search->createFunction( 'product.contains', $param ), 0 );
271
272
		$expr[] = $search->compare( '!=', 'product.type.id', null );
273
		$expr[] = $search->compare( '!=', 'product.type.siteid', null );
274
		$expr[] = $search->compare( '==', 'product.type.domain', 'product' );
275
		$expr[] = $search->compare( '==', 'product.type.code', 'default' );
276
		$expr[] = $search->compare( '==', 'product.type.label', 'Article' );
277
		$expr[] = $search->compare( '==', 'product.type.status', 1 );
278
		$expr[] = $search->compare( '==', 'product.type.editor', $this->editor );
279
280
		$expr[] = $search->compare( '!=', 'product.lists.id', null );
281
		$expr[] = $search->compare( '!=', 'product.lists.siteid', null );
282
		$expr[] = $search->compare( '!=', 'product.lists.parentid', null );
283
		$expr[] = $search->compare( '!=', 'product.lists.typeid', null );
284
		$expr[] = $search->compare( '==', 'product.lists.domain', 'product' );
285
		$expr[] = $search->compare( '>', 'product.lists.refid', 0 );
286
		$expr[] = $search->compare( '==', 'product.lists.datestart', null );
287
		$expr[] = $search->compare( '==', 'product.lists.dateend', null );
288
		$expr[] = $search->compare( '!=', 'product.lists.config', null );
289
		$expr[] = $search->compare( '==', 'product.lists.position', 0 );
290
		$expr[] = $search->compare( '==', 'product.lists.status', 1 );
291
		$expr[] = $search->compare( '==', 'product.lists.editor', $this->editor );
292
293
		$expr[] = $search->compare( '!=', 'product.lists.type.id', null );
294
		$expr[] = $search->compare( '!=', 'product.lists.type.siteid', null );
295
		$expr[] = $search->compare( '==', 'product.lists.type.domain', 'product' );
296
		$expr[] = $search->compare( '==', 'product.lists.type.code', 'suggestion' );
297
		$expr[] = $search->compare( '==', 'product.lists.type.label', 'Suggestion' );
298
		$expr[] = $search->compare( '==', 'product.lists.type.status', 1 );
299
		$expr[] = $search->compare( '==', 'product.lists.type.editor', $this->editor );
300
301
		$expr[] = $search->compare( '!=', 'product.property.id', null );
302
		$expr[] = $search->compare( '!=', 'product.property.siteid', null );
303
		$expr[] = $search->compare( '!=', 'product.property.typeid', null );
304
		$expr[] = $search->compare( '==', 'product.property.languageid', null );
305
		$expr[] = $search->compare( '==', 'product.property.value', '1' );
306
		$expr[] = $search->compare( '==', 'product.property.editor', $this->editor );
307
308
309
		$search->setConditions( $search->combine( '&&', $expr ) );
310
		$search->setSlice( 0, 1 );
311
312
		$results = $this->object->searchItems( $search, array(), $total );
313
		$this->assertEquals( 1, count( $results ) );
314
		$this->assertEquals( 1, $total );
315
316
		foreach( $results as $itemId => $item ) {
317
			$this->assertEquals( $itemId, $item->getId() );
318
		}
319
320
		$search = $this->object->createSearch();
321
		$search->setConditions( $search->compare( '==', 'product.editor', $this->editor ) );
322
		$search->setSlice( 0, 10 );
323
		$results = $this->object->searchItems( $search, array(), $total );
324
		$this->assertEquals( 10, count( $results ) );
325
		$this->assertEquals( 28, $total );
326
327
328
		$search = $this->object->createSearch( true );
329
		$expr = array(
330
			$search->compare( '==', 'product.code', array( 'CNC', 'CNE' ) ),
331
			$search->compare( '==', 'product.editor', $this->editor ),
332
			$search->getConditions(),
333
		);
334
		$search->setConditions( $search->combine( '&&', $expr ) );
335
		$result = $this->object->searchItems( $search, array( 'media' ) );
336
337
		$this->assertEquals( 2, count( $result ) );
338
	}
339
340
341
	public function testSearchWildcards()
342
	{
343
		$search = $this->object->createSearch();
344
		$search->setConditions( $search->compare( '=~', 'product.code', 'CN_' ) );
345
		$result = $this->object->searchItems( $search );
346
347
		$this->assertEquals( 0, count( $result ) );
348
349
350
		$search = $this->object->createSearch();
351
		$search->setConditions( $search->compare( '=~', 'product.code', 'CN%' ) );
352
		$result = $this->object->searchItems( $search );
353
354
		$this->assertEquals( 0, count( $result ) );
355
356
357
		$search = $this->object->createSearch();
358
		$search->setConditions( $search->compare( '=~', 'product.code', 'CN[C]' ) );
359
		$result = $this->object->searchItems( $search );
360
361
		$this->assertEquals( 0, count( $result ) );
362
	}
363
364
365
	public function testSearchItemsLimit()
366
	{
367
		$start = 0;
368
		$numproducts = 0;
369
370
		$search = $this->object->createSearch();
371
		$search->setConditions( $search->compare( '==', 'product.editor', 'core:unittest' ) );
372
		$search->setSlice( $start, 5 );
373
374
		do
375
		{
376
			$result = $this->object->searchItems( $search );
377
378
			foreach( $result as $item ) {
379
				$numproducts++;
380
			}
381
382
			$count = count( $result );
383
			$start += $count;
384
			$search->setSlice( $start, 5 );
385
		}
386
		while( $count > 0 );
387
388
		$this->assertEquals( 28, $numproducts );
389
	}
390
391
392
	public function testGetSubManager()
393
	{
394
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists' ) );
395
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists', 'Standard' ) );
396
397
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'property' ) );
398
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'property', 'Standard' ) );
399
400
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type' ) );
401
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type', 'Standard' ) );
402
403
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
404
		$this->object->getSubManager( 'unknown' );
405
	}
406
407
408
	public function testGetSubManagerInvalidName()
409
	{
410
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
411
		$this->object->getSubManager( 'lists', 'unknown' );
412
	}
413
}
414