Skip to content

Commit 0621f4f

Browse files
committed
MC-32414: API call bug on add to cart as guest
1 parent a73532a commit 0621f4f

File tree

2 files changed

+94
-116
lines changed

2 files changed

+94
-116
lines changed

app/code/Magento/Quote/Model/Quote/Item/Compare.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,14 @@ public function compare(Item $target, Item $compared)
7171
if ($target->getProductId() != $compared->getProductId()) {
7272
return false;
7373
}
74-
$targetOptions = $this->getOptions($target);
75-
$comparedOptions = $this->getOptions($compared);
7674

77-
if (array_diff_key($targetOptions, $comparedOptions) != array_diff_key($comparedOptions, $targetOptions)
78-
) {
75+
$targetOptionByCode = $target->getOptionsByCode();
76+
$comparedOptionsByCode = $compared->getOptionsByCode();
77+
if (!$target->compareOptions($targetOptionByCode, $comparedOptionsByCode)) {
7978
return false;
8079
}
81-
foreach ($targetOptions as $name => $value) {
82-
if ($comparedOptions[$name] != $value) {
83-
return false;
84-
}
80+
if (!$target->compareOptions($comparedOptionsByCode, $targetOptionByCode)) {
81+
return false;
8582
}
8683
return true;
8784
}

app/code/Magento/Quote/Test/Unit/Model/Quote/Item/CompareTest.php

Lines changed: 89 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,65 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Quote\Test\Unit\Model\Quote\Item;
87

8+
use Magento\Framework\Serialize\JsonValidator;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Quote\Model\Quote\Item;
12+
use Magento\Quote\Model\Quote\Item\Compare;
13+
use Magento\Quote\Model\Quote\Item\Option;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
917
/**
10-
* Class CompareTest
18+
* Tests the class that is used to compare Quote Item Options
1119
*/
12-
class CompareTest extends \PHPUnit\Framework\TestCase
20+
class CompareTest extends TestCase
1321
{
1422
/**
15-
* @var \Magento\Quote\Model\Quote\Item\Compare
23+
* @var Compare
1624
*/
1725
private $helper;
1826

1927
/**
20-
* @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject
28+
* @var Item|MockObject
2129
*/
2230
private $itemMock;
2331

2432
/**
25-
* @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject
33+
* @var Item|MockObject
2634
*/
2735
private $comparedMock;
2836

2937
/**
30-
* @var \Magento\Quote\Model\Quote\Item\Option|\PHPUnit_Framework_MockObject_MockObject
38+
* @var Option|MockObject
3139
*/
3240
private $optionMock;
3341

3442
/**
35-
* @var \Magento\Framework\Serialize\JsonValidator|\PHPUnit_Framework_MockObject_MockObject
43+
* @var JsonValidator|MockObject
3644
*/
3745
private $jsonValidatorMock;
3846

3947
/**
40-
* test setUp
48+
* @inheritdoc
4149
*/
4250
protected function setUp()
4351
{
4452
$this->itemMock = $this->createPartialMock(
45-
\Magento\Quote\Model\Quote\Item::class,
46-
['__wakeup', 'getProductId', 'getOptions']
53+
Item::class,
54+
['__wakeup', 'getProductId', 'getOptions', 'getOptionsByCode']
4755
);
4856
$this->comparedMock = $this->createPartialMock(
49-
\Magento\Quote\Model\Quote\Item::class,
50-
['__wakeup', 'getProductId', 'getOptions']
57+
Item::class,
58+
['__wakeup', 'getProductId', 'getOptions', 'getOptionsByCode']
5159
);
5260
$this->optionMock = $this->createPartialMock(
53-
\Magento\Quote\Model\Quote\Item\Option::class,
61+
Option::class,
5462
['__wakeup', 'getCode', 'getValue']
5563
);
56-
$serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
64+
$serializer = $this->getMockBuilder(Json::class)
5765
->setMethods(['unserialize'])
5866
->disableOriginalConstructor()
5967
->getMockForAbstractClass();
@@ -65,13 +73,13 @@ function ($value) {
6573
}
6674
);
6775

68-
$this->jsonValidatorMock = $this->getMockBuilder(\Magento\Framework\Serialize\JsonValidator::class)
76+
$this->jsonValidatorMock = $this->getMockBuilder(JsonValidator::class)
6977
->disableOriginalConstructor()
7078
->getMock();
7179

72-
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
80+
$objectManagerHelper = new ObjectManager($this);
7381
$this->helper = $objectManagerHelper->getObject(
74-
\Magento\Quote\Model\Quote\Item\Compare::class,
82+
Compare::class,
7583
[
7684
'serializer' => $serializer,
7785
'jsonValidator' => $this->jsonValidatorMock
@@ -82,17 +90,17 @@ function ($value) {
8290
/**
8391
* @param string $code
8492
* @param mixed $value
85-
* @return \PHPUnit_Framework_MockObject_MockObject
93+
* @return MockObject
8694
*/
8795
protected function getOptionMock($code, $value)
8896
{
8997
$optionMock = clone $this->optionMock;
90-
$optionMock->expects($this->once())
98+
$optionMock->expects($this->any())
9199
->method('getCode')
92-
->will($this->returnValue($code));
93-
$optionMock->expects($this->once())
100+
->willReturn($code);
101+
$optionMock->expects($this->any())
94102
->method('getValue')
95-
->will($this->returnValue($value));
103+
->willReturn($value);
96104
return $optionMock;
97105
}
98106

@@ -103,10 +111,10 @@ public function testCompareDifferentProduct()
103111
{
104112
$this->itemMock->expects($this->once())
105113
->method('getProductId')
106-
->will($this->returnValue(1));
114+
->willReturn(1);
107115
$this->itemMock->expects($this->once())
108116
->method('getProductId')
109-
->will($this->returnValue(2));
117+
->willReturn(2);
110118

111119
$this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
112120
}
@@ -116,36 +124,47 @@ public function testCompareDifferentProduct()
116124
*/
117125
public function testCompareProductWithDifferentOptions()
118126
{
127+
// Identical Product Ids
119128
$this->itemMock->expects($this->any())
120129
->method('getProductId')
121-
->will($this->returnValue(1));
130+
->willReturn(1);
122131
$this->comparedMock->expects($this->any())
123132
->method('getProductId')
124-
->will($this->returnValue(1));
133+
->willReturn(1);
125134

135+
// Identical Option Keys
126136
$this->itemMock->expects($this->any())
127137
->method('getOptions')
128-
->will(
129-
$this->returnValue(
130-
[
131-
$this->getOptionMock('option-1', 1),
132-
$this->getOptionMock('option-2', 'option-value'),
133-
$this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2]))
134-
]
135-
)
136-
);
138+
->willReturn([$this->getOptionMock('identical', 'value')]);
137139
$this->comparedMock->expects($this->any())
138140
->method('getOptions')
139-
->will($this->returnValue(
141+
->willReturn([$this->getOptionMock('identical', 'value')]);
142+
143+
// Different Option Values
144+
$this->itemMock->expects($this->once())
145+
->method('getOptionsByCode')
146+
->willReturn(
140147
[
141-
$this->getOptionMock('option-4', 1),
142-
$this->getOptionMock('option-2', 'option-value'),
143-
$this->getOptionMock('option-3', json_encode([
144-
'value' => 'value-1',
145-
'qty' => 2,
146-
])),
148+
'info_buyRequest' => $this->getOptionMock('info_buyRequest', ['value-1']),
149+
'option' => $this->getOptionMock('option', 1),
150+
'simple_product' => $this->getOptionMock('simple_product', 3),
151+
'product_qty_2' => $this->getOptionMock('product_qty_2', 10),
152+
'attributes' => $this->getOptionMock('attributes', 93),
147153
]
148-
));
154+
);
155+
156+
$this->comparedMock->expects($this->once())
157+
->method('getOptionsByCode')
158+
->willReturn(
159+
[
160+
'info_buyRequest' => $this->getOptionMock('info_buyRequest', ['value-2']),
161+
'option' => $this->getOptionMock('option', 1),
162+
'simple_product' => $this->getOptionMock('simple_product', 3),
163+
'product_qty_2' => $this->getOptionMock('product_qty_2', 10),
164+
'attributes' => $this->getOptionMock('attributes', 94),
165+
]
166+
);
167+
149168
$this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
150169
}
151170

@@ -156,24 +175,24 @@ public function testCompareItemWithComparedWithoutOption()
156175
{
157176
$this->itemMock->expects($this->any())
158177
->method('getProductId')
159-
->will($this->returnValue(1));
178+
->willReturn(1);
160179
$this->comparedMock->expects($this->any())
161180
->method('getProductId')
162-
->will($this->returnValue(1));
163-
$this->itemMock->expects($this->any())
164-
->method('getOptions')
165-
->will(
166-
$this->returnValue(
167-
[
168-
$this->getOptionMock('option-1', 1),
169-
$this->getOptionMock('option-2', 'option-value'),
170-
$this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2])),
171-
]
172-
)
181+
->willReturn(1);
182+
$this->itemMock->expects($this->once())
183+
->method('getOptionsByCode')
184+
->willReturn(
185+
[
186+
'info_buyRequest' => $this->getOptionMock('info_buyRequest', ['value-1']),
187+
'option' => $this->getOptionMock('option', 1),
188+
'simple_product' => $this->getOptionMock('simple_product', 3),
189+
'product_qty_2' => $this->getOptionMock('product_qty_2', 10),
190+
'attributes' => $this->getOptionMock('attributes', 93),
191+
]
173192
);
174193
$this->comparedMock->expects($this->any())
175-
->method('getOptions')
176-
->will($this->returnValue([]));
194+
->method('getOptionsByCode')
195+
->willReturn([]);
177196
$this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
178197
}
179198

