Skip to content

Commit 66b4dd2

Browse files
author
Oleksandr Dubovyk
committed
MAGETWO-66066: Remove usages of unserialize in module Magento/ImportExport
- Remove serialize - Added tests
1 parent 535860a commit 66b4dd2

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
99
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1010
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Serialize\Serializer\Json;
1113

1214
/**
1315
* Import entity abstract model
@@ -271,6 +273,13 @@ abstract class AbstractEntity
271273
*/
272274
protected $countItemsDeleted = 0;
273275

276+
/**
277+
* Json Serializer Instance
278+
*
279+
* @var Json
280+
*/
281+
private $json;
282+
274283
/**
275284
* @param \Magento\Framework\Stdlib\StringUtils $string
276285
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
@@ -279,6 +288,7 @@ abstract class AbstractEntity
279288
* @param \Magento\Framework\App\ResourceConnection $resource
280289
* @param ProcessingErrorAggregatorInterface $errorAggregator
281290
* @param array $data
291+
* @param Json|null $json
282292
* @SuppressWarnings(PHPMD.NPathComplexity)
283293
*/
284294
public function __construct(
@@ -288,9 +298,11 @@ public function __construct(
288298
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
289299
ResourceConnection $resource,
290300
ProcessingErrorAggregatorInterface $errorAggregator,
291-
array $data = []
301+
array $data = [],
302+
Json $json = null
292303
) {
293304
$this->_scopeConfig = $scopeConfig;
305+
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
294306
$this->_dataSourceModel = isset(
295307
$data['data_source_model']
296308
) ? $data['data_source_model'] : $importFactory->create()->getDataSourceModel();
@@ -447,7 +459,7 @@ protected function _saveValidatedBunches()
447459
foreach ($entityGroup as $key => $value) {
448460
$bunchRows[$key] = $value;
449461
}
450-
$productDataSize = strlen(serialize($bunchRows));
462+
$productDataSize = strlen($this->json->serialize($bunchRows));
451463

452464
/* Check if the new bunch should be started */
453465
$isBunchSizeExceeded = ($this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize);

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEntity.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\ImportExport\Model\Import as ImportExport;
1111
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
1212
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\Serialize\Serializer\Json;
1315

1416
/**
1517
* Import entity abstract model
@@ -246,6 +248,13 @@ abstract class AbstractEntity
246248
*/
247249
protected $metadataPool;
248250

251+
/**
252+
* Json Serializer Instance
253+
*
254+
* @var Json
255+
*/
256+
private $json;
257+
249258
/**
250259
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
251260
* @param \Magento\ImportExport\Helper\Data $importExportData
@@ -255,6 +264,7 @@ abstract class AbstractEntity
255264
* @param \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper
256265
* @param \Magento\Framework\Stdlib\StringUtils $string
257266
* @param ProcessingErrorAggregatorInterface $errorAggregator
267+
* @param Json|null $json
258268
* @throws \Magento\Framework\Exception\LocalizedException
259269
*/
260270
public function __construct(
@@ -265,13 +275,15 @@ public function __construct(
265275
ResourceConnection $resource,
266276
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
267277
\Magento\Framework\Stdlib\StringUtils $string,
268-
ProcessingErrorAggregatorInterface $errorAggregator
278+
ProcessingErrorAggregatorInterface $errorAggregator,
279+
Json $json = null
269280
) {
270281
$this->jsonHelper = $jsonHelper;
271282
$this->_importExportData = $importExportData;
272283
$this->_resourceHelper = $resourceHelper;
273284
$this->string = $string;
274285
$this->errorAggregator = $errorAggregator;
286+
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
275287

276288
foreach ($this->errorMessageTemplates as $errorCode => $message) {
277289
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
@@ -382,7 +394,7 @@ protected function _saveValidatedBunches()
382394
$this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows);
383395

384396
$bunchRows = $nextRowBackup;
385-
$currentDataSize = strlen(serialize($bunchRows));
397+
$currentDataSize = strlen($this->json->serialize($bunchRows));
386398
$startNewBunch = false;
387399
$nextRowBackup = [];
388400
}

app/code/Magento/ImportExport/Test/Unit/Model/Import/EntityAbstractTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ protected function _getModelDependencies()
6060
$importFactory = $this->getMock(\Magento\ImportExport\Model\ImportFactory::class, [], [], '', false);
6161
$resourceHelper = $this->getMock(\Magento\ImportExport\Model\ResourceModel\Helper::class, [], [], '', false);
6262
$resource = $this->getMock(\Magento\Framework\App\ResourceConnection::class, [], [], '', false);
63+
$json = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);
6364

6465
$data = [
6566
'coreString' => $string,
@@ -77,6 +78,7 @@ protected function _getModelDependencies()
7778
'bunch_size' => 1,
7879
'collection_by_pages_iterator' => 'not_used',
7980
],
81+
'json' => $json,
8082
];
8183

8284
return $data;

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,4 +1616,28 @@ public function validateRowDataProvider()
16161616
],
16171617
];
16181618
}
1619+
1620+
/**
1621+
* @covers \Magento\ImportExport\Model\Import\Entity\AbstractEntity::_saveValidatedBunches
1622+
*/
1623+
public function testValidateData()
1624+
{
1625+
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
1626+
->create(\Magento\Framework\Filesystem::class);
1627+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
1628+
1629+
$source = $this->objectManager->create(
1630+
\Magento\ImportExport\Model\Import\Source\Csv::class,
1631+
[
1632+
'file' => __DIR__ . '/_files/products_to_import.csv',
1633+
'directory' => $directory
1634+
]
1635+
);
1636+
1637+
$errors = $this->_model->setParameters(
1638+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
1639+
)->setSource($source)->validateData();
1640+
1641+
$this->assertTrue($errors->getErrorsCount() == 0);
1642+
}
16191643
}

dev/tests/integration/testsuite/Magento/ImportExport/Model/Import/EntityAbstractTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function testSaveValidatedBunches()
4343
$objectManager->get(\Magento\Framework\App\ResourceConnection::class),
4444
$objectManager->get(
4545
\Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface::class
46-
)
46+
),
47+
[],
48+
$objectManager->get(\Magento\Framework\Serialize\Serializer\Json::class)
4749
],
4850
'',
4951
true,

0 commit comments

Comments
 (0)