|
7 | 7 |
|
8 | 8 | namespace Magento\GraphQl\Catalog;
|
9 | 9 |
|
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
10 | 11 | use Magento\Catalog\Api\Data\ProductInterface;
|
11 | 12 | use Magento\Catalog\Api\Data\ProductLinkInterface;
|
12 | 13 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
| 14 | +use Magento\Catalog\Model\Category; |
13 | 15 | use Magento\Framework\DataObject;
|
14 | 16 | use Magento\Framework\EntityManager\MetadataPool;
|
15 | 17 | use Magento\TestFramework\ObjectManager;
|
@@ -970,4 +972,163 @@ private function eavAttributesToGraphQlSchemaFieldTranslator(string $eavAttribut
|
970 | 972 | }
|
971 | 973 | return $eavAttributeCode;
|
972 | 974 | }
|
| 975 | + |
| 976 | + /** |
| 977 | + * @magentoApiDataFixture Magento/Catalog/_files/categories.php |
| 978 | + */ |
| 979 | + public function testProductInAllAnchoredCategories() |
| 980 | + { |
| 981 | + $query = <<<QUERY |
| 982 | +{ |
| 983 | + products(filter: {sku: {like: "12345%"}}) |
| 984 | + { |
| 985 | + items |
| 986 | + { |
| 987 | + sku |
| 988 | + name |
| 989 | + categories { |
| 990 | + id |
| 991 | + name |
| 992 | + is_anchor |
| 993 | + } |
| 994 | + } |
| 995 | + } |
| 996 | +} |
| 997 | +QUERY; |
| 998 | + $response = $this->graphQlQuery($query); |
| 999 | + $this->assertNotEmpty($response['products']['items'][0]['categories'], "Categories must not be empty"); |
| 1000 | + /** @var ProductRepositoryInterface $productRepository */ |
| 1001 | + $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class); |
| 1002 | + /** @var CategoryRepositoryInterface $categoryRepository */ |
| 1003 | + $categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class); |
| 1004 | + $categoryIds = [3, 4, 5]; |
| 1005 | + |
| 1006 | + $productItemsInResponse = $response['products']['items']; |
| 1007 | + $this->assertEquals(1, count($productItemsInResponse)); |
| 1008 | + $this->assertCount(3, $productItemsInResponse[0]['categories']); |
| 1009 | + $categoriesInResponse = array_map(null, $categoryIds, $productItemsInResponse[0]['categories']); |
| 1010 | + foreach ($categoriesInResponse as $key => $categoryData) { |
| 1011 | + $this->assertNotEmpty($categoryData); |
| 1012 | + /** @var Category | Category $category */ |
| 1013 | + $category = $categoryRepository->get($categoriesInResponse[$key][0]); |
| 1014 | + $this->assertResponseFields( |
| 1015 | + $categoriesInResponse[$key][1], |
| 1016 | + [ |
| 1017 | + 'name' => $category->getName(), |
| 1018 | + 'id' => $category->getId(), |
| 1019 | + 'is_anchor' => $category->getIsAnchor() |
| 1020 | + ] |
| 1021 | + ); |
| 1022 | + } |
| 1023 | + } |
| 1024 | + |
| 1025 | + /** |
| 1026 | + * Set one of the categories directly assigned to the product as non -anchored. |
| 1027 | + * Verify that the non-anchored category still shows in the response |
| 1028 | + * |
| 1029 | + * @magentoApiDataFixture Magento/Catalog/_files/categories.php |
| 1030 | + */ |
| 1031 | + public function testProductWithNonAnchoredParentCategory() |
| 1032 | + { |
| 1033 | + $query = <<<QUERY |
| 1034 | +{ |
| 1035 | + products(filter: {sku: {like: "12345%"}}) |
| 1036 | + { |
| 1037 | + items |
| 1038 | + { |
| 1039 | + sku |
| 1040 | + name |
| 1041 | + categories { |
| 1042 | + id |
| 1043 | + name |
| 1044 | + is_anchor |
| 1045 | + } |
| 1046 | + } |
| 1047 | + } |
| 1048 | +} |
| 1049 | +QUERY; |
| 1050 | + /** @var CategoryRepositoryInterface $categoryRepository */ |
| 1051 | + $categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class); |
| 1052 | + /** @var Category $nonAnchorCategory */ |
| 1053 | + $nonAnchorCategory = $categoryRepository->get(4); |
| 1054 | + $nonAnchorCategory->setIsAnchor(false); |
| 1055 | + $categoryRepository->save($nonAnchorCategory); |
| 1056 | + $categoryIds = [3, 4, 5]; |
| 1057 | + |
| 1058 | + $response = $this->graphQlQuery($query); |
| 1059 | + $this->assertNotEmpty($response['products']['items'][0]['categories'], "Categories must not be empty"); |
| 1060 | + |
| 1061 | + $productItemsInResponse = $response['products']['items']; |
| 1062 | + $this->assertEquals(1, count($productItemsInResponse)); |
| 1063 | + $this->assertCount(3, $productItemsInResponse[0]['categories']); |
| 1064 | + $categoriesInResponse = array_map(null, $categoryIds, $productItemsInResponse[0]['categories']); |
| 1065 | + foreach ($categoriesInResponse as $key => $categoryData) { |
| 1066 | + $this->assertNotEmpty($categoryData); |
| 1067 | + /** @var Category | Category $category */ |
| 1068 | + $category = $categoryRepository->get($categoriesInResponse[$key][0]); |
| 1069 | + $this->assertResponseFields( |
| 1070 | + $categoriesInResponse[$key][1], |
| 1071 | + [ |
| 1072 | + 'name' => $category->getName(), |
| 1073 | + 'id' => $category->getId(), |
| 1074 | + 'is_anchor' => $category->getIsAnchor() |
| 1075 | + ] |
| 1076 | + ); |
| 1077 | + } |
| 1078 | + } |
| 1079 | + /** |
| 1080 | + * Set as non-anchored, one of the categories not directly assigned to the product |
| 1081 | + * Verify that the category doesn't show in the response |
| 1082 | + * |
| 1083 | + * @magentoApiDataFixture Magento/Catalog/_files/categories.php |
| 1084 | + */ |
| 1085 | + public function testProductInNonAnchoredSubCategories() |
| 1086 | + { |
| 1087 | + $query = <<<QUERY |
| 1088 | +{ |
| 1089 | + products(filter: {sku: {like: "12345%"}}) |
| 1090 | + { |
| 1091 | + items |
| 1092 | + { |
| 1093 | + sku |
| 1094 | + name |
| 1095 | + categories { |
| 1096 | + id |
| 1097 | + name |
| 1098 | + is_anchor |
| 1099 | + } |
| 1100 | + } |
| 1101 | + } |
| 1102 | +} |
| 1103 | +QUERY; |
| 1104 | + /** @var CategoryRepositoryInterface $categoryRepository */ |
| 1105 | + $categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class); |
| 1106 | + /** @var Category $nonAnchorCategory */ |
| 1107 | + $nonAnchorCategory = $categoryRepository->get(3); |
| 1108 | + //Set the parent category as non-anchored |
| 1109 | + $nonAnchorCategory->setIsAnchor(false); |
| 1110 | + $categoryRepository->save($nonAnchorCategory); |
| 1111 | + $categoryIds = [4, 5]; |
| 1112 | + |
| 1113 | + $response = $this->graphQlQuery($query); |
| 1114 | + $this->assertNotEmpty($response['products']['items'][0]['categories'], "Categories must not be empty"); |
| 1115 | + |
| 1116 | + $productItemsInResponse = $response['products']['items']; |
| 1117 | + $this->assertEquals(1, count($productItemsInResponse)); |
| 1118 | + $this->assertCount(2, $productItemsInResponse[0]['categories']); |
| 1119 | + $categoriesInResponse = array_map(null, $categoryIds, $productItemsInResponse[0]['categories']); |
| 1120 | + foreach ($categoriesInResponse as $key => $categoryData) { |
| 1121 | + $this->assertNotEmpty($categoryData); |
| 1122 | + /** @var Category | Category $category */ |
| 1123 | + $category = $categoryRepository->get($categoriesInResponse[$key][0]); |
| 1124 | + $this->assertResponseFields( |
| 1125 | + $categoriesInResponse[$key][1], |
| 1126 | + [ |
| 1127 | + 'name' => $category->getName(), |
| 1128 | + 'id' => $category->getId(), |
| 1129 | + 'is_anchor' => $category->getIsAnchor() |
| 1130 | + ] |
| 1131 | + ); |
| 1132 | + } |
| 1133 | + } |
973 | 1134 | }
|
0 commit comments