/**
  * Regenerate indexes for all invalid indexers
  *
  * @return void
  */
 public function reindexAllInvalid()
 {
     foreach (array_keys($this->config->getIndexers()) as $indexerId) {
         $indexer = $this->indexerFactory->create();
         $indexer->load($indexerId);
         if ($indexer->isInvalid()) {
             $indexer->reindexAll();
         }
     }
 }
Example #2
0
 public function setUp()
 {
     $this->_objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
     $this->_indexerMock = $this->getMock('Magento\\Indexer\\Model\\Indexer', array('getId', 'invalidate'), array(), '', false);
     $this->_indexerMock->expects($this->any())->method('getId')->will($this->returnValue(1));
     $this->_indexerFactoryMock = $this->getMock('Magento\\Indexer\\Model\\IndexerFactory', array('create'), array(), '', false);
     $this->_indexerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_indexerMock));
     $this->_stateMock = $this->getMock('Magento\\Catalog\\Model\\Indexer\\Product\\Flat\\State', array('isFlatEnabled'), array(), '', false);
     $this->_model = $this->_objectManager->getObject('Magento\\Catalog\\Model\\Indexer\\Product\\Flat\\Processor', array('indexerFactory' => $this->_indexerFactoryMock, 'state' => $this->_stateMock));
 }
Example #3
0
 /**
  * After change Catalog Search Type process
  *
  * @return $this
  */
 protected function _afterSave()
 {
     $newValue = $this->getValue();
     $oldValue = $this->_config->getValue(Fulltext::XML_PATH_CATALOG_SEARCH_TYPE, $this->getScope(), $this->getScopeId());
     if ($newValue != $oldValue) {
         $this->_catalogSearchFulltext->resetSearchResults();
         $indexer = $this->indexerFactory->create();
         $indexer->load(FulltextIndexer::INDEXER_ID);
         $indexer->invalidate();
     }
     return $this;
 }
Example #4
0
 /**
  * @param string $args
  * @dataProvider runExceptionDataProvider
  */
 public function testRunException($args)
 {
     $indexerMock = $this->getMock('\\Magento\\Indexer\\Model\\Indexer', ['reindexAll', 'turnViewOff', 'turnViewOn'], [], '', false);
     $this->model->setRawArgs(['testme.php', '--', $args]);
     if ($args == 'reindex') {
         $indexerMock->expects($this->any())->method('reindexAll')->will($this->throwException(new \Exception()));
     }
     if ($args == '--mode-realtime') {
         $indexerMock->expects($this->any())->method('turnViewOff')->will($this->throwException(new \Exception()));
     }
     if ($args == 'reindexall') {
         $indexerMock->expects($this->any())->method('reindexAll')->will($this->throwException(new \Magento\Framework\Exception\LocalizedException(__('Something went wrong during reindexing all.'))));
     }
     if ($args == '--mode-schedule') {
         $indexerMock->expects($this->any())->method('turnViewOn')->will($this->throwException(new \Magento\Framework\Exception\LocalizedException(__('Something went wrong during turning view on.'))));
     }
     if ($args == '--reindex=price') {
         $this->indexerFactoryMock->expects($this->once())->method('create')->will($this->returnSelf());
         $this->indexerFactoryMock->expects($this->any())->method('load')->will($this->throwException(new \Exception()));
     } else {
         $this->indexersFactoryMock->expects($this->once())->method('create')->will($this->returnSelf());
         $this->indexersFactoryMock->expects($this->once())->method('getItems')->will($this->returnValue([$indexerMock]));
     }
     ob_start();
     $this->assertInstanceOf('\\Magento\\Indexer\\Model\\Shell', $this->model->run());
     ob_end_clean();
     $this->assertEquals(true, $this->model->hasErrors());
 }
Example #5
0
 public function testReindexAllInvalid()
 {
     $indexers = ['indexer1' => [], 'indexer2' => []];
     $this->configMock->expects($this->once())->method('getIndexers')->will($this->returnValue($indexers));
     $state1Mock = $this->getMock('Magento\\Indexer\\Model\\Indexer\\State', ['getStatus', '__wakeup'], [], '', false);
     $state1Mock->expects($this->once())->method('getStatus')->will($this->returnValue(Indexer\State::STATUS_INVALID));
     $indexer1Mock = $this->getMock('Magento\\Indexer\\Model\\Indexer', ['load', 'getState', 'reindexAll'], [], '', false);
     $indexer1Mock->expects($this->once())->method('getState')->will($this->returnValue($state1Mock));
     $indexer1Mock->expects($this->once())->method('reindexAll');
     $state2Mock = $this->getMock('Magento\\Indexer\\Model\\Indexer\\State', ['getStatus', '__wakeup'], [], '', false);
     $state2Mock->expects($this->once())->method('getStatus')->will($this->returnValue(Indexer\State::STATUS_VALID));
     $indexer2Mock = $this->getMock('Magento\\Indexer\\Model\\Indexer', ['load', 'getState', 'reindexAll'], [], '', false);
     $indexer2Mock->expects($this->never())->method('reindexAll');
     $indexer2Mock->expects($this->once())->method('getState')->will($this->returnValue($state2Mock));
     $this->indexerFactoryMock->expects($this->at(0))->method('create')->will($this->returnValue($indexer1Mock));
     $this->indexerFactoryMock->expects($this->at(1))->method('create')->will($this->returnValue($indexer2Mock));
     $this->model->reindexAllInvalid();
 }
