Skip to content

Commit 2951448

Browse files
author
Oleksandr Dubovyk
committed
MAGETWO-66067: Remove usages of unserialize in module Magento/Wishlist
- Remove serialize - Added unit test
1 parent aa27856 commit 2951448

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-2
lines changed

app/code/Magento/Wishlist/Controller/Index/DownloadCustomOption.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\Framework\App\Action;
99
use Magento\Framework\App\Filesystem\DirectoryList;
1010
use Magento\Framework\Controller\ResultFactory;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Serialize\Serializer\Json;
1113

1214
class DownloadCustomOption extends \Magento\Wishlist\Controller\AbstractIndex
1315
{
@@ -16,15 +18,25 @@ class DownloadCustomOption extends \Magento\Wishlist\Controller\AbstractIndex
1618
*/
1719
protected $_fileResponseFactory;
1820

21+
/**
22+
* Json Serializer Instance
23+
*
24+
* @var Json
25+
*/
26+
protected $json;
27+
1928
/**
2029
* @param Action\Context $context
2130
* @param \Magento\Framework\App\Response\Http\FileFactory $fileResponseFactory
31+
* @param Json|null $json
2232
*/
2333
public function __construct(
2434
Action\Context $context,
25-
\Magento\Framework\App\Response\Http\FileFactory $fileResponseFactory
35+
\Magento\Framework\App\Response\Http\FileFactory $fileResponseFactory,
36+
Json $json = null
2637
) {
2738
$this->_fileResponseFactory = $fileResponseFactory;
39+
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
2840
parent::__construct($context);
2941
}
3042

@@ -73,7 +85,7 @@ public function execute()
7385
}
7486

7587
try {
76-
$info = unserialize($option->getValue());
88+
$info = $this->json->unserialize($option->getValue());
7789
$secretKey = $this->getRequest()->getParam('key');
7890

7991
if ($secretKey == $info['secret_key']) {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Wishlist\Test\Unit\Controller\Index;
7+
8+
class DownloadCustomOptionTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Wishlist\Controller\Index\DownloadCustomOption
12+
*/
13+
protected $model;
14+
15+
/**
16+
* @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
protected $contextMock;
19+
20+
/**
21+
* @var \Magento\Framework\App\Response\Http\FileFactory|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $fileResponseFactoryMock;
24+
25+
/**
26+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
protected $requestMock;
29+
30+
/**
31+
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
protected $objectManagerMock;
34+
35+
/**
36+
* @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
protected $resultFactoryMock;
39+
40+
/**
41+
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
protected $jsonMock;
44+
45+
protected function setUp()
46+
{
47+
$this->fileResponseFactoryMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http\FileFactory::class)
48+
->disableOriginalConstructor()
49+
->getMock();
50+
51+
$this->jsonMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
52+
->disableOriginalConstructor()
53+
->getMock();
54+
55+
$this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
56+
->disableOriginalConstructor()
57+
->setMethods(['create', 'get', 'configure'])
58+
->getMock();
59+
60+
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
61+
->disableOriginalConstructor()
62+
->getMock();
63+
64+
$this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
65+
->disableOriginalConstructor()
66+
->getMock();
67+
68+
$this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->contextMock->expects($this->any())
72+
->method('getObjectManager')
73+
->willReturn($this->objectManagerMock);
74+
$this->contextMock->expects($this->any())
75+
->method('getRequest')
76+
->willReturn($this->requestMock);
77+
$this->contextMock->expects($this->any())
78+
->method('getResultFactory')
79+
->willReturn($this->resultFactoryMock);
80+
81+
$this->model = new \Magento\Wishlist\Controller\Index\DownloadCustomOption(
82+
$this->contextMock,
83+
$this->fileResponseFactoryMock,
84+
$this->jsonMock
85+
);
86+
}
87+
88+
public function testExecute() {
89+
$data = [
90+
'number' => 42,
91+
'string' => 'string_value',
92+
'boolean' => true,
93+
'collection' => [1, 2, 3],
94+
'secret_key' => 999
95+
];
96+
$serialized_data = json_encode($data);
97+
98+
$optionMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item\Option::class)
99+
->disableOriginalConstructor()
100+
->getMock();
101+
$optionMock->expects($this->any())
102+
->method('load')
103+
->willReturnSelf();
104+
$optionMock->expects($this->any())
105+
->method('getId')
106+
->willReturn(true);
107+
$optionMock->expects($this->any())
108+
->method('getProductId')
109+
->willReturn('some_value');
110+
$optionMock->expects($this->any())
111+
->method('getValue')
112+
->willReturn($serialized_data);
113+
114+
$productOptionMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class)
115+
->disableOriginalConstructor()
116+
->getMock();
117+
$productOptionMock->expects($this->any())
118+
->method('load')
119+
->willReturnSelf();
120+
$productOptionMock->expects($this->any())
121+
->method('getId')
122+
->willReturn(true);
123+
$productOptionMock->expects($this->any())
124+
->method('getProductId')
125+
->willReturn('some_value');
126+
$productOptionMock->expects($this->any())
127+
->method('getType')
128+
->willReturn('file');
129+
130+
$this->objectManagerMock->expects($this->any())
131+
->method('create')
132+
->willReturnMap(
133+
[
134+
[\Magento\Wishlist\Model\Item\Option::class, [], $optionMock],
135+
[\Magento\Catalog\Model\Product\Option::class, [], $productOptionMock]
136+
]
137+
);
138+
139+
$this->requestMock->expects($this->any())
140+
->method('getParam')
141+
->willReturn(1);
142+
143+
$this->jsonMock->expects($this->once())
144+
->method('unserialize')
145+
->willReturnCallback(function ($value) {
146+
return json_decode($value, true);
147+
});
148+
149+
$this->assertEquals(null, $this->model->execute());
150+
}
151+
}

0 commit comments

Comments
 (0)