Skip to content

Commit a2e6b6b

Browse files
committed
MAGETWO-64547: Bundle Products - The options you selected are not available.
1 parent 06e054c commit a2e6b6b

File tree

2 files changed

+74
-23
lines changed

2 files changed

+74
-23
lines changed

app/code/Magento/Bundle/Test/Unit/Block/Catalog/Product/View/Type/Bundle/OptionTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function setUp()
5959

6060
public function testSetOption()
6161
{
62+
$selectionId = 315;
6263
$this->product->expects($this->atLeastOnce())
6364
->method('hasPreconfiguredValues')
6465
->will($this->returnValue(true));
@@ -71,7 +72,9 @@ public function testSetOption()
7172
$option = $this->getMock(\Magento\Bundle\Model\Option::class, [], [], '', false);
7273
$option->expects($this->any())->method('getId')->will($this->returnValue(15));
7374

74-
$otherOption = $this->getMock(\Magento\Bundle\Model\Option::class, [], [], '', false);
75+
$otherOption = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)
76+
->disableOriginalConstructor()
77+
->getMock();
7578
$otherOption->expects($this->any())->method('getId')->will($this->returnValue(16));
7679

7780
$selection = $this->getMock(
@@ -81,7 +84,16 @@ public function testSetOption()
8184
'',
8285
false
8386
);
84-
$selection->expects($this->atLeastOnce())->method('getSelectionId')->will($this->returnValue(315));
87+
$otherSelection = $this->getMock(
88+
\Magento\Catalog\Model\Product::class,
89+
['getSelectionId', '__wakeup'],
90+
[],
91+
'',
92+
false
93+
);
94+
$otherOption->expects($this->any())->method('getSelectionById')->willReturn($selection);
95+
$selection->expects($this->atLeastOnce())->method('getSelectionId')->willReturn($selectionId);
96+
$option->expects($this->once())->method('getSelectionById')->with(315)->willReturn($otherSelection);
8597

8698
$this->assertSame($this->block, $this->block->setOption($option));
8799
$this->assertTrue($this->block->isSelected($selection));

app/code/Magento/Bundle/Test/Unit/Model/Product/TypeTest.php

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Catalog\Model\Product\Option\Type\DefaultType;
1313
use Magento\Framework\DataObject;
14+
use Magento\Framework\EntityManager\EntityMetadataInterface;
15+
use Magento\Framework\EntityManager\MetadataPool;
1416
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\Serialize\Serializer\Json;
1618

@@ -38,7 +40,7 @@ class TypeTest extends \PHPUnit_Framework_TestCase
3840
/**
3941
* @var \Magento\Bundle\Model\ResourceModel\Selection\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
4042
*/
41-
protected $bundleCollection;
43+
protected $bundleCollectionFactory;
4244

4345
/**
4446
* @var \Magento\Catalog\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
@@ -75,6 +77,11 @@ class TypeTest extends \PHPUnit_Framework_TestCase
7577
*/
7678
private $priceCurrency;
7779

