/** * @param bool $isFlat * @param string $path * @param mixed $default * @param array $inputData * @param array $outputData * @dataProvider aroundGetDataProvider */ public function testAroundGet($isFlat, $path, $default, $inputData, $outputData) { $this->stateMock->expects($this->once())->method('isFlatEnabled')->will($this->returnValue($isFlat)); $closureMock = function () use($inputData) { return $inputData; }; $this->assertEquals($outputData, $this->model->aroundGet($this->subjectMock, $closureMock, $path, $default)); }
public function testAddCatalogToTopMenuItemsWithFlat() { $observer = $this->_preparationData(); $this->_category->expects($this->once())->method('getChildrenNodes')->will($this->returnValue([$this->_childrenCategory])); $this->_category->expects($this->once())->method('getUseFlatResource')->will($this->returnValue(true)); $this->_categoryFlatState->expects($this->once())->method('isFlatEnabled')->will($this->returnValue(true)); $this->_observer->execute($observer); }
public function testAroundSaveNotNew() { $this->stateMock->expects($this->never())->method('isFlatEnabled'); $this->groupMock = $this->getMock('Magento\\Store\\Model\\Group', array('dataHasChangedFor', 'isObjectNew', '__wakeup'), array(), '', false); $this->groupMock->expects($this->once())->method('dataHasChangedFor')->with('root_category_id')->will($this->returnValue(true)); $this->groupMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); $this->assertFalse($this->model->aroundSave($this->subjectMock, $this->closureMock, $this->groupMock)); }
/** * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $subject * @param callable $proceed * @param \Magento\Framework\Model\AbstractModel $group * * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Framework\Model\ResourceModel\Db\AbstractDb $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $group) { $needInvalidating = $this->validate($group); $objectResource = $proceed($group); if ($needInvalidating && $this->state->isFlatEnabled()) { $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID)->invalidate(); } return $objectResource; }
/** * @param $isAvailable * @param $isFlatEnabled * @param $isValid * @param $result * @dataProvider isAvailableDataProvider */ public function testIsAvailable($isAvailable, $isFlatEnabled, $isValid, $result) { $this->flatIndexerMock->expects($this->any())->method('load')->with('catalog_category_flat'); $this->flatIndexerMock->expects($this->any())->method('isValid')->will($this->returnValue($isValid)); $this->indexerRegistryMock->expects($this->any())->method('get')->with(\Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID)->will($this->returnValue($this->flatIndexerMock)); $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->with('catalog/frontend/flat_catalog_category')->will($this->returnValue($isFlatEnabled)); $this->model = new \Magento\Catalog\Model\Indexer\Category\Flat\State($this->scopeConfigMock, $this->indexerRegistryMock, $isAvailable); $this->assertEquals($result, $this->model->isAvailable()); }
/** * @param \Magento\Framework\Model\Resource\Db\AbstractDb $subject * @param callable $proceed * @param \Magento\Framework\Model\AbstractModel $group * * @return \Magento\Framework\Model\Resource\Db\AbstractDb * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Framework\Model\Resource\Db\AbstractDb $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $group) { $needInvalidating = $this->validate($group); $objectResource = $proceed($group); if ($needInvalidating && $this->state->isFlatEnabled()) { $this->getIndexer()->invalidate(); } return $objectResource; }
/** * Unset indexer data in configuration if flat is disabled * * @param \Magento\Indexer\Model\Config\Data $subject * @param callable $proceed * @param string $path * @param mixed $default * * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundGet(\Magento\Indexer\Model\Config\Data $subject, \Closure $proceed, $path = null, $default = null) { $data = $proceed($path, $default); if (!$this->state->isFlatEnabled()) { $indexerId = \Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID; if (!$path && isset($data[$indexerId])) { unset($data[$indexerId]); } elseif ($path) { list($firstKey, ) = explode('/', $path); if ($firstKey == $indexerId) { $data = $default; } } } return $data; }
/** * Recursively adds categories to top menu * * @param \Magento\Framework\Data\Tree\Node\Collection|array $categories * @param \Magento\Framework\Data\Tree\Node $parentCategoryNode * @param \Magento\Theme\Block\Html\Topmenu $block * @return void */ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block) { foreach ($categories as $category) { if (!$category->getIsActive()) { continue; } $block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId()); $tree = $parentCategoryNode->getTree(); $categoryData = $this->menuCategoryData->getMenuCategoryData($category); $categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode); $parentCategoryNode->addChild($categoryNode); if ($this->categoryFlatState->isFlatEnabled() && $category->getUseFlatResource()) { $subcategories = (array) $category->getChildrenNodes(); } else { $subcategories = $category->getChildren(); } $this->_addCategoriesToMenu($subcategories, $categoryNode, $block); } }
/** * @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(); }
/** * Get current category * * @param \Magento\Catalog\Model\Category $category * * @return Category */ public function isActive($category) { $activeCategory = $this->_coreRegistry->registry('current_category'); $activeProduct = $this->_coreRegistry->registry('current_product'); if (!$activeCategory) { // Check if we're on a product page if ($activeProduct !== null) { return in_array($category->getId(), $activeProduct->getCategoryIds()); } return false; } // Check if this is the active category if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource() and $category->getId() == $activeCategory->getId()) { return true; } // Check if a subcategory of this category is active $childrenIds = $category->getAllChildren(true); if (!is_null($childrenIds) and in_array($activeCategory->getId(), $childrenIds)) { return true; } // Fallback - If Flat categories is not enabled the active category does not give an id return $category->getName() == $activeCategory->getName() ? true : false; }
/** * Init indexing process after category save * * @return void */ public function reindex() { if ($this->flatState->isFlatEnabled()) { $flatIndexer = $this->indexerRegistry->get(Indexer\Category\Flat\State::INDEXER_ID); if (!$flatIndexer->isScheduled()) { $flatIndexer->reindexRow($this->getId()); } } $affectedProductIds = $this->getAffectedProductIds(); $productIndexer = $this->indexerRegistry->get(Indexer\Category\Product::INDEXER_ID); if (!$productIndexer->isScheduled() && !empty($affectedProductIds)) { $productIndexer->reindexList($this->getPathIds()); } }
/** * Init indexing process after category save * * @return void */ public function reindex() { if ($this->flatState->isFlatEnabled()) { $flatIndexer = $this->indexerRegistry->get(Indexer\Category\Flat\State::INDEXER_ID); if (!$flatIndexer->isScheduled()) { $flatIndexer->reindexRow($this->getId()); } } $productIndexer = $this->indexerRegistry->get(Indexer\Category\Product::INDEXER_ID); if (!$productIndexer->isScheduled() && (!empty($this->getAffectedProductIds()) || $this->dataHasChangedFor('is_anchor'))) { $productIndexer->reindexList($this->getPathIds()); } }
public function testGetUseFlatResourceTrue() { $this->flatState->expects($this->any())->method('isAvailable')->will($this->returnValue(true)); $category = $this->getCategoryModel(); $this->assertEquals(true, $category->getUseFlatResource()); }
protected function mockConfigFlatEnabledNeever() { $this->stateMock->expects($this->never())->method('isFlatEnabled'); }
/** * Render category to html * * @param Category $category * @param int $level Nesting level number * @param boolean $isLast Whether ot not this item is last, affects list item class * @param boolean $isFirst Whether ot not this item is first, affects list item class * @param boolean $isOutermost Whether ot not this item is outermost, affects list item class * @param string $outermostItemClass Extra class of outermost list items * @param string $childrenWrapClass If specified wraps children list in div with this class * @param boolean $noEventAttributes Whether ot not to add on* attributes to list item * @return string */ protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false, $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false) { if (!$category->getIsActive()) { return ''; } // get all children if ($this->flatState->isAvailable()) { $children = (array) $category->getChildrenNodes(); } else { $children = $category->getChildren(); } // select active children $activeChildren = array(); foreach ($children as $child) { if ($child->getIsActive()) { $activeChildren[] = $child; } } $activeChildrenCount = count($activeChildren); $hasActiveChildren = $activeChildrenCount > 0; // prepare list item html classes $classes = array(); $classes[] = 'level' . $level; $classes[] = 'nav-' . $this->_getItemPosition($level); if ($this->isCategoryActive($category)) { $classes[] = 'active'; } $linkClass = ''; if ($isOutermost && $outermostItemClass) { $classes[] = $outermostItemClass; $linkClass = ' class="' . $outermostItemClass . '"'; } if ($isFirst) { $classes[] = 'first'; } if ($isLast) { $classes[] = 'last'; } if ($hasActiveChildren) { $classes[] = 'parent'; } // prepare list item attributes $attributes = array(); if (count($classes) > 0) { $attributes['class'] = implode(' ', $classes); } if ($hasActiveChildren && !$noEventAttributes) { $attributes['onmouseover'] = 'toggleMenu(this,1)'; $attributes['onmouseout'] = 'toggleMenu(this,0)'; } // assemble list item with attributes $htmlLi = '<li'; foreach ($attributes as $attrName => $attrValue) { $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\\"', $attrValue) . '"'; } $htmlLi .= '>'; $html = array(); $html[] = $htmlLi; $html[] = '<a href="' . $this->getCategoryUrl($category) . '"' . $linkClass . '>'; $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>'; $html[] = '</a>'; // render children $htmlChildren = ''; $j = 0; foreach ($activeChildren as $child) { $htmlChildren .= $this->_renderCategoryMenuItemHtml($child, $level + 1, $j == $activeChildrenCount - 1, $j == 0, false, $outermostItemClass, $childrenWrapClass, $noEventAttributes); $j++; } if (!empty($htmlChildren)) { if ($childrenWrapClass) { $html[] = '<div class="' . $childrenWrapClass . '">'; } $html[] = '<ul class="level' . $level . '">'; $html[] = $htmlChildren; $html[] = '</ul>'; if ($childrenWrapClass) { $html[] = '</div>'; } } $html[] = '</li>'; $html = implode("\n", $html); return $html; }