Skip to content

Commit

Permalink
fix: more unneeded string decoration removed
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Aug 7, 2024
1 parent c94056c commit e57f442
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 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
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
66 changes: 65 additions & 1 deletion tests/wpunit/ProductAttributeQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ),
Expand Down Expand Up @@ -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 ) ) ]
);
}
}

0 comments on commit e57f442

Please sign in to comment.