|
| 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\EavGraphQl; |
| 9 | + |
| 10 | +use Magento\Customer\Api\CustomerMetadataInterface; |
| 11 | +use Magento\Catalog\Setup\CategorySetup; |
| 12 | +use Magento\Eav\Test\Fixture\Attribute; |
| 13 | +use Magento\Eav\Api\Data\AttributeInterface; |
| 14 | +use Magento\Sales\Setup\SalesSetup; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test EAV attributes metadata retrieval for entity type via GraphQL API |
| 21 | + */ |
| 22 | +#[ |
| 23 | + DataFixture( |
| 24 | + Attribute::class, |
| 25 | + [ |
| 26 | + 'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, |
| 27 | + 'frontend_input' => 'boolean', |
| 28 | + 'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' |
| 29 | + ], |
| 30 | + 'customerAttribute0' |
| 31 | + ), |
| 32 | + DataFixture( |
| 33 | + Attribute::class, |
| 34 | + [ |
| 35 | + 'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, |
| 36 | + 'frontend_input' => 'boolean', |
| 37 | + 'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' |
| 38 | + ], |
| 39 | + 'customerAttribute1' |
| 40 | + ), |
| 41 | + DataFixture( |
| 42 | + Attribute::class, |
| 43 | + [ |
| 44 | + 'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, |
| 45 | + 'frontend_input' => 'boolean', |
| 46 | + 'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' |
| 47 | + ], |
| 48 | + 'customerAttribute2' |
| 49 | + ), |
| 50 | + DataFixture( |
| 51 | + Attribute::class, |
| 52 | + [ |
| 53 | + 'entity_type_id' => CategorySetup::CATALOG_PRODUCT_ENTITY_TYPE_ID, |
| 54 | + 'frontend_input' => 'boolean', |
| 55 | + 'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' |
| 56 | + ], |
| 57 | + 'catalogAttribute3' |
| 58 | + ), |
| 59 | + DataFixture( |
| 60 | + Attribute::class, |
| 61 | + [ |
| 62 | + 'entity_type_id' => CategorySetup::CATALOG_PRODUCT_ENTITY_TYPE_ID, |
| 63 | + 'frontend_input' => 'boolean', |
| 64 | + 'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' |
| 65 | + ], |
| 66 | + 'catalogAttribute4' |
| 67 | + ), |
| 68 | + DataFixture( |
| 69 | + Attribute::class, |
| 70 | + [ |
| 71 | + 'entity_type_id' => SalesSetup::CREDITMEMO_PRODUCT_ENTITY_TYPE_ID, |
| 72 | + 'frontend_input' => 'boolean', |
| 73 | + 'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' |
| 74 | + ], |
| 75 | + 'creditmemoAttribute5' |
| 76 | + ) |
| 77 | +] |
| 78 | +class AttributesListTest extends GraphQlAbstract |
| 79 | +{ |
| 80 | + private const ATTRIBUTE_NOT_FOUND_ERROR = "Attribute was not found in query result"; |
| 81 | + |
| 82 | + |
| 83 | + public function testAttributesListForCustomerEntityType(): void |
| 84 | + { |
| 85 | + $queryResult = $this->graphQlQuery(<<<QRY |
| 86 | + { |
| 87 | + attributesList(entityType: CUSTOMER) { |
| 88 | + items { |
| 89 | + uid |
| 90 | + code |
| 91 | + } |
| 92 | + errors { |
| 93 | + type |
| 94 | + message |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +QRY); |
| 99 | + |
| 100 | + $this->assertArrayHasKey('items', $queryResult['attributesList'], 'Query result does not contain items'); |
| 101 | + $this->assertGreaterThanOrEqual(3, count($queryResult['attributesList']['items'])); |
| 102 | + |
| 103 | + /** @var AttributeInterface $attribute */ |
| 104 | + $creditmemoAttribute5 = DataFixtureStorageManager::getStorage()->get('creditmemoAttribute5'); |
| 105 | + |
| 106 | + /** @var AttributeInterface $attribute */ |
| 107 | + $customerAttribute0 = DataFixtureStorageManager::getStorage()->get('customerAttribute0'); |
| 108 | + /** @var AttributeInterface $attribute */ |
| 109 | + $customerAttribute1 = DataFixtureStorageManager::getStorage()->get('customerAttribute1'); |
| 110 | + /** @var AttributeInterface $attribute */ |
| 111 | + $customerAttribute2 = DataFixtureStorageManager::getStorage()->get('customerAttribute2'); |
| 112 | + |
| 113 | + $this->assertEquals( |
| 114 | + $customerAttribute0->getAttributeCode(), |
| 115 | + $this->getAttributeByCode($queryResult['attributesList']['items'], $customerAttribute0->getAttributeCode())['code'], |
| 116 | + self::ATTRIBUTE_NOT_FOUND_ERROR |
| 117 | + ); |
| 118 | + |
| 119 | + $this->assertEquals( |
| 120 | + $customerAttribute1->getAttributeCode(), |
| 121 | + $this->getAttributeByCode($queryResult['attributesList']['items'], $customerAttribute1->getAttributeCode())['code'], |
| 122 | + self::ATTRIBUTE_NOT_FOUND_ERROR |
| 123 | + ); |
| 124 | + $this->assertEquals( |
| 125 | + $customerAttribute2->getAttributeCode(), |
| 126 | + $this->getAttributeByCode($queryResult['attributesList']['items'], $customerAttribute2->getAttributeCode())['code'], |
| 127 | + self::ATTRIBUTE_NOT_FOUND_ERROR |
| 128 | + ); |
| 129 | + $this->assertEquals( |
| 130 | + [], |
| 131 | + $this->getAttributeByCode($queryResult['attributesList']['items'], $creditmemoAttribute5->getAttributeCode()) |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + public function testAttributesListForCatalogProductEntityType(): void |
| 136 | + { |
| 137 | + $queryResult = $this->graphQlQuery(<<<QRY |
| 138 | + { |
| 139 | + attributesList(entityType: CATALOG_PRODUCT) { |
| 140 | + items { |
| 141 | + uid |
| 142 | + code |
| 143 | + } |
| 144 | + errors { |
| 145 | + type |
| 146 | + message |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +QRY); |
| 151 | + $this->assertArrayHasKey('items', $queryResult['attributesList'], 'Query result does not contain items'); |
| 152 | + $this->assertGreaterThanOrEqual(2, count($queryResult['attributesList']['items'])); |
| 153 | + |
| 154 | + /** @var AttributeInterface $attribute */ |
| 155 | + $creditmemoAttribute5 = DataFixtureStorageManager::getStorage()->get('creditmemoAttribute5'); |
| 156 | + |
| 157 | + /** @var AttributeInterface $attribute */ |
| 158 | + $catalogAttribute3 = DataFixtureStorageManager::getStorage()->get('catalogAttribute3'); |
| 159 | + /** @var AttributeInterface $attribute */ |
| 160 | + $catalogAttribute4 = DataFixtureStorageManager::getStorage()->get('catalogAttribute4'); |
| 161 | + |
| 162 | + $this->assertEquals( |
| 163 | + $catalogAttribute3->getAttributeCode(), |
| 164 | + $this->getAttributeByCode($queryResult['attributesList']['items'], $catalogAttribute3->getAttributeCode())['code'], |
| 165 | + self::ATTRIBUTE_NOT_FOUND_ERROR |
| 166 | + ); |
| 167 | + $this->assertEquals( |
| 168 | + $catalogAttribute4->getAttributeCode(), |
| 169 | + $this->getAttributeByCode($queryResult['attributesList']['items'], $catalogAttribute4->getAttributeCode())['code'], |
| 170 | + self::ATTRIBUTE_NOT_FOUND_ERROR |
| 171 | + ); |
| 172 | + $this->assertEquals( |
| 173 | + [], |
| 174 | + $this->getAttributeByCode($queryResult['attributesList']['items'], $creditmemoAttribute5->getAttributeCode()) |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Finds attribute in query result |
| 180 | + * |
| 181 | + * @param array $items |
| 182 | + * @param string $attribute_code |
| 183 | + * @return array |
| 184 | + */ |
| 185 | + private function getAttributeByCode(array $items, string $attribute_code): array |
| 186 | + { |
| 187 | + $attribute = array_filter($items, function ($item) use ($attribute_code) { |
| 188 | + return $item['code'] == $attribute_code; |
| 189 | + }); |
| 190 | + return $attribute[array_key_first($attribute)] ?? []; |
| 191 | + } |
| 192 | +} |
0 commit comments