Skip to content

Commit f03accf

Browse files
author
Oleksandr Dubovyk
committed
MAGETWO-66067: Remove usages of unserialize in module Magento/Wishlist
- Removed unserialize - Added Unit test
1 parent 75b91c0 commit f03accf

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

app/code/Magento/Wishlist/Model/Wishlist.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
1111
use Magento\Wishlist\Model\ResourceModel\Wishlist as ResourceWishlist;
1212
use Magento\Wishlist\Model\ResourceModel\Wishlist\Collection;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\Serialize\Serializer\Json;
1315

1416
/**
1517
* Wishlist model
@@ -118,6 +120,13 @@ class Wishlist extends \Magento\Framework\Model\AbstractModel implements \Magent
118120
*/
119121
protected $productRepository;
120122

123+
/**
124+
* Json Serializer Instance
125+
*
126+
* @var Json
127+
*/
128+
protected $serializer;
129+
121130
/**
122131
* @param \Magento\Framework\Model\Context $context
123132
* @param \Magento\Framework\Registry $registry
@@ -135,6 +144,7 @@ class Wishlist extends \Magento\Framework\Model\AbstractModel implements \Magent
135144
* @param ProductRepositoryInterface $productRepository
136145
* @param bool $useCurrentWebsite
137146
* @param array $data
147+
* @param Json|null $serializer
138148
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
139149
*/
140150
public function __construct(
@@ -153,7 +163,8 @@ public function __construct(
153163
\Magento\Framework\Stdlib\DateTime $dateTime,
154164
ProductRepositoryInterface $productRepository,
155165
$useCurrentWebsite = true,
156-
array $data = []
166+
array $data = [],
167+
Json $serializer = null
157168
) {
158169
$this->_useCurrentWebsite = $useCurrentWebsite;
159170
$this->_catalogProduct = $catalogProduct;
@@ -165,6 +176,7 @@ public function __construct(
165176
$this->_productFactory = $productFactory;
166177
$this->mathRandom = $mathRandom;
167178
$this->dateTime = $dateTime;
179+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
168180
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
169181
$this->productRepository = $productRepository;
170182
}
@@ -403,7 +415,7 @@ public function addNewItem($product, $buyRequest = null, $forciblySetQty = false
403415
if ($buyRequest instanceof \Magento\Framework\DataObject) {
404416
$_buyRequest = $buyRequest;
405417
} elseif (is_string($buyRequest)) {
406-
$_buyRequest = new \Magento\Framework\DataObject(unserialize($buyRequest));
418+
$_buyRequest = new \Magento\Framework\DataObject($this->serializer->unserialize($buyRequest));
407419
} elseif (is_array($buyRequest)) {
408420
$_buyRequest = new \Magento\Framework\DataObject($buyRequest);
409421
} else {

app/code/Magento/Wishlist/Test/Unit/Model/WishlistTest.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class WishlistTest extends \PHPUnit_Framework_TestCase
8787
*/
8888
protected $productRepository;
8989

90+
/**
91+
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
92+
*/
93+
protected $serializer;
94+
9095
protected function setUp()
9196
{
9297
$context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
@@ -134,6 +139,9 @@ protected function setUp()
134139
->disableOriginalConstructor()
135140
->getMock();
136141
$this->productRepository = $this->getMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
142+
$this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
143+
->disableOriginalConstructor()
144+
->getMock();
137145

138146
$context->expects($this->once())
139147
->method('getEventDispatcher')
@@ -154,7 +162,9 @@ protected function setUp()
154162
$this->mathRandom,
155163
$this->dateTime,
156164
$this->productRepository,
157-
false
165+
false,
166+
[],
167+
$this->serializer
158168
);
159169
}
160170

@@ -286,7 +296,61 @@ public function testUpdateItem($itemId, $buyRequest, $param)
286296
public function updateItemDataProvider()
287297
{
288298
return [
289-
'0' => [1, new \Magento\Framework\DataObject(), null],
299+
'0' => [1, new \Magento\Framework\DataObject(), null]
290300
];
291301
}
302+
303+
public function testAddNewItem()
304+
{
305+
$productId = 1;
306+
$storeId = 1;
307+
$buyRequest = json_encode([
308+
'number' => 42,
309+
'string' => 'string_value',
310+
'boolean' => true,
311+
'collection' => [1, 2, 3],
312+
'product' => 1,
313+
'form_key' => 'abc'
314+
]);
315+
$result = 'product';
316+
317+
$instanceType = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
318+
->disableOriginalConstructor()
319+
->getMock();
320+
$instanceType->expects($this->once())
321+
->method('processConfiguration')
322+
->willReturn('product');
323+
324+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
325+
->disableOriginalConstructor()
326+
->setMethods(['getId', 'hasWishlistStoreId', 'getStoreId', 'getTypeInstance'])
327+
->getMock();
328+
$productMock->expects($this->once())
329+
->method('getId')
330+
->willReturn($productId);
331+
$productMock->expects($this->once())
332+
->method('hasWishlistStoreId')
333+
->willReturn(false);
334+
$productMock->expects($this->once())
335+
->method('getStoreId')
336+
->willReturn($storeId);
337+
$productMock->expects($this->once())
338+
->method('getTypeInstance')
339+
->willReturn($instanceType);
340+
341+
$this->productRepository->expects($this->once())
342+
->method('getById')
343+
->with($productId, false, $storeId)
344+
->willReturn($productMock);
345+
346+
$this->serializer->expects($this->once())
347+
->method('unserialize')
348+
->willReturnCallback(
349+
function ($value) {
350+
return json_decode($value, true);
351+
}
352+
);
353+
354+
$this->assertEquals($result, $this->wishlist->addNewItem($productMock, $buyRequest));
355+
}
292356
}

0 commit comments

Comments
 (0)