Skip to content

Commit

Permalink
fix: wrong taxonomy label case in product attributes (#869)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
creative-andrew and kidunot89 authored Aug 7, 2024
1 parent 962410d commit d65c6a7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion includes/type/interface/class-product-attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function get_fields() {
'type' => 'String',
'description' => __( 'Attribute label', 'wp-graphql-woocommerce' ),
'resolve' => static function ( $attribute ) {
return ! empty( $attribute->get_name() ) ? ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) ) : null;
return ! empty( $attribute->get_name() ) ? $attribute->get_name() : null;
},
],
'options' => [
Expand Down
2 changes: 1 addition & 1 deletion includes/type/object/class-product-attribute-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function register() {
'description' => __( 'Attribute label', 'wp-graphql-woocommerce' ),
'resolve' => static function ( $attribute ) {
$taxonomy = get_taxonomy( $attribute->get_name() );
return $taxonomy ? ucwords( $taxonomy->labels->singular_name ) : null;
return $taxonomy ? $taxonomy->labels->singular_name : null;
},
],
'name' => [
Expand Down
4 changes: 1 addition & 3 deletions includes/type/object/class-variation-attribute-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public static function register() {
return null;
}

$slug = \wc_attribute_taxonomy_slug( $source['name'] );
$label = preg_replace( '/(-|_)/', ' ', $slug );
return ! empty( $label ) ? ucwords( $label ) : null;
return \wc_attribute_taxonomy_slug( $source['name'] );
},
],
'name' => [
Expand Down
67 changes: 65 additions & 2 deletions tests/wpunit/ProductAttributeQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function expectedProductAttributeData( $product_id, $path ) {
$this->expectedField(
'label',
$attribute->is_taxonomy()
? ucwords( get_taxonomy( $attribute->get_name() )->labels->singular_name )
: ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) )
? get_taxonomy( $attribute->get_name() )->labels->singular_name
: $attribute->get_name()
),
$this->expectedField( 'options', $attribute->get_slugs() ),
$this->expectedField( 'position', $attribute->get_position() ),
Expand Down Expand Up @@ -170,4 +170,67 @@ static function ( $attribute ) {

$this->assertQuerySuccessful( $response, $expected );
}

public function testProductAttributeMatchesVariationAttributeCounterpart() {
$product_id = $this->factory->product->createVariable();
$variation_ids = $this->factory->product_variation->createSome( $product_id )['variations'];

$query = '
query attributeQuery( $id: ID! ) {
product( id: $id ) {
id
attributes {
nodes {
name
label
options
}
}
... on ProductWithVariations {
variations {
nodes {
id
attributes {
nodes {
name
label
value
}
}
}
}
}
}
}
';

$variables = [ 'id' => $this->toRelayId( 'post', $product_id ) ];
$response = $this->graphql( compact( 'query', 'variables' ) );

/**
* Assert that the product attributes match the variation attributes
* without modification to confirm variations can be identified by product attribute.
*/
$attributes = $this->lodashGet( $response, 'data.product.attributes.nodes', [] );
$variations = $this->lodashGet( $response, 'data.product.variations.nodes', [] );

foreach( $variations as $variation ) {
$variation_attributes = $this->lodashGet( $variation, 'attributes.nodes', [] );
foreach( $variation_attributes as $variation_attribute ) {
$attribute_name = $variation_attribute['name'];
$attribute = array_search( $attribute_name, array_column( $attributes, 'name' ) );
$this->assertNotFalse( $attribute, sprintf( 'Variation attribute not found in product attributes for %s', $attribute_name ) );
if ( "" === $variation_attribute['value'] ) {
continue;
}

$this->assertContains( $variation_attribute['value'], $attributes[ $attribute ]['options'] );
}
}

$this->assertQuerySuccessful(
$response,
[ $this->expectedField( 'product.id', $this->toRelayId( 'post', $product_id ) ) ]
);
}
}

0 comments on commit d65c6a7

Please sign in to comment.