Skip to content

Commit 9a51e38

Browse files
committed
Merge branch 'MAGETWO-66067' of github.com:magento-troll/magento2ce into MAGETWO-65622
2 parents 0b26d9d + 6b2117d commit 9a51e38

File tree

4 files changed

+246
-6
lines changed

4 files changed

+246
-6
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+
private $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']) {

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+
private $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 {
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
{
90+
$data = [
91+
'number' => 42,
92+
'string' => 'string_value',
93+
'boolean' => true,
94+
'collection' => [1, 2, 3],
95+
'secret_key' => 999
96+
];
97+
$serialized_data = json_encode($data);
98+
99+
$optionMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item\Option::class)
100+
->disableOriginalConstructor()
101+
->getMock();
102+
$optionMock->expects($this->any())
103+
->method('load')
104+
->willReturnSelf();
105+
$optionMock->expects($this->any())
106+
->method('getId')
107+
->willReturn(true);
108+
$optionMock->expects($this->any())
109+
->method('getProductId')
110+
->willReturn('some_value');
111+
$optionMock->expects($this->any())
112+
->method('getValue')
113+
->willReturn($serialized_data);
114+
115+
$productOptionMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class)
116+
->disableOriginalConstructor()
117+
->getMock();
118+
$productOptionMock->expects($this->any())
119+
->method('load')
120+
->willReturnSelf();
121+
$productOptionMock->expects($this->any())
122+
->method('getId')
123+
->willReturn(true);
124+
$productOptionMock->expects($this->any())
125+
->method('getProductId')
126+
->willReturn('some_value');
127+
$productOptionMock->expects($this->any())
128+
->method('getType')
129+
->willReturn('file');
130+
131+
$this->objectManagerMock->expects($this->any())
132+
->method('create')
133+
->willReturnMap(
134+
[
135+
[\Magento\Wishlist\Model\Item\Option::class, [], $optionMock],
136+
[\Magento\Catalog\Model\Product\Option::class, [], $productOptionMock]
137+
]
138+
);
139+
140+
$this->requestMock->expects($this->any())
141+
->method('getParam')
142+
->willReturn(1);
143+
144+
$this->jsonMock->expects($this->once())
145+
->method('unserialize')
146+
->willReturnCallback(function ($value) {
147+
return json_decode($value, true);
148+
});
149+
150+
$this->assertEquals(null, $this->model->execute());
151+
}
152+
}

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)