Skip to content

Commit

Permalink
Merge pull request #274 from magento-fearless-kiwis/Fearless-Kiwis-MA…
Browse files Browse the repository at this point in the history
…GETWO-50123-Unable-to-assign-blank-value-to-attribute

[Fearless Kiwis] MAGETWO-50123 : unable to assign blank value to attribute
  • Loading branch information
heyitsroberthe authored Aug 22, 2016
2 parents 8904497 + 76d3f43 commit 3ef1e28
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav;
use Magento\Eav\Model\Config;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\EntityManager\EventManager;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Ui\DataProvider\EavValidationRules;
Expand All @@ -27,11 +28,15 @@
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Framework\Api\AttributeInterface;
use Magento\Eav\Api\Data\AttributeGroupInterface;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Framework\Currency;
use Magento\Framework\Locale\Currency as CurrencyLocale;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory as EavAttributeFactory;
use Magento\Framework\Event\ManagerInterface;

/**
* Class EavTest
Expand Down Expand Up @@ -157,6 +162,26 @@ class EavTest extends AbstractModifierTest
*/
protected $currencyLocaleMock;

/**
* @var ProductAttributeInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $productAttributeMock;

/**
* @var ArrayManager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $arrayManagerMock;

/**
* @var EavAttributeFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eavAttributeFactoryMock;

/**
* @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eventManagerMock;

/**
* @var ObjectManager
*/
Expand All @@ -167,6 +192,9 @@ class EavTest extends AbstractModifierTest
*/
protected $eav;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function setUp()
{
parent::setUp();
Expand Down Expand Up @@ -228,10 +256,24 @@ protected function setUp()
$this->searchResultsMock = $this->getMockBuilder(SearchResultsInterface::class)
->getMockForAbstractClass();
$this->eavAttributeMock = $this->getMockBuilder(Attribute::class)
->setMethods(['getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])
->setMethods(['load', 'getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])
->disableOriginalConstructor()
->getMock();
$this->productAttributeMock = $this->getMockBuilder(ProductAttributeInterface::class)
->getMock();
$this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
->getMock();
$this->eavAttributeFactoryMock = $this->getMockBuilder(EavAttributeFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)
->disableOriginalConstructor()
->getMock();

$this->eavAttributeFactoryMock->expects($this->any())
->method('create')
->willReturn($this->eavAttributeMock);
$this->groupCollectionFactoryMock->expects($this->any())
->method('create')
->willReturn($this->groupCollectionMock);
Expand Down Expand Up @@ -277,6 +319,9 @@ protected function setUp()
->disableOriginalConstructor()
->setMethods(['getCurrency'])
->getMock();
$this->eavAttributeMock->expects($this->any())
->method('load')
->willReturnSelf();

$this->eav =$this->getModel();
$this->objectManager->setBackwardCompatibleProperty(
Expand Down Expand Up @@ -304,6 +349,9 @@ protected function createModel()
'attributeGroupRepository' => $this->attributeGroupRepositoryMock,
'sortOrderBuilder' => $this->sortOrderBuilderMock,
'attributeRepository' => $this->attributeRepositoryMock,
'arrayManager' => $this->arrayManagerMock,
'eavAttributeFactory' => $this->eavAttributeFactoryMock,
'_eventManager' => $this->eventManagerMock
]);
}

Expand Down Expand Up @@ -399,4 +447,162 @@ public function testModifyData()

$this->assertEquals($sourceData, $this->eav->modifyData([]));
}

/**
* @param int $productId
* @param bool $productRequired
* @param string $attrValue
* @param array $expected
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::isProductExists
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::setupAttributeMeta
* @dataProvider setupAttributeMetaDataProvider
*/
public function testSetupAttributeMetaDefaultAttribute($productId, $productRequired, $attrValue, $expected)
{
$configPath = 'arguments/data/config';
$groupCode = 'product-details';
$sortOrder = '0';

$this->productMock->expects($this->any())
->method('getId')
->willReturn($productId);

$this->productAttributeMock->expects($this->any())
->method('getIsRequired')
->willReturn($productRequired);

$this->productAttributeMock->expects($this->any())
->method('getDefaultValue')
->willReturn('required_value');

$this->productAttributeMock->expects($this->any())
->method('getAttributeCode')
->willReturn('code');

$this->productAttributeMock->expects($this->any())
->method('getValue')
->willReturn('value');

$attributeMock = $this->getMockBuilder(AttributeInterface::class)
->disableOriginalConstructor()
->getMock();

$attributeMock->expects($this->any())
->method('getValue')
->willReturn($attrValue);

$this->productMock->expects($this->any())
->method('getCustomAttribute')
->willReturn($attributeMock);

$this->arrayManagerMock->expects($this->any())
->method('set')
->with(
$configPath,
[],
$expected
)
->willReturn($expected);

$this->arrayManagerMock->expects($this->any())
->method('merge')
->willReturn($expected);

$this->arrayManagerMock->expects($this->any())
->method('get')
->willReturn([]);

$this->arrayManagerMock->expects($this->any())
->method('exists');

$this->assertEquals(
$expected,
$this->eav->setupAttributeMeta($this->productAttributeMock, $groupCode, $sortOrder)
);
}

/**
* @return array
*/
public function setupAttributeMetaDataProvider()
{
return [
'default_null_prod_not_new_and_required' => [
'productId' => 1,
'productRequired' => true,
'attrValue' => 'val',
'expected' => [
'dataType' => null,
'formElement' => null,
'visible' => null,
'required' => true,
'notice' => null,
'default' => null,
'label' => null,
'code' => 'code',
'source' => 'product-details',
'scopeLabel' => '',
'globalScope' => false,
'sortOrder' => 0
],
],
'default_null_prod_not_new_and_not_required' => [
'productId' => 1,
'productRequired' => false,
'attrValue' => 'val',
'expected' => [
'dataType' => null,
'formElement' => null,
'visible' => null,
'required' => false,
'notice' => null,
'default' => null,
'label' => null,
'code' => 'code',
'source' => 'product-details',
'scopeLabel' => '',
'globalScope' => false,
'sortOrder' => 0
],
],
'default_null_prod_new_and_not_required' => [
'productId' => null,
'productRequired' => false,
'attrValue' => null,
'expected' => [
'dataType' => null,
'formElement' => null,
'visible' => null,
'required' => false,
'notice' => null,
'default' => 'required_value',
'label' => null,
'code' => 'code',
'source' => 'product-details',
'scopeLabel' => '',
'globalScope' => false,
'sortOrder' => 0
],
],
'default_null_prod_new_and_required' => [
'productId' => null,
'productRequired' => false,
'attrValue' => null,
'expected' => [
'dataType' => null,
'formElement' => null,
'visible' => null,
'required' => false,
'notice' => null,
'default' => 'required_value',
'label' => null,
'code' => 'code',
'source' => 'product-details',
'scopeLabel' => '',
'globalScope' => false,
'sortOrder' => 0
],
]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,16 @@ private function getPreviousSetAttributes()
return $this->prevSetAttributes;
}

/**
* Check is product already new or we trying to create one
*
* @return bool
*/
private function isProductExists()
{
return (bool) $this->locator->getProduct()->getId();
}

/**
* Initial meta setup
*
Expand All @@ -531,6 +541,7 @@ private function getPreviousSetAttributes()
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @api
*/
public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupCode, $sortOrder)
Expand All @@ -543,7 +554,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
'visible' => $attribute->getIsVisible(),
'required' => $attribute->getIsRequired(),
'notice' => $attribute->getNote(),
'default' => $attribute->getDefaultValue(),
'default' => (!$this->isProductExists()) ? $attribute->getDefaultValue() : null,
'label' => $attribute->getDefaultFrontendLabel(),
'code' => $attribute->getAttributeCode(),
'source' => $groupCode,
Expand Down
24 changes: 21 additions & 3 deletions app/code/Magento/Ui/view/base/web/js/form/element/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ define([
if (_.isUndefined(caption)) {
caption = node.label;
}
} else {
return node;
}

return node;
});

return {
Expand Down Expand Up @@ -194,7 +194,7 @@ define([
},

/**
* Matches specfied value with existing options
* Matches specified value with existing options
* or, if value is not specified, returns value of the first option.
*
* @returns {*}
Expand Down Expand Up @@ -286,6 +286,11 @@ define([
return preview;
},

/**
*
* @param {Number} value
* @returns {Object} Chainable
*/
getOption: function (value) {
return this.indexedOptions[value];
},
Expand All @@ -301,6 +306,19 @@ define([
this.value(value);

return this;
},

/**
* Initializes observable properties of instance
*
* @returns {Object} Chainable.
*/
setInitialValue: function () {
if (_.isUndefined(this.value()) && !this.default) {
this.clear();
}

return this._super();
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ public function testModifyMeta()
$this->prepareDataForComparison($actualMeta, $expectedMeta);
$this->assertEquals($expectedMeta, $actualMeta);
}

public function testModifyMetaNewProduct()
{
$this->objectManager->get(\Magento\Eav\Model\Entity\AttributeCache::class)->clear();
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->setAttributeSetId(4);
$this->locatorMock->expects($this->any())->method('getProduct')->willReturn($product);
$expectedMeta = include __DIR__ . '/_files/eav_expected_meta_output_w_default.php';
$actualMeta = $this->eavModifier->modifyMeta([]);
$this->prepareDataForComparison($actualMeta, $expectedMeta);
$this->assertEquals($expectedMeta, $actualMeta);
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_admin_store.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"visible" => "1",
"required" => "0",
"label" => "Enable Product",
"default" => "1",
"source" => "product-details",
"scopeLabel" => "[WEBSITE]",
"globalScope" => false,
Expand Down
Loading

0 comments on commit 3ef1e28

Please sign in to comment.