Skip to content

Commit 080dba1

Browse files
committed
MAGETWO-91434: Default option for 'Status' attribute not being set
1 parent 0522c09 commit 080dba1

File tree

2 files changed

+106
-3
lines changed
  • app/code/Magento/Catalog

2 files changed

+106
-3
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/GeneralTest.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Ui\DataProvider\Product\Form\Modifier;
77

8-
use Magento\Catalog\Model\Product\Type;
8+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
99
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\General;
10+
use Magento\Eav\Api\AttributeRepositoryInterface;
11+
use Magento\Eav\Api\Data\AttributeInterface;
12+
use Magento\Framework\Stdlib\ArrayManager;
1013

1114
/**
1215
* Class GeneralTest
@@ -15,6 +18,35 @@
1518
*/
1619
class GeneralTest extends AbstractModifierTest
1720
{
21+
/**
22+
* @var AttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $attributeRepositoryMock;
25+
26+
/**
27+
* @var General
28+
*/
29+
private $generalModifier;
30+
31+
protected function setUp()
32+
{
33+
parent::setUp();
34+
35+
$this->attributeRepositoryMock = $this->getMockBuilder(AttributeRepositoryInterface::class)
36+
->getMockForAbstractClass();
37+
38+
$arrayManager = $this->objectManager->getObject(ArrayManager::class);
39+
40+
$this->generalModifier = $this->objectManager->getObject(
41+
General::class,
42+
[
43+
'attributeRepository' => $this->attributeRepositoryMock,
44+
'locator' => $this->locatorMock,
45+
'arrayManager' => $arrayManager,
46+
]
47+
);
48+
}
49+
1850
/**
1951
* {@inheritdoc}
2052
*/
@@ -40,4 +72,59 @@ public function testModifyMeta()
4072
]
4173
]));
4274
}
75+
76+
/**
77+
* @param array $data
78+
* @param int $defaultStatusValue
79+
* @param array $expectedResult
80+
* @throws \Magento\Framework\Exception\NoSuchEntityException
81+
* @dataProvider modifyDataDataProvider
82+
*/
83+
public function testModifyDataNewProduct(array $data, int $defaultStatusValue, array $expectedResult)
84+
{
85+
$attributeMock = $this->getMockBuilder(AttributeInterface::class)
86+
->getMockForAbstractClass();
87+
$attributeMock
88+
->method('getDefaultValue')
89+
->willReturn($defaultStatusValue);
90+
$this->attributeRepositoryMock
91+
->method('get')
92+
->with(
93+
ProductAttributeInterface::ENTITY_TYPE_CODE,
94+
ProductAttributeInterface::CODE_STATUS
95+
)
96+
->willReturn($attributeMock);
97+
$this->assertSame($expectedResult, $this->generalModifier->modifyData($data));
98+
}
99+
100+
/**
101+
* @return array
102+
*/
103+
public function modifyDataDataProvider(): array
104+
{
105+
return [
106+
'With default status value' => [
107+
'data' => [],
108+
'defaultStatusAttributeValue' => 5,
109+
'expectedResult' => [
110+
null => [
111+
General::DATA_SOURCE_DEFAULT => [
112+
ProductAttributeInterface::CODE_STATUS => 5,
113+
],
114+
],
115+
],
116+
],
117+
'Without default status value' => [
118+
'data' => [],
119+
'defaultStatusAttributeValue' => 0,
120+
'expectedResult' => [
121+
null => [
122+
General::DATA_SOURCE_DEFAULT => [
123+
ProductAttributeInterface::CODE_STATUS => 1,
124+
],
125+
],
126+
],
127+
],
128+
];
129+
}
43130
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Api\Data\ProductAttributeInterface;
99
use Magento\Catalog\Model\Locator\LocatorInterface;
10+
use Magento\Eav\Api\AttributeRepositoryInterface;
1011
use Magento\Ui\Component\Form;
1112
use Magento\Framework\Stdlib\ArrayManager;
1213

@@ -35,21 +36,31 @@ class General extends AbstractModifier
3536
*/
3637
private $localeCurrency;
3738

39+
/**
40+
* @var AttributeRepositoryInterface
41+
*/
42+
private $attributeRepository;
43+
3844
/**
3945
* @param LocatorInterface $locator
4046
* @param ArrayManager $arrayManager
47+
* @param AttributeRepositoryInterface|null $attributeRepository
4148
*/
4249
public function __construct(
4350
LocatorInterface $locator,
44-
ArrayManager $arrayManager
51+
ArrayManager $arrayManager,
52+
AttributeRepositoryInterface $attributeRepository = null
4553
) {
4654
$this->locator = $locator;
4755
$this->arrayManager = $arrayManager;
56+
$this->attributeRepository = $attributeRepository
57+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeRepositoryInterface::class);
4858
}
4959

5060
/**
5161
* {@inheritdoc}
5262
* @since 101.0.0
63+
* @throws \Magento\Framework\Exception\NoSuchEntityException
5364
*/
5465
public function modifyData(array $data)
5566
{
@@ -58,7 +69,12 @@ public function modifyData(array $data)
5869
$modelId = $this->locator->getProduct()->getId();
5970

6071
if (!isset($data[$modelId][static::DATA_SOURCE_DEFAULT][ProductAttributeInterface::CODE_STATUS])) {
61-
$data[$modelId][static::DATA_SOURCE_DEFAULT][ProductAttributeInterface::CODE_STATUS] = '1';
72+
$attributeStatus = $this->attributeRepository->get(
73+
ProductAttributeInterface::ENTITY_TYPE_CODE,
74+
ProductAttributeInterface::CODE_STATUS
75+
);
76+
$data[$modelId][static::DATA_SOURCE_DEFAULT][ProductAttributeInterface::CODE_STATUS] =
77+
$attributeStatus->getDefaultValue() ?: 1;
6278
}
6379

6480
return $data;

0 commit comments

Comments
 (0)