Skip to content

Commit dd9a4f0

Browse files
committed
MAGETWO-66077: Remove usages of unserialize in module Magento/Paypal
1 parent 00efa0d commit dd9a4f0

File tree

2 files changed

+92
-22
lines changed

2 files changed

+92
-22
lines changed

app/code/Magento/Paypal/Model/Report/Settlement.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class Settlement extends \Magento\Framework\Model\AbstractModel
165165

166166
/**
167167
* Columns with DateTime data type
168-
*
168+
*
169169
* @var array
170170
*/
171171
private $dateTimeColumns = ['transaction_initiation_date', 'transaction_completion_date'];
@@ -178,15 +178,21 @@ class Settlement extends \Magento\Framework\Model\AbstractModel
178178
private $amountColumns = ['gross_transaction_amount', 'fee_amount'];
179179

180180
/**
181-
* @param \Magento\Framework\Model\Context $context
182-
* @param \Magento\Framework\Registry $registry
183-
* @param \Magento\Framework\Filesystem $filesystem
184-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
185-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
186-
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
187-
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
188-
* @param array $data
189-
*/
181+
* @var \Magento\Framework\Serialize\Serializer\Json
182+
*/
183+
private $serializer;
184+
185+
/**
186+
* @param \Magento\Framework\Model\Context $context
187+
* @param \Magento\Framework\Registry $registry
188+
* @param \Magento\Framework\Filesystem $filesystem
189+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
190+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
191+
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
192+
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
193+
* @param array $data
194+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
195+
*/
190196
public function __construct(
191197
\Magento\Framework\Model\Context $context,
192198
\Magento\Framework\Registry $registry,
@@ -195,12 +201,15 @@ public function __construct(
195201
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
196202
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
197203
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
198-
array $data = []
204+
array $data = [],
205+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
199206
) {
200207
$this->_tmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
201208
$this->_storeManager = $storeManager;
202209
$this->_scopeConfig = $scopeConfig;
203210
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
211+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
212+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
204213
}
205214

206215
/**
@@ -305,14 +314,14 @@ public function fetchAndSave(\Magento\Framework\Filesystem\Io\Sftp $connection)
305314
public static function createConnection(array $config)
306315
{
307316
if (!isset(
308-
$config['hostname']
309-
) || !isset(
310-
$config['username']
311-
) || !isset(
312-
$config['password']
313-
) || !isset(
314-
$config['path']
315-
)
317+
$config['hostname']
318+
) || !isset(
319+
$config['username']
320+
) || !isset(
321+
$config['password']
322+
) || !isset(
323+
$config['path']
324+
)
316325
) {
317326
throw new \InvalidArgumentException('Required config elements: hostname, username, password, path');
318327
}
@@ -424,7 +433,7 @@ private function getBodyItems(array $line, array $sectionColumns, array $rowMap)
424433

425434
/**
426435
* Format date columns in UTC
427-
*
436+
*
428437
* @param string $lineItem
429438
* @return string
430439
*/
@@ -574,10 +583,10 @@ public function getSftpCredentials($automaticMode = false)
574583
$cfg['path'] = self::REPORTS_PATH;
575584
}
576585
// avoid duplicates
577-
if (in_array(serialize($cfg), $uniques)) {
586+
if (in_array($this->serializer->serialize($cfg), $uniques)) {
578587
continue;
579588
}
580-
$uniques[] = serialize($cfg);
589+
$uniques[] = $this->serializer->serialize($cfg);
581590
$configs[] = $cfg;
582591
}
583592
return $configs;

dev/tests/integration/testsuite/Magento/Paypal/Model/Report/SettlementTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ public function testCreateConnectionException($config)
3333
\Magento\Paypal\Model\Report\Settlement::createConnection($config);
3434
}
3535

36+
/**
37+
* @param array $automaticMode
38+
* @param array $expectedResult
39+
*
40+
* @dataProvider createAutomaticModeDataProvider
41+
*
42+
* @magentoConfigFixture default_store paypal/fetch_reports/active 0
43+
* @magentoConfigFixture default_store paypal/fetch_reports/ftp_ip 192.168.0.1
44+
* @magentoConfigFixture current_store paypal/fetch_reports/active 1
45+
* @magentoConfigFixture current_store paypal/fetch_reports/ftp_ip 127.0.0.1
46+
* @magentoConfigFixture current_store paypal/fetch_reports/ftp_path /tmp
47+
* @magentoConfigFixture current_store paypal/fetch_reports/ftp_login login
48+
* @magentoConfigFixture current_store paypal/fetch_reports/ftp_password password
49+
* @magentoConfigFixture current_store paypal/fetch_reports/ftp_sandbox 0
50+
* @magentoDbIsolation enabled
51+
*/
52+
public function testGetSftpCredentials($automaticMode, $expectedResult)
53+
{
54+
/** @var $model \Magento\Paypal\Model\Report\Settlement; */
55+
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
56+
\Magento\Paypal\Model\Report\Settlement::class
57+
);
58+
59+
$result = $model->getSftpCredentials($automaticMode);
60+
61+
$this->assertEquals($expectedResult, $result);
62+
}
63+
3664
/**
3765
* @return array
3866
*/
@@ -46,4 +74,37 @@ public function createConnectionExceptionDataProvider()
4674
[['hostname' => 'example.com', 'username' => 'test', 'password' => 'test']]
4775
];
4876
}
77+
78+
/**
79+
* @return array
80+
*/
81+
public function createAutomaticModeDataProvider()
82+
{
83+
return [
84+
[
85+
true,
86+
[
87+
[
88+
'hostname' => '127.0.0.1',
89+
'path' => '/tmp',
90+
'username' => 'login',
91+
'password' => 'password',
92+
'sandbox' => '0'
93+
]
94+
]
95+
],
96+
[
97+
false,
98+
[
99+
[
100+
'hostname' => '127.0.0.1',
101+
'path' => '/tmp',
102+
'username' => 'login',
103+
'password' => 'password',
104+
'sandbox' => '0'
105+
]
106+
]
107+
],
108+
];
109+
}
49110
}

0 commit comments

Comments
 (0)