Skip to content

Commit de432f4

Browse files
authored
LYNX-127: Added test for options by store
1 parent bc48e9f commit de432f4

File tree

2 files changed

+291
-1
lines changed

2 files changed

+291
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Eav\Api\AttributeOptionManagementInterface;
1111
use Magento\Eav\Api\AttributeRepositoryInterface;
12+
use Magento\Eav\Api\Data\AttributeOptionInterface;
1213
use Magento\Framework\DataObject;
1314
use Magento\Framework\Exception\InvalidArgumentException;
1415
use Magento\TestFramework\Fixture\Api\DataMerger;
@@ -111,11 +112,33 @@ public function apply(array $data = []): ?DataObject
111112
$attribute = $this->attributeRepository->get($entityType, $attributeCode);
112113

113114
foreach ($attribute->getOptions() as $option) {
114-
if ($option->getLabel() === $mergedData['label']) {
115+
if ($this->getDefaultLabel($mergedData) === $option->getLabel()) {
115116
return $option;
116117
}
117118
}
118119

119120
return null;
120121
}
122+
123+
/**
124+
* Retrieve default label or label for default store
125+
*
126+
* @param array $mergedData
127+
* @return string
128+
*/
129+
private function getDefaultLabel(array $mergedData): string
130+
{
131+
$defaultLabel = $mergedData['label'];
132+
if (!isset($mergedData['store_labels']) || !is_array($mergedData['store_labels'])) {
133+
return $defaultLabel;
134+
}
135+
136+
foreach ($mergedData['store_labels'] as $label) {
137+
if (isset($label['store_id']) && $label['store_id'] === 0 && isset($label['label'])) {
138+
return $label['label'];
139+
}
140+
}
141+
142+
return $defaultLabel;
143+
}
121144
}
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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\Store\Api\Data\StoreInterface;
17+
use Magento\Store\Test\Fixture\Store;
18+
use Magento\TestFramework\Fixture\DataFixture;
19+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use Magento\TestFramework\TestCase\GraphQlAbstract;
22+
23+
/**
24+
* Test customer EAV attribute options retrieval via GraphQL API
25+
*/
26+
#[
27+
DataFixture(Store::class, as: 'store_1'),
28+
DataFixture(Store::class, as: 'store_2'),
29+
DataFixture(
30+
Attribute::class,
31+
[
32+
'frontend_labels' => [
33+
[
34+
'store_id' => 0,
35+
'label' => 'height'
36+
],
37+
[
38+
'store_id' => '$store_1.id$',
39+
'label' => 'hair'
40+
],
41+
[
42+
'store_id' => '$store_2.id$',
43+
'label' => 'eyes'
44+
],
45+
],
46+
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
47+
'frontend_input' => 'select'
48+
],
49+
'attribute'
50+
),
51+
DataFixture(
52+
AttributeOption::class,
53+
[
54+
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
55+
'attribute_code' => '$attribute.attribute_code$',
56+
'sort_order' => 10,
57+
'store_labels' => [
58+
[
59+
'store_id' => 0,
60+
'label' => 'tall'
61+
],
62+
[
63+
'store_id' => '$store_1.id$',
64+
'label' => 'red'
65+
],
66+
[
67+
'store_id' => '$store_2.id$',
68+
'label' => 'green'
69+
]
70+
],
71+
],
72+
'option1'
73+
),
74+
DataFixture(
75+
AttributeOption::class,
76+
[
77+
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
78+
'attribute_code' => '$attribute.attribute_code$',
79+
'sort_order' => 20,
80+
'store_labels' => [
81+
[
82+
'store_id' => 0,
83+
'label' => 'short'
84+
],
85+
[
86+
'store_id' => '$store_1.id$',
87+
'label' => 'brown'
88+
],
89+
[
90+
'store_id' => '$store_2.id$',
91+
'label' => 'blue'
92+
]
93+
],
94+
],
95+
'option2'
96+
),
97+
]
98+
class StoreViewOptionsTest extends GraphQlAbstract
99+
{
100+
private const QUERY = <<<QRY
101+
{
102+
attributesMetadata(input: {uids: ["%s"]}) {
103+
items {
104+
uid
105+
code
106+
label
107+
entity_type
108+
frontend_input
109+
is_required
110+
default_value
111+
is_unique
112+
options {
113+
label
114+
}
115+
}
116+
errors {
117+
type
118+
message
119+
}
120+
}
121+
}
122+
QRY;
123+
124+
public function testAttributeLabelsNoStoreViews(): void
125+
{
126+
/** @var AttributeInterface $attribute */
127+
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
128+
129+
/** @var AttributeOptionInterface $option1 */
130+
$option1 = DataFixtureStorageManager::getStorage()->get('option1');
131+
132+
/** @var AttributeOptionInterface $option2 */
133+
$option2 = DataFixtureStorageManager::getStorage()->get('option2');
134+
135+
/** @var string $uid */
136+
$uid = Bootstrap::getObjectManager()->get(Uid::class)->encode(
137+
'customer',
138+
$attribute->getAttributeCode()
139+
);
140+
141+
$this->assertEquals(
142+
[
143+
'attributesMetadata' => [
144+
'items' => [
145+
[
146+
'uid' => $uid,
147+
'code' => $attribute->getAttributeCode(),
148+
'label' => $attribute->getDefaultFrontendLabel(),
149+
'entity_type' => 'CUSTOMER',
150+
'frontend_input' => 'SELECT',
151+
'is_required' => false,
152+
'default_value' => '',
153+
'is_unique' => false,
154+
'options' => [
155+
[
156+
'label' => $option1->getLabel()
157+
],
158+
[
159+
'label' => $option2->getLabel()
160+
],
161+
]
162+
]
163+
],
164+
'errors' => []
165+
]
166+
],
167+
$this->graphQlQuery(
168+
sprintf(
169+
self::QUERY,
170+
$uid
171+
)
172+
)
173+
);
174+
}
175+
176+
public function testAttributeLabelsMultipleStoreViews(): void
177+
{
178+
/** @var AttributeInterface $attribute */
179+
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
180+
181+
/** @var StoreInterface $store1 */
182+
$store1 = DataFixtureStorageManager::getStorage()->get('store_1');
183+
184+
/** @var StoreInterface $store2 */
185+
$store2 = DataFixtureStorageManager::getStorage()->get('store_2');
186+
187+
/** @var string $uid */
188+
$uid = Bootstrap::getObjectManager()->get(Uid::class)->encode(
189+
'customer',
190+
$attribute->getAttributeCode()
191+
);
192+
193+
$this->assertEquals(
194+
[
195+
'attributesMetadata' => [
196+
'items' => [
197+
[
198+
'uid' => $uid,
199+
'code' => $attribute->getAttributeCode(),
200+
'label' => $attribute->getStoreLabel($store1->getId()),
201+
'entity_type' => 'CUSTOMER',
202+
'frontend_input' => 'SELECT',
203+
'is_required' => false,
204+
'default_value' => '',
205+
'is_unique' => false,
206+
'options' => [
207+
[
208+
'label' => 'red'
209+
],
210+
[
211+
'label' => 'brown'
212+
],
213+
]
214+
]
215+
],
216+
'errors' => []
217+
]
218+
],
219+
$this->graphQlQuery(
220+
sprintf(
221+
self::QUERY,
222+
$uid
223+
),
224+
[],
225+
'',
226+
['Store' => $store1->getCode()]
227+
)
228+
);
229+
230+
$this->assertEquals(
231+
[
232+
'attributesMetadata' => [
233+
'items' => [
234+
[
235+
'uid' => $uid,
236+
'code' => $attribute->getAttributeCode(),
237+
'label' => $attribute->getStoreLabel($store2->getId()),
238+
'entity_type' => 'CUSTOMER',
239+
'frontend_input' => 'SELECT',
240+
'is_required' => false,
241+
'default_value' => '',
242+
'is_unique' => false,
243+
'options' => [
244+
[
245+
'label' => 'green'
246+
],
247+
[
248+
'label' => 'blue'
249+
],
250+
]
251+
]
252+
],
253+
'errors' => []
254+
]
255+
],
256+
$this->graphQlQuery(
257+
sprintf(
258+
self::QUERY,
259+
$uid
260+
),
261+
[],
262+
'',
263+
['Store' => $store2->getCode()]
264+
)
265+
);
266+
}
267+
}

0 commit comments

Comments
 (0)