public function testGetTreeHasLevelField() { $rootId = \Magento\Catalog\Model\Category::TREE_ROOT_ID; $storeGroups = []; $storeId = 1; $rootLevel = 2; $level = 3; $this->collection->expects($this->any())->method('addAttributeToSelect')->willReturnMap([['url_key', false, $this->collection], ['is_anchor', false, $this->collection]]); $this->childNode->expects($this->atLeastOnce())->method('getLevel')->willReturn($level); $this->rootNode->expects($this->atLeastOnce())->method('getLevel')->willReturn($rootLevel); $this->rootNode->expects($this->once())->method('hasChildren')->willReturn(true); $this->rootNode->expects($this->once())->method('getChildren')->willReturn([$this->childNode]); $this->categoryTree->expects($this->once())->method('load')->with(null, 3)->willReturnSelf(); $this->categoryTree->expects($this->atLeastOnce())->method('addCollectionData')->with($this->collection)->willReturnSelf(); $this->categoryTree->expects($this->once())->method('getNodeById')->with($rootId)->willReturn($this->rootNode); $this->store->expects($this->atLeastOnce())->method('getId')->willReturn($storeId); $this->storeManager->expects($this->once())->method('getGroups')->willReturn($storeGroups); $this->storeManager->expects($this->atLeastOnce())->method('getStore')->willReturn($this->store); $this->context->expects($this->once())->method('getStoreManager')->willReturn($this->storeManager); $this->context->expects($this->once())->method('getRequest')->willReturn($this->request); $this->context->expects($this->once())->method('getEscaper')->willReturn($this->escaper); $this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager); /** @var \Magento\Widget\Block\Adminhtml\Widget\Catalog\Category\Chooser $chooser */ $chooser = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject('Magento\\Widget\\Block\\Adminhtml\\Widget\\Catalog\\Category\\Chooser', ['categoryTree' => $this->categoryTree, 'context' => $this->context]); $chooser->setData('category_collection', $this->collection); $result = $chooser->getTree(); $this->assertEquals($level, $result[0]['level']); }
/** * @return \Magento\Catalog\Model\Resource\Category\Collection */ public function getCategoryCollection() { if (is_null($this->categoryCollection)) { $this->categoryCollection = $this->getAuthor()->getSelectedCategoriesCollection()->setStore($this->_storeManager->getStore())->addAttributeToSelect(array('name', 'url_key', 'url_path'))->addAttributeToFilter('is_active', 1); $this->categoryCollection->getSelect()->order('at_position.position'); } return $this->categoryCollection; }
public function testToOptionArray() { $expect = [['label' => __('-- Please Select a Category --'), 'value' => ''], ['label' => 'name', 'value' => 3]]; $this->categoryCollection->expects($this->once())->method('addAttributeToSelect')->with($this->equalTo('name'))->will($this->returnValue($this->categoryCollection)); $this->categoryCollection->expects($this->once())->method('addRootLevelFilter')->will($this->returnValue($this->categoryCollection)); $this->categoryCollection->expects($this->once())->method('load'); $this->categoryCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->category]))); $this->category->expects($this->once())->method('getName')->will($this->returnValue('name')); $this->category->expects($this->once())->method('getId')->will($this->returnValue(3)); $this->assertEquals($expect, $this->model->toOptionArray()); }
/** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->_eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', array('getEntityType', 'getEntityTypeId'), array(), '', false); $this->_eavConfig->expects($this->atLeastOnce())->method('getEntityType')->with($this->equalTo('catalog_product'))->will($this->returnSelf()); $this->_eavConfig->expects($this->atLeastOnce())->method('getEntityTypeId')->will($this->returnValue('1')); $this->_optionModel = $this->getMock('\\Magento\\CatalogImportExport\\Model\\Import\\Product\\Option', array(), array(), '', false); $this->_optionFactory = $this->getMock('\\Magento\\CatalogImportExport\\Model\\Import\\Product\\OptionFactory', array('create'), array(), '', false); $this->_optionFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_optionModel)); $this->_storeManager = $this->getMock('\\Magento\\Store\\Model\\StoreManager', array('getWebsites', 'getStores'), array(), '', false); $this->_storeManager->expects($this->atLeastOnce())->method('getWebsites')->will($this->returnValue(array())); $this->_storeManager->expects($this->atLeastOnce())->method('getStores')->will($this->returnValue(array())); $this->_setCol = $this->getMock('\\Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\Collection', array('setEntityTypeFilter'), array(), '', false); $this->_setCol->expects($this->atLeastOnce())->method('setEntityTypeFilter')->with($this->equalTo('1'))->will($this->returnValue(array())); $this->_setColFactory = $this->getMock('\\Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\CollectionFactory', array('create'), array(), '', false); $this->_setColFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_setCol)); $this->_importConfig = $this->getMock('\\Magento\\ImportExport\\Model\\Import\\Config', array('getEntityTypes'), array(), '', false); $this->_importConfig->expects($this->atLeastOnce())->method('getEntityTypes')->with('catalog_product')->will($this->returnValue(array())); $this->_categoryCol = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Category\\Collection', array('addNameToResult'), array(), '', false); $this->_categoryCol->expects($this->atLeastOnce())->method('addNameToResult')->will($this->returnValue(array())); $this->_categoryColFactory = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Category\\CollectionFactory', array('create'), array(), '', false); $this->_categoryColFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_categoryCol)); $this->_product = $this->getMock('\\Magento\\Catalog\\Model\\Product', array('getProductEntitiesInfo', '__wakeup'), array(), '', false); $this->_product->expects($this->atLeastOnce())->method('getProductEntitiesInfo')->with($this->equalTo(array('entity_id', 'type_id', 'attribute_set_id', 'sku')))->will($this->returnValue(array())); $this->_productFactory = $this->getMock('\\Magento\\Catalog\\Model\\ProductFactory', array('create'), array(), '', false); $this->_productFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_product)); $this->_customerGroupService = $this->getMock('Magento\\Customer\\Service\\V1\\CustomerGroupService', array('getGroups'), array(), '', false); $this->_customerGroupService->expects($this->atLeastOnce())->method('getGroups')->will($this->returnValue(array())); $objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); $this->_model = $objectManager->getObject('\\Magento\\CatalogImportExport\\Model\\Import\\Product', array('config' => $this->_eavConfig, 'optionFactory' => $this->_optionFactory, 'storeManager' => $this->_storeManager, 'setColFactory' => $this->_setColFactory, 'importConfig' => $this->_importConfig, 'categoryColFactory' => $this->_categoryColFactory, 'productFactory' => $this->_productFactory, 'customerGroupService' => $this->_customerGroupService)); }
/** * Get categories assigned to product * * @return \Magento\Catalog\Model\Resource\Category\Collection */ protected function getCategories() { if (!$this->categories) { $this->categories = $this->product->getCategoryCollection(); $this->categories->addAttributeToSelect('url_key'); $this->categories->addAttributeToSelect('url_path'); } return $this->categories; }
public function testSkipGenerationForNotStoreRootCategory() { $this->product->expects($this->any())->method('getStoreId')->will($this->returnValue(1)); $this->product->expects($this->never())->method('getStoreIds'); $category = $this->getMock('Magento\\Catalog\\Model\\Category', [], [], '', false); $category->expects($this->any())->method('getParentIds')->will($this->returnValue(['root-id', 'root-for-store-id'])); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue('not-root-id')); $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); $this->categoriesCollection->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$category])); $this->initObjectRegistryFactory([]); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate')->will($this->returnValue(['canonical'])); $this->categoriesUrlRewriteGenerator->expects($this->any())->method('generate')->will($this->returnValue([])); $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')->will($this->returnValue([])); $this->assertEquals(['canonical'], $this->productUrlRewriteGenerator->generate($this->product)); }
/** * Initialize categories ID to text-path hash. * * @return $this */ protected function _initCategories() { $collection = $this->_categoryColFactory->create()->addNameToResult(); /* @var $collection \Magento\Catalog\Model\Resource\Category\Collection */ foreach ($collection as $category) { $structure = preg_split('#/+#', $category->getPath()); $pathSize = count($structure); if ($pathSize > 1) { $path = array(); for ($i = 1; $i < $pathSize; $i++) { $path[] = $collection->getItemById($structure[$i])->getName(); } $this->_rootCategories[$category->getId()] = array_shift($path); if ($pathSize > 2) { $this->_categories[$category->getId()] = implode('/', $path); } } } return $this; }
/** * @return void */ protected function prepareCollection() { $storeId = $this->storeManager->getStore()->getId(); $this->categoryCollection->addAttributeToSelect('name')->addAttributeToSelect('is_active')->setProductStoreId($storeId)->setLoadProductCount(true)->setStoreId($storeId); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->_state->setAreaCode('crontab'); $indexersData = $this->indexerCollection->getItems(); if (!is_null($input->getOption(self::INPUT_KEY_MAX_PRODUCTS))) { $productsNum = $input->getOption(self::INPUT_KEY_MAX_PRODUCTS); } else { $productsNum = $this->productCollection->getSize(); } if (!is_null($input->getOption(self::INPUT_KEY_MAX_CATEGORIES))) { $categoriesNum = $input->getOption(self::INPUT_KEY_MAX_CATEGORIES); } else { $categoriesNum = $this->categoryCollection->getSize(); } if (!is_null($input->getOption(self::INPUT_KEY_MAX_CUSTOMERS))) { $customersNum = $input->getOption(self::INPUT_KEY_MAX_CUSTOMERS); } else { $customersNum = $this->customerCollection->getSize(); } if (!is_null($input->getOption(self::INPUT_KEY_MAX_RULES))) { $rulesNum = $input->getOption(self::INPUT_KEY_MAX_RULES); } else { $rulesNum = $this->ruleCollection->getSize(); } //These indexers are not ready to run in single-row mode $excludeIndexers = ['catalogrule_rule', 'catalogsearch_fulltext']; $productIndexers = ['catalog_product_flat', 'catalog_product_price', 'catalog_product_attribute', 'cataloginventory_stock', 'catalogrule_product', 'targetrule_product_rule', 'catalogpermissions_product', 'catalogrule_rule', 'catalogsearch_fulltext']; $categoryIndexers = ['catalog_category_flat', 'catalog_category_product', 'catalog_product_category', 'catalogpermissions_category']; $ruleIndexers = ['targetrule_rule_product']; $customerIndexres = ['customer_grid']; $requestedIndexer = $input->getOption('indexer'); if ($requestedIndexer == 'all') { $runThese = []; } else { $runThese = explode(',', $input->getOption('indexer')); } foreach ($indexersData as $indexer) { if (in_array($indexer->getIndexerId(), $excludeIndexers)) { continue; } if (count($runThese) > 0 && !in_array($indexer->getIndexerId(), $runThese)) { continue; } $max = 0; $entity = ''; if (in_array($indexer->getIndexerId(), $productIndexers)) { $max = $productsNum; $entity = 'Product'; } else { if (in_array($indexer->getIndexerId(), $categoryIndexers)) { $max = $categoriesNum; $entity = 'Category'; } else { if (in_array($indexer->getIndexerId(), $customerIndexres)) { $max = $customersNum; $entity = 'Customer'; } else { if (in_array($indexer->getIndexerId(), $ruleIndexers)) { $max = $rulesNum; $entity = 'Rule'; } } } } if (!is_null($input->getOption(self::INPUT_KEY_REINDEX_ID))) { $id = $input->getOption(self::INPUT_KEY_REINDEX_ID); } else { $id = mt_rand(1, $max); } $output->writeln('<info>' . $entity . '# id ' . $id . ' (out of ' . $max . ') for ' . $indexer->getIndexerId() . ' </info>'); $this->indexRegistry->get($indexer->getIndexerId())->reindexRow($id); $output->writeln('<info>' . $indexer->getDescription() . ' completed.</info>'); } }