Skip to content

Commit d65c6a7

Browse files
fix: wrong taxonomy label case in product attributes (wp-graphql#869)
* fix: wrong taxonomy label in product attributes * fix: adjust test to remove ucwords * fix: more unneeded string decoration removed * devops: Unstable test assertion removed --------- Co-authored-by: Geoff Taylor <geoff@axistaylor.com>
1 parent 962410d commit d65c6a7

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

includes/type/interface/class-product-attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function get_fields() {
6565
'type' => 'String',
6666
'description' => __( 'Attribute label', 'wp-graphql-woocommerce' ),
6767
'resolve' => static function ( $attribute ) {
68-
return ! empty( $attribute->get_name() ) ? ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) ) : null;
68+
return ! empty( $attribute->get_name() ) ? $attribute->get_name() : null;
6969
},
7070
],
7171
'options' => [

includes/type/object/class-product-attribute-types.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function register() {
7171
'description' => __( 'Attribute label', 'wp-graphql-woocommerce' ),
7272
'resolve' => static function ( $attribute ) {
7373
$taxonomy = get_taxonomy( $attribute->get_name() );
74-
return $taxonomy ? ucwords( $taxonomy->labels->singular_name ) : null;
74+
return $taxonomy ? $taxonomy->labels->singular_name : null;
7575
},
7676
],
7777
'name' => [

includes/type/object/class-variation-attribute-type.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public static function register() {
4848
return null;
4949
}
5050

51-
$slug = \wc_attribute_taxonomy_slug( $source['name'] );
52-
$label = preg_replace( '/(-|_)/', ' ', $slug );
53-
return ! empty( $label ) ? ucwords( $label ) : null;
51+
return \wc_attribute_taxonomy_slug( $source['name'] );
5452
},
5553
],
5654
'name' => [

tests/wpunit/ProductAttributeQueriesTest.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function expectedProductAttributeData( $product_id, $path ) {
1717
$this->expectedField(
1818
'label',
1919
$attribute->is_taxonomy()
20-
? ucwords( get_taxonomy( $attribute->get_name() )->labels->singular_name )
21-
: ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) )
20+
? get_taxonomy( $attribute->get_name() )->labels->singular_name
21+
: $attribute->get_name()
2222
),
2323
$this->expectedField( 'options', $attribute->get_slugs() ),
2424
$this->expectedField( 'position', $attribute->get_position() ),
@@ -170,4 +170,67 @@ static function ( $attribute ) {
170170

171171
$this->assertQuerySuccessful( $response, $expected );
172172
}
173+
174+
public function testProductAttributeMatchesVariationAttributeCounterpart() {
175+
$product_id = $this->factory->product->createVariable();
176+
$variation_ids = $this->factory->product_variation->createSome( $product_id )['variations'];
177+
178+
$query = '
179+
query attributeQuery( $id: ID! ) {
180+
product( id: $id ) {
181+
id
182+
attributes {
183+
nodes {
184+
name
185+
label
186+
options
187+
}
188+
}
189+
... on ProductWithVariations {
190+
variations {
191+
nodes {
192+
id
193+
attributes {
194+
nodes {
195+
name
196+
label
197+
value
198+
}
199+
}
200+
}
201+
}
202+
}
203+
}
204+
}
205+
';
206+
207+
$variables = [ 'id' => $this->toRelayId( 'post', $product_id ) ];
208+
$response = $this->graphql( compact( 'query', 'variables' ) );
209+
210+
/**
211+
* Assert that the product attributes match the variation attributes
212+
* without modification to confirm variations can be identified by product attribute.
213+
*/
214+
$attributes = $this->lodashGet( $response, 'data.product.attributes.nodes', [] );
215+
$variations = $this->lodashGet( $response, 'data.product.variations.nodes', [] );
216+
217+
foreach( $variations as $variation ) {
218+
$variation_attributes = $this->lodashGet( $variation, 'attributes.nodes', [] );
219+
foreach( $variation_attributes as $variation_attribute ) {
220+
$attribute_name = $variation_attribute['name'];
221+
$attribute = array_search( $attribute_name, array_column( $attributes, 'name' ) );
222+
$this->assertNotFalse( $attribute, sprintf( 'Variation attribute not found in product attributes for %s', $attribute_name ) );
223+
if ( "" === $variation_attribute['value'] ) {
224+
continue;
225+
}
226+
227+
$this->assertContains( $variation_attribute['value'], $attributes[ $attribute ]['options'] );
228+
}
229+
}
230+
231+
$this->assertQuerySuccessful(
232+
$response,
233+
[ $this->expectedField( 'product.id', $this->toRelayId( 'post', $product_id ) ) ]
234+
);
235+
}
173236
}

0 commit comments

Comments
 (0)