Skip to content

Commit 411a1d6

Browse files
authored
Merge pull request #975 from magento-troll/MAGETWO-66039
- MAGETWO-66039 Remove usages of unserialize in modules /Magento/Framework/*/. CE - missed usages - MAGETWO-66036 Remove usages of unserialize in module /Magento/Paypal/. CE - missed usages
2 parents 7b47dc6 + 7412657 commit 411a1d6

File tree

19 files changed

+349
-60
lines changed

19 files changed

+349
-60
lines changed

app/code/Magento/Backend/Model/Url.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Backend\Model;
77

8+
use Magento\Framework\Serialize\Serializer\Json;
89
use Magento\Framework\Url\HostChecker;
910
use Magento\Framework\App\ObjectManager;
1011

@@ -101,6 +102,7 @@ class Url extends \Magento\Framework\Url implements \Magento\Backend\Model\UrlIn
101102
* @param \Magento\Framework\Data\Form\FormKey $formKey
102103
* @param array $data
103104
* @param HostChecker|null $hostChecker
105+
* @param Json $serializer
104106
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
105107
*/
106108
public function __construct(
@@ -123,7 +125,8 @@ public function __construct(
123125
\Magento\Store\Model\StoreFactory $storeFactory,
124126
\Magento\Framework\Data\Form\FormKey $formKey,
125127
array $data = [],
126-
HostChecker $hostChecker = null
128+
HostChecker $hostChecker = null,
129+
Json $serializer = null
127130
) {
128131
$this->_encryptor = $encryptor;
129132
$hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
@@ -140,7 +143,8 @@ public function __construct(
140143
$routeParamsPreprocessor,
141144
$scopeType,
142145
$data,
143-
$hostChecker
146+
$hostChecker,
147+
$serializer
144148
);
145149
$this->_backendHelper = $backendHelper;
146150
$this->_menuConfig = $menuConfig;

app/code/Magento/Backend/Test/Unit/Model/UrlTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Backend\Test\Unit\Model;
77

8+
use Magento\Framework\Serialize\Serializer\Json;
89
use Magento\Framework\Url\HostChecker;
910

1011
/**
@@ -68,6 +69,11 @@ class UrlTest extends \PHPUnit_Framework_TestCase
6869
*/
6970
protected $_encryptor;
7071

72+
/**
73+
* @var Json|\PHPUnit_Framework_MockObject_MockObject
74+
*/
75+
private $serializerMock;
76+
7177
/**
7278
* @return void
7379
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -151,6 +157,20 @@ protected function setUp()
151157
->willReturn($routeParamsResolver);
152158
/** @var HostChecker|\PHPUnit_Framework_MockObject_MockObject $hostCheckerMock */
153159
$hostCheckerMock = $this->getMock(HostChecker::class, [], [], '', false);
160+
$this->serializerMock = $this->getMockBuilder(Json::class)
161+
->setMethods(['serialize'])
162+
->disableOriginalConstructor()
163+
->getMock();
164+
165+
$this->serializerMock->expects($this->any())
166+
->method('serialize')
167+
->will(
168+
$this->returnCallback(
169+
function ($value) {
170+
return json_encode($value);
171+
}
172+
)
173+
);
154174
$this->_model = $objectManager->getObject(
155175
\Magento\Backend\Model\Url::class,
156176
[
@@ -161,7 +181,8 @@ protected function setUp()
161181
'authSession' => $this->_authSessionMock,
162182
'encryptor' => $this->_encryptor,
163183
'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock,
164-
'hostChecker' => $hostCheckerMock
184+
'hostChecker' => $hostCheckerMock,
185+
'serializer' => $this->serializerMock
165186
]
166187
);
167188
$this->_requestMock = $this->getMock(\Magento\Framework\App\Request\Http::class, [], [], '', false);

app/code/Magento/Catalog/Test/Unit/Block/Rss/CategoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function setUp()
9595
$this->request->expects($this->at(0))->method('getParam')->with('cid')->will($this->returnValue(1));
9696
$this->request->expects($this->at(1))->method('getParam')->with('store_id')->will($this->returnValue(null));
9797

98-
$this->httpContext = $this->getMock(\Magento\Framework\App\Http\Context::class);
98+
$this->httpContext = $this->getMock(\Magento\Framework\App\Http\Context::class, [], [], '', false);
9999
$this->catalogHelper = $this->getMock(\Magento\Catalog\Helper\Data::class, [], [], '', false);
100100
$this->categoryFactory = $this->getMockBuilder(\Magento\Catalog\Model\CategoryFactory::class)
101101
->setMethods(['create'])

app/code/Magento/CatalogWidget/Test/Unit/Block/Product/ProductsListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function setUp()
9393
->setMethods(['getVisibleInCatalogIds'])
9494
->disableOriginalConstructor()
9595
->getMock();
96-
$this->httpContext = $this->getMock(\Magento\Framework\App\Http\Context::class);
96+
$this->httpContext = $this->getMock(\Magento\Framework\App\Http\Context::class, [], [], '', false);
9797
$this->builder = $this->getMock(\Magento\Rule\Model\Condition\Sql\Builder::class, [], [], '', false);
9898
$this->rule = $this->getMock(\Magento\CatalogWidget\Model\Rule::class, [], [], '', false);
9999
$this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);

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
}

