Skip to content

Commit 6e9f410

Browse files
author
okarpenko
committed
Merge branch 'MAGETWO-34757' into BUGS
2 parents f774044 + 2edf50e commit 6e9f410

File tree

2 files changed

+95
-28
lines changed

2 files changed

+95
-28
lines changed

app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,19 @@ public function save($product)
326326
return $this;
327327
}
328328

329-
protected function getProductInfo(\Magento\Framework\Object $buyRequest, $product)
329+
protected function getProductInfo(\Magento\Framework\Object $buyRequest, $product, $isStrictProcessMode)
330330
{
331331
$productsInfo = $buyRequest->getSuperGroup() ?: [];
332-
333332
$associatedProducts = $this->getAssociatedProducts($product);
333+
334+
if (!is_array($productsInfo)) {
335+
return __('Please specify the quantity of product(s).')->render();
336+
}
334337
foreach ($associatedProducts as $subProduct) {
335338
if (!isset($productsInfo[$subProduct->getId()])) {
339+
if ($isStrictProcessMode && !$subProduct->getQty()) {
340+
return __('Please specify the quantity of product(s).')->render();
341+
}
336342
$productsInfo[$subProduct->getId()] = intval($subProduct->getQty());
337343
}
338344
}
@@ -354,16 +360,15 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ
354360
{
355361
$products = [];
356362
$associatedProductsInfo = [];
357-
$productsInfo = $this->getProductInfo($buyRequest, $product);
358363
$isStrictProcessMode = $this->_isStrictProcessMode($processMode);
364+
$productsInfo = $this->getProductInfo($buyRequest, $product, $isStrictProcessMode);
365+
if (is_string($productsInfo)) {
366+
return $productsInfo;
367+
}
359368
$associatedProducts = !$isStrictProcessMode || !empty($productsInfo)
360369
? $this->getAssociatedProducts($product)
361370
: false;
362371

363-
if ($associatedProducts && empty($productsInfo)) {
364-
return __('Please specify the quantity of product(s).')->render();
365-
}
366-
367372
foreach ($associatedProducts as $subProduct) {
368373
$qty = $productsInfo[$subProduct->getId()];
369374
if (!is_numeric($qty)) {

app/code/Magento/GroupedProduct/Test/Unit/Model/Product/Type/GroupedTest.php

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -300,20 +300,82 @@ public function testGetChildrenMsrpWhenNoChildrenWithMsrp()
300300

301301
public function testPrepareForCartAdvancedEmpty()
302302
{
303+
$this->product = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
303304
$buyRequest = new \Magento\Framework\Object();
304305
$expectedMsg = "Please specify the quantity of product(s).";
305306

306-
$this->assertEquals(
307-
$expectedMsg,
308-
$this->_model->prepareForCartAdvanced($buyRequest, $this->product)
307+
$productCollection = $this->getMock(
308+
'Magento\Catalog\Model\Resource\Product\Link\Product\Collection',
309+
[],
310+
[],
311+
'',
312+
false
309313
);
314+
$productCollection
315+
->expects($this->atLeastOnce())
316+
->method('setFlag')
317+
->willReturnSelf();
318+
$productCollection
319+
->expects($this->atLeastOnce())
320+
->method('setIsStrongMode')
321+
->willReturnSelf();
322+
$productCollection
323+
->expects($this->atLeastOnce())
324+
->method('setProduct');
325+
$productCollection
326+
->expects($this->atLeastOnce())
327+
->method('addAttributeToSelect')
328+
->willReturnSelf();
329+
$productCollection
330+
->expects($this->atLeastOnce())
331+
->method('addFilterByRequiredOptions')
332+
->willReturnSelf();
333+
$productCollection
334+
->expects($this->atLeastOnce())
335+
->method('setPositionOrder')
336+
->willReturnSelf();
337+
$productCollection
338+
->expects($this->atLeastOnce())
339+
->method('addStoreFilter')
340+
->willReturnSelf();
341+
$productCollection
342+
->expects($this->atLeastOnce())
343+
->method('addAttributeToFilter')
344+
->willReturnSelf();
345+
$items = [
346+
$this->getMock('Magento\Catalog\Model\Product', [], [], '', false),
347+
$this->getMock('Magento\Catalog\Model\Product', [], [], '', false)
348+
];
349+
$productCollection
350+
->expects($this->atLeastOnce())
351+
->method('getIterator')
352+
->willReturn(new \ArrayIterator($items));
353+
354+
$link = $this->getMock('Magento\Catalog\Model\Product\Link', [], [], '', false);
355+
$link
356+
->expects($this->any())
357+
->method('setLinkTypeId');
358+
$link
359+
->expects($this->atLeastOnce())
360+
->method('getProductCollection')
361+
->willReturn($productCollection);
362+
363+
$this->product
364+
->expects($this->atLeastOnce())
365+
->method('getLinkInstance')
366+
->willReturn($link);
367+
368+
$this->product
369+
->expects($this->any())
370+
->method('getData')
371+
->willReturn($items);
310372

311-
$buyRequest->setSuperGroup([]);
312373
$this->assertEquals(
313374
$expectedMsg,
314375
$this->_model->prepareForCartAdvanced($buyRequest, $this->product)
315376
);
316377

378+
317379
$buyRequest->setSuperGroup(1);
318380
$this->assertEquals(
319381
$expectedMsg,
@@ -329,8 +391,8 @@ public function testPrepareForCartAdvancedNoProductsStrictTrue()
329391

330392
$cached = true;
331393
$associatedProducts = [];
332-
$this->product->expects($this->once())->method('hasData')->will($this->returnValue($cached));
333-
$this->product->expects($this->once())->method('getData')->will($this->returnValue($associatedProducts));
394+
$this->product->expects($this->atLeastOnce())->method('hasData')->will($this->returnValue($cached));
395+
$this->product->expects($this->atLeastOnce())->method('getData')->will($this->returnValue($associatedProducts));
334396

335397
$this->assertEquals(
336398
$expectedMsg,
@@ -345,8 +407,8 @@ public function testPrepareForCartAdvancedNoProductsStrictFalse()
345407

346408
$cached = true;
347409
$associatedProducts = [];
348-
$this->product->expects($this->once())->method('hasData')->will($this->returnValue($cached));
349-
$this->product->expects($this->once())->method('getData')->will($this->returnValue($associatedProducts));
410+
$this->product->expects($this->atLeastOnce())->method('hasData')->will($this->returnValue($cached));
411+
$this->product->expects($this->atLeastOnce())->method('getData')->will($this->returnValue($associatedProducts));
350412

351413
$this->assertEquals(
352414
[0 => $this->product],
@@ -358,7 +420,7 @@ public function testPrepareForCartAdvancedWithProductsStrictFalseStringResult()
358420
{
359421
$associatedProduct = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
360422
$associatedId = 9384;
361-
$associatedProduct->expects($this->once())->method('getId')->will($this->returnValue($associatedId));
423+
$associatedProduct->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($associatedId));
362424

363425
$typeMock = $this->getMock(
364426
'Magento\Catalog\Model\Product\Type\AbstractType',
@@ -376,8 +438,8 @@ public function testPrepareForCartAdvancedWithProductsStrictFalseStringResult()
376438
$buyRequest->setSuperGroup([$associatedId => 1]);
377439

378440
$cached = true;
379-
$this->product->expects($this->once())->method('hasData')->will($this->returnValue($cached));
380-
$this->product->expects($this->once())->method('getData')->will($this->returnValue([$associatedProduct]));
441+
$this->product->expects($this->atLeastOnce())->method('hasData')->will($this->returnValue($cached));
442+
$this->product->expects($this->atLeastOnce())->method('getData')->will($this->returnValue([$associatedProduct]));
381443

382444
$this->assertEquals(
383445
$associatedPrepareResult,
@@ -387,10 +449,10 @@ public function testPrepareForCartAdvancedWithProductsStrictFalseStringResult()
387449

388450
public function testPrepareForCartAdvancedWithProductsStrictFalseEmptyArrayResult()
389451
{
390-
$expectedMsg = "We cannot process the item.";
452+
$expectedMsg = "Cannot process the item.";
391453
$associatedProduct = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
392454
$associatedId = 9384;
393-
$associatedProduct->expects($this->once())->method('getId')->will($this->returnValue($associatedId));
455+
$associatedProduct->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($associatedId));
394456

395457
$typeMock = $this->getMock(
396458
'Magento\Catalog\Model\Product\Type\AbstractType',
@@ -408,8 +470,8 @@ public function testPrepareForCartAdvancedWithProductsStrictFalseEmptyArrayResul
408470
$buyRequest->setSuperGroup([$associatedId => 1]);
409471

410472
$cached = true;
411-
$this->product->expects($this->once())->method('hasData')->will($this->returnValue($cached));
412-
$this->product->expects($this->once())->method('getData')->will($this->returnValue([$associatedProduct]));
473+
$this->product->expects($this->atLeastOnce())->method('hasData')->will($this->returnValue($cached));
474+
$this->product->expects($this->atLeastOnce())->method('getData')->will($this->returnValue([$associatedProduct]));
413475

414476
$this->assertEquals(
415477
$expectedMsg,
@@ -421,7 +483,7 @@ public function testPrepareForCartAdvancedWithProductsStrictFalse()
421483
{
422484
$associatedProduct = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
423485
$associatedId = 9384;
424-
$associatedProduct->expects($this->once())->method('getId')->will($this->returnValue($associatedId));
486+
$associatedProduct->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($associatedId));
425487

426488
$typeMock = $this->getMock(
427489
'Magento\Catalog\Model\Product\Type\AbstractType',
@@ -439,8 +501,8 @@ public function testPrepareForCartAdvancedWithProductsStrictFalse()
439501
$buyRequest->setSuperGroup([$associatedId => 1]);
440502

441503
$cached = true;
442-
$this->product->expects($this->once())->method('hasData')->will($this->returnValue($cached));
443-
$this->product->expects($this->once())->method('getData')->will($this->returnValue([$associatedProduct]));
504+
$this->product->expects($this->atLeastOnce())->method('hasData')->will($this->returnValue($cached));
505+
$this->product->expects($this->atLeastOnce())->method('getData')->will($this->returnValue([$associatedProduct]));
444506

445507
$this->assertEquals(
446508
[$this->product],
@@ -452,7 +514,7 @@ public function testPrepareForCartAdvancedWithProductsStrictTrue()
452514
{
453515
$associatedProduct = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
454516
$associatedId = 9384;
455-
$associatedProduct->expects($this->once())->method('getId')->will($this->returnValue($associatedId));
517+
$associatedProduct->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($associatedId));
456518

457519
$typeMock = $this->getMock(
458520
'Magento\Catalog\Model\Product\Type\AbstractType',
@@ -470,8 +532,8 @@ public function testPrepareForCartAdvancedWithProductsStrictTrue()
470532
$buyRequest->setSuperGroup([$associatedId => 1]);
471533

472534
$cached = true;
473-
$this->product->expects($this->once())->method('hasData')->will($this->returnValue($cached));
474-
$this->product->expects($this->once())->method('getData')->will($this->returnValue([$associatedProduct]));
535+
$this->product->expects($this->atLeastOnce())->method('hasData')->will($this->returnValue($cached));
536+
$this->product->expects($this->atLeastOnce())->method('getData')->will($this->returnValue([$associatedProduct]));
475537

476538
$this->assertEquals(
477539
$associatedPrepareResult,

0 commit comments

Comments
 (0)