Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into MAGETWO-48557-FA…
Browse files Browse the repository at this point in the history
…T-failure-after-pr342
  • Loading branch information
Joan He committed Feb 2, 2016
2 parents a24b73d + 50ff187 commit bf6d905
Show file tree
Hide file tree
Showing 22 changed files with 581 additions and 373 deletions.
52 changes: 12 additions & 40 deletions app/code/Magento/Bundle/Model/Product/CopyConstructor/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,30 @@
*/
namespace Magento\Bundle\Model\Product\CopyConstructor;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Type;

class Bundle implements \Magento\Catalog\Model\Product\CopyConstructorInterface
{
/**
* Duplicating bundle options and selections
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Product $duplicate
* @param Product $product
* @param Product $duplicate
* @return void
*/
public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate)
public function build(Product $product, Product $duplicate)
{
if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
if ($product->getTypeId() != Type::TYPE_BUNDLE) {
//do nothing if not bundle
return;
}

$product->getTypeInstance()->setStoreFilter($product->getStoreId(), $product);
$optionCollection = $product->getTypeInstance()->getOptionsCollection($product);
$selectionCollection = $product->getTypeInstance()->getSelectionsCollection(
$product->getTypeInstance()->getOptionsIds($product),
$product
);
$optionCollection->appendSelections($selectionCollection);

$optionRawData = [];
$selectionRawData = [];

$i = 0;
foreach ($optionCollection as $option) {
$optionRawData[$i] = [
'required' => $option->getData('required'),
'position' => $option->getData('position'),
'type' => $option->getData('type'),
'title' => $option->getData('title') ? $option->getData('title') : $option->getData('default_title'),
'delete' => '',
];
foreach ($option->getSelections() as $selection) {
$selectionRawData[$i][] = [
'product_id' => $selection->getProductId(),
'position' => $selection->getPosition(),
'is_default' => $selection->getIsDefault(),
'selection_price_type' => $selection->getSelectionPriceType(),
'selection_price_value' => $selection->getSelectionPriceValue(),
'selection_qty' => $selection->getSelectionQty(),
'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
'delete' => '',
];
}
$i++;
$bundleOptions = $product->getExtensionAttributes()->getBundleProductOptions();
$duplicatedBundleOptions = [];
foreach ($bundleOptions as $key => $bundleOption) {
$duplicatedBundleOptions[$key] = clone $bundleOption;
}

$duplicate->setBundleOptionsData($optionRawData);
$duplicate->setBundleSelectionsData($selectionRawData);
$duplicate->getExtensionAttributes()->setBundleProductOptions($duplicatedBundleOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,38 @@
*/
namespace Magento\Bundle\Test\Unit\Model\Product\CopyConstructor;

use Magento\Bundle\Api\Data\BundleOptionInterface;
use Magento\Bundle\Model\Product\CopyConstructor\Bundle;
use Magento\Catalog\Api\Data\ProductExtensionInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Type;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class BundleTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
protected $product;

/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
protected $duplicate;

/**
* @var \Magento\Bundle\Model\Product\CopyConstructor\Bundle
* @var Bundle
*/
protected $model;

/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
*/
protected $objectManager;

protected function setUp()
{
// Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate
$this->product = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
$this->duplicate = $this->getMock(
'Magento\Catalog\Model\Product',
['setBundleOptionsData', 'setBundleSelectionsData', '__wakeup'],
[],
'',
false
);
$this->model = new \Magento\Bundle\Model\Product\CopyConstructor\Bundle();
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$objectManager = new ObjectManager($this);
$this->model = $objectManager->getObject(Bundle::class);
}

public function testBuildNegative()
{
$this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('other product'));
$this->product->expects($this->never())->method('getTypeInstance');
$this->model->build($this->product, $this->duplicate);
$product = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$duplicate = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$product->expects($this->once())
->method('getTypeId')
->willReturn('other product type');
$this->model->build($product, $duplicate);
}

/**
Expand All @@ -55,112 +45,48 @@ public function testBuildNegative()
*/
public function testBuildPositive()
{
//prepare mocks and data samples
$instance = $this->getMock(
'Magento\Bundle\Model\Product\Type',
['setStoreFilter', 'getOptionsCollection', 'getSelectionsCollection', 'getOptionsIds'],
[],
'',
false
);
$option = $this->getMock(
'Magento\Bundle\Model\Option',
['getSelections', '__wakeup', 'getData'],
[],
'',
false
);
$options = [$option];
$optionCollection = $this->objectManager->getCollectionMock(
'Magento\Bundle\Model\ResourceModel\Option\Collection',
$options
);
$optionRawData = [
['required' => true, 'position' => 100, 'type' => 'someType', 'title' => 'title', 'delete' => ''],
];
$selectionRawData = [
[
[
'product_id' => 123,
'position' => 500,
'is_default' => false,
'selection_price_type' => 'priceType',
'selection_price_value' => 'priceVal',
'selection_qty' => 21,
'selection_can_change_qty' => 11,
'delete' => '',
],
],
];

$selection = $this->getMock(
'Magento\Bundle\Model\Selection',
[
'getProductId',
'getPosition',
'getIsDefault',
'getSelectionPriceType',
'getSelectionPriceValue',
'getSelectionQty',
'getSelectionCanChangeQty',
'__wakeup'
],
[],
'',
false
);
$selections = [$selection];
$selectionCollection = $this->getMock(
'Magento\Bundle\Model\ResourceModel\Selection\Collection',
[],
[],
'',
false
);
$product = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$extensionAttributesProduct = $this->getMockBuilder(ProductExtensionInterface::class)
->setMethods(['getBundleProductOptions'])
->disableOriginalConstructor()
->getMockForAbstractClass();

// method flow
$this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle'));
$this->product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($instance));
$instance->expects($this->once())->method('setStoreFilter')->with(null, $this->product);
$instance->expects(
$this->once()
)->method(
'getOptionsCollection'
)->with(
$this->product
)->will(
$this->returnValue($optionCollection)
);
$instance->expects(
$this->once()
)->method(
'getSelectionsCollection'
)->with(
null,
$this->product
)->will(
$this->returnValue($selectionCollection)
);
$optionCollection->expects($this->once())->method('appendSelections')->with($selectionCollection);
$option->expects($this->any())->method('getSelections')->will($this->returnValue($selections));
$product->expects($this->once())
->method('getTypeId')
->willReturn(Type::TYPE_BUNDLE);
$product->expects($this->once())
->method('getExtensionAttributes')
->willReturn($extensionAttributesProduct);

$option->expects($this->at(0))->method('getData')->with('required')->will($this->returnValue(true));
$option->expects($this->at(1))->method('getData')->with('position')->will($this->returnValue(100));
$option->expects($this->at(2))->method('getData')->with('type')->will($this->returnValue('someType'));
$option->expects($this->at(3))->method('getData')->with('title')->will($this->returnValue('title'));
$option->expects($this->at(4))->method('getData')->with('title')->will($this->returnValue('title'));
$bundleOptions = [
$this->getMockBuilder(BundleOptionInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass(),
$this->getMockBuilder(BundleOptionInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass()
];
$extensionAttributesProduct->expects($this->once())
->method('getBundleProductOptions')
->willReturn($bundleOptions);

$selection->expects($this->once())->method('getProductId')->will($this->returnValue(123));
$selection->expects($this->once())->method('getPosition')->will($this->returnValue(500));
$selection->expects($this->once())->method('getIsDefault')->will($this->returnValue(false));
$selection->expects($this->once())->method('getSelectionPriceType')->will($this->returnValue('priceType'));
$selection->expects($this->once())->method('getSelectionPriceValue')->will($this->returnValue('priceVal'));
$selection->expects($this->once())->method('getSelectionQty')->will($this->returnValue(21));
$selection->expects($this->once())->method('getSelectionCanChangeQty')->will($this->returnValue(11));
$duplicate = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$extensionAttributesDuplicate = $this->getMockBuilder(ProductExtensionInterface::class)
->setMethods(['setBundleProductOptions'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->duplicate->expects($this->once())->method('setBundleOptionsData')->with($optionRawData);
$this->duplicate->expects($this->once())->method('setBundleSelectionsData')->with($selectionRawData);
$duplicate->expects($this->once())
->method('getExtensionAttributes')
->willReturn($extensionAttributesDuplicate);
$extensionAttributesDuplicate->expects($this->once())
->method('setBundleProductOptions')
->withConsecutive([$bundleOptions]);

$this->model->build($this->product, $this->duplicate);
$this->model->build($product, $duplicate);
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
}

if (!$product->getOptionsReadonly()) {
$product->setCanSaveCustomOptions(true);
}

$validationResult = $this->resourceModel->validate($product);
if (true !== $validationResult) {
throw new \Magento\Framework\Exception\CouldNotSaveException(
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/etc/eav_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</attribute>
<attribute code="category_ids">
<field code="is_searchable" locked="true" />
<field code="used_for_sort_by" locked="true" />
</attribute>
<attribute code="media_gallery">
<field code="is_searchable" locked="true" />
Expand Down
9 changes: 8 additions & 1 deletion app/code/Magento/Catalog/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Magento\Catalog\Model\ResourceModel\Category\Collection\FetchStrategy" type="Magento\Framework\Data\Collection\Db\FetchStrategy\Cache">
<arguments>
<argument name="cacheTags" xsi:type="array">
<item name="categoryTag" xsi:type="const">Magento\Catalog\Model\Category::CACHE_TAG</item>
</argument>
</arguments>
</virtualType>
<type name="Magento\Catalog\Model\ResourceModel\Category\Collection">
<arguments>
<argument name="fetchStrategy" xsi:type="object">Magento\Framework\Data\Collection\Db\FetchStrategy\Cache</argument>
<argument name="fetchStrategy" xsi:type="object">Magento\Catalog\Model\ResourceModel\Category\Collection\FetchStrategy</argument>
</arguments>
</type>
<type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
Expand Down
16 changes: 12 additions & 4 deletions app/code/Magento/Catalog/view/adminhtml/web/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ define([
'use strict';

return function (config) {
var optionDefaultInputType = 'radio',
attributeOption = {
var attributeOption = {
table: $('attribute-options-table'),
itemCount: 0,
totalItems: 0,
Expand All @@ -39,7 +38,7 @@ define([
}

if (!data.intype) {
data.intype = optionDefaultInputType;
data.intype = this.getOptionInputType();
}

if (!this.totalItems) {
Expand Down Expand Up @@ -128,6 +127,15 @@ define([
'.ignore-validate textarea';

jQuery('#edit_form').data('validator').settings.forceIgnore = ignore;
},
getOptionInputType: function () {
var optionDefaultInputType = 'radio';

if ($('frontend_input') && $('frontend_input').value === 'multiselect') {
optionDefaultInputType = 'checkbox';
}

return optionDefaultInputType;
}
};

Expand Down Expand Up @@ -167,7 +175,7 @@ define([
}

window.attributeOption = attributeOption;
window.optionDefaultInputType = optionDefaultInputType;
window.optionDefaultInputType = attributeOption.getOptionInputType();

rg.set('manage-options-panel', attributeOption);
};
Expand Down
Loading

0 comments on commit bf6d905

Please sign in to comment.