@@ -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