Skip to content

Commit

Permalink
Merge pull request #908 from magento-tsg/pr-2.1.6
Browse files Browse the repository at this point in the history
[TSG] Bugfixes - 2.1.6
  • Loading branch information
Volodymyr Klymenko authored Mar 9, 2017
2 parents 1c9c696 + fc0c729 commit e1c9045
Show file tree
Hide file tree
Showing 45 changed files with 1,845 additions and 247 deletions.
131 changes: 76 additions & 55 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
use Magento\Catalog\Model\Product\Visibility;

/**
* Import entity product model
* @SuppressWarnings(PHPMD.TooManyFields)
Expand Down Expand Up @@ -1190,7 +1191,7 @@ protected function _saveLinks()
}

$linkKey = "{$productId}-{$linkedId}-{$linkId}";
if(empty($productLinkKeys[$linkKey])) {
if (empty($productLinkKeys[$linkKey])) {
$productLinkKeys[$linkKey] = $nextLinkId;
}
if (!isset($linkRows[$linkKey])) {
Expand Down Expand Up @@ -2087,65 +2088,15 @@ protected function _saveProductWebsites(array $websiteData)
protected function _saveStockItem()
{
$indexer = $this->indexerRegistry->get('catalog_product_category');

/** @var $stockResource \Magento\CatalogInventory\Model\ResourceModel\Stock\Item */
$stockResource = $this->_stockResItemFac->create();
$entityTable = $stockResource->getMainTable();
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$stockData = [];
$productIdsToReindex = [];
// Format bunch to stock data rows
foreach ($bunch as $rowNum => $rowData) {
if (!$this->isRowAllowedToImport($rowData, $rowNum)) {
continue;
}

$row = [];
$row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id'];
$productIdsToReindex[] = $row['product_id'];

$row['website_id'] = $this->stockConfiguration->getDefaultScopeId();
$row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();

$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
$existStockData = $stockItemDo->getData();

$row = array_merge(
$this->defaultStockData,
array_intersect_key($existStockData, $this->defaultStockData),
array_intersect_key($rowData, $this->defaultStockData),
$row
);

if ($this->stockConfiguration->isQty(
$this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id']
)) {
$stockItemDo->setData($row);
$row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo);
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
$row['low_stock_date'] = $this->dateTime->gmDate(
'Y-m-d H:i:s',
(new \DateTime())->getTimestamp()
);
}
$row['stock_status_changed_auto'] =
(int) !$this->stockStateProvider->verifyStock($stockItemDo);
} else {
$row['qty'] = 0;
}
if (!isset($stockData[$rowData[self::COL_SKU]])) {
$stockData[$rowData[self::COL_SKU]] = $row;
}
}

// Insert rows
if (!empty($stockData)) {
$this->_connection->insertOnDuplicate($entityTable, array_values($stockData));
}

if ($productIdsToReindex) {
$indexer->reindexList($productIdsToReindex);
}
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$this->formatBunchToStockDataRows($bunch, $entityTable, $indexer);
}

return $this;
}

Expand Down Expand Up @@ -2739,4 +2690,74 @@ private function getProductIdentifierField()
}
return $this->productEntityIdentifierField;
}

/**
* Get stock data rows from bunch.
*
* @param array $bunch
* @param string $entityTable
* @param \Magento\Framework\Indexer\IndexerInterface $indexer
* @return void
*/
private function formatBunchToStockDataRows(
$bunch,
$entityTable,
\Magento\Framework\Indexer\IndexerInterface $indexer
) {
$stockData = [];
$productIdsToReindex = [];

foreach ($bunch as $rowNum => $rowData) {
if (!$this->isRowAllowedToImport($rowData, $rowNum)) {
continue;
}

$row = [];
$row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id'];
$row['website_id'] = $this->stockConfiguration->getDefaultScopeId();
$row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();

$productIdsToReindex[] = $row['product_id'];
$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
$existStockData = $stockItemDo->getData();

$row = array_merge(
$this->defaultStockData,
array_intersect_key($existStockData, $this->defaultStockData),
array_intersect_key($rowData, $this->defaultStockData),
$row
);

if ($this->stockConfiguration->isQty(
$this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id']
)
) {
$stockItemDo->setData($row);
$row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo);
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
$row['low_stock_date'] = $this->dateTime->gmDate(
'Y-m-d H:i:s',
(new \DateTime())->getTimestamp()
);
}
$row['stock_status_changed_auto'] =
(int)!$this->stockStateProvider->verifyStock($stockItemDo);
} else {
$row['qty'] = 0;
}