80+
/**
81+
* @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
82+
*/
83+
private $metadataPool;
84+
7885
/**
7986
* @var Json
8087
*/
@@ -85,7 +92,7 @@ class TypeTest extends \PHPUnit_Framework_TestCase
8592
*/
8693
protected function setUp()
8794
{
88-
$this->bundleCollection =
95+
$this->bundleCollectionFactory =
8996
$this->getMockBuilder(\Magento\Bundle\Model\ResourceModel\Selection\CollectionFactory::class)
9097
->setMethods(
9198
[
@@ -148,21 +155,26 @@ protected function setUp()
148155
->disableOriginalConstructor()
149156
->getMock();
150157

158+
$this->metadataPool = $this->getMockBuilder(MetadataPool::class)
159+
->disableOriginalConstructor()
160+
->getMock();
161+
151162
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
152163
$this->model = $objectHelper->getObject(
153164
\Magento\Bundle\Model\Product\Type::class,
154165
[
155166
'bundleModelSelection' => $this->bundleModelSelection,
156167
'bundleFactory' => $this->bundleFactory,
157-
'bundleCollection' => $this->bundleCollection,
168+
'bundleCollection' => $this->bundleCollectionFactory,
158169
'bundleOption' => $this->bundleOptionFactory,
159170
'catalogData' => $this->catalogData,
160171
'storeManager' => $this->storeManager,
161172
'stockRegistry' => $this->stockRegistry,
162173
'stockState' => $this->stockState,
163174
'catalogProduct' => $this->catalogProduct,
164175
'priceCurrency' => $this->priceCurrency,
165-
'serializer' => $this->serializer
176+
'serializer' => $this->serializer,
177+
'metadataPool' => $this->metadataPool,
166178
]
167179
);
168180
}
@@ -1935,6 +1947,7 @@ public function testGetSelectionsByIds()
19351947
$storeId = 2;
19361948
$websiteId = 1;
19371949
$storeFilter = 'store_filter';
1950+
$this->expectProductEntityMetadata();
19381951
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
19391952
->disableOriginalConstructor()
19401953
->getMock();
@@ -1984,7 +1997,7 @@ public function testGetSelectionsByIds()
19841997
->method('getWebsiteId')
19851998
->will($this->returnValue($websiteId));
19861999

1987-
$this->bundleCollection->expects($this->once())
2000+
$this->bundleCollectionFactory->expects($this->once())
19882001
->method('create')
19892002
->will($this->returnValue($usedSelectionsMock));
19902003

@@ -2155,9 +2168,11 @@ public function testIsSalableWithRequiredOptionsTrue()
21552168
$option3->method('getId')
21562169
->willReturn(30);
21572170

2171+
$this->expectProductEntityMetadata();
2172+
21582173
$optionCollectionMock = $this->getOptionCollectionMock([$option1, $option2, $option3]);
21592174
$selectionCollectionMock = $this->getSelectionCollectionMock([$option1, $option2]);
2160-
$this->bundleCollection->expects($this->atLeastOnce())
2175+
$this->bundleCollectionFactory->expects($this->atLeastOnce())
21612176
->method('create')
21622177
->will($this->returnValue($selectionCollectionMock));
21632178

@@ -2196,8 +2211,9 @@ public function testIsSalableWithEmptySelectionsCollection()
21962211
$option = $this->getRequiredOptionMock(1, 10);
21972212
$optionCollectionMock = $this->getOptionCollectionMock([$option]);
21982213
$selectionCollectionMock = $this->getSelectionCollectionMock([]);
2214+
$this->expectProductEntityMetadata();
21992215

2200-
$this->bundleCollection->expects($this->once())
2216+
$this->bundleCollectionFactory->expects($this->once())
22012217
->method('create')
22022218
->will($this->returnValue($selectionCollectionMock));
22032219

@@ -2220,6 +2236,7 @@ public function testIsSalableWithNonSalableRequiredOptions()
22202236
$option1 = $this->getRequiredOptionMock(10, 10);
22212237
$option2 = $this->getRequiredOptionMock(20, 10);
22222238
$optionCollectionMock = $this->getOptionCollectionMock([$option1, $option2]);
2239+
$this->expectProductEntityMetadata();
22232240

22242241
$selection1 = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
22252242
->setMethods(['isSalable'])
@@ -2242,7 +2259,7 @@ public function testIsSalableWithNonSalableRequiredOptions()
22422259
$selectionCollectionMock1 = $this->getSelectionCollectionMock([$selection1]);
22432260
$selectionCollectionMock2 = $this->getSelectionCollectionMock([$selection2]);
22442261

2245-
$this->bundleCollection->expects($this->exactly(2))
2262+
$this->bundleCollectionFactory->expects($this->exactly(2))
22462263
->method('create')
22472264
->will($this->onConsecutiveCalls(
22482265
$selectionCollectionMock1,
@@ -2438,14 +2455,15 @@ public function testGetSelectionsCollection()
24382455
]
24392456
)
24402457
->getMock();
2458+
$this->expectProductEntityMetadata();
24412459
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
24422460
->disableOriginalConstructor()
24432461
->setMethods(['getWebsiteId'])
24442462
->getMock();
24452463

24462464
$product->expects($this->once())->method('getStoreId')->willReturn('store_id');
24472465
$selectionCollection = $this->getSelectionCollection();
2448-
$this->bundleCollection->expects($this->once())->method('create')->willReturn($selectionCollection);
2466+
$this->bundleCollectionFactory->expects($this->once())->method('create')->willReturn($selectionCollection);
24492467
$this->storeManager->expects($this->once())->method('getStore')->willReturn($store);
24502468
$store->expects($this->once())->method('getWebsiteId')->willReturn('website_id');
24512469
$selectionCollection->expects($this->any())->method('joinPrices')->with('website_id')->willReturnSelf();
@@ -2496,6 +2514,7 @@ public function testGetProductsToPurchaseByReqGroups()
24962514
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
24972515
->disableOriginalConstructor()
24982516
->getMock();
2517+
$this->expectProductEntityMetadata();
24992518
$resourceClassName = \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::class;
25002519
$dbResourceMock = $this->getMockBuilder($resourceClassName)
25012520
->setMethods(['getItems'])
@@ -2506,7 +2525,7 @@ public function testGetProductsToPurchaseByReqGroups()
25062525
->setMethods(['getId', 'getRequired'])
25072526
->getMock();
25082527
$selectionCollection = $this->getSelectionCollection();
2509-
$this->bundleCollection->expects($this->once())->method('create')->willReturn($selectionCollection);
2528+
$this->bundleCollectionFactory->expects($this->once())->method('create')->willReturn($selectionCollection);
25102529

25112530
$selectionItem = $this->getMockBuilder(\Magento\Framework\DataObject::class)
25122531
->disableOriginalConstructor()
@@ -2554,6 +2573,7 @@ public function testHasOptions()
25542573
->disableOriginalConstructor()
25552574
->setMethods(['_wakeup', 'hasData', 'getData', 'setData', 'getId', 'getStoreId'])
25562575
->getMock();
2576+
$this->expectProductEntityMetadata();
25572577
$optionCollection = $this->getMockBuilder(\Magento\Bundle\Model\ResourceModel\Option\Collection::class)
25582578
->disableOriginalConstructor()
25592579
->setMethods(['getAllIds'])
@@ -2563,7 +2583,7 @@ public function testHasOptions()
25632583
->expects($this->any())
25642584
->method('getSize')
25652585
->willReturn(1);
2566-
$this->bundleCollection->expects($this->once())->method('create')->willReturn($selectionCollection);
2586+
$this->bundleCollectionFactory->expects($this->once())->method('create')->willReturn($selectionCollection);
25672587

25682588
$product->expects($this->any())->method('getStoreId')->willReturn(0);
25692589
$product->expects($this->once())
@@ -2590,6 +2610,7 @@ public function testCheckProductBuyStateEmptyOptionsException()
25902610
{
25912611
$this->mockBundleCollection();
25922612
$product = $this->getProductMock();
2613+
$this->expectProductEntityMetadata();
25932614
$product->method('getCustomOption')->willReturnMap([
25942615
['bundle_selection_ids', new DataObject(['value' => '[]'])],
25952616
['info_buyRequest', new DataObject(['value' => json_encode(['bundle_option' => ''])])],
@@ -2615,13 +2636,14 @@ public function testCheckProductBuyStateMissedOptionException($element, $expecte
26152636
{
26162637
$this->mockBundleCollection();
26172638
$product = $this->getProductMock();
2639+
$this->expectProductEntityMetadata();
26182640
$product->method('getCustomOption')->willReturnMap([
26192641
['bundle_selection_ids', new DataObject(['value' => json_encode([1])])],
26202642
['info_buyRequest', new DataObject(['value' => json_encode(['bundle_option' => [1]])])],
26212643
]);
26222644
$product->setCustomOption(json_encode([]));
26232645

2624-
$this->bundleCollection->method('getItemById')->willReturn($element);
2646+
$this->bundleCollectionFactory->method('getItemById')->willReturn($element);
26252647
$this->catalogProduct->setSkipSaleableCheck($check);
26262648

26272649
try {
@@ -2644,6 +2666,7 @@ public function testCheckProductBuyStateRequiredOptionException()
26442666
{
26452667
$this->mockBundleCollection();
26462668
$product = $this->getProductMock();
2669+
$this->expectProductEntityMetadata();
26472670
$product->method('getCustomOption')->willReturnMap([
26482671
['bundle_selection_ids', new DataObject(['value' => json_encode([])])],
26492672
['info_buyRequest', new DataObject(['value' => json_encode(['bundle_option' => [1]])])],
@@ -2656,7 +2679,7 @@ public function testCheckProductBuyStateRequiredOptionException()
26562679
->getMock();
26572680
$falseSelection->method('isSalable')->willReturn(false);
26582681

2659-
$this->bundleCollection->method('getItemById')->willReturn($falseSelection);
2682+
$this->bundleCollectionFactory->method('getItemById')->willReturn($falseSelection);
26602683
$this->catalogProduct->setSkipSaleableCheck(false);
26612684

26622685
try {
@@ -2709,14 +2732,18 @@ public function getProductMock()
27092732
*/
27102733
public function mockBundleCollection()
27112734
{
2712-
$this->bundleCollection->method('create')->willReturn($this->bundleCollection);
2713-
$this->bundleCollection->method('addAttributeToSelect')->willReturn($this->bundleCollection);
2714-
$this->bundleCollection->method('setFlag')->willReturn($this->bundleCollection);
2715-
$this->bundleCollection->method('setPositionOrder')->willReturn($this->bundleCollection);
2716-
$this->bundleCollection->method('addStoreFilter')->willReturn($this->bundleCollection);
2717-
$this->bundleCollection->method('setStoreId')->willReturn($this->bundleCollection);
2718-
$this->bundleCollection->method('addFilterByRequiredOptions')->willReturn($this->bundleCollection);
2719-
$this->bundleCollection->method('setOptionIdsFilter')->willReturn($this->bundleCollection);
2735+
$selectionCollectionMock = $this->getSelectionCollectionMock([]);
2736+
$this->bundleCollectionFactory->expects($this->once())
2737+
->method('create')
2738+
->will($this->returnValue($selectionCollectionMock));
2739+
$this->bundleCollectionFactory->method('create')->willReturn($selectionCollectionMock);
2740+
$selectionCollectionMock->method('addAttributeToSelect')->willReturn($selectionCollectionMock);
2741+
$selectionCollectionMock->method('setFlag')->willReturn($selectionCollectionMock);
2742+
$selectionCollectionMock->method('setPositionOrder')->willReturn($selectionCollectionMock);
2743+
$selectionCollectionMock->method('addStoreFilter')->willReturn($selectionCollectionMock);
2744+
$selectionCollectionMock->method('setStoreId')->willReturn($selectionCollectionMock);
2745+
$selectionCollectionMock->method('addFilterByRequiredOptions')->willReturn($selectionCollectionMock);
2746+
$selectionCollectionMock->method('setOptionIdsFilter')->willReturn($selectionCollectionMock);
27202747
}
27212748

27222749
/**
@@ -2743,4 +2770,16 @@ public function notAvailableOptionProvider()
27432770
],
27442771
];
27452772
}
2773+
2774+
/**
2775+
* @return void
2776+
*/
2777+
private function expectProductEntityMetadata()
2778+
{
2779+
$entityMetadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
2780+
->getMockForAbstractClass();
2781+
$this->metadataPool->expects($this->any())->method('getMetadata')
2782+
->with(\Magento\Catalog\Api\Data\ProductInterface::class)
2783+
->willReturn($entityMetadataMock);
2784+
}
27462785
}

0 commit comments

Comments
 (0)