Skip to content

Commit 3f54947

Browse files
author
Joan He
committed
MAGETWO-62999: Remove serialize in \Magento\Config\Model\Config\Backend\Serialized and child classes and usages
1 parent b1e56ed commit 3f54947

File tree

10 files changed

+316
-52
lines changed

10 files changed

+316
-52
lines changed

app/code/Magento/Config/Model/Config/Backend/Serialized.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,38 @@
55
*/
66
namespace Magento\Config\Model\Config\Backend;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
811
class Serialized extends \Magento\Framework\App\Config\Value
912
{
13+
/**
14+
* @var Json
15+
*/
16+
private $serializer;
17+
18+
public function __construct(
19+
\Magento\Framework\Model\Context $context,
20+
\Magento\Framework\Registry $registry,
21+
\Magento\Framework\App\Config\ScopeConfigInterface $config,
22+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
23+
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
24+
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
25+
array $data = [],
26+
Json $serializer = null
27+
) {
28+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
29+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
30+
}
31+
1032
/**
1133
* @return void
1234
*/
1335
protected function _afterLoad()
1436
{
15-
if (!is_array($this->getValue())) {
16-
$value = $this->getValue();
17-
$this->setValue(empty($value) ? false : unserialize($value));
37+
$value = $this->getValue();
38+
if (!is_array($value)) {
39+
$this->setValue(empty($value) ? false : $this->serializer->unserialize($value));
1840
}
1941
}
2042

@@ -24,8 +46,9 @@ protected function _afterLoad()
2446
public function beforeSave()
2547
{
2648
if (is_array($this->getValue())) {
27-
$this->setValue(serialize($this->getValue()));
49+
$this->setValue($this->serializer->serialize($this->getValue()));
2850
}
29-
return parent::beforeSave();
51+
parent::beforeSave();
52+
return $this;
3053
}
3154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Test\Unit\Model\Config\Backend;
7+
8+
use Magento\Config\Model\Config\Backend\Serialized;
9+
use Magento\Framework\Model\Context;
10+
use Magento\Framework\Serialize\Serializer\Json;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
13+
class SerializedTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/** @var \Magento\Config\Model\Config\Backend\Serialized */
16+
private $serializedConfig;
17+
18+
/** @var Json|\PHPUnit_Framework_MockObject_MockObject */
19+
private $serializerMock;
20+
21+
protected function setUp()
22+
{
23+
$objectManager = new ObjectManager($this);
24+
$this->serializerMock = $this->getMock(Json::class, [], [], '', false);
25+
$contextMock = $this->getMock(Context::class, [], [], '', false);
26+
$eventManagerMock = $this->getMock(\Magento\Framework\Event\ManagerInterface::class, [], [], '', false);
27+
$contextMock->method('getEventDispatcher')
28+
->willReturn($eventManagerMock);
29+
$this->serializedConfig = $objectManager->getObject(
30+
Serialized::class,
31+
[
32+
'serializer' => $this->serializerMock,
33+
'context' => $contextMock,
34+
]
35+
);
36+
}
37+
38+
/**
39+
* @param int|double|string|array|boolean|null $expected
40+
* @param int|double|string|array|boolean|null $value
41+
* @param int $numCalls
42+
* @param array $unserializedValue
43+
* @dataProvider afterLoadDataProvider
44+
*/
45+
public function testAfterLoad($expected, $value, $numCalls, $unserializedValue = null)
46+
{
47+
$this->serializedConfig->setValue($value);
48+
$this->serializerMock->expects($this->exactly($numCalls))
49+
->method('unserialize')
50+
->willReturn($unserializedValue);
51+
$this->serializedConfig->afterLoad();
52+
$this->assertEquals($expected, $this->serializedConfig->getValue());
53+
}
54+
55+
public function afterLoadDataProvider()
56+
{
57+
return [
58+
'empty value' => [
59+
false,
60+
'',
61+
0,
62+
],
63+
'value' => [
64+
['string array'],
65+
'string array',
66+
1,
67+
['string array']
68+
]
69+
];
70+
}
71+
72+
/**
73+
* @param string $expected
74+
* @param int|double|string|array|boolean|null $value
75+
* @param int $numCalls
76+
* @param string|null $serializedValue
77+
* @dataProvider beforeSaveDataProvider
78+
*/
79+
public function testBeforeSave($expected, $value, $numCalls, $serializedValue = null)
80+
{
81+
$this->serializedConfig->setId('id');
82+
$this->serializedConfig->setValue($value);
83+
$this->serializerMock->expects($this->exactly($numCalls))
84+
->method('serialize')
85+
->willReturn($serializedValue);
86+
$this->serializedConfig->beforeSave();
87+
$this->assertEquals($expected, $this->serializedConfig->getValue());
88+
}
89+
90+
public function beforeSaveDataProvider()
91+
{
92+
return [
93+
'string' => [
94+
'string',
95+
'string',
96+
0,
97+
],
98+
'array' => [
99+
'string array',
100+
['string array'],
101+
1,
102+
'string array'
103+
]
104+
];
105+
}
106+
}

app/code/Magento/PageCache/Model/Config.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\PageCache\Model;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\Filesystem;
910
use Magento\Framework\Module\Dir;
11+
use Magento\Framework\Serialize\Serializer\Json;
1012

1113
/**
1214
* Model is responsible for replacing default vcl template
@@ -73,22 +75,30 @@ class Config
7375
*/
7476
protected $reader;
7577

78+
/**
79+
* @var Json
80+
*/
81+
private $serializer;
82+
7683
/**
7784
* @param Filesystem\Directory\ReadFactory $readFactory
7885
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
7986
* @param \Magento\Framework\App\Cache\StateInterface $cacheState
8087
* @param Dir\Reader $reader
88+
* @param Json|null $serializer
8189
*/
8290
public function __construct(
8391
\Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
8492
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
8593
\Magento\Framework\App\Cache\StateInterface $cacheState,
86-
\Magento\Framework\Module\Dir\Reader $reader
94+
\Magento\Framework\Module\Dir\Reader $reader,
95+
Json $serializer = null
8796
) {
8897
$this->readFactory = $readFactory;
8998
$this->_scopeConfig = $scopeConfig;
9099
$this->_cacheState = $cacheState;
91100
$this->reader = $reader;
101+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
92102
}
93103

94104
/**
@@ -209,7 +219,7 @@ protected function _getDesignExceptions()
209219
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
210220
);
211221
if ($expressions) {
212-
$rules = array_values(unserialize($expressions));
222+
$rules = array_values($this->serializer->unserialize($expressions));
213223
foreach ($rules as $i => $rule) {
214224
if (preg_match('/^[\W]{1}(.*)[\W]{1}(\w+)?$/', $rule['regexp'], $matches)) {
215225
if (!empty($matches[2])) {

app/code/Magento/PageCache/Test/Unit/Model/ConfigTest.php

+37-20
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,52 @@
55
*/
66
namespace Magento\PageCache\Test\Unit\Model;
77

8+
use Magento\Framework\Serialize\Serializer\Json;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
810
use Magento\PageCache\Model\Config;
911

1012
class ConfigTest extends \PHPUnit_Framework_TestCase
1113
{
1214
/**
1315
* @var \Magento\PageCache\Model\Config
1416
*/
15-
protected $_model;
17+
private $config;
1618

1719
/**
1820
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Config\ScopeConfigInterface
1921
*/
20-
protected $_coreConfigMock;
22+
private $coreConfigMock;
2123

2224
/**
2325
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\StateInterface
2426
*/
25-
protected $_cacheState;
27+
private $cacheState;
2628

2729
/**
2830
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Module\Dir\Reader
2931
*/
30-
protected $moduleReader;
32+
private $moduleReader;
33+
34+
/**
35+
* @var \PHPUnit_Framework_MockObject_MockObject|Json
36+
*/
37+
private $serializerMock;
3138

3239
/**
3340
* setUp all mocks and data function
3441
*/
3542
protected function setUp()
3643
{
44+
$objectManager = new ObjectManager($this);
3745
$readFactoryMock = $this->getMock(
3846
\Magento\Framework\Filesystem\Directory\ReadFactory::class,
3947
[],
4048
[],
4149
'',
4250
false
4351
);
44-
$this->_coreConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
45-
$this->_cacheState = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\StateInterface::class);
52+
$this->coreConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
53+
$this->cacheState = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\StateInterface::class);
4654

4755
$modulesDirectoryMock = $this->getMock(
4856
\Magento\Framework\Filesystem\Directory\Write::class,
@@ -65,7 +73,7 @@ protected function setUp()
6573
)->will(
6674
$this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl'))
6775
);
68-
$this->_coreConfigMock->expects(
76+
$this->coreConfigMock->expects(
6977
$this->any()
7078
)->method(
7179
'getValue'
@@ -94,18 +102,23 @@ protected function setUp()
94102
\Magento\PageCache\Model\Config::XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX,
95103
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
96104
null,
97-
serialize([['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']])
105+
'serializedConfig'
98106
],
99107
]
100108
)
101109
);
102110

