diff --git a/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php b/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php index 0013ba1f17f1bb404640c5da4b3f8ddb2d77d632..86ed5c6a10766bb8a3f475e9b2bf70e0d4c9e279 100644 --- a/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php +++ b/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php @@ -59,33 +59,27 @@ class Stock extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\ "bo.parent_id = product.$linkField", [] ); - $this->_addWebsiteJoinToSelect($select, false); $status = new \Zend_Db_Expr( 'MAX(' . $connection->getCheckSql('e.required_options = 0', 'i.stock_status', '0') . ')' ); - $select->columns( - 'website_id', - 'cw' - )->join( + $select->join( ['cis' => $this->getTable('cataloginventory_stock')], '', - ['stock_id'] + ['website_id', 'stock_id'] )->joinLeft( ['bs' => $this->getTable('catalog_product_bundle_selection')], 'bs.option_id = bo.option_id', [] )->joinLeft( ['i' => $idxTable], - 'i.product_id = bs.product_id AND i.website_id = cw.website_id AND i.stock_id = cis.stock_id', + 'i.product_id = bs.product_id AND i.website_id = cis.website_id AND i.stock_id = cis.stock_id', [] )->joinLeft( ['e' => $this->getTable('catalog_product_entity')], 'e.entity_id = bs.product_id', [] - )->where( - 'cw.website_id != 0' )->group( - ['product.entity_id', 'cw.website_id', 'cis.stock_id', 'bo.option_id'] + ['product.entity_id', 'cis.website_id', 'cis.stock_id', 'bo.option_id'] )->columns( ['option_id' => 'bo.option_id', 'status' => $status] ); @@ -118,45 +112,19 @@ class Stock extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false) { $this->_prepareBundleOptionStockData($entityIds, $usePrimaryTable); - $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $connection = $this->getConnection(); - $select = $connection->select()->from( - ['e' => $this->getTable('catalog_product_entity')], - ['entity_id'] - ); - $this->_addWebsiteJoinToSelect($select, true); - $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id'); - $select->columns( - 'cw.website_id' - )->join( - ['cis' => $this->getTable('cataloginventory_stock')], - '', - ['stock_id'] - )->joinLeft( - ['cisi' => $this->getTable('cataloginventory_stock_item')], - 'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id', - [] + $select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable); + $select->reset( + \Magento\Framework\DB\Select::COLUMNS + )->columns( + ['e.entity_id', 'cis.website_id', 'cis.stock_id'] )->joinLeft( ['o' => $this->_getBundleOptionTable()], - 'o.entity_id = e.entity_id AND o.website_id = cw.website_id AND o.stock_id = cis.stock_id', + 'o.entity_id = e.entity_id AND o.website_id = cis.website_id AND o.stock_id = cis.stock_id', [] )->columns( ['qty' => new \Zend_Db_Expr('0')] - )->where( - 'cw.website_id != 0' - )->where( - 'e.type_id = ?', - $this->getTypeId() - )->group( - ['e.entity_id', 'cw.website_id', 'cis.stock_id'] - ); - - // add limitation of status - $condition = $connection->quoteInto( - '=?', - \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED ); - $this->_addAttributeToSelect($select, 'status', "e.$linkField", 'cs.store_id', $condition); $statusExpr = $this->getStatusExpression($connection); $select->columns( diff --git a/app/code/Magento/CatalogInventory/Helper/Stock.php b/app/code/Magento/CatalogInventory/Helper/Stock.php index 599b2d44e2ae38e45e7477a5c45edb426e0a164b..13e311d1fe5aeb4af1282edcf29215fe67570199 100644 --- a/app/code/Magento/CatalogInventory/Helper/Stock.php +++ b/app/code/Magento/CatalogInventory/Helper/Stock.php @@ -12,9 +12,11 @@ use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory; use Magento\CatalogInventory\Model\ResourceModel\Stock\Status; use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection; use Magento\Catalog\Model\Product; +use Magento\CatalogInventory\Api\StockConfigurationInterface; /** * Class Stock + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Stock { @@ -47,6 +49,11 @@ class Stock */ private $stockRegistryProvider; + /** + * @var StockConfigurationInterface + */ + private $stockConfiguration; + /** * @param StoreManagerInterface $storeManager * @param ScopeConfigInterface $scopeConfig @@ -75,7 +82,7 @@ class Stock public function assignStatusToProduct(Product $product, $status = null) { if ($status === null) { - $websiteId = $product->getStore()->getWebsiteId(); + $websiteId = $this->getStockConfiguration()->getDefaultScopeId(); $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $websiteId); $status = $stockStatus->getStockStatus(); } @@ -90,7 +97,7 @@ class Stock */ public function addStockStatusToProducts(AbstractCollection $productCollection) { - $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId(); + $websiteId = $this->getStockConfiguration()->getDefaultScopeId(); foreach ($productCollection as $product) { $productId = $product->getId(); $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId); @@ -161,4 +168,18 @@ class Stock } return $this->stockStatusResource; } + + /** + * @return StockConfigurationInterface + * + * @deprecated + */ + private function getStockConfiguration() + { + if ($this->stockConfiguration === null) { + $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Api\StockConfigurationInterface'); + } + return $this->stockConfiguration; + } } diff --git a/app/code/Magento/CatalogInventory/Model/Config/Backend/Backorders.php b/app/code/Magento/CatalogInventory/Model/Config/Backend/Backorders.php index db78c8e17322bd12129cc488248cd0703bb83781..1915a576939ac0aca869e5072abb3e828820dea6 100644 --- a/app/code/Magento/CatalogInventory/Model/Config/Backend/Backorders.php +++ b/app/code/Magento/CatalogInventory/Model/Config/Backend/Backorders.php @@ -25,7 +25,6 @@ class Backorders extends AbstractValue || $this->getValue() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO ) ) { - $this->stockIndex->rebuild(); $this->_stockIndexerProcessor->markIndexerAsInvalid(); } return parent::afterSave(); diff --git a/app/code/Magento/CatalogInventory/Model/Config/Backend/Managestock.php b/app/code/Magento/CatalogInventory/Model/Config/Backend/Managestock.php index 559e6235951dbae06698ce2def0eccbbd5dfd915..6edab6f3d4eb629e2947676c21d07fe827d52cdd 100644 --- a/app/code/Magento/CatalogInventory/Model/Config/Backend/Managestock.php +++ b/app/code/Magento/CatalogInventory/Model/Config/Backend/Managestock.php @@ -20,7 +20,6 @@ class Managestock extends AbstractValue public function afterSave() { if ($this->isValueChanged()) { - $this->stockIndex->rebuild(); $this->_stockIndexerProcessor->markIndexerAsInvalid(); } return parent::afterSave(); diff --git a/app/code/Magento/CatalogInventory/Model/Configuration.php b/app/code/Magento/CatalogInventory/Model/Configuration.php index 445057c4c08d8174768a9237359bdb4f830066cd..d1509eec3d4adce87cfd4b8ecf75f63485bbeff5 100644 --- a/app/code/Magento/CatalogInventory/Model/Configuration.php +++ b/app/code/Magento/CatalogInventory/Model/Configuration.php @@ -148,7 +148,9 @@ class Configuration implements StockConfigurationInterface */ public function getDefaultScopeId() { - return (int) $this->storeManager->getWebsite()->getId(); + // TODO: should be fixed in MAGETWO-46043 + // "0" is id of admin website, which is used in backend during save entity + return 0; } /** diff --git a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php index 82e9e1b1c94c607103230917753f0aa1c5afaede..df910254082185145212c325568a971a75a9bd85 100644 --- a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php +++ b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php @@ -24,6 +24,7 @@ class AroundProductRepositorySave /** * @var StoreManagerInterface + * @deprecated */ protected $storeManager; @@ -80,7 +81,7 @@ class AroundProductRepositorySave // set fields that the customer should not care about $stockItem->setProductId($product->getId()); - $stockItem->setWebsiteId($this->storeManager->getStore($product->getStoreId())->getWebsiteId()); + $stockItem->setWebsiteId($this->stockConfiguration->getDefaultScopeId()); $this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem); diff --git a/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php b/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php index 3bb9ef4c4d5c6f847d17365df179f54b0b678752..40245db2f3372234ad18a988140195e88a11b971 100644 --- a/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php +++ b/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php @@ -9,6 +9,7 @@ namespace Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock; use Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer; use Magento\CatalogInventory\Model\Stock; use Magento\Framework\DB\Adapter\AdapterInterface; +use Magento\CatalogInventory\Api\StockConfigurationInterface; /** * CatalogInventory Default Stock Status Indexer Resource Model @@ -42,6 +43,11 @@ class DefaultStock extends AbstractIndexer implements StockInterface */ private $queryProcessorComposite; + /** + * @var StockConfigurationInterface + */ + protected $stockConfiguration; + /** * Class constructor * @@ -176,43 +182,29 @@ class DefaultStock extends AbstractIndexer implements StockInterface */ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false) { - $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $connection = $this->getConnection(); $qtyExpr = $connection->getCheckSql('cisi.qty > 0', 'cisi.qty', 0); $select = $connection->select()->from( ['e' => $this->getTable('catalog_product_entity')], ['entity_id'] ); - $this->_addWebsiteJoinToSelect($select, true); - $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id'); - $select->columns('cw.website_id')->join( + $select->join( ['cis' => $this->getTable('cataloginventory_stock')], '', - ['stock_id'] - )->joinLeft( + ['website_id', 'stock_id'] + )->joinInner( ['cisi' => $this->getTable('cataloginventory_stock_item')], 'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id', [] - )->columns(['qty' => new \Zend_Db_Expr('SUM(' . $qtyExpr . ')')]) - ->where('cw.website_id != 0') - ->where('e.type_id = ?', $this->getTypeId()) - ->group(['e.entity_id', 'cw.website_id']); - - // add limitation of status - $condition = $connection->quoteInto( - '=?', - \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED - ); - $this->_addAttributeToSelect( - $select, - 'status', - 'e.' . $metadata->getLinkField(), - 'cs.store_id', - $condition - ); + )->columns( + ['qty' => $qtyExpr] + )->where( + 'cis.website_id = ?', + $this->getStockConfiguration()->getDefaultScopeId() + )->where('e.type_id = ?', $this->getTypeId()) + ->group(['e.entity_id', 'cis.website_id', 'cis.stock_id']); $select->columns(['status' => $this->getStatusExpression($connection, true)]); - if ($entityIds !== null) { $select->where('e.entity_id IN(?)', $entityIds); } @@ -325,6 +317,20 @@ class DefaultStock extends AbstractIndexer implements StockInterface return $statusExpr; } + /** + * @return StockConfigurationInterface + * + * @deprecated + */ + protected function getStockConfiguration() + { + if ($this->stockConfiguration === null) { + $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Api\StockConfigurationInterface'); + } + return $this->stockConfiguration; + } + /** * @return QueryProcessorComposite */ diff --git a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php index f4877e6a3de68fd72a6550d1b6bf6c5b611593bb..17a3e0c9a99494de5efdb4f6825c33fae9e41e54 100644 --- a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php +++ b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php @@ -75,6 +75,7 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme /** * @var StoreManagerInterface + * @deprecated */ protected $storeManager; @@ -189,11 +190,12 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme * Set items out of stock basing on their quantities and config settings * * @param string|int $website + * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @return void */ public function updateSetOutOfStock($website = null) { - $websiteId = $this->storeManager->getWebsite($website)->getId(); + $websiteId = $this->stockConfiguration->getDefaultScopeId(); $this->_initConfig(); $connection = $this->getConnection(); $values = ['is_in_stock' => 0, 'stock_status_changed_auto' => 1]; @@ -223,11 +225,12 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme * Set items in stock basing on their quantities and config settings * * @param int|string $website + * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @return void */ public function updateSetInStock($website) { - $websiteId = $this->storeManager->getWebsite($website)->getId(); + $websiteId = $this->stockConfiguration->getDefaultScopeId(); $this->_initConfig(); $connection = $this->getConnection(); $values = ['is_in_stock' => 1]; @@ -255,11 +258,12 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme * Update items low stock date basing on their quantities and config settings * * @param int|string $website + * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @return void */ public function updateLowStockDate($website) { - $websiteId = $this->storeManager->getWebsite($website)->getId(); + $websiteId = $this->stockConfiguration->getDefaultScopeId(); $this->_initConfig(); $connection = $this->getConnection(); diff --git a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item.php b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item.php index d08bd31df9eeb517f3577ce3aff34de85c42479f..32d21f388f03e30510dfb1179fdf335654b4c0a4 100644 --- a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item.php +++ b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item.php @@ -57,13 +57,13 @@ class Item extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb * * @param \Magento\CatalogInventory\Api\Data\StockItemInterface $item * @param int $productId - * @param int $websiteId + * @param int $stockId * @return $this */ - public function loadByProductId(\Magento\CatalogInventory\Api\Data\StockItemInterface $item, $productId, $websiteId) + public function loadByProductId(\Magento\CatalogInventory\Api\Data\StockItemInterface $item, $productId, $stockId) { - $select = $this->_getLoadSelect('product_id', $productId, $item)->where('website_id = :website_id'); - $data = $this->getConnection()->fetchRow($select, [':website_id' => $websiteId]); + $select = $this->_getLoadSelect('product_id', $productId, $item)->where('stock_id = :stock_id'); + $data = $this->getConnection()->fetchRow($select, [':stock_id' => $stockId]); if ($data) { $item->setData($data); } else { diff --git a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item/StockItemCriteriaMapper.php b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item/StockItemCriteriaMapper.php index 551f5e0c3b8fffd04eaac0e5c18bd1a1977990d6..d1e038071ecc52a7adf9f5452076e95cd06358e5 100644 --- a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item/StockItemCriteriaMapper.php +++ b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item/StockItemCriteriaMapper.php @@ -13,6 +13,7 @@ use Magento\Framework\Data\ObjectFactory; use Magento\Store\Model\StoreManagerInterface; use Psr\Log\LoggerInterface as Logger; use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; +use Magento\CatalogInventory\Api\StockConfigurationInterface; /** * Interface StockItemCriteriaMapper @@ -20,6 +21,17 @@ use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; */ class StockItemCriteriaMapper extends GenericMapper { + /** + * @var StockConfigurationInterface + */ + private $stockConfiguration; + + /** + * @var StoreManagerInterface + * @deprecated + */ + private $storeManager; + /** * @param Logger $logger * @param FetchStrategyInterface $fetchStrategy @@ -107,10 +119,11 @@ class StockItemCriteriaMapper extends GenericMapper /** * @inheritdoc + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function mapStockStatus($storeId = null) { - $websiteId = $this->storeManager->getStore($storeId)->getWebsiteId(); + $websiteId = $this->getStockConfiguration()->getDefaultScopeId(); $this->getSelect()->joinLeft( ['status_table' => $this->getTable('cataloginventory_stock_status')], 'main_table.product_id=status_table.product_id' . @@ -149,4 +162,18 @@ class StockItemCriteriaMapper extends GenericMapper } $this->addFieldToFilter('main_table.qty', [$methods[$comparisonMethod] => $qty]); } + + /** + * @return StockConfigurationInterface + * + * @deprecated + */ + private function getStockConfiguration() + { + if ($this->stockConfiguration === null) { + $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Api\StockConfigurationInterface'); + } + return $this->stockConfiguration; + } } diff --git a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Status.php b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Status.php index 75b325458ccaa2c9c4bfb3c0c16ffc381c7cab90..f7410de156fb42e4549b3d1910d5c5d9d5fb0d27 100644 --- a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Status.php +++ b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Status.php @@ -6,9 +6,11 @@ namespace Magento\CatalogInventory\Model\ResourceModel\Stock; use Magento\CatalogInventory\Model\Stock; +use Magento\CatalogInventory\Api\StockConfigurationInterface; /** * CatalogInventory Stock Status per website Resource Model + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { @@ -16,6 +18,7 @@ class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb * Store model manager * * @var \Magento\Store\Model\StoreManagerInterface + * @deprecated */ protected $_storeManager; @@ -31,6 +34,11 @@ class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ protected $eavConfig; + /** + * @var StockConfigurationInterface + */ + private $stockConfiguration; + /** * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Store\Model\StoreManagerInterface $storeManager @@ -190,11 +198,12 @@ class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb * * @param \Magento\Framework\DB\Select $select * @param \Magento\Store\Model\Website $website + * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @return Status */ public function addStockStatusToSelect(\Magento\Framework\DB\Select $select, \Magento\Store\Model\Website $website) { - $websiteId = $website->getId(); + $websiteId = $this->getStockConfiguration()->getDefaultScopeId(); $select->joinLeft( ['stock_status' => $this->getMainTable()], 'e.entity_id = stock_status.product_id AND stock_status.website_id=' . $websiteId, @@ -211,7 +220,7 @@ class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ public function addStockDataToCollection($collection, $isFilterInStock) { - $websiteId = $this->_storeManager->getStore($collection->getStoreId())->getWebsiteId(); + $websiteId = $this->getStockConfiguration()->getDefaultScopeId(); $joinCondition = $this->getConnection()->quoteInto( 'e.entity_id = stock_status_index.product_id' . ' AND stock_status_index.website_id = ?', $websiteId @@ -245,7 +254,7 @@ class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ public function addIsInStockFilterToCollection($collection) { - $websiteId = $this->_storeManager->getStore($collection->getStoreId())->getWebsiteId(); + $websiteId = $this->getStockConfiguration()->getDefaultScopeId(); $joinCondition = $this->getConnection()->quoteInto( 'e.entity_id = stock_status_index.product_id' . ' AND stock_status_index.website_id = ?', $websiteId @@ -325,4 +334,18 @@ class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb } return $statuses; } + + /** + * @return StockConfigurationInterface + * + * @deprecated + */ + private function getStockConfiguration() + { + if ($this->stockConfiguration === null) { + $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Api\StockConfigurationInterface'); + } + return $this->stockConfiguration; + } } diff --git a/app/code/Magento/CatalogInventory/Model/StockIndex.php b/app/code/Magento/CatalogInventory/Model/StockIndex.php index a30bc203c638f251e167fa5f88eb6d9eaca6ef21..226293c8205e67acd32953463485713d64729128 100644 --- a/app/code/Magento/CatalogInventory/Model/StockIndex.php +++ b/app/code/Magento/CatalogInventory/Model/StockIndex.php @@ -78,6 +78,7 @@ class StockIndex implements StockIndexInterface * * @param int $productId * @param int $scopeId + * @deprecated * @return true * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ @@ -108,6 +109,7 @@ class StockIndex implements StockIndexInterface * * @param int $productId * @param int $websiteId + * @deprecated * @return void */ public function updateProductStockStatus($productId, $websiteId) diff --git a/app/code/Magento/CatalogInventory/Model/StockState.php b/app/code/Magento/CatalogInventory/Model/StockState.php index a5f60dce93ca7b45d3d4c4aa8783df95e72a7d80..0c8b84d473e45a47ab2ceaf69cf5089f5818a9b3 100644 --- a/app/code/Magento/CatalogInventory/Model/StockState.php +++ b/app/code/Magento/CatalogInventory/Model/StockState.php @@ -52,9 +52,9 @@ class StockState implements StockStateInterface */ public function verifyStock($productId, $scopeId = null) { - if ($scopeId === null) { + // if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + // } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->verifyStock($stockItem); } @@ -66,9 +66,9 @@ class StockState implements StockStateInterface */ public function verifyNotification($productId, $scopeId = null) { - if ($scopeId === null) { + // if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + // } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->verifyNotification($stockItem); } @@ -84,9 +84,9 @@ class StockState implements StockStateInterface */ public function checkQty($productId, $qty, $scopeId = null) { - if ($scopeId === null) { + // if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + // } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->checkQty($stockItem, $qty); } @@ -102,9 +102,9 @@ class StockState implements StockStateInterface */ public function suggestQty($productId, $qty, $scopeId = null) { - if ($scopeId === null) { + // if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + // } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->suggestQty($stockItem, $qty); } @@ -118,9 +118,9 @@ class StockState implements StockStateInterface */ public function getStockQty($productId, $scopeId = null) { - if ($scopeId === null) { + // if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + // } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->getStockQty($stockItem); } @@ -133,9 +133,9 @@ class StockState implements StockStateInterface */ public function checkQtyIncrements($productId, $qty, $websiteId = null) { - if ($websiteId === null) { + // if ($websiteId === null) { $websiteId = $this->stockConfiguration->getDefaultScopeId(); - } + // } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId); return $this->stockStateProvider->checkQtyIncrements($stockItem, $qty); } @@ -150,9 +150,9 @@ class StockState implements StockStateInterface */ public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $scopeId = null) { - if ($scopeId === null) { + // if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + // } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty); } diff --git a/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php b/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php index d700b296871aa2faf5fcc62a5ef3b6822c2cbe24..72c9b35394717deb7b22b24fa89236d5e559e1bd 100644 --- a/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php +++ b/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php @@ -17,6 +17,7 @@ class SaveInventoryDataObserver implements ObserverInterface { /** * @var StockIndexInterface + * @deprecated */ protected $stockIndex; @@ -96,14 +97,7 @@ class SaveInventoryDataObserver implements ObserverInterface public function execute(EventObserver $observer) { $product = $observer->getEvent()->getProduct(); - if ($product->getStockData() === null) { - if ($product->getIsChangedWebsites() || $product->dataHasChangedFor('status')) { - $this->stockIndex->rebuild( - $product->getId(), - $product->getStore()->getWebsiteId() - ); - } return $this; } diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php index 1f1c7a98f0907a6cea0b863477a6f57af1200f7d..df5ea40e933075bb07dd398ab3514e2cf18960d6 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php @@ -37,6 +37,11 @@ class StockTest extends \PHPUnit_Framework_TestCase */ protected $statusFactoryMock; + /** + * @var \Magento\CatalogInventory\Api\StockConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stockConfiguration; + protected function setUp() { $this->stockRegistryProviderMock = $this->getMockBuilder( @@ -55,12 +60,20 @@ class StockTest extends \PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); + $this->stockConfiguration = $this->getMockBuilder('Magento\CatalogInventory\Api\StockConfigurationInterface') + ->getMock(); $this->stock = new Stock( $this->storeManagerMock, $this->scopeConfigMock, $this->statusFactoryMock, $this->stockRegistryProviderMock ); + + // Todo: \Magento\Framework\TestFramework\Unit\Helper\ObjectManager to do this automatically (MAGETWO-49793) + $reflection = new \ReflectionClass(get_class($this->stock)); + $reflectionProperty = $reflection->getProperty('stockConfiguration'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($this->stock, $this->stockConfiguration); } public function testAssignStatusToProduct() @@ -77,19 +90,12 @@ class StockTest extends \PHPUnit_Framework_TestCase $this->stockRegistryProviderMock->expects($this->any()) ->method('getStockStatus') ->willReturn($stockStatusMock); - $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') - ->disableOriginalConstructor() - ->getMock(); - $storeMock->expects($this->once()) - ->method('getWebsiteId') - ->willReturn($websiteId); + $this->stockConfiguration->expects($this->once())->method('getDefaultScopeId')->willReturn($websiteId); + $productMock = $this->getMockBuilder('Magento\Catalog\Model\Product') ->disableOriginalConstructor() - ->setMethods(['setIsSalable', 'getStore', 'getId']) + ->setMethods(['setIsSalable', 'getId']) ->getMock(); - $productMock->expects($this->any()) - ->method('getStore') - ->willReturn($storeMock); $productMock->expects($this->once()) ->method('setIsSalable') ->with($status); @@ -134,12 +140,6 @@ class StockTest extends \PHPUnit_Framework_TestCase $productCollectionMock->expects($this->any()) ->method('getIterator') ->willReturn($iteratorMock); - $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') - ->disableOriginalConstructor() - ->getMock(); - $this->storeManagerMock->expects($this->once()) - ->method('getStore') - ->willReturn($storeMock); $this->stockRegistryProviderMock->expects($this->once()) ->method('getStockStatus') ->withAnyParameters() diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Config/Backend/ManagestockTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Config/Backend/ManagestockTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b180ba0e035173514ebb444bec4e2c4487567577 --- /dev/null +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Config/Backend/ManagestockTest.php @@ -0,0 +1,53 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\CatalogInventory\Test\Unit\Model\Config\Backend; + +class ManagestockTest extends \PHPUnit_Framework_TestCase +{ + /** @var \Magento\CatalogInventory\Model\Indexer\Stock\Processor|\PHPUnit_Framework_MockObject_MockObject */ + protected $stockIndexerProcessor; + + /** @var \Magento\CatalogInventory\Model\Config\Backend\Managestock */ + protected $model; + + protected function setUp() + { + $this->stockIndexerProcessor = $this->getMockBuilder('Magento\CatalogInventory\Model\Indexer\Stock\Processor') + ->disableOriginalConstructor() + ->getMock(); + $this->model = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( + 'Magento\CatalogInventory\Model\Config\Backend\Managestock', + [ + 'stockIndexerProcessor' => $this->stockIndexerProcessor, + ] + ); + } + + /** + * Data provider for testSaveAndRebuildIndex + * @return array + */ + public function saveAndRebuildIndexDataProvider() + { + return [ + [1, 1], + [0, 0], + ]; + } + + /** + * @dataProvider saveAndRebuildIndexDataProvider + * + * @param int $newStockValue new value for stock status + * @param int $callCount count matcher + */ + public function testSaveAndRebuildIndex($newStockValue, $callCount) + { + $this->model->setValue($newStockValue); + $this->stockIndexerProcessor->expects($this->exactly($callCount))->method('markIndexerAsInvalid'); + $this->model->afterSave(); + } +} diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/ConfigurationTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/ConfigurationTest.php index 90b05b2d10649ce527debbea0f368d5e385f55bc..a0b2cc7225c9efe60c158b0e71946a22d5cb5512 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/ConfigurationTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/ConfigurationTest.php @@ -61,17 +61,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase public function testGetDefaultWebsiteId() { - $id = 1; - $websiteMock = $this->getMockBuilder('Magento\Store\Model\Website') - ->disableOriginalConstructor() - ->getMock(); - $websiteMock->expects($this->once()) - ->method('getId') - ->willReturn($id); - $this->storeManagerMock->expects($this->once()) - ->method('getWebsite') - ->willReturn($websiteMock); - $this->assertEquals($id, $this->model->getDefaultScopeId()); + $this->assertEquals(0, $this->model->getDefaultScopeId()); } public function testGetIsQtyTypeIds() diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php index 534960b4397833d268c56ab62c9a45d46cc997f9..cb26fda9be79e928dac5585694e71778661bf0f6 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php @@ -154,11 +154,7 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase ->method('getStockItem') ->willReturn(null); - $storeMock = $this->getMockBuilder('\Magento\Store\Model\Store') - ->disableOriginalConstructor()->getMock(); - $storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); - $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock); - + $this->stockConfiguration->expects($this->once())->method('getDefaultScopeId')->willReturn(1); $this->stockRegistry->expects($this->once())->method('getStockItem')->willReturn($this->stockItem); $this->stockRegistry->expects($this->once())->method('updateStockItemBySku'); @@ -180,11 +176,10 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase public function testAroundSave() { $productId = 5494; - $websiteId = 1; $storeId = 2; $sku = 'my product that needs saving'; $defaultScopeId = 100; - $this->stockConfiguration->expects($this->once()) + $this->stockConfiguration->expects($this->exactly(2)) ->method('getDefaultScopeId') ->willReturn($defaultScopeId); $this->stockRegistry->expects($this->once()) @@ -209,17 +204,12 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase ->method('getStockItem') ->willReturn($storedStockItem); - $storeMock = $this->getMockBuilder('\Magento\Store\Model\Store') - ->disableOriginalConstructor()->getMock(); - $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId); - $this->storeManager->expects($this->once())->method('getStore')->with($storeId)->willReturn($storeMock); - $this->product->expects(($this->exactly(2)))->method('getId')->willReturn($productId); $this->product->expects(($this->atLeastOnce()))->method('getStoreId')->willReturn($storeId); $this->product->expects($this->atLeastOnce())->method('getSku')->willReturn($sku); $this->stockItem->expects($this->once())->method('setProductId')->with($productId); - $this->stockItem->expects($this->once())->method('setWebsiteId')->with($websiteId); + $this->stockItem->expects($this->once())->method('setWebsiteId')->with($defaultScopeId); $this->stockRegistry->expects($this->once()) ->method('updateStockItemBySku') diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Observer/SaveInventoryDataObserverTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Observer/SaveInventoryDataObserverTest.php deleted file mode 100644 index 1b8347539485d11ff30b22ea3803201cf08004f2..0000000000000000000000000000000000000000 --- a/app/code/Magento/CatalogInventory/Test/Unit/Observer/SaveInventoryDataObserverTest.php +++ /dev/null @@ -1,95 +0,0 @@ -<?php -/** - * Copyright © 2016 Magento. All rights reserved. - * See COPYING.txt for license details. - */ -namespace Magento\CatalogInventory\Test\Unit\Observer; - -use Magento\CatalogInventory\Observer\SaveInventoryDataObserver; - -class SaveInventoryDataObserverTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var SaveInventoryDataObserver - */ - protected $observer; - - /** - * @var \Magento\CatalogInventory\Api\StockIndexInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $stockIndex; - - /** - * @var \Magento\Framework\Event|\PHPUnit_Framework_MockObject_MockObject - */ - protected $event; - - /** - * @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject - */ - protected $eventObserver; - - protected function setUp() - { - $this->stockIndex = $this->getMockForAbstractClass( - 'Magento\CatalogInventory\Api\StockIndexInterface', - ['rebuild'], - '', - false - ); - - $this->event = $this->getMockBuilder('Magento\Framework\Event') - ->disableOriginalConstructor() - ->setMethods(['getProduct']) - ->getMock(); - - $this->eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') - ->disableOriginalConstructor() - ->setMethods(['getEvent']) - ->getMock(); - - $this->eventObserver->expects($this->atLeastOnce()) - ->method('getEvent') - ->will($this->returnValue($this->event)); - - $this->observer = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( - 'Magento\CatalogInventory\Observer\SaveInventoryDataObserver', - [ - 'stockIndex' => $this->stockIndex, - ] - ); - } - - public function testSaveInventoryData() - { - $productId = 4; - $websiteId = 5; - $stockData = null; - $websitesChanged = true; - $statusChanged = true; - - $store = $this->getMock('Magento\Store\Model\Store', ['getWebsiteId'], [], '', false); - $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); - - $product = $this->getMock( - 'Magento\Catalog\Model\Product', - ['getStockData', 'getIsChangedWebsites', 'dataHasChangedFor', 'getId', 'getStore'], - [], - '', - false - ); - $product->expects($this->once())->method('getStockData')->will($this->returnValue($stockData)); - $product->expects($this->any())->method('getIsChangedWebsites')->will($this->returnValue($websitesChanged)); - $product->expects($this->any())->method('dataHasChangedFor')->will($this->returnValue($statusChanged)); - $product->expects($this->once())->method('getId')->will($this->returnValue($productId)); - $product->expects($this->once())->method('getStore')->will($this->returnValue($store)); - - $this->stockIndex->expects($this->once())->method('rebuild')->will($this->returnValue(true)); - - $this->event->expects($this->once()) - ->method('getProduct') - ->will($this->returnValue($product)); - - $this->observer->execute($this->eventObserver); - } -} diff --git a/app/code/Magento/CatalogSearch/Model/Search/IndexBuilder.php b/app/code/Magento/CatalogSearch/Model/Search/IndexBuilder.php index 72134fb16912939f5a80e42e8803fd7120e6f3a5..31ce6d550b116455052cf674e99155db3e3f7af2 100644 --- a/app/code/Magento/CatalogSearch/Model/Search/IndexBuilder.php +++ b/app/code/Magento/CatalogSearch/Model/Search/IndexBuilder.php @@ -6,20 +6,18 @@ namespace Magento\CatalogSearch\Model\Search; -use Magento\CatalogSearch\Model\Search\TableMapper; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\Select; use Magento\Framework\Search\Adapter\Mysql\ConditionManager; use Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface; use Magento\Framework\Search\Request\Dimension; -use Magento\Framework\Search\Request\Filter\BoolExpression; -use Magento\Framework\Search\Request\FilterInterface; -use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface; use Magento\Framework\Search\RequestInterface; use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; +use Magento\CatalogInventory\Api\StockConfigurationInterface; +use Magento\CatalogInventory\Model\Stock; use Magento\Framework\App\ScopeResolverInterface; /** @@ -40,6 +38,7 @@ class IndexBuilder implements IndexBuilderInterface /** * @var StoreManagerInterface + * @deprecated */ private $storeManager; @@ -63,6 +62,11 @@ class IndexBuilder implements IndexBuilderInterface */ private $dimensionScopeResolver; + /** + * @var StockConfigurationInterface + */ + private $stockConfiguration; + /** * @param \Magento\Framework\App\ResourceConnection $resource * @param ScopeConfigInterface $config @@ -124,16 +128,30 @@ class IndexBuilder implements IndexBuilderInterface 'search_index.entity_id = stock_index.product_id' . $this->resource->getConnection()->quoteInto( ' AND stock_index.website_id = ?', - $this->storeManager->getWebsite()->getId() + $this->getStockConfiguration()->getDefaultScopeId() ), [] ); - $select->where('stock_index.stock_status = ?', 1); + $select->where('stock_index.stock_status = ?', Stock::DEFAULT_STOCK_ID); } return $select; } + /** + * @return StockConfigurationInterface + * + * @deprecated + */ + private function getStockConfiguration() + { + if ($this->stockConfiguration === null) { + $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Api\StockConfigurationInterface'); + } + return $this->stockConfiguration; + } + /** * Add filtering by dimensions * diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/IndexBuilderTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/IndexBuilderTest.php index 73dfd3867b0dcaf870794e6195d508309f440600..32176594936b2d996b1e14c758cf0671c76a73f5 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/IndexBuilderTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/IndexBuilderTest.php @@ -40,6 +40,9 @@ class IndexBuilderTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Search\Model\IndexScopeResolver|\PHPUnit_Framework_MockObject_MockObject */ private $resource; + /** @var \Magento\CatalogInventory\Api\StockConfigurationInterface|MockObject */ + private $stockConfiguration; + /** * @var \Magento\CatalogSearch\Model\Search\IndexBuilder */ @@ -55,6 +58,10 @@ class IndexBuilderTest extends \PHPUnit_Framework_TestCase */ private $scopeInterface; + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @return void + */ protected function setUp() { $this->select = $this->getMockBuilder('\Magento\Framework\DB\Select') @@ -139,6 +146,9 @@ class IndexBuilderTest extends \PHPUnit_Framework_TestCase '', false ); + $this->stockConfiguration = $this + ->getMockBuilder('\Magento\CatalogInventory\Api\StockConfigurationInterface') + ->getMock(); $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->target = $objectManagerHelper->getObject( @@ -153,6 +163,12 @@ class IndexBuilderTest extends \PHPUnit_Framework_TestCase 'dimensionScopeResolver' => $this->dimensionScopeResolver ] ); + + // Todo: \Magento\Framework\TestFramework\Unit\Helper\ObjectManager to do this automatically (MAGETWO-49793) + $reflection = new \ReflectionClass(get_class($this->target)); + $reflectionProperty = $reflection->getProperty('stockConfiguration'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($this->target, $this->stockConfiguration); } public function testBuildWithOutOfStock() @@ -198,15 +214,13 @@ class IndexBuilderTest extends \PHPUnit_Framework_TestCase $this->mockBuild($index, $tableSuffix, false); + $this->stockConfiguration->expects($this->once())->method('getDefaultScopeId')->willReturn(1); $this->config->expects($this->once()) ->method('isSetFlag') ->with('cataloginventory/options/show_out_of_stock') ->will($this->returnValue(false)); $this->connection->expects($this->once())->method('quoteInto') ->with(' AND stock_index.website_id = ?', 1)->willReturn(' AND stock_index.website_id = 1'); - $website = $this->getMockBuilder('Magento\Store\Model\Website')->disableOriginalConstructor()->getMock(); - $website->expects($this->once())->method('getId')->willReturn(1); - $this->storeManager->expects($this->once())->method('getWebsite')->willReturn($website); $this->select->expects($this->at(2)) ->method('where') ->with('(someName=someValue)') diff --git a/app/code/Magento/CatalogSearch/composer.json b/app/code/Magento/CatalogSearch/composer.json index 709009300b724a41dbca6c80773684af9c15e91e..860d5080b9bdffd0282c5b989c8373b886d498af 100644 --- a/app/code/Magento/CatalogSearch/composer.json +++ b/app/code/Magento/CatalogSearch/composer.json @@ -11,6 +11,7 @@ "magento/module-eav": "100.0.*", "magento/module-backend": "100.0.*", "magento/module-theme": "100.0.*", + "magento/module-catalog-inventory": "100.0.*", "magento/framework": "100.0.*" }, "type": "magento2-module", diff --git a/app/code/Magento/CatalogWidget/Model/Rule/Condition/Combine.php b/app/code/Magento/CatalogWidget/Model/Rule/Condition/Combine.php index 386e77580fa4abab025f3119b056f08b2be88084..6f2cd40e97f7ad19fb5cd217881403a11a594268 100644 --- a/app/code/Magento/CatalogWidget/Model/Rule/Condition/Combine.php +++ b/app/code/Magento/CatalogWidget/Model/Rule/Condition/Combine.php @@ -71,7 +71,9 @@ class Combine extends \Magento\Rule\Model\Condition\Combine */ public function collectValidatedAttributes($productCollection) { + $alias = array_keys($productCollection->getSelect()->getPart('from'))[0]; foreach ($this->getConditions() as $condition) { + $condition->setData('attribute', $alias . '.' . $condition->getData('attribute')); $condition->addToCollection($productCollection); } return $this; diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/Rule/Condition/CombineTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/Rule/Condition/CombineTest.php index 34c14626d8bf7423d435c62e952c663a8b52f9ae..b0a3fdab8eb32c6c97ef798a94390f7a942f04a2 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/Rule/Condition/CombineTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/Rule/Condition/CombineTest.php @@ -73,8 +73,13 @@ class CombineTest extends \PHPUnit_Framework_TestCase public function testCollectValidatedAttributes() { $collection = $this->getMockBuilder('\Magento\Catalog\Model\ResourceModel\Product\Collection') - ->disableOriginalConstructor() + ->disableOriginalConstructor()->setMethods(['getSelect']) + ->getMock(); + $select = $this->getMockBuilder('\Magento\Framework\DB\Select') + ->disableOriginalConstructor()->setMethods(['getPart']) ->getMock(); + $select->expects($this->any())->method('getPart')->with('from')->willReturn(['alias_table' => 'table_name']); + $collection->expects($this->any())->method('getSelect')->willReturn($select); $condition = $this->getMockBuilder('Magento\CatalogWidget\Model\Rule\Condition\Combine') ->disableOriginalConstructor()->setMethods(['addToCollection']) ->getMock(); diff --git a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php index 551df288ebcb03e2941a618ec28e667265b0af99..6f2b989e5e9bfaa9fa91643b1958be6a99c0fdc1 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php +++ b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php @@ -28,19 +28,11 @@ class Configurable extends \Magento\CatalogInventory\Model\ResourceModel\Indexer $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $connection = $this->getConnection(); $idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable(); - $select = $connection->select()->from(['e' => $this->getTable('catalog_product_entity')], ['entity_id']); - $this->_addWebsiteJoinToSelect($select, true); - $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id'); - $select->columns( - 'cw.website_id' - )->join( - ['cis' => $this->getTable('cataloginventory_stock')], - '', - ['stock_id'] - )->joinLeft( - ['cisi' => $this->getTable('cataloginventory_stock_item')], - 'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id', - [] + $select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable); + $select->reset( + \Magento\Framework\DB\Select::COLUMNS + )->columns( + ['e.entity_id', 'cis.website_id', 'cis.stock_id'] )->joinLeft( ['l' => $this->getTable('catalog_product_super_link')], 'l.parent_id = e.' . $metadata->getLinkField(), @@ -51,24 +43,14 @@ class Configurable extends \Magento\CatalogInventory\Model\ResourceModel\Indexer [] )->joinLeft( ['i' => $idxTable], - 'i.product_id = l.product_id AND cw.website_id = i.website_id AND cis.stock_id = i.stock_id', + 'i.product_id = l.product_id AND cis.website_id = i.website_id AND cis.stock_id = i.stock_id', [] )->columns( ['qty' => new \Zend_Db_Expr('0')] - )->where( - 'cw.website_id != 0' - )->where( - 'e.type_id = ?', - $this->getTypeId() - )->group( - ['e.entity_id', 'cw.website_id', 'cis.stock_id'] ); - $psExpr = $this->_addAttributeToSelect($select, 'status', 'e.' . $metadata->getLinkField(), 'cs.store_id'); - $psCond = $connection->quoteInto($psExpr . '=?', ProductStatus::STATUS_ENABLED); - $statusExpr = $this->getStatusExpression($connection); - $optExpr = $connection->getCheckSql("{$psCond} AND le.required_options = 0", 'i.stock_status', 0); + $optExpr = $connection->getCheckSql("le.required_options = 0", 'i.stock_status', 0); $stockStatusExpr = $connection->getLeastSql(["MAX({$optExpr})", "MIN({$statusExpr})"]); $select->columns(['status' => $stockStatusExpr]); diff --git a/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php b/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php index 9587a195b5e7c67d06f8a76b89d6957b5d62c403..8a3f0f1d0e900a5f4d3283076bb6ed8831c9f47a 100644 --- a/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php +++ b/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php @@ -26,23 +26,12 @@ class Grouped extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stoc { $connection = $this->getConnection(); $idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable(); - $select = $connection->select()->from( - ['e' => $this->getTable('catalog_product_entity')], - ['entity_id'] - ); - $this->_addWebsiteJoinToSelect($select, true); - $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id'); $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); - $select->columns( - 'cw.website_id' - )->join( - ['cis' => $this->getTable('cataloginventory_stock')], - '', - ['stock_id'] - )->joinLeft( - ['cisi' => $this->getTable('cataloginventory_stock_item')], - 'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id', - [] + $select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable); + $select->reset( + \Magento\Framework\DB\Select::COLUMNS + )->columns( + ['e.entity_id', 'cis.website_id', 'cis.stock_id'] )->joinLeft( ['l' => $this->getTable('catalog_product_link')], 'e.' . $metadata->getLinkField() . ' = l.product_id AND l.link_type_id=' . @@ -54,31 +43,15 @@ class Grouped extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stoc [] )->joinLeft( ['i' => $idxTable], - 'i.product_id = l.linked_product_id AND cw.website_id = i.website_id AND cis.stock_id = i.stock_id', + 'i.product_id = l.linked_product_id AND cis.website_id = i.website_id AND cis.stock_id = i.stock_id', [] )->columns( ['qty' => new \Zend_Db_Expr('0')] - )->where( - 'cw.website_id != 0' - )->where( - 'e.type_id = ?', - $this->getTypeId() - )->group( - ['e.entity_id', 'cw.website_id', 'cis.stock_id'] - ); - - // add limitation of status - $productStatusExpr = $this->_addAttributeToSelect( - $select, - 'status', - 'e.' . $metadata->getLinkField(), - 'cs.store_id' ); - $productStatusCond = $connection->quoteInto($productStatusExpr . '=?', ProductStatus::STATUS_ENABLED); $statusExpression = $this->getStatusExpression($connection); - $optExpr = $connection->getCheckSql("{$productStatusCond} AND le.required_options = 0", 'i.stock_status', 0); + $optExpr = $connection->getCheckSql("le.required_options = 0", 'i.stock_status', 0); $stockStatusExpr = $connection->getLeastSql(["MAX({$optExpr})", "MIN({$statusExpression})"]); $select->columns(['status' => $stockStatusExpr]); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/Layer/Filter/Price/_files/products_advanced.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/Layer/Filter/Price/_files/products_advanced.php index 8146a04b34398a758c815c787feb9cfe047c786d..2ae37ed9b71946b9924064a3cd1fa0a30e6b3c88 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/Layer/Filter/Price/_files/products_advanced.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/Layer/Filter/Price/_files/products_advanced.php @@ -80,6 +80,11 @@ foreach ($prices as $price) { 'simple-' . $productId )->setPrice( $price + )->setStockData( + [ + 'qty' => 100, + 'is_in_stock' => 1, + ] )->setWeight( 18 )->setCategoryIds( diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual.php index 4992805824538bc35ec803abea5056b6456bb134..7ec85f110a5b58bc9c721593e50e2c6fb4fbb682 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual.php @@ -16,4 +16,9 @@ $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL) ->setTaxClassId(0) ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) - ->save(); + ->setStockData( + [ + 'qty' => 100, + 'is_in_stock' => 1, + ] + )->save(); diff --git a/dev/tests/integration/testsuite/Magento/CatalogInventory/Model/Config/Backend/ManagestockTest.php b/dev/tests/integration/testsuite/Magento/CatalogInventory/Model/Config/Backend/ManagestockTest.php deleted file mode 100644 index 12aae4429203d85c9095ba607dfe25f620852353..0000000000000000000000000000000000000000 --- a/dev/tests/integration/testsuite/Magento/CatalogInventory/Model/Config/Backend/ManagestockTest.php +++ /dev/null @@ -1,71 +0,0 @@ -<?php -/** - * Copyright © 2016 Magento. All rights reserved. - * See COPYING.txt for license details. - */ -namespace Magento\CatalogInventory\Model\Config\Backend; - -use Magento\TestFramework\Helper\Bootstrap as Bootstrap; - -/** - * Class ManagestockTest - */ -class ManagestockTest extends \PHPUnit_Framework_TestCase -{ - /** - * Data provider for testSaveAndRebuildIndex - * @return array - */ - public function saveAndRebuildIndexDataProvider() - { - return [ - [1, 1], - [0, 0], - ]; - } - - /** - * Test rebuild stock indexer on stock status config save - * - * @dataProvider saveAndRebuildIndexDataProvider - * - * @magentoAppIsolation enabled - * @magentoDbIsolation enabled - * @magentoConfigFixture default/cataloginventory/item_options/manage_stock 0 - * - * @param int $newStockValue new value for stock status - * @param int $callCount count matcher - */ - public function testSaveAndRebuildIndex($newStockValue, $callCount) - { - /** @var \Magento\CatalogInventory\Model\StockIndex */ - $stockManagement = $this->getMock( - '\Magento\CatalogInventory\Model\StockIndex', - ['rebuild'], - [], - '', - false - ); - - $stockManagement->expects($this->exactly($callCount)) - ->method('rebuild'); - - $manageStock = new Managestock( - Bootstrap::getObjectManager()->get('Magento\Framework\Model\Context'), - Bootstrap::getObjectManager()->get('Magento\Framework\Registry'), - Bootstrap::getObjectManager()->get('Magento\Framework\App\Config\ScopeConfigInterface'), - Bootstrap::getObjectManager()->get('Magento\Framework\App\Cache\TypeListInterface'), - $stockManagement, - Bootstrap::getObjectManager()->get('Magento\CatalogInventory\Model\Indexer\Stock\Processor'), - Bootstrap::getObjectManager()->get('Magento\Config\Model\ResourceModel\Config') - ); - - $manageStock->setPath('cataloginventory/item_options/manage_stock'); - $manageStock->setScope('default'); - $manageStock->setScopeId(0); - $manageStock->setValue($newStockValue); - - // assert - $manageStock->save(); - } -} diff --git a/dev/tests/integration/testsuite/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStockTest.php b/dev/tests/integration/testsuite/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStockTest.php index 8fbcad5267cf774401910a63b7dcf7b2c8bcd427..02ddb8e23b9924bc2acd357831724196ae1fbcf8 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStockTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStockTest.php @@ -17,12 +17,19 @@ class DefaultStockTest extends \PHPUnit_Framework_TestCase */ private $indexer; + /** + * @var \Magento\CatalogInventory\Api\StockConfigurationInterface + */ + private $stockConfiguration; + protected function setUp() { $this->indexer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\DefaultStock::class ); - + $this->stockConfiguration = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + \Magento\CatalogInventory\Api\StockConfigurationInterface::class + ); } /** @@ -53,7 +60,7 @@ class DefaultStockTest extends \PHPUnit_Framework_TestCase ); $criteria = $criteriaFactory->create(); $criteria->setProductsFilter([$product->getId()]); - $criteria->addFilter('website', 'website_id', $testWebsite->getId()); + $criteria->addFilter('website', 'website_id', $this->stockConfiguration->getDefaultScopeId()); $items = $stockStatusRepository->getList($criteria)->getItems(); $this->assertEquals($product->getId(), $items[$product->getId()]->getProductId()); } diff --git a/dev/tests/integration/testsuite/Magento/Downloadable/_files/product_downloadable.php b/dev/tests/integration/testsuite/Magento/Downloadable/_files/product_downloadable.php index 7ccfba9b3c1b7cf743cfeb30512ffc11902ddf80..4c15a2a9e08877c2b3516ad86f5458df7ed5acd3 100644 --- a/dev/tests/integration/testsuite/Magento/Downloadable/_files/product_downloadable.php +++ b/dev/tests/integration/testsuite/Magento/Downloadable/_files/product_downloadable.php @@ -17,7 +17,13 @@ $product ->setPrice(10) ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) - ->setLinksPurchasedSeparately(true); + ->setLinksPurchasedSeparately(true) + ->setStockData( + [ + 'qty' => 100, + 'is_in_stock' => 1, + ] + ); /** * @var \Magento\Downloadable\Api\Data\LinkInterfaceFactory $linkFactory diff --git a/dev/tests/integration/testsuite/Magento/Paypal/_files/quote_payment_express.php b/dev/tests/integration/testsuite/Magento/Paypal/_files/quote_payment_express.php index c94078c41d741682c467819a704610ec5cdad799..8fb3c96eae0b5993b4ec93b3eac13cfccb2de97e 100644 --- a/dev/tests/integration/testsuite/Magento/Paypal/_files/quote_payment_express.php +++ b/dev/tests/integration/testsuite/Magento/Paypal/_files/quote_payment_express.php @@ -28,7 +28,12 @@ $product->setTypeId('simple') ->setPrice(10) ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) - ->save(); + ->setStockData( + [ + 'qty' => 100, + 'is_in_stock' => 1, + ] + )->save(); $product->load(1); $billingData = [ diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php b/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php index 2304dcdc21399e3dbe7936c83fc880ab06b38139..95f387213d16ccb97ffb4d41321cd2aa941f8fef 100644 --- a/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/quote.php @@ -17,7 +17,12 @@ $product->setTypeId('simple') ->setMetaDescription('meta description') ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) - ->save(); + ->setStockData( + [ + 'qty' => 100, + 'is_in_stock' => 1, + ] + )->save(); $productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() ->create('Magento\Catalog\Api\ProductRepositoryInterface');