Example #6
0
 /**
  * Regenerate indexes for all invalid indexers
  *
  * @return void
  */
 public function reindexAllInvalid()
 {
     $sharedIndexesComplete = [];
     foreach (array_keys($this->config->getIndexers()) as $indexerId) {
         /** @var Indexer $indexer */
         $indexer = $this->indexerFactory->create();
         $indexer->load($indexerId);
         $indexerConfig = $this->config->getIndexer($indexerId);
         if ($indexer->isInvalid()) {
             // Skip indexers having shared index that was already complete
             if (!in_array($indexerConfig['shared_index'], $sharedIndexesComplete)) {
                 $indexer->reindexAll();
             } else {
                 /** @var \Magento\Indexer\Model\Indexer\State $state */
                 $state = $indexer->getState();
                 $state->setStatus(StateInterface::STATUS_VALID);
                 $state->save();
             }
             if ($indexerConfig['shared_index']) {
                 $sharedIndexesComplete[] = $indexerConfig['shared_index'];
             }
         }
     }
 }
 /**
  * @param \Magento\Indexer\Model\IndexerFactory $indexerFactory
  */
 public function __construct(\Magento\Indexer\Model\IndexerFactory $indexerFactory)
 {
     $this->_indexer = $indexerFactory->create();
 }
Example #8
0
 /**
  * Stock item saving.
  *
  * @return $this
  */
 protected function _saveStockItem()
 {
     $indexer = $this->indexerFactory->create()->load('catalog_product_category');
     /** @var $stockResource \Magento\CatalogInventory\Model\Resource\Stock\Item */
     $stockResource = $this->_stockResItemFac->create();
     $entityTable = $stockResource->getMainTable();
     while ($bunch = $this->_dataSourceModel->getNextBunch()) {
         $stockData = array();
         $productIdsToReindex = array();
         // Format bunch to stock data rows
         foreach ($bunch as $rowNum => $rowData) {
             if (!$this->isRowAllowedToImport($rowData, $rowNum)) {
                 continue;
             }
             // only SCOPE_DEFAULT can contain stock data
             if (self::SCOPE_DEFAULT != $this->getRowScope($rowData)) {
                 continue;
             }
             $row = array();
             $row['product_id'] = $this->_newSku[$rowData[self::COL_SKU]]['entity_id'];
             $productIdsToReindex[] = $row['product_id'];
             $row['stock_id'] = \Magento\CatalogInventory\Model\Stock\Item::DEFAULT_STOCK_ID;
             $stockItemDo = $this->stockItemService->getStockItem($row['product_id']);
             $existStockData = $stockItemDo->__toArray();
             $row = array_merge($this->defaultStockData, array_intersect_key($existStockData, $this->defaultStockData), array_intersect_key($rowData, $this->defaultStockData), $row);
             if ($this->stockItemService->isQty($this->_newSku[$rowData[self::COL_SKU]]['type_id'])) {
                 $row = $this->stockItemService->processIsInStock($row);
                 if ($this->stockItemService->verifyNotification($row['product_id'])) {
                     $row['low_stock_date'] = $this->_localeDate->date(null, null, null, false)->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT);
                 }
                 $row['stock_status_changed_auto'] = (int) (!$this->stockItemService->verifyStock($row['product_id']));
             } else {
                 $row['qty'] = 0;
             }
             $stockData[] = $row;
         }
         // Insert rows
         if (!empty($stockData)) {
             $this->_connection->insertOnDuplicate($entityTable, $stockData);
         }
         if ($productIdsToReindex) {
             $indexer->reindexList($productIdsToReindex);
         }
     }
     return $this;
 }
Example #9
0
 /**
  * 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) {
         $indexer = $this->indexerFactory->create();
         try {
             $indexer->load($indexerId);
             $indexer->invalidate();
         } catch (\InvalidArgumentException $e) {
         }
     }
     return $this;
 }
Example #10
0
 /**
  * @param \Magento\Indexer\Model\IndexerFactory $indexerFactory
  * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $state
  */
 public function __construct(\Magento\Indexer\Model\IndexerFactory $indexerFactory, \Magento\Catalog\Model\Indexer\Product\Flat\State $state)
 {
     $this->_indexer = $indexerFactory->create();
     $this->_state = $state;
 }