public function testAfterSave() { $productIds = [1, 2, 3]; $this->subject->expects($this->any())->method('getAffectedProductIds')->will($this->returnValue($productIds)); $this->productRuleProcessor->expects($this->once())->method('reindexList')->with($productIds); $this->assertEquals($this->subject, $this->plugin->afterSave($this->subject, $this->subject)); }
protected function setUp() { $this->_category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Category'); $this->_category->load(5); $layer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Category', ['data' => ['current_category' => $this->_category]]); $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Filter\\Category', ['layer' => $layer]); }
/** * Build response for ajax request * * @param \Magento\Catalog\Model\Category $category * @param \Magento\Backend\Model\View\Result\Page $resultPage * * @return \Magento\Framework\Controller\Result\Json * * @deprecated */ protected function ajaxRequestResponse($category, $resultPage) { // prepare breadcrumbs of selected category, if any $breadcrumbsPath = $category->getPath(); if (empty($breadcrumbsPath)) { // but if no category, and it is deleted - prepare breadcrumbs from path, saved in session $breadcrumbsPath = $this->_objectManager->get('Magento\\Backend\\Model\\Auth\\Session')->getDeletedPath(true); if (!empty($breadcrumbsPath)) { $breadcrumbsPath = explode('/', $breadcrumbsPath); // no need to get parent breadcrumbs if deleting category level 1 if (count($breadcrumbsPath) <= 1) { $breadcrumbsPath = ''; } else { array_pop($breadcrumbsPath); $breadcrumbsPath = implode('/', $breadcrumbsPath); } } } $eventResponse = new \Magento\Framework\DataObject(['content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml() . $resultPage->getLayout()->getBlock('category.tree')->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'), 'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(), 'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()]); $this->_eventManager->dispatch('category_prepare_ajax_response', ['response' => $eventResponse, 'controller' => $this]); /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->_objectManager->get('Magento\\Framework\\Controller\\Result\\Json'); $resultJson->setHeader('Content-type', 'application/json', true); $resultJson->setData($eventResponse->getData()); return $resultJson; }
/** * Creates a new child category for $parentCategory * * @param \Magento\Catalog\Model\Category $parentCategory * @return \Magento\Catalog\Model\Category */ protected function createCategory($parentCategory) { /** @var \Magento\Catalog\Model\Category $category */ $category = $this->objectManager->create('Magento\\Catalog\\Model\\Category'); $category->setStoreId(self::DEFAULT_STORE_ID)->setParentId($parentCategory->getId())->setName(self::NAMES_PREFIX . $this->titlesGenerator->generateCategoryTitle())->setAttributeSetId($category->getDefaultAttributeSetId())->setLevel($parentCategory->getLevel() + 1)->setPath($parentCategory->getPath())->setIsActive(1)->save(); return $category; }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testLoadNoSuchEntityException() { $categoryId = 333; $this->category->expects($this->once())->method('load')->with($this->equalTo($categoryId)); $this->category->expects($this->once())->method('getId')->will($this->returnValue(false)); $this->model->load($categoryId); }
public function aroundGetUrlInstance(Category $category, \Closure $proceed) { if ($category->getStoreId() == 0) { return $proceed(); } else { return $this->objectManager->create(self::FRONTEND_URL)->setStoreId($category->getStoreId()); } }
/** * Reindex category's products after reindexing the category * * @param \Magento\Catalog\Model\Category $subject The cateogry being reindexed * @param callable $proceed The parent function we are plugged on * : Magento\Catalog\Model\Category::reindex() * * @return \Magento\Catalog\Model\Category */ public function aroundReindex(\Magento\Catalog\Model\Category $subject, callable $proceed) { $proceed(); if (!empty($subject->getAffectedProductIds())) { $this->processFullTextIndex($subject->getAffectedProductIds()); } return; }
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); }
/** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId * @return array */ protected function generateForCustom($url, $storeId) { $urls = []; $targetPath = !$url->getRedirectType() ? $url->getTargetPath() : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId); if ($url->getRequestPath() !== $targetPath) { $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create()->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($this->category->getId())->setRequestPath($url->getRequestPath())->setTargetPath($targetPath)->setRedirectType($url->getRedirectType())->setStoreId($storeId)->setDescription($url->getDescription())->setIsAutogenerated(0)->setMetadata($url->getMetadata()); } return $urls; }
/** * @param \Magento\Catalog\Model\Category $category * @return Node */ protected function getNode(\Magento\Catalog\Model\Category $category) { $nodeId = $category->getId(); $node = $this->categoryTree->loadNode($nodeId); $node->loadChildren(); $this->prepareCollection(); $this->categoryTree->addCollectionData($this->categoryCollection); return $node; }
/** * @param \Magento\Catalog\Model\Category $subject * @param \Magento\Catalog\Model\Category $result * @return \Magento\Catalog\Model\Category * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterSave(\Magento\Catalog\Model\Category $subject, \Magento\Catalog\Model\Category $result) { /** @var \Magento\Catalog\Model\Category $result */ $productIds = $result->getAffectedProductIds(); if ($productIds) { $this->productRuleProcessor->reindexList($productIds); } return $result; }
/** * @access public * @param \Magento\Catalog\Model\Category $category * @return mixed */ public function getSelectedAuthors(CategoryModel $category) { if (!$category->hasSelectedAuthors()) { $authors = []; foreach ($this->getSelectedAuthorsCollection($category) as $author) { $authors[] = $author; } $category->setSelectedAuthors($authors); } return $category->getData('selected_authors'); }
/** * @access public * @param \Magento\Catalog\Model\Category $category * @return mixed */ public function getSelectedArticles(CategoryModel $category) { if (!$category->hasSelectedArticles()) { $articles = []; foreach ($this->getSelectedArticlesCollection($category) as $article) { $articles[] = $article; } $category->setSelectedArticles($articles); } return $category->getData('selected_articles'); }
/** * Generate list of urls for global scope * * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ protected function generateForGlobalScope() { $urls = []; $categoryId = $this->category->getId(); foreach ($this->category->getStoreIds() as $id) { if (!$this->isGlobalScope($id) && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $categoryId, Category::ENTITY)) { $urls = array_merge($urls, $this->generateForSpecificStoreView($id)); } } return $urls; }
/** * Reindex category's data after into search engine after reindexing the category * * @param \Magento\Catalog\Model\Category $subject The category being reindexed * @param callable $proceed The parent function we are plugged on * : Magento\Catalog\Model\Category::reindex() * * @return \Magento\Catalog\Model\Category */ public function aroundReindex(\Magento\Catalog\Model\Category $subject, callable $proceed) { $proceed(); if ($subject->getLevel() > 1) { $categoryIndexer = $this->indexerRegistry->get(\Smile\ElasticsuiteCatalog\Model\Category\Indexer\Fulltext::INDEXER_ID); if (!$categoryIndexer->isScheduled()) { $categoryIndexer->reindexRow($subject->getId()); } } return; }
/** * @param Category $category * @return string */ protected function buildPath(Category $category) { $data = []; $path = $category->getPath(); foreach (explode('/', $path) as $categoryId) { $category = $this->_categoryRepository->get($categoryId); if ($category && $category->getLevel() > 1) { $data[] = $category->getName(); } } return count($data) ? '/' . implode('/', $data) : ''; }
/** * Get Category from request * * @return Category */ protected function _getCategory() { if (!$this->_category) { $this->_category = $this->_objectManager->create('Magento\\Catalog\\Model\\Category'); $categoryId = (int) $this->getRequest()->getParam('category', 0); if (!$categoryId && $this->_getUrlRewrite()->getId()) { $categoryId = $this->_getUrlRewrite()->getCategoryId(); } if ($categoryId) { $this->_category->load($categoryId); } } return $this->_category; }
/** * @param \Magento\Catalog\Model\Category $category * @param int $storeId * @return $this */ public function getProductCollection(\Magento\Catalog\Model\Category $category, $storeId) { /** @var $layer \Magento\Catalog\Model\Layer */ $layer = $this->catalogLayer->setStore($storeId); $collection = $category->getResourceCollection(); $collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load(); /** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $productCollection = $this->collectionFactory->create(); $currentCategory = $layer->setCurrentCategory($category); $layer->prepareProductCollection($productCollection); $productCollection->addCountToCategories($collection); $category->getProductCollection()->setStoreId($storeId); $products = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50); return $products; }
/** * @param Category $category * @param int $storeId * @param bool $saveRewriteHistory * @return UrlRewrite[] */ public function getCategoryProductsUrlRewrites(Category $category, $storeId, $saveRewriteHistory) { /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $category->getProductCollection()->addAttributeToSelect('name')->addAttributeToSelect('url_key')->addAttributeToSelect('url_path'); $productUrls = []; foreach ($productCollection as $product) { if (in_array($product->getId(), $this->isSkippedProduct)) { continue; } $this->isSkippedProduct[] = $product->getId(); $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); $productUrls = array_merge($productUrls, $this->productUrlRewriteGenerator->generate($product)); } return $productUrls; }
/** * @param Category $category * @return void */ public function processDelete(Category $category) { /** @var \Magento\Catalog\Model\ResourceModel\Category $resourceModel */ $resourceModel = $category->getResource(); /** * Update children count for all parent categories */ $parentIds = $category->getParentIds(); if ($parentIds) { $childDecrease = $category->getChildrenCount() + 1; // +1 is itself $data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)]; $where = ['entity_id IN(?)' => $parentIds]; $resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where); } }
public function testGetCustomAttributes() { $nameAttributeCode = 'name'; $descriptionAttributeCode = 'description'; $interfaceAttribute = $this->getMock('\\Magento\\Framework\\Api\\MetadataObjectInterface'); $interfaceAttribute->expects($this->once())->method('getAttributeCode')->willReturn($nameAttributeCode); $descriptionAttribute = $this->getMock('\\Magento\\Framework\\Api\\MetadataObjectInterface'); $descriptionAttribute->expects($this->once())->method('getAttributeCode')->willReturn($descriptionAttributeCode); $customAttributesMetadata = [$interfaceAttribute, $descriptionAttribute]; $this->metadataServiceMock->expects($this->once())->method('getCustomAttributesMetadata')->willReturn($customAttributesMetadata); $this->category->setData($nameAttributeCode, "sub"); //The color attribute is not set, expect empty custom attribute array $this->assertEquals([], $this->category->getCustomAttributes()); //Set the color attribute; $this->category->setData($descriptionAttributeCode, "description"); $attributeValue = new \Magento\Framework\Api\AttributeValue(); $attributeValue2 = new \Magento\Framework\Api\AttributeValue(); $this->attributeValueFactory->expects($this->exactly(2))->method('create')->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2); $this->assertEquals(1, count($this->category->getCustomAttributes())); $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode)); $this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue()); //Change the attribute value, should reflect in getCustomAttribute $this->category->setData($descriptionAttributeCode, "new description"); $this->assertEquals(1, count($this->category->getCustomAttributes())); $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode)); $this->assertEquals("new description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue()); }
public function testIsInRootCategoryList() { $this->assertFalse($this->_model->isInRootCategoryList()); $this->_model->unsetData(); $this->_model->load(3); $this->assertTrue($this->_model->isInRootCategoryList()); }
public function testInfo() { $id = 3; $this->category->expects($this->once())->method('load')->with($this->equalTo($id)); $this->category->expects($this->once())->method('getId')->will($this->returnValue(true)); $this->assertInstanceOf('Magento\\Catalog\\Service\\V1\\Data\\Eav\\Category\\Info\\Metadata', $this->model->info($id)); }
/** * @dataProvider getUrlKeyDataProvider * @param string|null|bool $urlKey * @param string|null|bool $name * @param string $result */ public function testGetUrlKey($urlKey, $name, $result) { $this->category->expects($this->once())->method('getUrlKey')->will($this->returnValue($urlKey)); $this->category->expects($this->any())->method('getName')->will($this->returnValue($name)); $this->category->expects($this->once())->method('formatUrlKey')->will($this->returnArgument(0)); $this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlKey($this->category)); }
public function testGetCategoryWithAppliedId() { $storeId = 1234; $categoryId = 4321; $this->store->expects($this->once())->method('getId')->will($this->returnValue($storeId)); $this->layer->expects($this->any())->method('getCurrentCategory')->will($this->returnValue($this->category)); $this->category->expects($this->once())->method('setStoreId')->with($this->equalTo($storeId))->will($this->returnSelf()); $this->category->expects($this->once())->method('load')->with($this->equalTo($categoryId))->will($this->returnSelf()); $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); $this->category->expects($this->any())->method('getPathIds')->will($this->returnValue([20, 10])); $this->coreRegistry->expects($this->once())->method('register')->with($this->equalTo('current_category_filter'), $this->equalTo($this->category), $this->equalTo(true))->will($this->returnSelf()); $this->target->setCategoryId($categoryId); $this->assertSame($this->category, $this->target->getCategory()); $this->assertSame(20, $this->target->getResetValue()); return $this->target; }
/** * Run test execute method * * @param int|bool $categoryId * @param int $storeId * @return void * * @dataProvider dataProviderExecute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute($categoryId, $storeId) { $rootCategoryId = 2; $this->requestMock->expects($this->atLeastOnce())->method('getParam')->will($this->returnValueMap([['id', false, $categoryId], ['store', null, $storeId]])); $this->requestMock->expects($this->atLeastOnce())->method('getQuery')->with('isAjax')->will($this->returnValue(false)); $this->mockInitCategoryCall(); $this->sessionMock->expects($this->once())->method('__call')->will($this->returnValue([])); if (!$categoryId) { if ($storeId) { $this->storeManagerInterfaceMock->expects($this->once())->method('getStore')->with($storeId)->will($this->returnSelf()); } else { $this->storeManagerInterfaceMock->expects($this->once())->method('getDefaultStoreView')->will($this->returnSelf()); } $this->storeManagerInterfaceMock->expects($this->once())->method('getRootCategoryId')->will($this->returnValue($rootCategoryId)); $categoryId = $rootCategoryId; } $this->requestMock->expects($this->atLeastOnce())->method('setParam')->with('id', $categoryId)->will($this->returnValue(true)); $this->categoryMock->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($categoryId)); /** * @var \Magento\Framework\View\Element\Template * |\PHPUnit_Framework_MockObject_MockObject $blockMock */ $blockMock = $this->getMock('Magento\\Framework\\View\\Element\\Template', ['setStoreId'], [], '', false); $blockMock->expects($this->once())->method('setStoreId')->with($storeId); $this->resultPageMock->expects($this->once())->method('getLayout')->will($this->returnSelf()); $this->resultPageMock->expects($this->once())->method('getBlock')->willReturn($blockMock); $this->edit->execute(); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetItems() { $this->category->expects($this->any())->method('getIsActive')->will($this->returnValue(true)); $category1 = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Category')->disableOriginalConstructor()->setMethods(['getId', 'getName', 'getIsActive'])->getMock(); $category1->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(120)); $category1->expects($this->once())->method('getName')->will($this->returnValue('Category 1')); $category1->expects($this->once())->method('getIsActive')->will($this->returnValue(true)); $category2 = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Category')->disableOriginalConstructor()->setMethods(['getId', 'getName', 'getIsActive'])->getMock(); $category2->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(5641)); $category2->expects($this->once())->method('getName')->will($this->returnValue('Category 2')); $category2->expects($this->once())->method('getIsActive')->will($this->returnValue(true)); $category3 = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Category')->disableOriginalConstructor()->setMethods(['getId', 'getName', 'getIsActive'])->getMock(); $category3->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(777)); $category3->expects($this->never())->method('getName'); $category3->expects($this->once())->method('getIsActive')->will($this->returnValue(true)); $categories = [$category1, $category2, $category3]; $this->category->expects($this->once())->method('getChildrenCategories')->will($this->returnValue($categories)); $facetedData = [120 => ['count' => 10], 5641 => ['count' => 45], 777 => ['count' => 80]]; $this->fulltextCollection->expects($this->once())->method('getSize')->will($this->returnValue(50)); $this->fulltextCollection->expects($this->once())->method('getFacetedData')->with('category')->will($this->returnValue($facetedData)); $builtData = [['label' => 'Category 1', 'value' => 120, 'count' => 10], ['label' => 'Category 2', 'value' => 5641, 'count' => 45]]; $this->itemDataBuilder->expects($this->at(0))->method('addItemData')->with('Category 1', 120, 10)->will($this->returnSelf()); $this->itemDataBuilder->expects($this->at(1))->method('addItemData')->with('Category 2', 5641, 45)->will($this->returnSelf()); $this->itemDataBuilder->expects($this->once())->method('build')->will($this->returnValue($builtData)); $this->target->getItems(); }
public function getObject(Category $category) { /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */ $productCollection = $category->getProductCollection(); $productCollection = $productCollection->addMinimalPrice(); $category->setProductCount($productCollection->getSize()); $transport = new DataObject(); $this->eventManager->dispatch('algolia_category_index_before', ['category' => $category, 'custom_data' => $transport]); $customData = $transport->getData(); $storeId = $category->getStoreId(); $category->getUrlInstance()->setStore($storeId); $path = ''; foreach ($category->getPathIds() as $categoryId) { if ($path != '') { $path .= ' / '; } $path .= $this->getCategoryName($categoryId, $storeId); } $image_url = null; try { $image_url = $category->getImageUrl(); } catch (\Exception $e) { /* no image, no default: not fatal */ } $data = ['objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), 'include_in_menu' => $category->getIncludeInMenu(), '_tags' => ['category'], 'popularity' => 1, 'product_count' => $category->getProductCount()]; if (!empty($image_url)) { $data['image_url'] = $image_url; } foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) { $value = $category->getData($attribute['attribute']); $attribute_resource = $category->getResource()->getAttribute($attribute['attribute']); if ($attribute_resource) { $value = $attribute_resource->getFrontend()->getValue($category); } if (isset($data[$attribute['attribute']])) { $value = $data[$attribute['attribute']]; } if ($value) { $data[$attribute['attribute']] = $value; } } $data = array_merge($data, $customData); foreach ($data as &$data0) { $data0 = $this->try_cast($data0); } return $data; }
public function testGenerate() { $requestPath = 'category.html'; $targetPath = 'target-path'; $storeId = 'store_id'; $categoryId = 'category_id'; $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($requestPath)); $this->categoryUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->will($this->returnValue($targetPath)); $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setEntityId')->with($categoryId)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setEntityType')->with(CategoryUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setRequestPath')->with($requestPath)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($targetPath)->will($this->returnSelf()); $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite)); $this->assertEquals(['category.html_store_id' => $this->urlRewrite], $this->canonicalUrlRewriteGenerator->generate($storeId, $this->category)); }
public function testGetCategoryId() { $this->category->expects($this->once())->method('getId')->will($this->returnValue(10)); $this->registry->expects($this->at(0))->method('registry'); $this->registry->expects($this->at(1))->method('registry')->will($this->returnValue($this->category)); $this->assertFalse($this->model->getCategoryId()); $this->assertEquals(10, $this->model->getCategoryId()); }