103111
$this->moduleReader = $this->getMock(\Magento\Framework\Module\Dir\Reader::class, [], [], '', false);
104-
$this->_model = new \Magento\PageCache\Model\Config(
105-
$readFactoryMock,
106-
$this->_coreConfigMock,
107-
$this->_cacheState,
108-
$this->moduleReader
112+
$this->serializerMock = $this->getMock(Json::class, [], [], '', false);
113+
$this->config = $objectManager->getObject(
114+
\Magento\PageCache\Model\Config::class,
115+
[
116+
'readFactory' => $readFactoryMock,
117+
'scopeConfig' => $this->coreConfigMock,
118+
'cacheState' => $this->cacheState,
119+
'reader' => $this->moduleReader,
120+
'serializer' => $this->serializerMock,
121+
]
109122
);
110123
}
111124

@@ -117,30 +130,34 @@ public function testGetVcl()
117130
$this->moduleReader->expects($this->once())
118131
->method('getModuleDir')
119132
->willReturn('/magento/app/code/Magento/PageCache');
120-
$test = $this->_model->getVclFile(Config::VARNISH_3_CONFIGURATION_PATH);
133+
$this->serializerMock->expects($this->once())
134+
->method('unserialize')
135+
->with('serializedConfig')
136+
->willReturn([['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']]);
137+
$test = $this->config->getVclFile(Config::VARNISH_3_CONFIGURATION_PATH);
121138
$this->assertEquals(file_get_contents(__DIR__ . '/_files/result.vcl'), $test);
122139
}
123140

