Skip to content

Commit

Permalink
Merge branch '2.1-develop-mainline' into PULL-12965-BACKPORT
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bystritsky committed Jan 4, 2018
2 parents 3230447 + 53d0dab commit 634afa8
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 68 deletions.
14 changes: 10 additions & 4 deletions app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

Expand Down Expand Up @@ -126,7 +126,7 @@ public function lockProductsStock($productIds, $websiteId)
}
$itemTable = $this->getTable('cataloginventory_stock_item');
$select = $this->getConnection()->select()->from(['si' => $itemTable])
->where('website_id=?', $websiteId)
->where('website_id = ?', $websiteId)
->where('product_id IN(?)', $productIds)
->forUpdate(true);

Expand All @@ -139,9 +139,15 @@ public function lockProductsStock($productIds, $websiteId)
'type_id' => 'type_id'
]
);
$this->getConnection()->query($select);
$items = [];

return $this->getConnection()->fetchAll($selectProducts);
foreach ($this->getConnection()->query($select)->fetchAll() as $si) {
$items[$si['product_id']] = $si;
}
foreach ($this->getConnection()->fetchAll($selectProducts) as $p) {
$items[$p['product_id']]['type_id'] = $p['type_id'];
}
return $items;
}

/**
Expand Down
18 changes: 15 additions & 3 deletions app/code/Magento/CatalogInventory/Model/StockManagement.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogInventory\Model;
Expand Down Expand Up @@ -48,28 +48,37 @@ class StockManagement implements StockManagementInterface
*/
private $qtyCounter;

/**
* @var StockRegistryStorage
*/
private $stockRegistryStorage;

/**
* @param ResourceStock $stockResource
* @param StockRegistryProviderInterface $stockRegistryProvider
* @param StockState $stockState
* @param StockConfigurationInterface $stockConfiguration
* @param ProductRepositoryInterface $productRepository
* @param QtyCounterInterface $qtyCounter
* @param StockRegistryStorage|null $stockRegistryStorage
*/
public function __construct(
ResourceStock $stockResource,
StockRegistryProviderInterface $stockRegistryProvider,
StockState $stockState,
StockConfigurationInterface $stockConfiguration,
ProductRepositoryInterface $productRepository,
QtyCounterInterface $qtyCounter
QtyCounterInterface $qtyCounter,
StockRegistryStorage $stockRegistryStorage = null
) {
$this->stockRegistryProvider = $stockRegistryProvider;
$this->stockState = $stockState;
$this->stockConfiguration = $stockConfiguration;
$this->productRepository = $productRepository;
$this->qtyCounter = $qtyCounter;
$this->resource = $stockResource;
$this->stockRegistryStorage = $stockRegistryStorage ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(StockRegistryStorage::class);
}

/**
Expand All @@ -92,9 +101,12 @@ public function registerProductsSale($items, $websiteId = null)
$fullSaveItems = $registeredItems = [];
foreach ($lockedItems as $lockedItemRecord) {
$productId = $lockedItemRecord['product_id'];
$this->stockRegistryStorage->removeStockItem($productId, $websiteId);

/** @var StockItemInterface $stockItem */
$orderedQty = $items[$productId];
$stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
$stockItem->setQty($lockedItemRecord['qty']); // update data from locked item
$canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
continue;
Expand Down Expand Up @@ -180,7 +192,7 @@ protected function getProductType($productId)
}

/**
* @return Stock
* @return ResourceStock
*/
protected function getResource()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogInventory\Test\Unit\Model\ResourceModel;
Expand Down Expand Up @@ -67,6 +67,11 @@ class StockTest extends \PHPUnit_Framework_TestCase
*/
protected $selectMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Zend_Db_Statement_Interface
*/
protected $statementMock;