@@ -184,62 +203,24 @@ public function testCompareItemWithoutOptionWithCompared()
184203
{
185204
$this->itemMock->expects($this->any())
186205
->method('getProductId')
187-
->will($this->returnValue(1));
206+
->willReturn(1);
188207
$this->comparedMock->expects($this->any())
189208
->method('getProductId')
190-
->will($this->returnValue(1));
191-
$this->comparedMock->expects($this->any())
192-
->method('getOptions')
193-
->will($this->returnValue(
194-
[
195-
$this->getOptionMock('option-1', 1),
196-
$this->getOptionMock('option-2', 'option-value'),
197-
$this->getOptionMock(
198-
'option-3',
199-
json_encode(['value' => 'value-1', 'qty' => 2])
200-
),
201-
]
202-
));
203-
$this->itemMock->expects($this->any())
204-
->method('getOptions')
205-
->will($this->returnValue([]));
206-
$this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
207-
}
208-
209-
/**
210-
* Verify that compare ignores empty options.
211-
*/
212-
public function testCompareWithEmptyValues()
213-
{
214-
$itemOptionValue = '{"non-empty-option":"test","empty_option":""}';
215-
$comparedOptionValue = '{"non-empty-option":"test"}';
216-
217-
$this->jsonValidatorMock->expects($this->any())
218-
->method('isValid')
219-
->willReturn(true);
220-
221-
$this->itemMock->expects($this->any())
222-
->method('getProductId')
223-
->will($this->returnValue(1));
224-
$this->comparedMock->expects($this->any())
225-
->method('getProductId')
226-
->will($this->returnValue(1));
227-
228-
$this->itemMock->expects($this->once())
229-
->method('getOptions')
230-
->willReturn(
231-
[
232-
$this->getOptionMock('option-1', $itemOptionValue)
233-
]
234-
);
209+
->willReturn(1);
235210
$this->comparedMock->expects($this->once())
236-
->method('getOptions')
211+
->method('getOptionsByCode')
237212
->willReturn(
238213
[
239-
$this->getOptionMock('option-1', $comparedOptionValue)
214+
'info_buyRequest' => $this->getOptionMock('info_buyRequest', ['value-2']),
215+
'option' => $this->getOptionMock('option', 1),
216+
'simple_product' => $this->getOptionMock('simple_product', 3),
217+
'product_qty_2' => $this->getOptionMock('product_qty_2', 10),
218+
'attributes' => $this->getOptionMock('attributes', 94),
240219
]
241220
);
242-
243-
$this->assertTrue($this->helper->compare($this->itemMock, $this->comparedMock));
221+
$this->itemMock->expects($this->any())
222+
->method('getOptionsByCode')
223+
->willReturn([]);
224+
$this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
244225
}
245226
}

0 commit comments

Comments
 (0)