Skip to content

Commit

Permalink
Merge pull request #4816 from magento-chaika/Chaika-PR-2019-09-20
Browse files Browse the repository at this point in the history
Chaika-PR-2019-09-20
  • Loading branch information
dhorytskyi authored Sep 25, 2019
2 parents 502abab + 488fb58 commit fc6b5aa
Show file tree
Hide file tree
Showing 18 changed files with 1,148 additions and 656 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/
namespace Magento\AdvancedPricingImportExport\Model\Export;

use Magento\ImportExport\Model\Export;
use Magento\Store\Model\Store;
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as ImportAdvancedPricing;
use Magento\Catalog\Model\Product as CatalogProduct;
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
use Magento\ImportExport\Model\Export;
use Magento\Store\Model\Store;

/**
* Export Advanced Pricing
Expand Down Expand Up @@ -150,6 +150,8 @@ public function __construct(
}

/**
* Init type models
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
Expand All @@ -172,7 +174,9 @@ protected function initTypeModels()
}
if ($model->isSuitable()) {
$this->_productTypeModels[$productTypeName] = $model;
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$this->_disabledAttrs = array_merge($this->_disabledAttrs, $model->getDisabledAttrs());
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$this->_indexValueAttributes = array_merge(
$this->_indexValueAttributes,
$model->getIndexValueAttributes()
Expand All @@ -197,6 +201,7 @@ protected function initTypeModels()
public function export()
{
//Execution time may be very long
// phpcs:ignore Magento2.Functions.DiscouragedFunction
set_time_limit(0);

$writer = $this->getWriter();
Expand Down Expand Up @@ -234,16 +239,6 @@ public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entit

foreach ($collection as $attribute) {
if (in_array($attribute->getAttributeCode(), $this->_disabledAttrs)) {
if (isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_SKIP])) {
if ($attribute->getAttributeCode() == ImportAdvancedPricing::COL_TIER_PRICE
&& in_array(
$attribute->getId(),
$this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_SKIP]
)
) {
$this->_passTierPrice = 1;
}
}
$collection->removeItemByKey($attribute->getId());
}
}
Expand Down Expand Up @@ -363,6 +358,7 @@ private function prepareExportData(
$linkedTierPricesData = [];
foreach ($tierPricesData as $tierPriceData) {
$sku = $productLinkIdToSkuMap[$tierPriceData['product_link_id']];
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$linkedTierPricesData[] = array_merge(
$tierPriceData,
[ImportAdvancedPricing::COL_SKU => $sku]
Expand Down Expand Up @@ -471,7 +467,7 @@ private function fetchTierPrices(array $productIds): array
ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty',
ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value',
ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value',
'product_link_id' => 'ap.' .$productEntityLinkField,
'product_link_id' => 'ap.' . $productEntityLinkField,
];
if ($exportFilter && array_key_exists('tier_price', $exportFilter)) {
if (!empty($exportFilter['tier_price'][0])) {
Expand All @@ -488,7 +484,7 @@ private function fetchTierPrices(array $productIds): array
$selectFields
)
->where(
'ap.'.$productEntityLinkField.' IN (?)',
'ap.' . $productEntityLinkField . ' IN (?)',
$productIds
);

Expand Down Expand Up @@ -602,7 +598,7 @@ protected function _getWebsiteCode(int $websiteId): string
}

if ($storeName && $currencyCode) {
$code = $storeName.' ['.$currencyCode.']';
$code = $storeName . ' [' . $currencyCode . ']';
} else {
$code = $storeName;
}
Expand Down
36 changes: 33 additions & 3 deletions app/code/Magento/CatalogImportExport/Model/Export/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ protected function initTypeModels()
}
if ($model->isSuitable()) {
$this->_productTypeModels[$productTypeName] = $model;
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$this->_disabledAttrs = array_merge($this->_disabledAttrs, $model->getDisabledAttrs());
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$this->_indexValueAttributes = array_merge(
$this->_indexValueAttributes,
$model->getIndexValueAttributes()
Expand Down Expand Up @@ -526,7 +528,7 @@ protected function getMediaGallery(array $productIds)
if (empty($productIds)) {
return [];
}

$productEntityJoinField = $this->getProductEntityLinkField();

$select = $this->_connection->select()->from(
Expand Down Expand Up @@ -710,6 +712,21 @@ public function _getHeaderColumns()
return $this->_customHeadersMapping($this->rowCustomizer->addHeaderColumns($this->_headerColumns));
}

/**
* Return non-system attributes
* @return array
*/
private function getNonSystemAttributes(): array
{
$attrKeys = [];
foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) {
$attrKeys[] = $attribute->getAttributeCode();
}

