|
| 1 | +<?php |
| 2 | + |
| 3 | +class ProductTaxonomyQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase { |
| 4 | + public function testProductCategoriesToProductsQuery() { |
| 5 | + // Create categories. |
| 6 | +$clothing_category_id = $this->factory->product->createProductCategory( 'clothing' ); |
| 7 | +$shoes_id = $this->factory->product->createProductCategory( 'shoes', [ 'parent' => $clothing_category_id ] ); |
| 8 | + $accessories_id = $this->factory->product->createProductCategory( 'accessories', [ 'parent' => $clothing_category_id ] ); |
| 9 | + $electronics_category_id = $this->factory->product->createProductCategory( 'electronics' ); |
| 10 | + $smartphones_id = $this->factory->product->createProductCategory( 'smartphones', [ 'parent' => $electronics_category_id ] ); |
| 11 | + $laptops_id = $this->factory->product->createProductCategory( 'laptops', [ 'parent' => $electronics_category_id ] ); |
| 12 | + |
| 13 | + // Create products. |
| 14 | + $clothing_ids = $this->factory->product->create_many( 5, [ 'category_ids' => [ $clothing_category_id ] ] ); |
| 15 | + $shoes_ids = $this->factory->product->create_many( 5, [ 'category_ids' => [ $shoes_id, $accessories_id ] ] ); |
| 16 | + $accessories_ids = $this->factory->product->create_many( 5, [ 'category_ids' => [ $accessories_id ] ] ); |
| 17 | + $electronics_ids = $this->factory->product->create_many( 5, [ 'category_ids' => [ $electronics_category_id ] ] ); |
| 18 | + $smartphones_ids = $this->factory->product->create_many( 5, [ 'category_ids' => [ $smartphones_id ] ] ); |
| 19 | + $laptops_ids = $this->factory->product->create_many( 5, [ 'category_ids' => [ $laptops_id, $electronics_category_id ] ] ); |
| 20 | + |
| 21 | + $query = 'query ($id: ID!) { |
| 22 | + productCategory(id: $id idType: SLUG) { |
| 23 | + id |
| 24 | + slug |
| 25 | + products(first: 100) { |
| 26 | + nodes { |
| 27 | + databaseId |
| 28 | + productCategories { |
| 29 | + nodes { |
| 30 | + id |
| 31 | + slug |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + }'; |
| 38 | + |
| 39 | + $variables = [ |
| 40 | + 'id' => 'clothing' |
| 41 | + ]; |
| 42 | + |
| 43 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 44 | + $expected = array_merge( |
| 45 | + [ |
| 46 | + $this->expectedField( 'productCategory.id', $this->toRelayId( 'term', $clothing_category_id ) ), |
| 47 | + $this->expectedField( 'productCategory.slug', 'clothing' ), |
| 48 | + ], |
| 49 | + array_map( |
| 50 | + function( $id ) { |
| 51 | + return $this->expectedField( 'productCategory.products.nodes.#.databaseId', $id ); |
| 52 | + }, |
| 53 | + $clothing_ids |
| 54 | + ), |
| 55 | + array_map( |
| 56 | + function( $id ) { |
| 57 | + return $this->not()->expectedField( 'productCategory.products.nodes.#.databaseId', $id ); |
| 58 | + }, |
| 59 | + array_merge( $smartphones_ids, $shoes_ids, $accessories_ids ) |
| 60 | + ), |
| 61 | + ); |
| 62 | + |
| 63 | + $this->assertQuerySuccessful( $response, $expected ); |
| 64 | + |
| 65 | + $variables = [ |
| 66 | + 'id' => 'accessories' |
| 67 | + ]; |
| 68 | + |
| 69 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 70 | + $expected = array_merge( |
| 71 | + [ |
| 72 | + $this->expectedField( 'productCategory.id', $this->toRelayId( 'term', $accessories_id ) ), |
| 73 | + $this->expectedField( 'productCategory.slug', 'accessories' ), |
| 74 | + ], |
| 75 | + array_map( |
| 76 | + function( $id ) { |
| 77 | + return $this->expectedField( 'productCategory.products.nodes.#.databaseId', $id ); |
| 78 | + }, |
| 79 | + array_merge( $accessories_ids, $shoes_ids ) |
| 80 | + ), |
| 81 | + array_map( |
| 82 | + function( $id ) { |
| 83 | + return $this->not()->expectedField( 'productCategory.products.nodes.#.databaseId', $id ); |
| 84 | + }, |
| 85 | + array_merge( $clothing_ids, $smartphones_ids, $laptops_ids ) |
| 86 | + ), |
| 87 | + ); |
| 88 | + |
| 89 | + $this->assertQuerySuccessful( $response, $expected ); |
| 90 | + |
| 91 | + $variables = [ |
| 92 | + 'id' => 'electronics' |
| 93 | + ]; |
| 94 | + |
| 95 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 96 | + $expected = array_merge( |
| 97 | + [ |
| 98 | + $this->expectedField( 'productCategory.id', $this->toRelayId( 'term', $electronics_category_id ) ), |
| 99 | + $this->expectedField( 'productCategory.slug', 'electronics' ), |
| 100 | + ], |
| 101 | + array_map( |
| 102 | + function( $id ) { |
| 103 | + return $this->expectedField( 'productCategory.products.nodes.#.databaseId', $id ); |
| 104 | + }, |
| 105 | + array_merge( $electronics_ids, $laptops_ids ) |
| 106 | + ), |
| 107 | + array_map( |
| 108 | + function( $id ) { |
| 109 | + return $this->not()->expectedField( 'productCategory.products.nodes.#.databaseId', $id ); |
| 110 | + }, |
| 111 | + array_merge( $clothing_ids, $shoes_ids, $accessories_ids, $smartphones_ids ) |
| 112 | + ), |
| 113 | + ); |
| 114 | + |
| 115 | + $this->assertQuerySuccessful( $response, $expected ); |
| 116 | + } |
| 117 | + |
| 118 | + public function testProductTagsToProductsQuery() { |
| 119 | + // Create tags. |
| 120 | + $tag1_id = $this->factory->product->createProductTag( 'tag1' ); |
| 121 | + $tag2_id = $this->factory->product->createProductTag( 'tag2' ); |
| 122 | + $tag3_id = $this->factory->product->createProductTag( 'tag3' ); |
| 123 | + |
| 124 | + // Create products. |
| 125 | + $tag1_product_ids = $this->factory->product->create_many( 5, [ 'tag_ids' => [ $tag1_id ] ] ); |
| 126 | + $tag2_product_ids = $this->factory->product->create_many( 5, [ 'tag_ids' => [ $tag2_id, $tag3_id ] ] ); |
| 127 | + $tag3_product_ids = $this->factory->product->create_many( 5, [ 'tag_ids' => [ $tag3_id ] ] ); |
| 128 | + |
| 129 | + $query = 'query ($id: ID!) { |
| 130 | + productTag(id: $id idType: SLUG) { |
| 131 | + id |
| 132 | + slug |
| 133 | + products(first: 100) { |
| 134 | + nodes { |
| 135 | + databaseId |
| 136 | + productTags { |
| 137 | + nodes { |
| 138 | + id |
| 139 | + slug |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + }'; |
| 146 | + |
| 147 | + $variables = [ |
| 148 | + 'id' => 'tag1' |
| 149 | + ]; |
| 150 | + |
| 151 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 152 | + $expected = array_merge( |
| 153 | + [ |
| 154 | + $this->expectedField( 'productTag.id', $this->toRelayId( 'term', $tag1_id ) ), |
| 155 | + $this->expectedField( 'productTag.slug', 'tag1' ), |
| 156 | + ], |
| 157 | + array_map( |
| 158 | + function( $id ) { |
| 159 | + return $this->expectedField( 'productTag.products.nodes.#.databaseId', $id ); |
| 160 | + }, |
| 161 | + $tag1_product_ids, |
| 162 | + ), |
| 163 | + array_map( |
| 164 | + function( $id ) { |
| 165 | + return $this->not()->expectedField( 'productTag.products.nodes.#.databaseId', $id ); |
| 166 | + }, |
| 167 | + array_merge( $tag2_product_ids, $tag3_product_ids ) |
| 168 | + ), |
| 169 | + ); |
| 170 | + |
| 171 | + $this->assertQuerySuccessful( $response, $expected ); |
| 172 | + |
| 173 | + $variables = [ |
| 174 | + 'id' => 'tag2' |
| 175 | + ]; |
| 176 | + |
| 177 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 178 | + $expected = array_merge( |
| 179 | + [ |
| 180 | + $this->expectedField( 'productTag.id', $this->toRelayId( 'term', $tag2_id ) ), |
| 181 | + $this->expectedField( 'productTag.slug', 'tag2' ), |
| 182 | + ], |
| 183 | + array_map( |
| 184 | + function( $id ) { |
| 185 | + return $this->expectedField( 'productTag.products.nodes.#.databaseId', $id ); |
| 186 | + }, |
| 187 | + $tag2_product_ids, |
| 188 | + ), |
| 189 | + array_map( |
| 190 | + function( $id ) { |
| 191 | + return $this->not()->expectedField( 'productTag.products.nodes.#.databaseId', $id ); |
| 192 | + }, |
| 193 | + array_merge( $tag1_product_ids, $tag3_product_ids ) |
| 194 | + ), |
| 195 | + ); |
| 196 | + |
| 197 | + $this->assertQuerySuccessful( $response, $expected ); |
| 198 | + |
| 199 | + $variables = [ |
| 200 | + 'id' => 'tag3' |
| 201 | + ]; |
| 202 | + |
| 203 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 204 | + $expected = array_merge( |
| 205 | + [ |
| 206 | + $this->expectedField( 'productTag.id', $this->toRelayId( 'term', $tag3_id ) ), |
| 207 | + $this->expectedField( 'productTag.slug', 'tag3' ), |
| 208 | + ], |
| 209 | + array_map( |
| 210 | + function( $id ) { |
| 211 | + return $this->expectedField( 'productTag.products.nodes.#.databaseId', $id ); |
| 212 | + }, |
| 213 | + array_merge( $tag2_product_ids, $tag3_product_ids ) |
| 214 | + ), |
| 215 | + array_map( |
| 216 | + function( $id ) { |
| 217 | + return $this->not()->expectedField( 'productTag.products.nodes.#.databaseId', $id ); |
| 218 | + }, |
| 219 | + $tag1_product_ids, |
| 220 | + ), |
| 221 | + ); |
| 222 | + |
| 223 | + $this->assertQuerySuccessful( $response, $expected ); |
| 224 | + } |
| 225 | +} |
0 commit comments