dev/tests/static/framework/bootstrap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
$componentRegistrar = new ComponentRegistrar();
2020
$dirSearch = new DirSearch($componentRegistrar, new ReadFactory(new DriverPool()));
2121
$themePackageList = new ThemePackageList($componentRegistrar, new ThemePackageFactory());
22+
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
2223
\Magento\Framework\App\Utility\Files::setInstance(
23-
new Files($componentRegistrar, $dirSearch, $themePackageList)
24+
new Files($componentRegistrar, $dirSearch, $themePackageList, $serializer)
2425
);
2526

2627
/**

lib/internal/Magento/Framework/App/Http/Context.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Framework\App\Http;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
811
/**
912
* Context data for requests
1013
*/
@@ -27,14 +30,21 @@ class Context
2730
*/
2831
protected $default = [];
2932

33+
/**
34+
* @var Json
35+
*/
36+
private $serializer;
37+
3038
/**
3139
* @param array $data
3240
* @param array $default
41+
* @param Json|null $serializer
3342
*/
34-
public function __construct(array $data = [], array $default = [])
43+
public function __construct(array $data = [], array $default = [], Json $serializer = null)
3544
{
3645
$this->data = $data;
3746
$this->default = $default;
47+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
3848
}
3949

4050
/**
@@ -105,7 +115,7 @@ public function getVaryString()
105115
$data = $this->getData();
106116
if (!empty($data)) {
107117
ksort($data);
108-
return sha1(serialize($data));
118+
return sha1($this->serializer->serialize($data));
109119
}
110120
return null;
111121
}

lib/internal/Magento/Framework/App/PageCache/Identifier.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Framework\App\PageCache;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
811
/**
912
* Page unique identifier
1013
*/
@@ -20,16 +23,24 @@ class Identifier
2023
*/
2124
protected $context;
2225

26+
/**
27+
* @var Json
28+
*/
29+
private $serializer;
30+
2331
/**
2432
* @param \Magento\Framework\App\Request\Http $request
2533
* @param \Magento\Framework\App\Http\Context $context
34+
* @param Json|null $serializer
2635
*/
2736
public function __construct(
2837
\Magento\Framework\App\Request\Http $request,
29-
\Magento\Framework\App\Http\Context $context
38+
\Magento\Framework\App\Http\Context $context,
39+
Json $serializer = null
3040
) {
3141
$this->request = $request;
3242
$this->context = $context;
43+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
3344
}
3445

3546
/**
@@ -45,6 +56,6 @@ public function getValue()
4556
$this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
4657
?: $this->context->getVaryString()
4758
];
48-
return md5(serialize($data));
59+
return sha1($this->serializer->serialize($data));
4960
}
5061
}

0 commit comments

Comments
 (0)