/** * @param $productScheduled * @param $expectedProductReindexCall * * @dataProvider reindexFlatDisabledTestDataProvider */ public function testReindexFlatDisabled($productScheduled, $expectedProductReindexCall) { $affectedProductIds = ["1", "2"]; $this->category->setAffectedProductIds($affectedProductIds); $pathIds = ['path/1/2', 'path/2/3']; $this->category->setData('path_ids', $pathIds); $this->category->setId('123'); $this->flatState->expects($this->any())->method('isFlatEnabled')->will($this->returnValue(false)); $this->productIndexer->expects($this->exactly(1))->method('isScheduled')->will($this->returnValue($productScheduled)); $this->productIndexer->expects($this->exactly($expectedProductReindexCall))->method('reindexList')->with($pathIds); $this->indexerRegistry->expects($this->at(0))->method('get')->with(Indexer\Category\Product::INDEXER_ID)->will($this->returnValue($this->productIndexer)); $this->category->reindex(); }
/** * Save category products relation * * @param \Magento\Catalog\Model\Category $category * @return $this */ protected function _saveCategoryProducts($category) { $category->setIsChangedProductList(false); $id = $category->getId(); /** * new category-product relationships */ $products = $category->getPostedProducts(); /** * Example re-save category */ if ($products === null) { return $this; } /** * old category-product relationships */ $oldProducts = $category->getProductsPosition(); $insert = array_diff_key($products, $oldProducts); $delete = array_diff_key($oldProducts, $products); /** * Find product ids which are presented in both arrays * and saved before (check $oldProducts array) */ $update = array_intersect_key($products, $oldProducts); $update = array_diff_assoc($update, $oldProducts); $adapter = $this->_getWriteAdapter(); /** * Delete products from category */ if (!empty($delete)) { $cond = array('product_id IN(?)' => array_keys($delete), 'category_id=?' => $id); $adapter->delete($this->_categoryProductTable, $cond); } /** * Add products to category */ if (!empty($insert)) { $data = array(); foreach ($insert as $productId => $position) { $data[] = array('category_id' => (int) $id, 'product_id' => (int) $productId, 'position' => (int) $position); } $adapter->insertMultiple($this->_categoryProductTable, $data); } /** * Update product positions in category */ if (!empty($update)) { foreach ($update as $productId => $position) { $where = array('category_id = ?' => (int) $id, 'product_id = ?' => (int) $productId); $bind = array('position' => (int) $position); $adapter->update($this->_categoryProductTable, $bind, $where); } } if (!empty($insert) || !empty($delete)) { $productIds = array_unique(array_merge(array_keys($insert), array_keys($delete))); $this->_eventManager->dispatch('catalog_category_change_products', array('category' => $category, 'product_ids' => $productIds)); } if (!empty($insert) || !empty($update) || !empty($delete)) { $category->setIsChangedProductList(true); /** * Setting affected products to category for third party engine index refresh */ $productIds = array_keys($insert + $delete + $update); $category->setAffectedProductIds($productIds); } return $this; }