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-2018 |
||
7 | */ |
||
8 | |||
9 | |||
10 | namespace Aimeos\MShop\Index\Manager; |
||
11 | |||
12 | |||
13 | class StandardTest extends \PHPUnit\Framework\TestCase |
||
14 | { |
||
15 | private static $products; |
||
16 | private $context; |
||
17 | private $object; |
||
18 | private $editor = ''; |
||
19 | |||
20 | |||
21 | public static function setUpBeforeClass() |
||
22 | { |
||
23 | $context = \TestHelperMShop::getContext(); |
||
24 | |||
25 | $manager = new \Aimeos\MShop\Index\Manager\Standard( $context ); |
||
26 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context ); |
||
27 | |||
28 | $search = $productManager->createSearch(); |
||
29 | $conditions = array( |
||
30 | $search->compare( '==', 'product.code', array( 'CNC', 'CNE' ) ), |
||
31 | $search->compare( '==', 'product.editor', $context->getEditor() ), |
||
32 | ); |
||
33 | $search->setConditions( $search->combine( '&&', $conditions ) ); |
||
34 | $result = $productManager->searchItems( $search, array( 'attribute', 'price', 'text', 'product' ) ); |
||
35 | |||
36 | if( count( $result ) !== 2 ) { |
||
37 | throw new \RuntimeException( 'Products not available' ); |
||
38 | } |
||
39 | |||
40 | foreach( $result as $item ) |
||
41 | { |
||
42 | self::$products[$item->getCode()] = $item; |
||
43 | $manager->saveItem( $item ); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | |||
48 | protected function setUp() |
||
49 | { |
||
50 | $this->context = \TestHelperMShop::getContext(); |
||
51 | $this->editor = $this->context->getEditor(); |
||
52 | $this->object = new \Aimeos\MShop\Index\Manager\Standard( $this->context ); |
||
53 | } |
||
54 | |||
55 | |||
56 | protected function tearDown() |
||
57 | { |
||
58 | unset( $this->object ); |
||
59 | } |
||
60 | |||
61 | |||
62 | public function testCreateItem() |
||
63 | { |
||
64 | $this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $this->object->createItem() ); |
||
65 | } |
||
66 | |||
67 | |||
68 | public function testCreateSearch() |
||
69 | { |
||
70 | $this->assertInstanceOf( \Aimeos\MW\Criteria\Iface::class, $this->object->createSearch() ); |
||
71 | } |
||
72 | |||
73 | |||
74 | public function testAggregate() |
||
75 | { |
||
76 | $manager = \Aimeos\MShop\Factory::createManager( \TestHelperMShop::getContext(), 'attribute' ); |
||
77 | |||
78 | $search = $manager->createSearch(); |
||
79 | $expr = array( |
||
80 | $search->compare( '==', 'attribute.code', 'white' ), |
||
81 | $search->compare( '==', 'attribute.domain', 'product' ), |
||
82 | $search->compare( '==', 'attribute.type.code', 'color' ), |
||
83 | ); |
||
84 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
85 | |||
86 | $items = $manager->searchItems( $search ); |
||
87 | |||
88 | if( ( $item = reset( $items ) ) === false ) { |
||
89 | throw new \RuntimeException( 'No attribute found' ); |
||
90 | } |
||
91 | |||
92 | |||
93 | $search = $this->object->createSearch( true ); |
||
94 | $result = $this->object->aggregate( $search, 'index.attribute.id' ); |
||
95 | |||
96 | $this->assertEquals( 15, count( $result ) ); |
||
97 | $this->assertArrayHasKey( $item->getId(), $result ); |
||
98 | $this->assertEquals( 4, $result[$item->getId()] ); |
||
99 | } |
||
100 | |||
101 | |||
102 | public function testGetItem() |
||
103 | { |
||
104 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context ); |
||
105 | $search = $productManager->createSearch(); |
||
106 | $search->setSlice( 0, 1 ); |
||
107 | $result = $productManager->searchItems( $search ); |
||
108 | |||
109 | if( ( $expected = reset( $result ) ) === false ) { |
||
110 | throw new \RuntimeException( 'No item found' ); |
||
111 | } |
||
112 | |||
113 | $item = $this->object->getItem( $expected->getId() ); |
||
114 | $this->assertEquals( $expected, $item ); |
||
115 | } |
||
116 | |||
117 | |||
118 | public function testGetResourceType() |
||
119 | { |
||
120 | $result = $this->object->getResourceType(); |
||
121 | |||
122 | $this->assertContains( 'index', $result ); |
||
123 | $this->assertContains( 'index/attribute', $result ); |
||
124 | $this->assertContains( 'index/supplier', $result ); |
||
125 | $this->assertContains( 'index/catalog', $result ); |
||
126 | $this->assertContains( 'index/price', $result ); |
||
127 | $this->assertContains( 'index/text', $result ); |
||
128 | } |
||
129 | |||
130 | |||
131 | public function testGetSearchAttributes() |
||
132 | { |
||
133 | $attributes = $this->object->getSearchAttributes(); |
||
134 | |||
135 | foreach( $attributes as $attribute ) { |
||
136 | $this->assertInstanceOf( \Aimeos\MW\Criteria\Attribute\Iface::class, $attribute ); |
||
137 | } |
||
138 | |||
139 | $this->assertArrayHasKey( 'index.attribute.id', $attributes ); |
||
140 | $this->assertArrayHasKey( 'index.catalog.id', $attributes ); |
||
141 | $this->assertArrayHasKey( 'index.supplier.id', $attributes ); |
||
142 | } |
||
143 | |||
144 | |||
145 | public function testSaveDeleteItem() |
||
146 | { |
||
147 | $item = self::$products['CNE']; |
||
148 | |||
149 | $context = $this->context; |
||
150 | $dbm = $context->getDatabaseManager(); |
||
151 | $siteId = $context->getLocale()->getSiteId(); |
||
152 | |||
153 | $sqlAttribute = 'SELECT COUNT(*) as count FROM "mshop_index_attribute" WHERE "siteid" = ? AND "prodid" = ?'; |
||
154 | $sqlCatalog = 'SELECT COUNT(*) as count FROM "mshop_index_catalog" WHERE "siteid" = ? AND "prodid" = ?'; |
||
155 | $sqlPrice = 'SELECT COUNT(*) as count FROM "mshop_index_price" WHERE "siteid" = ? AND "prodid" = ?'; |
||
156 | $sqlText = 'SELECT COUNT(*) as count FROM "mshop_index_text" WHERE "siteid" = ? AND "prodid" = ?'; |
||
157 | |||
158 | $this->object->saveItem( $item ); |
||
159 | |||
160 | $cntAttributeA = $this->getValue( $dbm, $sqlAttribute, 'count', $siteId, $item->getId() ); |
||
161 | $cntCatalogA = $this->getValue( $dbm, $sqlCatalog, 'count', $siteId, $item->getId() ); |
||
162 | $cntPriceA = $this->getValue( $dbm, $sqlPrice, 'count', $siteId, $item->getId() ); |
||
163 | $cntTextA = $this->getValue( $dbm, $sqlText, 'count', $siteId, $item->getId() ); |
||
164 | |||
165 | |||
166 | $this->object->deleteItem( $item->getId() ); |
||
167 | |||
168 | $cntAttributeB = $this->getValue( $dbm, $sqlAttribute, 'count', $siteId, $item->getId() ); |
||
169 | $cntCatalogB = $this->getValue( $dbm, $sqlCatalog, 'count', $siteId, $item->getId() ); |
||
170 | $cntPriceB = $this->getValue( $dbm, $sqlPrice, 'count', $siteId, $item->getId() ); |
||
171 | $cntTextB = $this->getValue( $dbm, $sqlText, 'count', $siteId, $item->getId() ); |
||
172 | |||
173 | |||
174 | // recreate index for CNE |
||
175 | $result = $this->object->saveItem( $item ); |
||
176 | |||
177 | $this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $result ); |
||
178 | |||
179 | $this->assertEquals( 8, $cntAttributeA ); |
||
180 | $this->assertEquals( 5, $cntCatalogA ); |
||
181 | $this->assertEquals( 2, $cntPriceA ); |
||
182 | $this->assertEquals( 21, $cntTextA ); |
||
183 | |||
184 | $this->assertEquals( 0, $cntAttributeB ); |
||
185 | $this->assertEquals( 0, $cntCatalogB ); |
||
186 | $this->assertEquals( 0, $cntPriceB ); |
||
187 | $this->assertEquals( 0, $cntTextB ); |
||
188 | } |
||
189 | |||
190 | |||
191 | public function testSaveDeleteItemNoName() |
||
192 | { |
||
193 | $context = $this->context; |
||
194 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context ); |
||
195 | |||
196 | $search = $productManager->createSearch(); |
||
197 | $search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) ); |
||
198 | $result = $productManager->searchItems( $search ); |
||
199 | |||
200 | if( ( $item = reset( $result ) ) === false ) { |
||
201 | throw new \RuntimeException( 'Product not available' ); |
||
202 | } |
||
203 | |||
204 | |||
205 | $dbm = $context->getDatabaseManager(); |
||
206 | $siteId = $context->getLocale()->getSiteId(); |
||
207 | $langid = $context->getLocale()->getLanguageId(); |
||
208 | |||
209 | $sqlProd = 'SELECT "value" FROM "mshop_index_text" |
||
210 | WHERE "siteid" = ? AND "prodid" = ? AND "langid" = \'' . $langid . '\' |
||
211 | AND "type" = \'name\' AND domain = \'product\''; |
||
212 | $sqlAttr = 'SELECT "value" FROM "mshop_index_text" |
||
213 | WHERE "siteid" = ? AND "prodid" = ? AND type = \'name\' AND domain = \'attribute\''; |
||
214 | |||
215 | $this->object->saveItem( $item ); |
||
216 | $attrText = $this->getValue( $dbm, $sqlAttr, 'value', $siteId, $item->getId() ); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
217 | $prodText = $this->getValue( $dbm, $sqlProd, 'value', $siteId, $item->getId() ); |
||
218 | $this->object->deleteItem( $item->getId() ); |
||
219 | |||
220 | $this->assertEquals( 'unterproduct 3', $prodText ); |
||
221 | $this->assertEquals( 'xl', $attrText ); |
||
222 | } |
||
223 | |||
224 | |||
225 | public function testSearchItems() |
||
226 | { |
||
227 | $total = 0; |
||
228 | $search = $this->object->createSearch(); |
||
229 | $search->setSlice( 0, 1 ); |
||
230 | |||
231 | $expr = array( |
||
232 | $search->compare( '~=', 'product.label', 'Cafe Noire' ), |
||
233 | $search->compare( '==', 'product.editor', $this->editor ), |
||
234 | $search->compare( '!=', 'index.catalog.id', null ), |
||
235 | ); |
||
236 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
237 | |||
238 | $result = $this->object->searchItems( $search, [], $total ); |
||
239 | |||
240 | $this->assertEquals( 1, count( $result ) ); |
||
241 | $this->assertEquals( 2, $total ); |
||
242 | |||
243 | |||
244 | // with base criteria |
||
245 | $search = $this->object->createSearch( true ); |
||
246 | $conditions = array( |
||
247 | $search->compare( '==', 'product.editor', $this->editor ), |
||
248 | $search->getConditions() |
||
249 | ); |
||
250 | $search->setConditions( $search->combine( '&&', $conditions ) ); |
||
251 | $products = $this->object->searchItems( $search ); |
||
252 | $this->assertEquals( 22, count( $products ) ); |
||
253 | |||
254 | foreach( $products as $itemId => $item ) { |
||
255 | $this->assertEquals( $itemId, $item->getId() ); |
||
256 | } |
||
257 | |||
258 | |||
259 | $search = $this->object->createSearch( true ); |
||
260 | $expr = array( |
||
261 | $search->compare( '==', 'product.code', array( 'CNC', 'CNE' ) ), |
||
262 | $search->compare( '==', 'product.editor', $this->editor ), |
||
263 | $search->getConditions(), |
||
264 | ); |
||
265 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
266 | $result = $this->object->searchItems( $search, array( 'media' ) ); |
||
267 | |||
268 | $this->assertEquals( 2, count( $result ) ); |
||
269 | } |
||
270 | |||
271 | |||
272 | public function testSearchItemsAttributeId() |
||
273 | { |
||
274 | $attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( $this->context ); |
||
275 | $attrWidthItem = $attributeManager->findItem( '29', [], 'product', 'width' ); |
||
276 | |||
277 | $total = 0; |
||
278 | $search = $this->object->createSearch(); |
||
279 | $search->setSlice( 0, 1 ); |
||
280 | |||
281 | $conditions = array( |
||
282 | $search->compare( '==', 'index.attribute.id', $attrWidthItem->getId() ), |
||
283 | $search->compare( '==', 'product.editor', $this->editor ), |
||
284 | ); |
||
285 | $search->setConditions( $search->combine( '&&', $conditions ) ); |
||
286 | $result = $this->object->searchItems( $search, [], $total ); |
||
287 | |||
288 | $this->assertEquals( 1, count( $result ) ); |
||
289 | $this->assertEquals( 3, $total ); |
||
290 | } |
||
291 | |||
292 | |||
293 | public function testSearchItemsAttributeIdNotNull() |
||
294 | { |
||
295 | $total = 0; |
||
296 | $search = $this->object->createSearch(); |
||
297 | $search->setSlice( 0, 1 ); |
||
298 | |||
299 | $expr = array( |
||
300 | $search->compare( '!=', 'index.attribute.id', null ), |
||
301 | $search->compare( '!=', 'index.catalog.id', null ), |
||
302 | $search->compare( '==', 'product.editor', $this->editor ) |
||
303 | ); |
||
304 | |||
305 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
306 | $result = $this->object->searchItems( $search, [], $total ); |
||
307 | |||
308 | $this->assertEquals( 1, count( $result ) ); |
||
309 | $this->assertEquals( 6, $total ); |
||
310 | } |
||
311 | |||
312 | |||
313 | public function testSearchItemsCatalog() |
||
314 | { |
||
315 | $catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context ); |
||
316 | $catItem = $catalogManager->findItem( 'cafe' ); |
||
317 | |||
318 | $search = $this->object->createSearch()->setSlice( 0, 1 ); |
||
319 | $search->setConditions( $search->compare( '==', 'product.editor', $this->editor ) ); |
||
320 | |||
321 | $sortfunc = $search->createFunction( 'sort:index.catalog:position', array( 'promotion', [$catItem->getId()] ) ); |
||
322 | $search->setSortations( array( $search->sort( '+', $sortfunc ) ) ); |
||
323 | |||
324 | $this->assertEquals( 1, count( $this->object->searchItems( $search ) ) ); |
||
325 | } |
||
326 | |||
327 | |||
328 | public function testSearchItemsCatalogId() |
||
329 | { |
||
330 | $catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context ); |
||
331 | $catItem = $catalogManager->findItem( 'cafe' ); |
||
332 | |||
333 | $search = $this->object->createSearch()->setSlice( 0, 1 ); |
||
334 | $total = 0; |
||
335 | |||
336 | $conditions = array( |
||
337 | $search->compare( '==', 'index.catalog.id', $catItem->getId() ), // catalog ID |
||
338 | $search->compare( '==', 'product.editor', $this->editor ) |
||
339 | ); |
||
340 | $search->setConditions( $search->combine( '&&', $conditions ) ); |
||
341 | $result = $this->object->searchItems( $search, [], $total ); |
||
342 | |||
343 | $this->assertEquals( 1, count( $result ) ); |
||
344 | $this->assertEquals( 2, $total ); |
||
345 | } |
||
346 | |||
347 | |||
348 | public function testSearchItemsCatalogIdNotNull() |
||
349 | { |
||
350 | $search = $this->object->createSearch()->setSlice( 0, 1 ); |
||
351 | $total = 0; |
||
352 | |||
353 | $conditions = array( |
||
354 | $search->compare( '!=', 'index.catalog.id', null ), // catalog ID |
||
355 | $search->compare( '==', 'product.editor', $this->editor ) |
||
356 | ); |
||
357 | $search->setConditions( $search->combine( '&&', $conditions ) ); |
||
358 | $result = $this->object->searchItems( $search, [], $total ); |
||
359 | |||
360 | $this->assertEquals( 1, count( $result ) ); |
||
361 | $this->assertEquals( 7, $total ); |
||
362 | } |
||
363 | |||
364 | |||
365 | public function testSearchItemsCatalogPosition() |
||
366 | { |
||
367 | $catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context ); |
||
368 | $catItem = $catalogManager->findItem( 'cafe' ); |
||
369 | |||
370 | $search = $this->object->createSearch()->setSlice( 0, 1 ); |
||
371 | $total = 0; |
||
372 | |||
373 | $func = $search->createFunction( 'index.catalog:position', array( 'promotion', [$catItem->getId()] ) ); |
||
374 | $conditions = array( |
||
375 | $search->compare( '!=', $func, null ), // position |
||
376 | $search->compare( '==', 'product.editor', $this->editor ) |
||
377 | ); |
||
378 | $search->setConditions( $search->combine( '&&', $conditions ) ); |
||
379 | |||
380 | $sortfunc = $search->createFunction( 'sort:index.catalog:position', array( 'promotion', [$catItem->getId()] ) ); |
||
381 | $search->setSortations( array( $search->sort( '+', $sortfunc ) ) ); |
||
382 | |||
383 | $result = $this->object->searchItems( $search, [], $total ); |
||
384 | |||
385 | $this->assertEquals( 1, count( $result ) ); |
||
386 | $this->assertEquals( 2, $total ); |
||
387 | } |
||
388 | |||
389 | |||
390 | public function testSearchItemsPrice() |
||
391 | { |
||
392 | $total = 0; |
||
393 | $search = $this->object->createSearch()->setSlice( 0, 1 ); |
||
394 | |||
395 | $func = $search->createFunction( 'index.price:value', array( 'default', 'EUR', 'default' ) ); |
||
396 | $expr = array( |
||
397 | $search->compare( '!=', 'index.catalog.id', null ), |
||
398 | $search->compare( '>=', $func, '18.00' ), |
||
399 | $search->compare( '==', 'product.editor', $this->editor ) |
||
400 | ); |
||
401 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
402 | |||
403 | $sortfunc = $search->createFunction( 'sort:index.price:value', array( 'default', 'EUR', 'default' ) ); |
||
404 | $search->setSortations( array( $search->sort( '+', $sortfunc ) ) ); |
||
405 | |||
406 | $result = $this->object->searchItems( $search, [], $total ); |
||
407 | |||
408 | $this->assertEquals( 1, count( $result ) ); |
||
409 | $this->assertEquals( 5, $total ); |
||
410 | } |
||
411 | |||
412 | |||
413 | public function testSearchItemsText() |
||
414 | { |
||
415 | $this->context->getConfig()->set( 'mshop/index/manager/text/name', 'Standard' ); |
||
416 | $object = new \Aimeos\MShop\Index\Manager\Standard( $this->context ); |
||
417 | |||
418 | $total = 0; |
||
419 | $search = $object->createSearch()->setSlice( 0, 1 ); |
||
420 | |||
421 | $func = $search->createFunction( 'index.text:relevance', array( 'de', 'Expr' ) ); |
||
422 | $conditions = array( |
||
423 | $search->compare( '>', $func, 0 ), // text relevance |
||
424 | $search->compare( '==', 'product.editor', $this->editor ) |
||
425 | ); |
||
426 | $search->setConditions( $search->combine( '&&', $conditions ) ); |
||
427 | |||
428 | $result = $object->searchItems( $search, [], $total ); |
||
429 | |||
430 | $this->assertEquals( 1, count( $result ) ); |
||
431 | $this->assertEquals( 2, $total ); |
||
432 | } |
||
433 | |||
434 | |||
435 | public function testOptimize() |
||
436 | { |
||
437 | $this->object->optimize(); |
||
438 | } |
||
439 | |||
440 | |||
441 | public function testCleanupIndex() |
||
442 | { |
||
443 | $this->object->cleanupIndex( '1970-01-01 00:00:00' ); |
||
444 | } |
||
445 | |||
446 | |||
447 | public function testRebuildIndexAll() |
||
448 | { |
||
449 | $config = $this->context->getConfig(); |
||
450 | |||
451 | $manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context ); |
||
452 | $search = $manager->createSearch( true ); |
||
453 | $search->setSlice( 0, 0x7fffffff ); |
||
454 | |||
455 | //delete whole catalog |
||
456 | $this->object->deleteItems( array_keys( $manager->searchItems( $search ) ) ); |
||
457 | |||
458 | //build catalog with all products |
||
459 | $config->set( 'mshop/index/manager/standard/index', 'all' ); |
||
460 | $this->object->rebuildIndex(); |
||
461 | |||
462 | $afterInsertAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' ); |
||
463 | $afterInsertPrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' ); |
||
464 | $afterInsertText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' ); |
||
465 | $afterInsertCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' ); |
||
466 | |||
467 | //restore index with categorized products only |
||
468 | $config->set( 'mshop/index/manager/standard/index', 'categorized' ); |
||
469 | $this->object->rebuildIndex(); |
||
470 | |||
471 | $this->assertEquals( 13, count( $afterInsertAttr ) ); |
||
472 | $this->assertEquals( 11, count( $afterInsertPrice ) ); |
||
473 | $this->assertEquals( 13, count( $afterInsertText ) ); |
||
474 | $this->assertEquals( 8, count( $afterInsertCat ) ); |
||
475 | } |
||
476 | |||
477 | |||
478 | public function testRebuildIndexWithList() |
||
479 | { |
||
480 | $manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context ); |
||
481 | $search = $manager->createSearch(); |
||
482 | $search->setSlice( 0, 0x7fffffff ); |
||
483 | |||
484 | //delete whole catalog |
||
485 | $this->object->deleteItems( array_keys( $manager->searchItems( $search ) ) ); |
||
486 | |||
487 | $afterDeleteAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' ); |
||
488 | $afterDeletePrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' ); |
||
489 | $afterDeleteText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' ); |
||
490 | $afterDeleteCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' ); |
||
491 | |||
492 | //insert cne, cnc |
||
493 | $search = $manager->createSearch(); |
||
494 | $search->setConditions( $search->compare( '==', 'product.code', array( 'CNE', 'CNC' ) ) ); |
||
495 | $items = $manager->searchItems( $search ); |
||
496 | |||
497 | $this->object->rebuildIndex( $items ); |
||
498 | |||
499 | $afterInsertAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' ); |
||
500 | $afterInsertPrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' ); |
||
501 | $afterInsertText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' ); |
||
502 | $afterInsertCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' ); |
||
503 | |||
504 | //delete cne, cnc |
||
505 | foreach( $items as $item ) { |
||
506 | $this->object->deleteItem( $item->getId() ); |
||
507 | } |
||
508 | |||
509 | //restores catalog |
||
510 | $this->object->rebuildIndex(); |
||
511 | |||
512 | //check delete |
||
513 | $this->assertEquals( [], $afterDeleteAttr ); |
||
514 | $this->assertEquals( [], $afterDeletePrice ); |
||
515 | $this->assertEquals( [], $afterDeleteText ); |
||
516 | $this->assertEquals( [], $afterDeleteCat ); |
||
517 | |||
518 | //check inserted items |
||
519 | $this->assertEquals( 2, count( $afterInsertAttr ) ); |
||
520 | $this->assertEquals( 2, count( $afterInsertPrice ) ); |
||
521 | $this->assertEquals( 2, count( $afterInsertText ) ); |
||
522 | $this->assertEquals( 2, count( $afterInsertCat ) ); |
||
523 | } |
||
524 | |||
525 | |||
526 | public function testRebuildIndexCategorizedOnly() |
||
527 | { |
||
528 | $context = $this->context; |
||
529 | $config = $context->getConfig(); |
||
530 | |||
531 | $manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context ); |
||
532 | |||
533 | //delete whole catalog |
||
534 | $search = $manager->createSearch(); |
||
535 | $search->setSlice( 0, 0x7fffffff ); |
||
536 | $this->object->deleteItems( array_keys( $manager->searchItems( $search ) ) ); |
||
537 | |||
538 | $config->set( 'mshop/index/manager/standard/index', 'categorized' ); |
||
539 | $this->object->rebuildIndex(); |
||
540 | |||
541 | $afterInsertAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' ); |
||
542 | $afterInsertPrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' ); |
||
543 | $afterInsertText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' ); |
||
544 | $afterInsertCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' ); |
||
545 | |||
546 | //check inserted items |
||
547 | $this->assertEquals( 7, count( $afterInsertAttr ) ); |
||
548 | $this->assertEquals( 7, count( $afterInsertPrice ) ); |
||
549 | $this->assertEquals( 7, count( $afterInsertText ) ); |
||
550 | $this->assertEquals( 8, count( $afterInsertCat ) ); |
||
551 | } |
||
552 | |||
553 | |||
554 | /** |
||
555 | * Returns value of a catalog_index column. |
||
556 | * |
||
557 | * @param \Aimeos\MW\DB\Manager\Iface $dbm Database Manager for connection |
||
558 | * @param string $sql Specified db query to find only one value |
||
559 | * @param string $column Column where to search |
||
560 | * @param integer $siteId Siteid of the db entry |
||
561 | * @param integer $productId Product id |
||
562 | * @return string $value Value returned for specified sql statement |
||
563 | * @throws \Exception If column not available or error during a connection to db |
||
564 | */ |
||
565 | protected function getValue( \Aimeos\MW\DB\Manager\Iface $dbm, $sql, $column, $siteId, $productId ) |
||
566 | { |
||
567 | $config = $this->context->getConfig(); |
||
568 | |||
569 | if( $config->get( 'resource/db-product' ) === null ) { |
||
570 | $dbname = $config->get( 'resource/default', 'db' ); |
||
571 | } else { |
||
572 | $dbname = 'db-product'; |
||
573 | } |
||
574 | |||
575 | $conn = $dbm->acquire( $dbname ); |
||
576 | |||
577 | try |
||
578 | { |
||
579 | $stmt = $conn->create( $sql ); |
||
580 | $stmt->bind( 1, $siteId, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
581 | $stmt->bind( 2, $productId, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
582 | $result = $stmt->execute(); |
||
583 | |||
584 | if( ( $row = $result->fetch() ) === false ) { |
||
585 | throw new \RuntimeException( 'No rows available' ); |
||
586 | } |
||
587 | |||
588 | if( !isset( $row[$column] ) ) { |
||
589 | throw new \RuntimeException( sprintf( 'Column "%1$s" not available for "%2$s"', $column, $sql ) ); |
||
590 | } |
||
591 | |||
592 | $value = $row[$column]; |
||
593 | |||
594 | $dbm->release( $conn, $dbname ); |
||
595 | } |
||
596 | catch( \Exception $e ) |
||
597 | { |
||
598 | $dbm->release( $conn, $dbname ); |
||
599 | throw $e; |
||
600 | } |
||
601 | |||
602 | return $value; |
||
603 | } |
||
604 | |||
605 | |||
606 | /** |
||
607 | * Gets product items of index subdomains specified by the key. |
||
608 | * |
||
609 | * @param string $key Key for searchItems |
||
610 | * @param string $domain Subdomain of index manager |
||
611 | */ |
||
612 | protected function getCatalogSubDomainItems( $key, $domain ) |
||
613 | { |
||
614 | $subIndex = $this->object->getSubManager( $domain ); |
||
615 | $search = $subIndex->createSearch(); |
||
616 | |||
617 | $expr = array( |
||
618 | $search->compare( '!=', $key, null ), |
||
619 | $search->compare( '==', 'product.editor', $this->editor ) |
||
620 | ); |
||
621 | |||
622 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
623 | |||
624 | return $subIndex->searchItems( $search ); |
||
625 | } |
||
626 | |||
627 | } |
||
628 |