From e57f4422a4824b5adbc4553a15ae467f469689a7 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 6 Aug 2024 20:05:47 -0400 Subject: [PATCH] fix: more unneeded string decoration removed --- .../interface/class-product-attribute.php | 2 +- .../object/class-variation-attribute-type.php | 4 +- tests/wpunit/ProductAttributeQueriesTest.php | 66 ++++++++++++++++++- 3 files changed, 67 insertions(+), 5 deletions(-) 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-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 6122a172..70bd35b6 100644 --- a/tests/wpunit/ProductAttributeQueriesTest.php +++ b/tests/wpunit/ProductAttributeQueriesTest.php @@ -18,7 +18,7 @@ public function expectedProductAttributeData( $product_id, $path ) { 'label', $attribute->is_taxonomy() ? get_taxonomy( $attribute->get_name() )->labels->singular_name - : ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) ) + : $attribute->get_name() ), $this->expectedField( 'options', $attribute->get_slugs() ), $this->expectedField( 'position', $attribute->get_position() ), @@ -170,4 +170,68 @@ 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 ) ); + $this->assertSame( $attributes[ $attribute ]['label'], $variation_attribute['label'] ); + 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 ) ) ] + ); + } }