Skip to content

Commit d8e66f7

Browse files
authored
WebAPI Tests for different attribute types (#93)
1 parent 0777d56 commit d8e66f7

File tree

9 files changed

+595
-19
lines changed

9 files changed

+595
-19
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,18 @@ private function getOptions(AttributeInterface $attribute): array
9595
return array_filter(
9696
array_map(
9797
function (AttributeOptionInterface $option) use ($attribute) {
98-
if (empty(trim($option->getValue())) && empty(trim($option->getLabel()))) {
98+
$value = (string)$option->getValue();
99+
$label = (string)$option->getLabel();
100+
if (empty(trim($value)) && empty(trim($label))) {
99101
return null;
100102
}
101103
return [
102-
'uid' => $this->uid->encode($option->getValue()),
103-
'label' => $option->getLabel(),
104-
'value' => $option->getValue(),
105-
'is_default' => in_array($option->getValue(), explode(',', $attribute->getDefaultValue()))
104+
'uid' => $this->uid->encode($value),
105+
'label' => $label,
106+
'value' => $value,
107+
'is_default' =>
108+
$attribute->getDefaultValue() ?
109+
in_array($value, explode(',', $attribute->getDefaultValue())): null
106110
];
107111
},
108112
$attribute->getOptions()

app/code/Magento/EavGraphQl/etc/graphql/di.xml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
<arguments>
1919
<argument name="map" xsi:type="array">
2020
<item name="AttributeFrontendInputEnum" xsi:type="array">
21-
<item name="text" xsi:type="string">text</item>
21+
<item name="boolean" xsi:type="string">boolean</item>
22+
<item name="date" xsi:type="string">date</item>
23+
<item name="datetime" xsi:type="string">datetime</item>
24+
<item name="file" xsi:type="string">file</item>
2225
<item name="gallery" xsi:type="string">gallery</item>
23-
<item name="media_image" xsi:type="string">media_image</item>
26+
<item name="hidden" xsi:type="string">hidden</item>
2427
<item name="image" xsi:type="string">image</item>
25-
<item name="textarea" xsi:type="string">textarea</item>
28+
<item name="media_image" xsi:type="string">media_image</item>
29+
<item name="multiline" xsi:type="string">multiline</item>
2630
<item name="multiselect" xsi:type="string">multiselect</item>
27-
<item name="date" xsi:type="string">date</item>
28-
<item name="datetime" xsi:type="string">datetime</item>
29-
<item name="select" xsi:type="string">select</item>
30-
<item name="boolean" xsi:type="string">boolean</item>
3131
<item name="price" xsi:type="string">price</item>
32+
<item name="select" xsi:type="string">select</item>
33+
<item name="text" xsi:type="string">text</item>
34+
<item name="textarea" xsi:type="string">textarea</item>
3235
<item name="weight" xsi:type="string">weight</item>
3336
</item>
3437
</argument>

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,19 @@ enum AttributeEntityTypeEnum @doc(description: "List of all entity types. Popula
9393
}
9494

9595
enum AttributeFrontendInputEnum @doc(description: "EAV attribute frontend input types.") {
96-
TEXT
96+
BOOLEAN
97+
DATE
98+
DATETIME
99+
FILE
97100
GALLERY
98-
MEDIA_IMAGE
101+
HIDDEN
99102
IMAGE
100-
TEXTAREA
103+
MEDIA_IMAGE
104+
MULTILINE
101105
MULTISELECT
102-
DATE
103-
DATETIME
104-
SELECT
105-
BOOLEAN
106106
PRICE
107+
SELECT
108+
TEXT
109+
TEXTAREA
107110
WEIGHT
108111
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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\Test\Fixture\Attribute;
13+
use Magento\EavGraphQl\Model\Uid;
14+
use Magento\TestFramework\Fixture\DataFixture;
15+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\TestCase\GraphQlAbstract;
18+
19+
/**
20+
* Test catalog EAV attributes metadata retrieval via GraphQL API
21+
*/
22+
class BooleanTest extends GraphQlAbstract
23+
{
24+
private const QUERY = <<<QRY
25+
{
26+
attributesMetadata(input: {uids: ["%s"]}) {
27+
items {
28+
uid
29+
code
30+
label
31+
entity_type
32+
frontend_input
33+
is_required
34+
default_value
35+
is_unique
36+
options {
37+
uid
38+
label
39+
value
40+
}
41+
}
42+
errors {
43+
type
44+
message
45+
}
46+
}
47+
}
48+
QRY;
49+
50+
#[
51+
DataFixture(
52+
Attribute::class,
53+
[
54+
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
55+
'frontend_input' => 'boolean',
56+
'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean'
57+
],
58+
'attribute'
59+
),
60+
]
61+
public function testMetadata(): void
62+
{
63+
/** @var AttributeInterface $attribute */
64+
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
65+
66+
$uid = Bootstrap::getObjectManager()->get(Uid::class)->encode(
67+
'customer',
68+
$attribute->getAttributeCode()
69+
);
70+
71+
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
72+
73+
$this->assertEquals(
74+
[
75+
'attributesMetadata' => [
76+
'items' => [
77+
[
78+
'uid' => $uid,
79+
'code' => $attribute->getAttributeCode(),
80+
'label' => $attribute->getDefaultFrontendLabel(),
81+
'entity_type' => 'CUSTOMER',
82+
'frontend_input' => 'BOOLEAN',
83+
'is_required' => false,
84+
'default_value' => $attribute->getDefaultValue(),
85+
'is_unique' => false,
86+
'options' => [
87+
[
88+
'uid' => 'MQ==',
89+
'label' => 'Yes',
90+
'value' => '1'
91+
],
92+
[
93+
'uid' => 'MA==',
94+
'label' => 'No',
95+
'value' => '0'
96+
]
97+
]
98+
99+
]
100+
],
101+
'errors' => []
102+
]
103+
],
104+
$result
105+
);
106+
}
107+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\Test\Fixture\Attribute;
13+
use Magento\EavGraphQl\Model\Uid;
14+
use Magento\TestFramework\Fixture\DataFixture;
15+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\TestCase\GraphQlAbstract;
18+
19+
/**
20+
* Test catalog EAV attributes metadata retrieval via GraphQL API
21+
*/
22+
class DateTest extends GraphQlAbstract
23+
{
24+
private const QUERY = <<<QRY
25+
{
26+
attributesMetadata(input: {uids: ["%s"]}) {
27+
items {
28+
uid
29+
code
30+
label
31+
entity_type
32+
frontend_input
33+
is_required
34+
default_value
35+
is_unique
36+
}
37+
errors {
38+
type
39+
message
40+
}
41+
}
42+
}
43+
QRY;
44+
45+
#[
46+
DataFixture(
47+
Attribute::class,
48+
[
49+
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
50+
'frontend_input' => 'date',
51+
'default_value' => '2023-03-22 00:00:00'
52+
],
53+
'attribute'
54+
)
55+
]
56+
public function testMetadata(): void
57+
{
58+
/** @var AttributeInterface $attribute */
59+
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
60+
61+
$uid = Bootstrap::getObjectManager()->get(Uid::class)->encode(
62+
'customer',
63+
$attribute->getAttributeCode()
64+
);
65+
66+
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
67+
68+
$this->assertEquals(
69+
[
70+
'attributesMetadata' => [
71+
'items' => [
72+
[
73+
'uid' => $uid,
74+
'code' => $attribute->getAttributeCode(),
75+
'label' => $attribute->getDefaultFrontendLabel(),
76+
'entity_type' => 'CUSTOMER',
77+
'frontend_input' => 'DATE',
78+
'is_required' => false,
79+
'default_value' => $attribute->getDefaultValue(),
80+
'is_unique' => false,
81+
]
82+
],
83+
'errors' => []
84+
]
85+
],
86+
$result
87+
);
88+
}
89+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\Test\Fixture\Attribute;
13+
use Magento\EavGraphQl\Model\Uid;
14+
use Magento\TestFramework\Fixture\DataFixture;
15+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\TestCase\GraphQlAbstract;
18+
19+
/**
20+
* Test catalog EAV attributes metadata retrieval via GraphQL API
21+
*/
22+
class FileTest extends GraphQlAbstract
23+
{
24+
private const QUERY = <<<QRY
25+
{
26+
attributesMetadata(input: {uids: ["%s"]}) {
27+
items {
28+
uid
29+
code
30+
label
31+
entity_type
32+
frontend_input
33+
is_required
34+
default_value
35+
is_unique
36+
}
37+
errors {
38+
type
39+
message
40+
}
41+
}
42+
}
43+
QRY;
44+
45+
#[
46+
DataFixture(
47+
Attribute::class,
48+
[
49+
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
50+
'frontend_input' => 'file'
51+
],
52+
'attribute'
53+
)
54+
]
55+
public function testMetadata(): void
56+
{
57+
/** @var AttributeInterface $attribute */
58+
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
59+
60+
$uid = Bootstrap::getObjectManager()->get(Uid::class)->encode(
61+
'customer',
62+
$attribute->getAttributeCode()
63+
);
64+
65+
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
66+
67+
$this->assertEquals(
68+
[
69+
'attributesMetadata' => [
70+
'items' => [
71+
[
72+
'uid' => $uid,
73+
'code' => $attribute->getAttributeCode(),
74+
'label' => $attribute->getDefaultFrontendLabel(),
75+
'entity_type' => 'CUSTOMER',
76+
'frontend_input' => 'FILE',
77+
'is_required' => false,
78+
'default_value' => $attribute->getDefaultValue(),
79+
'is_unique' => false,
80+
]
81+
],
82+
'errors' => []
83+
]
84+
],
85+
$result
86+
);
87+
}
88+
}

0 commit comments

Comments
 (0)