if (!isset($stockData[$rowData[self::COL_SKU]])) {
$stockData[$rowData[self::COL_SKU]] = $row;
}
}

// Insert rows
if (!empty($stockData)) {
$this->_connection->insertOnDuplicate($entityTable, array_values($stockData));
}

if ($productIdsToReindex && !$indexer->isScheduled()) {
$indexer->reindexList($productIdsToReindex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/
namespace Magento\CatalogImportExport\Test\Unit\Model\Import;

use Magento\CatalogImportExport\Model\Import\Product;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Stdlib\DateTime;
use Magento\ImportExport\Model\Import;

/**
Expand Down Expand Up @@ -398,7 +396,7 @@ protected function setUp()
'data' => $this->data
]
);
$reflection = new \ReflectionClass('\Magento\CatalogImportExport\Model\Import\Product');
$reflection = new \ReflectionClass(\Magento\CatalogImportExport\Model\Import\Product::class);
$reflectionProperty = $reflection->getProperty('metadataPool');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->importProduct, $metadataPoolMock);
Expand All @@ -410,7 +408,7 @@ protected function setUp()
protected function _objectConstructor()
{
$this->optionFactory = $this->getMock(
'\Magento\CatalogImportExport\Model\Import\Product\OptionFactory',
\Magento\CatalogImportExport\Model\Import\Product\OptionFactory::class,
['create'],
[],
'',
Expand Down Expand Up @@ -1683,4 +1681,89 @@ protected function createModelMockWithErrorAggregator(array $methods = [], array

return $importProduct;
}

/**
* Test indexer not run reindexList in update by schedule mode.
*
* @return void
*/
public function testStockItemReindexListNotCall()
{
$indexer = $this->getMockBuilder(\Magento\Framework\Indexer\IndexerInterface::class)
->disableOriginalConstructor()
->getMock();

$stockResource = $this->getMockBuilder(\Magento\CatalogInventory\Model\ResourceModel\Stock\Item::class)
->disableOriginalConstructor()
->getMock();

$stock = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockInterface::class)
->disableOriginalConstructor()
->getMock();

$stockItem = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
->setMethods(['getData'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->indexerRegistry->expects($this->once())
->method('get')
->with('catalog_product_category')
->willReturn($indexer);

$this->_stockResItemFac->expects($this->once())
->method('create')
->willReturn($stockResource);

$stockResource->expects($this->once())
->method('getMainTable')
->willReturn('mainTable');

$this->_dataSourceModel->expects($this->atLeastOnce())
->method('getNextBunch')
->willReturnOnConsecutiveCalls(
[
0 => [
'sku' => 'product_dynamic',
'product_type' => 'simple',
'_attribute_set' => 'attributeSet1'
]
],
[]
);

$this->validator->expects($this->once())
->method('isValid')
->willReturn(true);

$this->skuProcessor->expects($this->atLeastOnce())
->method('getNewSku')
->willReturn([
'sku' => 'product_dynamic_3326',
'type_id' => 'simple',
'attr_set_code' => 'attributeSet1',
'entity_id' => 1
]);

$this->stockRegistry->expects($this->once())
->method('getStock')
->willReturn($stock);

$this->stockRegistry->expects($this->once())
->method('getStockItem')
->willReturn($stockItem);

$stockItem->expects($this->once())
->method('getData')
->willReturn([]);

$indexer->expects($this->once())
->method('isScheduled')
->willReturn(true);

$indexer->expects($this->never())
->method('reindexList');

$this->invokeMethod($this->importProduct, '_saveStockItem');
}
}
4 changes: 3 additions & 1 deletion app/code/Magento/ImportExport/Model/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ public function invalidateIndex()
foreach (array_keys($relatedIndexers) as $indexerId) {
try {
$indexer = $this->indexerRegistry->get($indexerId);
$indexer->invalidate();
if (!$indexer->isScheduled()) {
$indexer->invalidate();
}
} catch (\InvalidArgumentException $e) {
}
}
Expand Down
32 changes: 28 additions & 4 deletions app/code/Magento/Swatches/Block/Product/Renderer/Configurable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Magento\Swatches\Helper\Data as SwatchData;
use Magento\Swatches\Helper\Media;
use Magento\Swatches\Model\Swatch;
use Magento\Framework\App\ObjectManager;
use Magento\Swatches\Model\SwatchAttributesProvider;

/**
* Swatch renderer block
Expand Down Expand Up @@ -60,10 +62,17 @@ class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\
/**
* Indicate if product has one or more Swatch attributes
*
* @deprecated unused
*
* @var boolean
*/
protected $isProductHasSwatchAttribute;

/**
* @var SwatchAttributesProvider
*/
private $swatchAttributesProvider;

/**
* @param Context $context
* @param ArrayUtils $arrayUtils
Expand All @@ -76,6 +85,7 @@ class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\
* @param SwatchData $swatchHelper
* @param Media $swatchMediaHelper
* @param array $data other data
* @param SwatchAttributesProvider $swatchAttributesProvider
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -89,11 +99,13 @@ public function __construct(
ConfigurableAttributeData $configurableAttributeData,
SwatchData $swatchHelper,
Media $swatchMediaHelper,
array $data = []
array $data = [],
SwatchAttributesProvider $swatchAttributesProvider = null
) {
$this->swatchHelper = $swatchHelper;
$this->swatchMediaHelper = $swatchMediaHelper;

$this->swatchAttributesProvider = $swatchAttributesProvider
?: ObjectManager::getInstance()->get(SwatchAttributesProvider::class);
parent::__construct(
$context,
$arrayUtils,
Expand Down Expand Up @@ -201,6 +213,9 @@ protected function getSwatchAttributesData()
}

/**
* @deprecated unused
* @see isProductHasSwatchAttribute().
*
* @codeCoverageIgnore
* @return void
*/
Expand All @@ -209,6 +224,16 @@ protected function initIsProductHasSwatchAttribute()
$this->isProductHasSwatchAttribute = $this->swatchHelper->isProductHasSwatch($this->getProduct());
}

/**
* @codeCoverageIgnore
* @return bool
*/
protected function isProductHasSwatchAttribute()
{
$swatchAttributes = $this->swatchAttributesProvider->provide($this->getProduct());
return count($swatchAttributes) > 0;
}

/**
* Add Swatch Data for attribute
*
Expand Down Expand Up @@ -371,7 +396,6 @@ protected function getConfigurableOptionsIds(array $attributeData)
*/
protected function _toHtml()
{
$this->initIsProductHasSwatchAttribute();
$this->setTemplate(
$this->getRendererTemplate()
);
Expand All @@ -384,7 +408,7 @@ protected function _toHtml()
*/
protected function getRendererTemplate()
{
return $this->isProductHasSwatchAttribute ?
return $this->isProductHasSwatchAttribute() ?
self::SWATCH_RENDERER_TEMPLATE : self::CONFIGURABLE_RENDERER_TEMPLATE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function _toHtml()
protected function getHtmlOutput()
{
$output = '';
if ($this->isProductHasSwatchAttribute) {
if ($this->isProductHasSwatchAttribute()) {
$output = parent::getHtmlOutput();
}

Expand Down
Loading

0 comments on commit e1c9045

Please sign in to comment.