diff --git a/includes/type/interface/class-product-attribute.php b/includes/type/interface/class-product-attribute.php index 3b514853..ead15792 100644 --- a/includes/type/interface/class-product-attribute.php +++ b/includes/type/interface/class-product-attribute.php @@ -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' => [ diff --git a/includes/type/object/class-product-attribute-types.php b/includes/type/object/class-product-attribute-types.php index ac792001..0bc146cf 100644 --- a/includes/type/object/class-product-attribute-types.php +++ b/includes/type/object/class-product-attribute-types.php @@ -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' => [ diff --git a/includes/type/object/class-variation-attribute-type.php b/includes/type/object/class-variation-attribute-type.php index 0942bb5d..7b1fcbcb 100644 --- a/includes/type/object/class-variation-attribute-type.php +++ b/includes/type/object/class-variation-attribute-type.php @@ -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' => [ diff --git a/tests/wpunit/ProductAttributeQueriesTest.php b/tests/wpunit/ProductAttributeQueriesTest.php index bd53478f..189229be 100644 --- a/tests/wpunit/ProductAttributeQueriesTest.php +++ b/tests/wpunit/ProductAttributeQueriesTest.php @@ -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() ), @@ -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 ) ) ] + ); + } }