Skip to content

Commit 0777d56

Browse files
bl4desivaschenko
andauthored
LYNX-133: Test for multiselect attribute type (#94)
* LYNX-133: Test for multiselect attribute type * LYNX-133: Test for multiselect attribute type * Update dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultiselectTest.php Co-authored-by: Sergii Ivashchenko <[email protected]> * LYNX-133: Fix unit tests * LYNX-133: Fix static test error (missing @see tag when @deprecated is present) * LYNX-133: Fix static test error (missing @see tag when @deprecated is present) * LYNX-133: Fix WebAPI tests * LYNX-133: Fix for API functional tests --------- Co-authored-by: Sergii Ivashchenko <[email protected]>
1 parent de432f4 commit 0777d56

File tree

7 files changed

+169
-5
lines changed

7 files changed

+169
-5
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private function saveOption(
139139
$options = [];
140140
$options['value'][$optionId][0] = $optionLabel;
141141
$options['order'][$optionId] = $option->getSortOrder();
142+
$options['is_default'][$optionId] = $option->getIsDefault();
142143
if (is_array($option->getStoreLabels())) {
143144
foreach ($option->getStoreLabels() as $label) {
144145
$options['value'][$optionId][$label->getStoreId()] = $label->getLabel();

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ protected function _afterDelete(AbstractModel $object)
256256
* Returns config instance
257257
*
258258
* @return Config
259-
* @deprecated 100.0.7
260259
*/
261260
private function getConfig()
262261
{
@@ -388,6 +387,10 @@ protected function _saveOption(AbstractModel $object)
388387
$defaultValue = $this->_processAttributeOptions($object, $option);
389388
}
390389

390+
if ($object->getDefaultValue()) {
391+
$defaultValue[] = $object->getDefaultValue();
392+
}
393+
391394
$this->_saveDefaultValue($object, $defaultValue);
392395
return $this;
393396
}

app/code/Magento/Eav/Test/Fixture/AttributeOption.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AttributeOption implements DataFixtureInterface
2525
'label' => 'Option Label %uniqid%',
2626
'sort_order' => null,
2727
'store_labels' => '',
28-
'is_default' => ''
28+
'is_default' => false
2929
];
3030

3131
/**
@@ -94,7 +94,10 @@ public function apply(array $data = []): ?DataObject
9494
}
9595

9696
$mergedData = array_filter(
97-
$this->processor->process($this, $this->dataMerger->merge(self::DEFAULT_DATA, $data))
97+
$this->processor->process($this, $this->dataMerger->merge(self::DEFAULT_DATA, $data)),
98+
function ($value) {
99+
return $value !== null;
100+
}
98101
);
99102

100103
$entityType = $mergedData['entity_type'];
@@ -113,6 +116,9 @@ public function apply(array $data = []): ?DataObject
113116

114117
foreach ($attribute->getOptions() as $option) {
115118
if ($this->getDefaultLabel($mergedData) === $option->getLabel()) {
119+
if (isset($mergedData['is_default']) && $mergedData['is_default']) {
120+
$option->setIsDefault(true);
121+
}
116122
return $option;
117123
}
118124
}

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public function testAdd(string $label): void
8080
],
8181
'order' => [
8282
'id_new_option' => $sortOder,
83+
],
84+
'is_default' => [
85+
'id_new_option' => true,
8386
]
8487
];
8588
$newOptionId = 10;
@@ -196,6 +199,9 @@ public function testAddWithCannotSaveException()
196199
],
197200
'order' => [
198201
'id_new_option' => $sortOder,
202+
],
203+
'is_default' => [
204+
'id_new_option' => true,
199205
]
200206
];
201207

@@ -253,6 +259,9 @@ public function testUpdate(string $label): void
253259
],
254260
'order' => [
255261
$optionId => $sortOder,
262+
],
263+
'is_default' => [
264+
$optionId => true,
256265
]
257266
];
258267

app/code/Magento/EavGraphQl/Model/Output/GetAttributeData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ private function getOptions(AttributeInterface $attribute): array
9494
}
9595
return array_filter(
9696
array_map(
97-
function (AttributeOptionInterface $option) {
97+
function (AttributeOptionInterface $option) use ($attribute) {
9898
if (empty(trim($option->getValue())) && empty(trim($option->getLabel()))) {
9999
return null;
100100
}
101101
return [
102102
'uid' => $this->uid->encode($option->getValue()),
103103
'label' => $option->getLabel(),
104-
'value' => $option->getValue()
104+
'value' => $option->getValue(),
105+
'is_default' => in_array($option->getValue(), explode(',', $attribute->getDefaultValue()))
105106
];
106107
},
107108
$attribute->getOptions()

app/code/Magento/EavGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ interface AttributeOptionInterface @typeResolver(class: "Magento\\EavGraphQl\\Mo
8080
uid: ID! @doc(description: "The unique ID of an attribute option.")
8181
label: String! @doc(description: "The label assigned to the attribute option.")
8282
value: String! @doc(description: "The attribute option value.")
83+
is_default: Boolean @doc(description: "Is the option value default.")
8384
}
8485

8586
type AttributeOptionMetadata implements AttributeOptionInterface @doc(description: "Base EAV implementation of AttributeOptionInterface.") {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Customer\Attribute;
9+
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Eav\Api\Data\AttributeInterface;
12+
use Magento\Eav\Api\Data\AttributeOptionInterface;
13+
use Magento\Eav\Test\Fixture\Attribute;
14+
use Magento\Eav\Test\Fixture\AttributeOption;
15+
use Magento\EavGraphQl\Model\Uid;
16+
use Magento\Framework\GraphQl\Query\Uid as FrameworkUid;
17+
use Magento\TestFramework\Fixture\DataFixture;
18+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\TestCase\GraphQlAbstract;
21+
22+
/**
23+
* Test catalog EAV attributes metadata retrieval via GraphQL API
24+
*/
25+
class MultiselectTest extends GraphQlAbstract
26+
{
27+
private const QUERY = <<<QRY
28+
{
29+
attributesMetadata(input: {uids: ["%s"]}) {
30+
items {
31+
uid
32+
default_value
33+
options {
34+
uid
35+
label
36+
value
37+
is_default
38+
}
39+
}
40+
errors {
41+
type
42+
message
43+
}
44+
}
45+
}
46+
QRY;
47+
48+
#[
49+
DataFixture(
50+
Attribute::class,
51+
[
52+
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
53+
'frontend_input' => 'multiselect',
54+
'source_model' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class
55+
],
56+
'attribute'
57+
),
58+
DataFixture(
59+
AttributeOption::class,
60+
[
61+
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
62+
'attribute_code' => '$attribute.attribute_code$',
63+
'sort_order' => 10
64+
],
65+
'option1'
66+
),
67+
DataFixture(
68+
AttributeOption::class,
69+
[
70+
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
71+
'attribute_code' => '$attribute.attribute_code$',
72+
'sort_order' => 20,
73+
'is_default' => true
74+
],
75+
'option2'
76+
),
77+
DataFixture(
78+
AttributeOption::class,
79+
[
80+
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
81+
'attribute_code' => '$attribute.attribute_code$',
82+
'sort_order' => 30,
83+
'is_default' => true
84+
],
85+
'option3'
86+
),
87+
]
88+
public function testMetadata(): void
89+
{
90+
/** @var AttributeInterface $attribute */
91+
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
92+
/** @var AttributeOptionInterface $option1 */
93+
$option1 = DataFixtureStorageManager::getStorage()->get('option1');
94+
/** @var AttributeOptionInterface $option2 */
95+
$option2 = DataFixtureStorageManager::getStorage()->get('option2');
96+
/** @var AttributeOptionInterface $option3 */
97+
$option3 = DataFixtureStorageManager::getStorage()->get('option3');
98+
99+
$uid = Bootstrap::getObjectManager()->get(Uid::class)->encode(
100+
'customer',
101+
$attribute->getAttributeCode()
102+
);
103+
104+
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
105+
106+
$this->assertEquals(
107+
[
108+
'attributesMetadata' => [
109+
'items' => [
110+
[
111+
'uid' => $uid,
112+
'default_value' => $option3->getValue() . ',' . $option2->getValue(),
113+
'options' => [
114+
$this->getOptionData($option1),
115+
$this->getOptionData($option2),
116+
$this->getOptionData($option3)
117+
]
118+
]
119+
],
120+
'errors' => []
121+
]
122+
],
123+
$result
124+
);
125+
126+
$this->assertEquals($option2->getIsDefault(), true);
127+
$this->assertEquals($option3->getIsDefault(), true);
128+
}
129+
130+
/**
131+
* @param AttributeOptionInterface $option
132+
* @return array
133+
*/
134+
private function getOptionData(AttributeOptionInterface $option): array
135+
{
136+
return [
137+
'uid' => Bootstrap::getObjectManager()->get(FrameworkUid::class)->encode($option->getValue()),
138+
'label' => $option->getLabel(),
139+
'value' => $option->getValue(),
140+
'is_default' => $option->getIsDefault()
141+
];
142+
}
143+
}

0 commit comments

Comments
 (0)