/**
* Prepare subjects for tests.
*
Expand Down Expand Up @@ -95,6 +100,7 @@ protected function setUp()
$this->connectionMock = $this->getMockBuilder(Mysql::class)
->disableOriginalConstructor()
->getMock();
$this->statementMock = $this->getMockForAbstractClass(\Zend_Db_Statement_Interface::class);
$this->stock = $this->getMockBuilder(Stock::class)
->setMethods(['getTable', 'getConnection'])
->setConstructorArgs(
Expand All @@ -119,7 +125,21 @@ public function testLockProductsStock()
{
$websiteId = 0;
$productIds = [1, 2, 3];
$result = ['testResult'];
$result = [
1 => [
'product_id' => 1,
'type_id' => 'simple'
],
2 => [
'product_id' => 2,
'type_id' => 'simple'
],
3 => [
'product_id' => 3,
'type_id' => 'simple'
]
];

$this->selectMock->expects(self::exactly(2))
->method('from')
->withConsecutive(
Expand All @@ -130,7 +150,7 @@ public function testLockProductsStock()
$this->selectMock->expects(self::exactly(3))
->method('where')
->withConsecutive(
[self::identicalTo('website_id=?'), self::identicalTo($websiteId)],
[self::identicalTo('website_id = ?'), self::identicalTo($websiteId)],
[self::identicalTo('product_id IN(?)'), self::identicalTo($productIds)],
[self::identicalTo('entity_id IN (?)'), self::identicalTo($productIds)]
)
Expand All @@ -149,10 +169,19 @@ public function testLockProductsStock()
->willReturn($this->selectMock);
$this->connectionMock->expects(self::once())
->method('query')
->with(self::identicalTo($this->selectMock));
->with(self::identicalTo($this->selectMock))
->willReturn($this->statementMock);
$this->statementMock->expects(self::once())
->method('fetchAll')
->willReturn([
1 => ['product_id' => 1],
2 => ['product_id' => 2],
3 => ['product_id' => 3]
]);

$this->connectionMock->expects(self::once())
->method('fetchAll')
->with($this->selectMock)
->with(self::identicalTo($this->selectMock))
->willReturn($result);

$this->stock->expects(self::exactly(2))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

Expand Down Expand Up @@ -35,7 +35,8 @@ public function getAlias(\Magento\Framework\Search\Request\FilterInterface $filt
$alias = 'price_index';
break;
case 'category_ids':
$alias = 'category_ids_index';
case 'visibility':
$alias = 'category_products_index';
break;
default:
$alias = $field . RequestGenerator::FILTER_SUFFIX;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Filter;
Expand Down Expand Up @@ -141,7 +141,10 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu
$query
);
} elseif ($filter->getField() === 'category_ids') {
return 'category_ids_index.category_id = ' . (int) $filter->getValue();
return "{$this->aliasResolver->getAlias($filter)}.category_id = "
. (int) $filter->getValue();
} elseif ($filter->getField() === 'visibility') {
return "{$this->aliasResolver->getAlias($filter)}." . $query;
} elseif ($attribute->isStatic()) {
$alias = $this->aliasResolver->getAlias($filter);
$resultQuery = str_replace(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Model\ResourceModel\Fulltext;
Expand Down Expand Up @@ -254,7 +254,7 @@ public function setFilterBuilder(\Magento\Framework\Api\FilterBuilder $object)
* Apply attribute filter to facet collection
*
* @param string $field
* @param null $condition
* @param null|string|array $condition
* @return $this
*/
public function addFieldToFilter($field, $condition = null)
Expand All @@ -265,22 +265,21 @@ public function addFieldToFilter($field, $condition = null)

$this->getSearchCriteriaBuilder();
$this->getFilterBuilder();
if (!is_array($condition) || !in_array(key($condition), ['from', 'to'])) {
$this->filterBuilder->setField($field);
$this->filterBuilder->setValue($condition);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
} else {
if (is_array($condition)
&& in_array(key($condition), ['from', 'to'], true)
) {
if (!empty($condition['from'])) {
$this->filterBuilder->setField("{$field}.from");
$this->filterBuilder->setValue($condition['from']);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
$this->addFieldToFilter("{$field}.from", $condition['from']);
}
if (!empty($condition['to'])) {
$this->filterBuilder->setField("{$field}.to");
$this->filterBuilder->setValue($condition['to']);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
$this->addFieldToFilter("{$field}.to", $condition['to']);
}
} else {
$this->filterBuilder->setField($field);
$this->filterBuilder->setValue($condition);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
}

return $this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

Expand Down Expand Up @@ -70,14 +70,18 @@ public function apply(
[]
);
$isApplied = true;
} elseif ('category_ids' === $field) {
} elseif ('category_ids' === $field || $field === 'visibility') {
$alias = $this->aliasResolver->getAlias($filter);
$tableName = $this->resourceConnection->getTableName('catalog_category_product_index');
$select->joinInner(
[$alias => $tableName],
'search_index.entity_id = category_ids_index.product_id',
[]
);
if (!array_key_exists($alias, $select->getPart('from'))) {
$tableName = $this->resourceConnection->getTableName(
'catalog_category_product_index'
);
$select->joinInner(
[$alias => $tableName],
"search_index.entity_id = $alias.product_id",
[]
);
}
$isApplied = true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © 2013-2018 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

Expand Down Expand Up @@ -63,7 +63,11 @@ public function aliasDataProvider()
],
'category_ids' => [
'field' => 'category_ids',
'alias' => 'category_ids_index',
'alias' => 'category_products_index',
],
'visibility' => [
'field' => 'visibility',
'alias' => 'category_products_index',
],
];
}
Expand Down
Loading

0 comments on commit 634afa8

Please sign in to comment.