Skip to content

Commit b2d6608

Browse files
authored
fix: Meta data type error fixed. (#728)
* fix: Meta data type error fixed. * devops: CouponQueriesTest updated. * chore: WPCS compliance met
1 parent c5cf6b6 commit b2d6608

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

includes/type/object/class-meta-data-type.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ public static function register() {
4444
'type' => 'String',
4545
'description' => __( 'Meta value.', 'wp-graphql-woocommerce' ),
4646
'resolve' => function ( $source ) {
47-
return ! empty( $source->value ) ? (string) $source->value : null;
47+
if ( empty( $source->value ) ) {
48+
return null;
49+
}
50+
51+
if ( is_array( $source->value ) || is_object( $source->value ) ) {
52+
return wp_json_encode( $source->value );
53+
}
54+
55+
return (string) $source->value;
4856
},
4957
],
5058
],

includes/type/object/class-root-query.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ public static function register_fields() {
115115
break;
116116
}
117117

118+
// Check if user authorized to view coupon.
119+
$post_type = get_post_type_object( 'shop_coupon' );
120+
$is_authorized = current_user_can( $post_type->cap->edit_others_posts );
121+
if ( ! $is_authorized ) {
122+
return null;
123+
}
124+
118125
if ( empty( $coupon_id ) ) {
119126
/* translators: %1$s: ID type, %2$s: ID value */
120127
throw new UserError( sprintf( __( 'No coupon ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
@@ -212,7 +219,7 @@ public static function register_fields() {
212219
// Check if user authorized to view order.
213220
$post_type = get_post_type_object( 'shop_order' );
214221
$is_authorized = current_user_can( $post_type->cap->edit_others_posts );
215-
if ( get_current_user_id() ) {
222+
if ( ! $is_authorized && get_current_user_id() ) {
216223
$orders = wc_get_orders(
217224
[
218225
'type' => 'shop_order',

tests/wpunit/CouponQueriesTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,24 @@ public function testCouponQuery() {
120120

121121
/**
122122
* Assertion One
123+
*
124+
* Confirm customer's can't query coupons by ID.
123125
*/
124126
$this->loginAsCustomer();
125127
$variables = [ 'id' => $this->toRelayId( 'shop_coupon', $coupon_id ) ];
126128
$response = $this->graphql( compact( 'query', 'variables' ) );
127-
$expected = $this->expectedCouponData( $coupon_id );
129+
$expected = [ $this->expectedField( 'coupon', self::IS_NULL ) ];
128130

129131
$this->assertQuerySuccessful( $response, $expected );
132+
133+
/**
134+
* Assertion Two
135+
*
136+
* Confirm shop managers can query coupons by ID.
137+
*/
138+
$this->loginAsShopManager();
139+
$response = $this->graphql( compact( 'query', 'variables' ) );
140+
$this->assertQuerySuccessful( $response, $this->expectedCouponData( $coupon_id ) );
130141
}
131142

132143
public function testCouponQueryAndIds() {
@@ -147,7 +158,7 @@ public function testCouponQueryAndIds() {
147158
*
148159
* Testing "ID" ID type.
149160
*/
150-
$this->loginAsCustomer();
161+
$this->loginAsShopManager();
151162
$variables = [
152163
'id' => $relay_id,
153164
'idType' => 'ID',

0 commit comments

Comments
 (0)