/** * Process event * * @param \Magento\Index\Model\Event $event * @return void */ protected function _processEvent(\Magento\Index\Model\Event $event) { $data = $event->getNewData(); if (!empty($data['catalog_url_reindex_all'])) { $this->reindexAll(); } // Force rewrites history saving $dataObject = $event->getDataObject(); if ($dataObject instanceof \Magento\Framework\Object && $dataObject->hasData('save_rewrites_history')) { $this->_catalogUrl->setShouldSaveRewritesHistory($dataObject->getData('save_rewrites_history')); } if (isset($data['rewrite_product_ids'])) { $this->_catalogUrl->clearStoreInvalidRewrites(); // Maybe some products were moved or removed from website foreach ($data['rewrite_product_ids'] as $productId) { $this->_catalogUrl->refreshProductRewrite($productId); } } if (isset($data['rewrite_category_ids'])) { $this->_catalogUrl->clearStoreInvalidRewrites(); // Maybe some categories were moved foreach ($data['rewrite_category_ids'] as $categoryId) { $this->_catalogUrl->refreshCategoryRewrite($categoryId, null, true, true); } } }
/** * @param string $targetPathExecute * @param bool $changeRequestPath * * @dataProvider refreshcategoryRewriteDataProvider */ public function testRefreshCategoryRewrite($targetPathExecute, $changeRequestPath) { $categoryId = 1; $rewrite = array('category/1' => $this->_rewriteModel); $this->_resourceModel->expects($this->once())->method('prepareRewrites')->will($this->returnValue($rewrite)); $this->_resourceModel->expects($this->at(0))->method('getStores')->will($this->returnValue(array($this->_storeModel))); $this->_resourceModel->expects($this->once())->method('getStores')->will($this->returnValue($this->_storeModel)); $this->_resourceModel->expects($this->any())->method('getCategoryModel')->will($this->returnValue($this->_categoryModel)); $this->_resourceModel->expects($this->once())->method('getCategoryParentPath')->will($this->returnValue('parent_path')); $this->_resourceModel->expects($this->any())->method('deleteRewriteRecord')->will($this->returnSelf()); $this->_resourceModel->expects($this->any())->method('saveCategoryAttribute')->will($this->returnSelf()); $this->_resourceModel->expects($this->any())->method('getProductsByCategory')->will($this->returnValue(null)); $this->_resourceModel->expects($this->any())->method('deleteCategoryProductStoreRewrites')->will($this->returnSelf()); $this->_resourceModel->expects($this->{$targetPathExecute}())->method('findFinalTargetPath')->will($this->returnValue('category/1')); $this->_categoryModel->expects($this->any())->method('getAllChilds')->will($this->returnValue(array($this->_categoryModel))); $this->_categoryModel->expects($this->any())->method('formatUrlKey')->will($this->returnValue('url_formatted')); $this->_categoryModel->expects($this->any())->method('getUrlKey')->will($this->returnValue('url_key')); $this->_categoryModel->expects($this->any())->method('getCategoryUrlPath')->will($this->returnValue('category_parent_path')); $this->_categoryHelper->expects($this->once())->method('getCategoryUrlPath')->will($this->returnValue('category_parent_path')); $this->_categoryHelper->expects($this->once())->method('getCategoryUrlSuffix')->will($this->returnValue('suffics')); $this->_rewriteModel->expects($this->once())->method('getRequestPath')->will($this->returnValue('category_parent_pathurl_formatted-1suffics')); $this->_model->refreshCategoryRewrite($categoryId, '', true, $changeRequestPath); }
/** * @magentoDataFixture Magento/Catalog/_files/url_rewrites_invalid.php */ public function testRefreshCategoryRewrite() { $this->assertNotEmpty($this->_loadRewrite('product/1/4')->getId()); $this->_model->refreshCategoryRewrite(4); $this->assertEmpty($this->_loadRewrite('product/1/4')->getId()); }