Skip to content

Commit 270000b

Browse files
Merge pull request #754 from magento-troll/MAGETWO-62324
Story: - MAGETWO-62324: Remove uses of unserialize in \Magento\Framework\Flag attribute flag_data
2 parents cdd83d5 + e315f2d commit 270000b

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

lib/internal/Magento/Framework/Flag.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,51 @@ class Flag extends Model\AbstractModel
2626
*/
2727
protected $_flagCode = null;
2828

29+
/**
30+
* Serializer for encode/decode string/data.
31+
*
32+
* @var \Magento\Framework\Serialize\Serializer\Json
33+
*/
34+
private $json;
35+
36+
/**
37+
* Serializer for encode/decode string/data.
38+
*
39+
* @var \Magento\Framework\Serialize\Serializer\Serialize
40+
*/
41+
private $serialize;
42+
43+
/**
44+
* @param \Magento\Framework\Model\Context $context
45+
* @param \Magento\Framework\Registry $registry
46+
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
47+
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
48+
* @param array $data
49+
* @param \Magento\Framework\Serialize\Serializer\Json $json
50+
* @param \Magento\Framework\Serialize\Serializer\Serialize $serialize
51+
*/
52+
public function __construct(
53+
\Magento\Framework\Model\Context $context,
54+
\Magento\Framework\Registry $registry,
55+
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
56+
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
57+
array $data = [],
58+
\Magento\Framework\Serialize\Serializer\Json $json = null,
59+
\Magento\Framework\Serialize\Serializer\Serialize $serialize = null
60+
) {
61+
$this->json = $json ?: \Magento\Framework\App\ObjectManager::getInstance()
62+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
63+
$this->serialize = $serialize ?: \Magento\Framework\App\ObjectManager::getInstance()
64+
->get(\Magento\Framework\Serialize\Serializer\Serialize::class);
65+
parent::__construct(
66+
$context,
67+
$registry,
68+
$resource,
69+
$resourceCollection,
70+
$data
71+
);
72+
}
73+
2974
/**
3075
* Init resource model
3176
* Set flag_code if it is specified in arguments
@@ -68,9 +113,13 @@ public function beforeSave()
68113
public function getFlagData()
69114
{
70115
if ($this->hasFlagData()) {
71-
return unserialize($this->getData('flag_data'));
72-
} else {
73-
return null;
116+
$flagData = $this->getData('flag_data');
117+
$data = $this->json->unserialize($flagData);
118+
if (JSON_ERROR_NONE == json_last_error()) {
119+
return $data;
120+
} else {
121+
return $this->serialize->unserialize($flagData);
122+
}
74123
}
75124
}
76125

@@ -82,7 +131,7 @@ public function getFlagData()
82131
*/
83132
public function setFlagData($value)
84133
{
85-
return $this->setData('flag_data', serialize($value));
134+
return $this->setData('flag_data', $this->json->serialize($value));
86135
}
87136

88137
/**

lib/internal/Magento/Framework/Test/Unit/FlagTest.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class FlagTest extends \PHPUnit_Framework_TestCase
1717
*/
1818
protected $flag;
1919

20+
/**
21+
* @var \Magento\Framework\Serialize\Serializer\Json
22+
*/
23+
private $json;
24+
25+
/**
26+
* @var \Magento\Framework\Serialize\Serializer\Serialize
27+
*/
28+
private $serialize;
29+
2030
protected function setUp()
2131
{
2232
$data = ['flag_code' => 'synchronize'];
@@ -71,13 +81,22 @@ protected function createInstance(array $data = [])
7181
$resourceCollection = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
7282
->disableOriginalConstructor()
7383
->getMockForAbstractClass();
84+
$this->json = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
85+
->setMethods(null)
86+
->getMock();
87+
88+
$this->serialize = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Serialize::class)
89+
->setMethods(null)
90+
->getMock();
7491

7592
$this->flag = new \Magento\Framework\Flag(
7693
$context,
7794
$registry,
7895
$resource,
7996
$resourceCollection,
80-
$data
97+
$data,
98+
$this->json,
99+
$this->serialize
81100
);
82101
}
83102

@@ -94,7 +113,17 @@ public function testConstruct()
94113
$this->assertEquals($flagCode, $this->flag->getFlagCode());
95114
}
96115

97-
public function testGetFlagData()
116+
public function testGetFlagDataJson()
117+
{
118+
$result = $this->flag->getFlagData();
119+
$this->assertNull($result);
120+
$flagData = json_encode('data');
121+
$this->flag->setData('flag_data', $flagData);
122+
$result = $this->flag->getFlagData();
123+
$this->assertEquals(json_decode($flagData), $result);
124+
}
125+
126+
public function testGetFlagDataSerialized()
98127
{
99128
$result = $this->flag->getFlagData();
100129
$this->assertNull($result);
@@ -108,7 +137,7 @@ public function testSetFlagData()
108137
{
109138
$flagData = 'data';
110139
$this->flag->setFlagData($flagData);
111-
$result = unserialize($this->flag->getData('flag_data'));
140+
$result = json_decode($this->flag->getData('flag_data'));
112141
$this->assertEquals($flagData, $result);
113142
}
114143

0 commit comments

Comments
 (0)