124141
public function testGetTll()
125142
{
126-
$this->_coreConfigMock->expects($this->once())->method('getValue')->with(Config::XML_PAGECACHE_TTL);
127-
$this->_model->getTtl();
143+
$this->coreConfigMock->expects($this->once())->method('getValue')->with(Config::XML_PAGECACHE_TTL);
144+
$this->config->getTtl();
128145
}
129146

130147
/**
131148
* Whether a cache type is enabled
132149
*/
133150
public function testIsEnabled()
134151
{
135-
$this->_cacheState->expects($this->at(0))
152+
$this->cacheState->expects($this->at(0))
136153
->method('isEnabled')
137154
->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)
138155
->will($this->returnValue(true));
139-
$this->_cacheState->expects($this->at(1))
156+
$this->cacheState->expects($this->at(1))
140157
->method('isEnabled')
141158
->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)
142159
->will($this->returnValue(false));
143-
$this->assertTrue($this->_model->isEnabled());
144-
$this->assertFalse($this->_model->isEnabled());
160+
$this->assertTrue($this->config->isEnabled());
161+
$this->assertFalse($this->config->isEnabled());
145162
}
146163
}

dev/tests/integration/testsuite/Magento/Framework/App/AreaTest.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public function testInitDesign()
6262

6363
// @codingStandardsIgnoreStart
6464
/**
65-
* @magentoConfigFixture current_store design/theme/ua_regexp a:1:{s:1:"_";a:2:{s:6:"regexp";s:10:"/firefox/i";s:5:"value";s:13:"Magento/blank";}}
66-
* @magentoConfigFixture current_store design/package/ua_regexp a:1:{s:1:"_";a:2:{s:6:"regexp";s:10:"/firefox/i";s:5:"value";s:13:"Magento/blank";}}
65+
* @magentoConfigFixture current_store design/theme/ua_regexp {"_":{"regexp":"\/firefox\/i","value":"Magento\/blank"}}
6766
* @magentoAppIsolation enabled
6867
*/
6968
// @codingStandardsIgnoreEnd
@@ -82,8 +81,7 @@ public function testDetectDesignUserAgent()
8281

8382
// @codingStandardsIgnoreStart
8483
/**
85-
* @magentoConfigFixture current_store design/theme/ua_regexp a:1:{s:1:"_";a:2:{s:6:"regexp";s:10:"/firefox/i";s:5:"value";s:13:"Magento/blank";}}
86-
* @magentoConfigFixture current_store design/package/ua_regexp a:1:{s:1:"_";a:2:{s:6:"regexp";s:10:"/firefox/i";s:5:"value";s:13:"Magento/blank";}}
84+
* @magentoConfigFixture current_store design/theme/ua_regexp {"_":{"regexp":"\/firefox\/i","value":"Magento\/blank"}}
8785
* @magentoDataFixture Magento/Theme/_files/design_change.php
8886
* @magentoAppIsolation enabled
8987
*/
@@ -101,7 +99,7 @@ public function testDetectDesignDesignChange()
10199
/**
102100
* Test that non-frontend areas are not affected neither by user-agent reg expressions, nor by the "design change"
103101
*
104-
* @magentoConfigFixture current_store design/theme/ua_regexp a:1:{s:1:"_";a:2:{s:6:"regexp";s:10:"/firefox/i";s:5:"value";s:13:"Magento/blank";}}
102+
* @magentoConfigFixture current_store design/theme/ua_regexp {"_":{"regexp":"\/firefox\/i","value":"Magento\/blank"}}
105103
* magentoDataFixture Magento/Theme/_files/design_change.php
106104
* @magentoAppIsolation enabled
107105
*/

0 commit comments

Comments
 (0)