/** * {@inheritdoc} */ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $indexer = $this->indexerRegistry->get(Config::DESIGN_CONFIG_GRID_INDEXER_ID); $indexer->reindexAll(); $setup->endSetup(); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); if (version_compare($context->getVersion(), '2.0.1', '<')) { $entityAttributes = ['customer' => ['website_id' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false], 'created_in' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true], 'email' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => true], 'group_id' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false], 'dob' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false], 'taxvat' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true], 'confirmation' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false], 'created_at' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false], 'gender' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false]], 'customer_address' => ['company' => ['is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true], 'street' => ['is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true], 'city' => ['is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true], 'country_id' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false], 'region' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true], 'region_id' => ['is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false], 'postcode' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => true], 'telephone' => ['is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => true], 'fax' => ['is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true]]]; $this->upgradeAttributes($entityAttributes, $customerSetup); } if (version_compare($context->getVersion(), '2.0.2') < 0) { $entityTypeId = $customerSetup->getEntityTypeId(Customer::ENTITY); $attributeId = $customerSetup->getAttributeId($entityTypeId, 'gender'); $option = ['attribute_id' => $attributeId, 'values' => [3 => 'Not Specified']]; $customerSetup->addAttributeOption($option); } if (version_compare($context->getVersion(), '2.0.3', '<')) { $entityAttributes = ['customer_address' => ['region_id' => ['is_used_in_grid' => false, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => false], 'firstname' => ['is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true], 'lastname' => ['is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => true]]]; $this->upgradeAttributes($entityAttributes, $customerSetup); } if (version_compare($context->getVersion(), '2.0.4', '<')) { $customerSetup->addAttribute(Customer::ENTITY, 'updated_at', ['type' => 'static', 'label' => 'Updated At', 'input' => 'date', 'required' => false, 'sort_order' => 87, 'visible' => false, 'system' => false]); } if (version_compare($context->getVersion(), '2.0.5', '<')) { $this->upgradeHash($setup); $entityAttributes = ['customer_address' => ['fax' => ['is_visible' => false, 'is_system' => false]]]; $this->upgradeAttributes($entityAttributes, $customerSetup); } $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID); $indexer->reindexAll(); $this->eavConfig->clear(); $setup->endSetup(); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.NPathComplexity) */ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); if (version_compare($context->getVersion(), '2.0.6', '<')) { $this->upgradeVersionTwoZeroSix($customerSetup); } if (version_compare($context->getVersion(), '2.0.1', '<')) { $this->upgradeVersionTwoZeroOne($customerSetup); } if (version_compare($context->getVersion(), '2.0.2') < 0) { $this->upgradeVersionTwoZeroTwo($customerSetup); } if (version_compare($context->getVersion(), '2.0.3', '<')) { $this->upgradeVersionTwoZeroThree($customerSetup); } if (version_compare($context->getVersion(), '2.0.4', '<')) { $this->upgradeVersionTwoZeroFour($customerSetup); } if (version_compare($context->getVersion(), '2.0.5', '<')) { $this->upgradeVersionTwoZeroFive($customerSetup, $setup); } if (version_compare($context->getVersion(), '2.0.6', '<')) { $setup->getConnection()->delete($setup->getTable('customer_form_attribute'), ['form_code = ?' => 'checkout_register']); } if (version_compare($context->getVersion(), '2.0.7', '<')) { $this->upgradeVersionTwoZeroSeven($customerSetup); $this->upgradeCustomerPasswordResetlinkExpirationPeriodConfig($setup); } $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID); $indexer->reindexAll(); $this->eavConfig->clear(); $setup->endSetup(); }
/** * Reindex by product if indexer is not scheduled * * @param int[] $productIds * @return void */ protected function reindexList(array $productIds) { $indexer = $this->indexerRegistry->get(Fulltext::INDEXER_ID); if (!$indexer->isScheduled()) { $indexer->reindexList($productIds); } }
/** * Test AfterImportSource() */ public function testAfterImportSource() { $this->_indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock->expects($this->any())->method('get')->with(\Magento\Catalog\Model\Indexer\Product\Price\Processor::INDEXER_ID)->will($this->returnValue($this->_indexerMock)); $this->_indexerMock->expects($this->any())->method('isScheduled')->will($this->returnValue(false)); $importMock = $this->getMock('Magento\\ImportExport\\Model\\Import', [], [], '', false); $this->assertEquals('return_value', $this->_model->afterImportSource($importMock, 'return_value')); }
/** * After save call * Invalidate catalog search index if engine was changed * * @return $this */ public function afterSave() { parent::afterSave(); if ($this->isValueChanged()) { $this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate(); } return $this; }
/** * Synchronize customer grid * * @return void */ public function syncCustomerGrid() { $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID); $customerIds = $this->getCustomerIdsForReindex(); if ($customerIds) { $indexer->reindexList($customerIds); } }
/** * Ensure the index is invalidated when synonyms are updated. * * @param QueryModel $subject Saved query. * @param \Closure $proceed Original save method. * * @return QueryModel */ public function aroundSave(QueryModel $subject, \Closure $proceed) { $needReindex = $subject->dataHasChangedFor('synonym_for'); $result = $proceed(); if ($needReindex) { $this->indexerRegistry->get(ThesaurusIndexer::INDEXER_ID)->invalidate(); } return $result; }
public function testAroundSave() { $this->stateMock->expects($this->once())->method('isFlatEnabled')->will($this->returnValue(true)); $this->indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock->expects($this->once())->method('get')->with(\Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID)->will($this->returnValue($this->indexerMock)); $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(false)); $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 mixed * @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->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Category\Product::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()); }
public function testAfterDelete() { /** @var \Magento\Store\Model\Group|\PHPUnit_Framework_MockObject_MockObject $subjectMock */ $subjectMock = $this->getMockBuilder('Magento\\Store\\Model\\Group')->disableOriginalConstructor()->getMock(); /** @var IndexerInterface|\PHPUnit_Framework_MockObject_MockObject $indexerMock */ $indexerMock = $this->getMockBuilder('Magento\\Framework\\Indexer\\IndexerInterface')->getMockForAbstractClass(); $indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock->expects($this->once())->method('get')->with(Config::DESIGN_CONFIG_GRID_INDEXER_ID)->willReturn($indexerMock); $this->assertEquals($subjectMock, $this->model->afterDelete($subjectMock, $subjectMock)); }
public function testDelete() { $this->designConfig->expects($this->exactly(2))->method('getExtensionAttributes')->willReturn($this->designExtension); $this->designExtension->expects($this->once())->method('getDesignConfigData')->willReturn([$this->designConfigData]); $this->configStorage->expects($this->once())->method('delete')->with($this->designConfig); $this->reinitableConfig->expects($this->once())->method('reinit'); $this->indexerRegistry->expects($this->once())->method('get')->with(Config::DESIGN_CONFIG_GRID_INDEXER_ID)->willReturn($this->indexer); $this->indexer->expects($this->once())->method('reindexAll'); $this->assertSame($this->designConfig, $this->repository->delete($this->designConfig)); }
public function setUp() { $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_subjectMock = $this->getMock('\\Magento\\Customer\\Api\\GroupRepositoryInterface', [], [], '', false); $indexerMock = $this->getMock('Magento\\Indexer\\Model\\Indexer', ['getId', 'invalidate'], [], '', false); $indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock = $this->getMock('Magento\\Framework\\Indexer\\IndexerRegistry', ['get'], [], '', false); $this->indexerRegistryMock->expects($this->once())->method('get')->with(\Magento\Catalog\Model\Indexer\Product\Price\Processor::INDEXER_ID)->will($this->returnValue($indexerMock)); $this->_model = $this->_objectManager->getObject('Magento\\Catalog\\Model\\Indexer\\Product\\Price\\Plugin\\CustomerGroup', ['indexerRegistry' => $this->indexerRegistryMock]); }
/** * 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; }
/** * Process flat enabled mode change * * @return void */ public function processValue() { if ((bool) $this->getValue() != (bool) $this->getOldValue()) { if ((bool) $this->getValue()) { $this->indexerState->loadByIndexer(\Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID); $this->indexerState->setStatus(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID); $this->indexerState->save(); } else { $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID)->setScheduled(false); } } }
/** * Execute action for single entity or list of entities * * @param int[] $ids * @return $this */ protected function executeAction($ids) { $ids = array_unique($ids); $indexer = $this->indexerRegistry->get(static::INDEXER_ID); /** @var Product\Action\Rows $action */ $action = $this->rowsActionFactory->create(); if ($indexer->isWorking()) { $action->execute($ids, true); } $action->execute($ids); return $this; }
/** * Execute materialization on ids entities * * @param int[] $ids * @return void */ public function execute($ids) { $indexer = $this->indexerRegistry->get(Flat\State::INDEXER_ID); if ($indexer->isInvalid()) { return; } /** @var Flat\Action\Rows $action */ $action = $this->rowsActionFactory->create(); if ($indexer->isWorking()) { $action->reindex($ids, true); } $action->reindex($ids); }
/** * @param array $attributes * @return array */ public function filter(array $attributes) { $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID); if ($indexer->getState()->getStatus() != StateInterface::STATUS_VALID) { $tableName = $this->flatScopeResolver->resolve(Customer::CUSTOMER_GRID_INDEXER_ID, []); $columns = $this->resource->getConnection()->describeTable($tableName); foreach (array_keys($attributes) as $attributeCode) { if (!isset($columns[$attributeCode])) { unset($attributes[$attributeCode]); } } } return $attributes; }
public function testFilter() { $attributeCode = 'attribute-code'; $attributeCodeTwo = 'attribute-code2'; $tableName = 'customer_grid_flat'; $attributes = [$attributeCode => [AttributeMetadataInterface::ATTRIBUTE_CODE => $attributeCode, AttributeMetadataInterface::FRONTEND_INPUT => 'input', AttributeMetadataInterface::FRONTEND_LABEL => 'Frontend label', AttributeMetadataInterface::BACKEND_TYPE => 'static', AttributeMetadataInterface::OPTIONS => [], AttributeMetadataInterface::IS_USED_IN_GRID => true, AttributeMetadataInterface::IS_VISIBLE_IN_GRID => true, AttributeMetadataInterface::IS_FILTERABLE_IN_GRID => true, AttributeMetadataInterface::IS_SEARCHABLE_IN_GRID => true], $attributeCodeTwo => [AttributeMetadataInterface::ATTRIBUTE_CODE => $attributeCodeTwo, AttributeMetadataInterface::FRONTEND_INPUT => 'input', AttributeMetadataInterface::FRONTEND_LABEL => 'Frontend label two', AttributeMetadataInterface::BACKEND_TYPE => 'static', AttributeMetadataInterface::OPTIONS => [], AttributeMetadataInterface::IS_USED_IN_GRID => false, AttributeMetadataInterface::IS_VISIBLE_IN_GRID => false, AttributeMetadataInterface::IS_FILTERABLE_IN_GRID => false, AttributeMetadataInterface::IS_SEARCHABLE_IN_GRID => false]]; $this->indexerRegistry->expects($this->once())->method('get')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID)->willReturn($this->indexer); $this->indexer->expects($this->once())->method('getState')->willReturn($this->indexerState); $this->indexerState->expects($this->once())->method('getStatus')->willReturn(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID); $this->flatScopeResolver->expects($this->once())->method('resolve')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID, [])->willReturn($tableName); $this->resource->expects($this->once())->method('getConnection')->willReturn($this->connection); $this->connection->expects($this->once())->method('describeTable')->with($tableName)->willReturn(['attribute-code' => ['Attribute data']]); $this->assertArrayNotHasKey($attributeCodeTwo, $this->model->filter($attributes)); }
/** * Execute materialization on ids entities * * @param int[] $ids * @return void */ public function execute($ids) { $indexer = $this->indexerRegistry->get(Flat\State::INDEXER_ID); if ($indexer->isInvalid()) { return; } /** @var Flat\Action\Rows $action */ $action = $this->rowsActionFactory->create(); if ($indexer->isWorking()) { $action->reindex($ids, true); } $action->reindex($ids); $this->getCacheContext()->registerEntities(\Magento\Catalog\Model\Category::CACHE_TAG, $ids); }
protected function prepareIndexer() { $this->indexerRegistryMock->expects($this->once()) ->method('get') ->with(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID) ->will($this->returnValue($this->indexerMock)); }
/** * @param array $indexerIds * @param \Exception $exception * @param array $expectsExceptionValues * @dataProvider executeDataProvider */ public function testExecute($indexerIds, $exception, $expectsExceptionValues) { $this->model = new \Magento\Indexer\Controller\Adminhtml\Indexer\MassChangelog($this->contextMock); $this->request->expects($this->any())->method('getParam')->with('indexer_ids')->will($this->returnValue($indexerIds)); if (!is_array($indexerIds)) { $this->messageManager->expects($this->once())->method('addError')->with(__('Please select indexers.'))->will($this->returnValue(1)); } else { $this->objectManager->expects($this->any())->method('get')->with('Magento\\Framework\\Indexer\\IndexerRegistry')->will($this->returnValue($this->indexReg)); $indexerInterface = $this->getMockForAbstractClass('Magento\\Framework\\Indexer\\IndexerInterface', ['setScheduled'], '', false); $this->indexReg->expects($this->any())->method('get')->with(1)->will($this->returnValue($indexerInterface)); if ($exception !== null) { $indexerInterface->expects($this->any())->method('setScheduled')->with(true)->will($this->throwException($exception)); } else { $indexerInterface->expects($this->any())->method('setScheduled')->with(true)->will($this->returnValue(1)); } $this->messageManager->expects($this->any())->method('addSuccess')->will($this->returnValue(1)); if ($exception !== null) { $this->messageManager->expects($this->exactly($expectsExceptionValues[2]))->method('addError')->with($exception->getMessage()); $this->messageManager->expects($this->exactly($expectsExceptionValues[1]))->method('addException')->with($exception, "We couldn't change indexer(s)' mode because of an error."); } } $this->helper->expects($this->any())->method("getUrl")->willReturn("magento.com"); $this->response->expects($this->any())->method("setRedirect")->willReturn(1); $this->model->executeInternal(); }
public function testSyncCustomerGrid() { $gridTable = 'customer_grid_flat'; $customerLogTable = 'customer_log'; $this->indexerRegistry->expects($this->once())->method('get')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID)->willReturn($this->indexer); $this->resource->expects($this->once())->method('getConnection')->willReturn($this->connection); $this->flatScopeResolver->expects($this->once())->method('resolve')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID, [])->willReturn($gridTable); $this->resource->expects($this->exactly(2))->method('getTableName')->willReturnMap([[$gridTable], [$customerLogTable]]); $this->connection->expects($this->exactly(2))->method('select')->willReturn($this->select); $this->select->expects($this->exactly(2))->method('from')->willReturnSelf(); $this->select->expects($this->once())->method('order')->with('last_visit_at DESC')->willReturnSelf(); $this->select->expects($this->once())->method('limit')->with(1)->willReturnSelf(); $this->connection->expects($this->atLeastOnce())->method('query')->with($this->select)->willReturn($this->queryResult); $this->queryResult->expects($this->once())->method('fetchColumn')->willReturn('2015-08-13 10:36:44'); $this->select->expects($this->once())->method('where')->with('last_login_at > ?', '2015-08-13 10:36:44')->willReturnSelf(); $this->queryResult->expects($this->once())->method('fetchAll')->willReturn([['customer_id' => 23], ['customer_id' => 65]]); $this->indexer->expects($this->once())->method('reindexList')->with(['23', '65']); $this->observer->syncCustomerGrid(); }
public function testReindexListForce() { $ids = [1]; $this->_stateMock->expects($this->once())->method('isFlatEnabled')->willReturn(true); $indexerMock = $this->getMockBuilder('Magento\\Indexer\\Model\\Indexer')->disableOriginalConstructor()->getMock(); $this->indexerRegistryMock->expects($this->any())->method('get')->with(Processor::INDEXER_ID)->willReturn($indexerMock); $indexerMock->expects($this->any())->method('isScheduled')->willReturn(true); $indexerMock->expects($this->any())->method('reindexList')->with($ids)->willReturnSelf(); $this->_model->reindexList($ids, true); }
/** * @param string $oldValue * @param string $value * @dataProvider dataProviderProcessValueOff */ public function testProcessValueOff($oldValue, $value) { $this->configMock->expects($this->once())->method('getValue')->with(null, 'default')->will($this->returnValue($oldValue)); $this->model->setValue($value); $this->indexerStateMock->expects($this->never())->method('loadByIndexer'); $this->indexerStateMock->expects($this->never())->method('setStatus'); $this->indexerStateMock->expects($this->never())->method('save'); $this->indexerRegistry->expects($this->once())->method('get')->with('catalog_category_flat')->willReturn($this->flatIndexer); $this->flatIndexer->expects($this->once())->method('setScheduled')->with(false); $this->model->processValue(); }
/** * Update websites for product action * * Allowed types: * - add * - remove * * @param array $productIds * @param array $websiteIds * @param string $type * @return void */ public function updateWebsites($productIds, $websiteIds, $type) { if ($type == 'add') { $this->_productWebsiteFactory->create()->addProducts($websiteIds, $productIds); } elseif ($type == 'remove') { $this->_productWebsiteFactory->create()->removeProducts($websiteIds, $productIds); } $this->setData(['product_ids' => array_unique($productIds), 'website_ids' => $websiteIds, 'action_type' => $type]); $categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID); if (!$categoryIndexer->isScheduled()) { $categoryIndexer->reindexList(array_unique($productIds)); } }
/** * @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(); }
/** * Assign product to given categories * * @param string $productSku * @param \int[] $categoryIds * @return bool */ public function assignProductToCategories($productSku, array $categoryIds) { $product = $this->productRepository->get($productSku); $assignedCategories = $this->productResource->getCategoryIds($product); foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) { $this->categoryLinkRepository->deleteByIds($categoryId, $productSku); } foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) { /** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $categoryProductLink */ $categoryProductLink = $this->productLinkFactory->create(); $categoryProductLink->setSku($productSku); $categoryProductLink->setCategoryId($categoryId); $categoryProductLink->setPosition(0); $this->categoryLinkRepository->save($categoryProductLink); } $productCategoryIndexer = $this->indexerRegistry->get(Indexer\Product\Category::INDEXER_ID); if (!$productCategoryIndexer->isScheduled()) { $productCategoryIndexer->reindexRow($product->getId()); } return true; }
/** * Invalidate indexes by process codes. * * @return $this */ public function invalidateIndex() { $relatedIndexers = $this->_importConfig->getRelatedIndexers($this->getEntity()); if (empty($relatedIndexers)) { return $this; } foreach (array_keys($relatedIndexers) as $indexerId) { try { $indexer = $this->indexerRegistry->get($indexerId); $indexer->invalidate(); } catch (\InvalidArgumentException $e) { } } return $this; }