return array_diff($this->_getExportMainAttrCodes(), $this->_customHeadersMapping($attrKeys));
}

/**
* Set headers columns
*
Expand All @@ -722,6 +739,18 @@ public function _getHeaderColumns()
*/
protected function setHeaderColumns($customOptionsData, $stockItemRows)
{
$exportAttributes = (
array_key_exists("skip_attr", $this->_parameters) && count($this->_parameters["skip_attr"])
) ?
array_intersect(
$this->_getExportMainAttrCodes(),
array_merge(
$this->_customHeadersMapping($this->_getExportAttrCodes()),
$this->getNonSystemAttributes()
)
) :
$this->_getExportMainAttrCodes();

if (!$this->_headerColumns) {
$this->_headerColumns = array_merge(
[
Expand All @@ -732,7 +761,7 @@ protected function setHeaderColumns($customOptionsData, $stockItemRows)
self::COL_CATEGORY,
self::COL_PRODUCT_WEBSITES,
],
$this->_getExportMainAttrCodes(),
$exportAttributes,
[self::COL_ADDITIONAL_ATTRIBUTES],
reset($stockItemRows) ? array_keys(end($stockItemRows)) : [],
[
Expand Down Expand Up @@ -923,6 +952,7 @@ protected function getExportData()
foreach ($rawData as $productId => $productData) {
foreach ($productData as $storeId => $dataRow) {
if ($storeId == Store::DEFAULT_STORE_ID && isset($stockItemRows[$productId])) {
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$dataRow = array_merge($dataRow, $stockItemRows[$productId]);
}
$this->appendMultirowData($dataRow, $multirawData);
Expand Down Expand Up @@ -1330,7 +1360,7 @@ private function appendMultirowData(&$dataRow, $multiRawData)
$dataRow[self::COL_SKU] = $sku;
$dataRow[self::COL_ATTR_SET] = $attributeSet;
$dataRow[self::COL_TYPE] = $type;

return $dataRow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/**
* Import entity abstract product type model
*
* phpcs:disable Magento2.Classes.AbstractApi
* @api
*
* @SuppressWarnings(PHPMD.TooManyFields)
Expand Down Expand Up @@ -543,7 +544,7 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
} else {
$resultAttrs[$attrCode] = $rowData[$attrCode];
}
} elseif (array_key_exists($attrCode, $rowData) && empty($rowData['_store'])) {
} elseif (array_key_exists($attrCode, $rowData)) {
$resultAttrs[$attrCode] = $rowData[$attrCode];
} elseif ($withDefaultValue && null !== $attrParams['default_value'] && empty($rowData['_store'])) {
$resultAttrs[$attrCode] = $attrParams['default_value'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ protected function setUp()
'setAttributeSetFilter'
]
);
$attribute = $this->createPartialMock(\Magento\Eav\Model\Entity\Attribute::class, [
$attribute = $this->createPartialMock(
\Magento\Eav\Model\Entity\Attribute::class,
[
'getAttributeCode',
'getId',
'getIsVisible',
Expand All @@ -85,7 +87,8 @@ protected function setUp()
'getDefaultValue',
'usesSource',
'getFrontendInput',
]);
]
);
$attribute->expects($this->any())->method('getIsVisible')->willReturn(true);
$attribute->expects($this->any())->method('getIsGlobal')->willReturn(true);
$attribute->expects($this->any())->method('getIsRequired')->willReturn(true);
Expand All @@ -107,6 +110,7 @@ protected function setUp()
];
$attribute1 = clone $attribute;
$attribute2 = clone $attribute;
$attribute3 = clone $attribute;

$attribute1->expects($this->any())->method('getId')->willReturn('1');
$attribute1->expects($this->any())->method('getAttributeCode')->willReturn('attr_code');
Expand All @@ -118,6 +122,11 @@ protected function setUp()
$attribute2->expects($this->any())->method('getFrontendInput')->willReturn('boolean');
$attribute2->expects($this->any())->method('isStatic')->willReturn(false);

$attribute3->expects($this->any())->method('getId')->willReturn('3');
$attribute3->expects($this->any())->method('getAttributeCode')->willReturn('text_attribute');
$attribute3->expects($this->any())->method('getFrontendInput')->willReturn('text');
$attribute3->expects($this->any())->method('isStatic')->willReturn(false);

$this->entityModel->expects($this->any())->method('getEntityTypeId')->willReturn(3);
$this->entityModel->expects($this->any())->method('getAttributeOptions')->willReturnOnConsecutiveCalls(
['option1', 'option2'],
Expand All @@ -126,7 +135,9 @@ protected function setUp()
$attrSetColFactory->expects($this->any())->method('create')->willReturn($attrSetCollection);
$attrSetCollection->expects($this->any())->method('setEntityTypeFilter')->willReturn([$attributeSet]);
$attrColFactory->expects($this->any())->method('create')->willReturn($attrCollection);
$attrCollection->expects($this->any())->method('setAttributeSetFilter')->willReturn([$attribute1, $attribute2]);
$attrCollection->expects($this->any())
->method('setAttributeSetFilter')
->willReturn([$attribute1, $attribute2, $attribute3]);
$attributeSet->expects($this->any())->method('getId')->willReturn(1);
$attributeSet->expects($this->any())->method('getAttributeSetName')->willReturn('attribute_set_name');

Expand Down Expand Up @@ -157,23 +168,29 @@ protected function setUp()
],
]
)
->willReturn([$attribute1, $attribute2]);
->willReturn([$attribute1, $attribute2, $attribute3]);

$this->connection = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, [
$this->connection = $this->createPartialMock(
\Magento\Framework\DB\Adapter\Pdo\Mysql::class,
[
'select',
'fetchAll',
'fetchPairs',
'joinLeft',
'insertOnDuplicate',
'delete',
'quoteInto'
]);
$this->select = $this->createPartialMock(\Magento\Framework\DB\Select::class, [
]
);
$this->select = $this->createPartialMock(
\Magento\Framework\DB\Select::class,
[
'from',
'where',
'joinLeft',
'getConnection',
]);
]
);
$this->select->expects($this->any())->method('from')->will($this->returnSelf());
$this->select->expects($this->any())->method('where')->will($this->returnSelf());
$this->select->expects($this->any())->method('joinLeft')->will($this->returnSelf());
Expand All @@ -189,10 +206,13 @@ protected function setUp()
->method('fetchAll')
->will($this->returnValue($entityAttributes));

$this->resource = $this->createPartialMock(\Magento\Framework\App\ResourceConnection::class, [
$this->resource = $this->createPartialMock(
\Magento\Framework\App\ResourceConnection::class,
[
'getConnection',
'getTableName',
]);
]
);
$this->resource->expects($this->any())->method('getConnection')->will(
$this->returnValue($this->connection)
);
Expand Down Expand Up @@ -257,9 +277,13 @@ public function testIsRowValidSuccess()
$rowNum = 1;
$this->entityModel->expects($this->any())->method('getRowScope')->willReturn(null);
$this->entityModel->expects($this->never())->method('addRowError');
$this->setPropertyValue($this->simpleType, '_attributes', [
$rowData[\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET] => [],
]);
$this->setPropertyValue(
$this->simpleType,
'_attributes',
[
$rowData[\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET] => [],
]
);
$this->assertTrue($this->simpleType->isRowValid($rowData, $rowNum));
}

Expand All @@ -278,13 +302,17 @@ public function testIsRowValidError()
'attr_code'
)
->willReturnSelf();
$this->setPropertyValue($this->simpleType, '_attributes', [
$rowData[\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET] => [
'attr_code' => [
'is_required' => true,
$this->setPropertyValue(
$this->simpleType,
'_attributes',
[
$rowData[\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET] => [
'attr_code' => [
'is_required' => true,
],
],
],
]);
]
);

$this->assertFalse($this->simpleType->isRowValid($rowData, $rowNum));
}
Expand Down Expand Up @@ -364,9 +392,14 @@ public function testPrepareAttributesWithDefaultValueForSave()
{
$rowData = [
'_attribute_set' => 'attributeSetName',
'boolean_attribute' => 'Yes'
'boolean_attribute' => 'Yes',
];

$expected = [
'boolean_attribute' => 1,
'text_attribute' => 'default_value'
];
$result = $this->simpleType->prepareAttributesWithDefaultValueForSave($rowData);
$this->assertEquals(['boolean_attribute' => 1], $result);
$this->assertEquals($expected, $result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ImportExport\Api\Data;

/**
* Extended export interface for implementation of Skipped Attributes which are missing from the basic interface
*/
interface ExtendedExportInfoInterface extends ExportInfoInterface
{
/**
* Returns skipped attributes
*
* @return mixed
*/
public function getSkipAttr();

/**
* Set skipped attributes
*
* @param string $skipAttr
* @return mixed
*/
public function setSkipAttr($skipAttr);
}
Loading

0 comments on commit fc6b5aa

Please sign in to comment.