Skip to content

Commit 8ee87f4

Browse files
committed
final tests cleanup
1 parent 74d4612 commit 8ee87f4

File tree

8 files changed

+189
-46
lines changed

8 files changed

+189
-46
lines changed

includes/data/connection/class-customer-connection-resolver.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,26 @@ public function should_execute() {
4141
* Creates query arguments array
4242
*/
4343
public function get_query_args() {
44+
/**
45+
* Prepare for later use
46+
*/
47+
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
48+
4449
/**
4550
* Set the $query_args based on various defaults and primary input $args
4651
*/
4752
$query_args['count_total'] = false;
48-
$query_args['offset'] = $this->get_offset();
4953
$query_args['orderby'] = 'ID';
5054
$query_args['order'] = ! empty( $this->args['last'] ) ? 'ASC' : 'DESC';
51-
$query_args['number'] = $this->get_query_amount();
55+
$query_args['number'] = $this->get_query_amount() + 1;
56+
57+
/**
58+
* Set the graphql_cursor_offset which is used by Config::graphql_wp_user_query_cursor_pagination_support
59+
* to filter the WP_User_Query to support cursor pagination
60+
*/
61+
$cursor_offset = $this->get_offset();
62+
$query_args['graphql_cursor_offset'] = $cursor_offset;
63+
$query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';
5264

5365
$input_fields = array();
5466
if ( ! empty( $this->args['where'] ) ) {
@@ -74,6 +86,51 @@ public function get_query_args() {
7486

7587
$query_args['fields'] = 'ID';
7688

89+
/**
90+
* Map the orderby inputArgs to the WP_User_Query
91+
*/
92+
if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) {
93+
$query_args['orderby'] = array();
94+
foreach ( $this->args['where']['orderby'] as $orderby_input ) {
95+
/**
96+
* These orderby options should not include the order parameter.
97+
*/
98+
if ( in_array( $orderby_input['field'], array( 'login__in', 'nicename__in' ), true ) ) {
99+
$query_args['orderby'] = esc_sql( $orderby_input['field'] );
100+
} elseif ( ! empty( $orderby_input['field'] ) ) {
101+
$query_args['orderby'] = array(
102+
esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ),
103+
);
104+
}
105+
}
106+
}
107+
108+
/**
109+
* Convert meta_value_num to seperate meta_value value field which our
110+
* graphql_wp_term_query_cursor_pagination_support knowns how to handle
111+
*/
112+
if ( isset( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) {
113+
$query_args['orderby'] = array(
114+
'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query OK.
115+
);
116+
unset( $query_args['order'] );
117+
$query_args['meta_type'] = 'NUMERIC';
118+
}
119+
/**
120+
* If there's no orderby params in the inputArgs, set order based on the first/last argument
121+
*/
122+
if ( empty( $query_args['orderby'] ) ) {
123+
$query_args['order'] = ! empty( $last ) ? 'ASC' : 'DESC';
124+
}
125+
126+
if (
127+
empty( $query_args['role'] ) &&
128+
empty( $query_args['role__in'] ) &&
129+
empty( $query_args['role__not_in'] )
130+
) {
131+
$query_args['role'] = 'customer';
132+
}
133+
77134
$query_args = apply_filters(
78135
'graphql_customer_connection_query_args',
79136
$query_args,

includes/data/mutation/class-order-mutation.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ class Order_Mutation {
1717
/**
1818
* Filterable authentication function.
1919
*
20-
* @param string $mutation Mutation being executed.
21-
* @param array $input Input data describing order.
22-
* @param AppContext $context AppContext instance.
23-
* @param ResolveInfo $info ResolveInfo instance.
20+
* @param string $mutation Mutation being executed.
21+
* @param integer|null $order_id Order ID.
22+
* @param array $input Input data describing order.
23+
* @param AppContext $context AppContext instance.
24+
* @param ResolveInfo $info ResolveInfo instance.
2425
*
2526
* @return boolean
2627
*/
27-
public static function authorized( $mutation = 'create', $input, $context, $info ) {
28+
public static function authorized( $mutation = 'create', $order_id = null, $input, $context, $info ) {
2829
$post_type_object = get_post_type_object( 'shop_order' );
2930

3031
return apply_filters(
@@ -34,6 +35,7 @@ public static function authorized( $mutation = 'create', $input, $context, $info
3435
? $post_type_object->cap->delete_posts
3536
: $post_type_object->cap->edit_posts
3637
),
38+
$order_id,
3739
$input,
3840
$context,
3941
$info

includes/mutation/class-order-create.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public static function get_output_fields() {
133133
*/
134134
public static function mutate_and_get_payload() {
135135
return function( $input, AppContext $context, ResolveInfo $info ) {
136-
if ( ! Order_Mutation::authorized( 'create', $input, $context, $info ) ) {
136+
// Check if authorized to create this order.
137+
if ( ! Order_Mutation::authorized( 'create', null, $input, $context, $info ) ) {
137138
throw new UserError( __( 'User does not have the capabilities necessary to create an order.', 'wp-graphql-woocommerce' ) );
138139
}
139140

includes/mutation/class-order-delete-items.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ public static function get_output_fields() {
8484
*/
8585
public static function mutate_and_get_payload() {
8686
return function( $input, AppContext $context, ResolveInfo $info ) {
87-
if ( ! Order_Mutation::authorized( 'delete-items', $input, $context, $info ) ) {
88-
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
89-
}
90-
9187
// Retrieve order ID.
9288
$order_id = null;
9389
if ( ! empty( $input['id'] ) ) {
@@ -102,6 +98,11 @@ public static function mutate_and_get_payload() {
10298
throw new UserError( __( 'No order ID provided.', 'wp-graphql-woocommerce' ) );
10399
}
104100

101+
// Check if authorized to delete items on this order.
102+
if ( ! Order_Mutation::authorized( 'delete-items', $order_id, $input, $context, $info ) ) {
103+
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
104+
}
105+
105106
// Confirm item IDs.
106107
if ( empty( $input['itemIds'] ) ) {
107108
throw new UserError( __( 'No item IDs provided.', 'wp-graphql-woocommerce' ) );

includes/mutation/class-order-delete.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ public static function get_output_fields() {
8484
*/
8585
public static function mutate_and_get_payload() {
8686
return function( $input, AppContext $context, ResolveInfo $info ) {
87-
if ( ! Order_Mutation::authorized( 'delete', $input, $context, $info ) ) {
88-
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
89-
}
90-
9187
// Retrieve order ID.
9288
$order_id = null;
9389
if ( ! empty( $input['id'] ) ) {
@@ -102,6 +98,11 @@ public static function mutate_and_get_payload() {
10298
throw new UserError( __( 'No order ID provided.', 'wp-graphql-woocommerce' ) );
10399
}
104100

101+
// Check if authorized to delete this order.
102+
if ( ! Order_Mutation::authorized( 'delete', $order_id, $input, $context, $info ) ) {
103+
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
104+
}
105+
105106
$force_delete = false;
106107
if ( ! empty( $input['forceDelete'] ) ) {
107108
$force_delete = $input['forceDelete'];

includes/mutation/class-order-update.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ public static function get_output_fields() {
8585
*/
8686
public static function mutate_and_get_payload() {
8787
return function( $input, AppContext $context, ResolveInfo $info ) {
88-
if ( ! Order_Mutation::authorized( 'update', $input, $context, $info ) ) {
89-
throw new UserError( __( 'User does not have the capabilities necessary to update an order.', 'wp-graphql-woocommerce' ) );
90-
}
91-
9288
// Retrieve order ID.
9389
$order_id = null;
9490
if ( ! empty( $input['id'] ) ) {
@@ -103,6 +99,11 @@ public static function mutate_and_get_payload() {
10399
throw new UserError( __( 'No order ID provided.', 'wp-graphql-woocommerce' ) );
104100
}
105101

102+
// Check if authorized to update this order.
103+
if ( ! Order_Mutation::authorized( 'update', $order_id, $input, $context, $info ) ) {
104+
throw new UserError( __( 'User does not have the capabilities necessary to update an order.', 'wp-graphql-woocommerce' ) );
105+
}
106+
106107
/**
107108
* Action called before order is updated.
108109
*

tests/wpunit/ConnectionPaginationTest.php

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public function setUp() {
1515

1616
// Create users.
1717
$this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) );
18-
$this->simple_customer = $this->factory->user->create( array( 'role' => 'customer' ) );
1918

2019
// Setup helpers.
2120
$this->coupons = $this->getModule('\Helper\Wpunit')->coupon();
@@ -127,7 +126,6 @@ function( $key_a, $key_b ) {
127126
$this->assertEquals( $expected, $actual );
128127
}
129128

130-
// tests
131129
public function testProductsPagination() {
132130
$products = array(
133131
$this->products->create_simple(),
@@ -220,7 +218,6 @@ function( $key_a, $key_b ) {
220218
$this->assertEquals( $expected, $actual );
221219
}
222220

223-
// tests
224221
public function testOrdersPagination() {
225222
$orders = array(
226223
$this->orders->create(),
@@ -315,7 +312,6 @@ function( $key_a, $key_b ) {
315312
$this->assertEquals( $expected, $actual );
316313
}
317314

318-
// tests
319315
public function testRefundsPagination() {
320316
$order = $this->orders->create();
321317
$refunds = array(
@@ -410,4 +406,98 @@ function( $key_a, $key_b ) {
410406

411407
$this->assertEquals( $expected, $actual );
412408
}
409+
410+
public function testCustomersPagination() {
411+
$customers = array(
412+
$this->customers->create(),
413+
$this->customers->create(),
414+
$this->customers->create(),
415+
$this->customers->create(),
416+
$this->customers->create(),
417+
);
418+
419+
usort(
420+
$customers,
421+
function( $key_a, $key_b ) {
422+
return $key_a < $key_b;
423+
}
424+
);
425+
426+
$query = '
427+
query ($first: Int, $last: Int, $after: String, $before: String) {
428+
customers(first: $first, last: $last, after: $after, before: $before) {
429+
nodes {
430+
id
431+
}
432+
pageInfo {
433+
hasPreviousPage
434+
hasNextPage
435+
startCursor
436+
endCursor
437+
}
438+
}
439+
}
440+
';
441+
442+
wp_set_current_user( $this->shop_manager );
443+
444+
/**
445+
* Assertion One
446+
*
447+
* Test "first" parameter.
448+
*/
449+
$variables = array( 'first' => 2 );
450+
$results = graphql(
451+
array(
452+
'query' => $query,
453+
'variables' => $variables,
454+
)
455+
);
456+
457+
// use --debug flag to view.
458+
codecept_debug( $results );
459+
460+
// Check pageInfo.
461+
$this->assertNotEmpty( $results['data'] );
462+
$this->assertNotEmpty( $results['data']['customers'] );
463+
$this->assertNotEmpty( $results['data']['customers']['pageInfo'] );
464+
$this->assertTrue( $results['data']['customers']['pageInfo']['hasNextPage'] );
465+
$this->assertNotEmpty( $results['data']['customers']['pageInfo']['endCursor'] );
466+
$end_cursor = $results['data']['customers']['pageInfo']['endCursor'];
467+
468+
// Check customers.
469+
$actual = $results['data']['customers']['nodes'];
470+
$expected = $this->customers->print_nodes( array_slice( $customers, 0, 2 ) );
471+
472+
$this->assertEquals( $expected, $actual );
473+
474+
/**
475+
* Assertion Two
476+
*
477+
* Test "after" parameter.
478+
*/
479+
$variables = array( 'first' => 3, 'after' => $end_cursor );
480+
$results = graphql(
481+
array(
482+
'query' => $query,
483+
'variables' => $variables,
484+
)
485+
);
486+
487+
// use --debug flag to view.
488+
codecept_debug( $results );
489+
490+
// Check pageInfo.
491+
$this->assertNotEmpty( $results['data'] );
492+
$this->assertNotEmpty( $results['data']['customers'] );
493+
$this->assertNotEmpty( $results['data']['customers']['pageInfo'] );
494+
$this->assertFalse( $results['data']['customers']['pageInfo']['hasNextPage'] );
495+
$this->assertNotEmpty( $results['data']['customers']['pageInfo']['endCursor'] );
496+
497+
// Check customers.
498+
$actual = $results['data']['customers']['nodes'];
499+
$expected = $this->customers->print_nodes( array_slice( $customers, 2, 3 ) );
500+
501+
$this->assertEquals( $expected, $actual );
502+
}
413503
}

tests/wpunit/CustomerQueriesTest.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ public function testCustomersQueryAndWhereArgs() {
190190
array (
191191
'count_total' => false,
192192
'order' => 'ASC',
193-
'fields' => 'ID'
193+
'fields' => 'ID',
194+
'role' => 'customer',
194195
)
195196
);
196197

@@ -374,22 +375,14 @@ public function testCustomersQueryAndWhereArgs() {
374375
/**
375376
* Assertion Seven
376377
*
377-
* Tests "role" where argument.
378+
* Tests "role" where argument.
378379
*/
379380
$variables = array( 'role' => 'SHOP_MANAGER' );
380381
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
381382
$expected = array(
382383
'data' => array(
383384
'customers' => array(
384-
'nodes' => $this->helper->print_nodes(
385-
$users,
386-
array(
387-
'filter' => function( $id ) {
388-
$customer = new \WC_Customer( $id );
389-
return 'shop_manager' === $customer->get_role();
390-
}
391-
)
392-
),
385+
'nodes' => $this->helper->print_nodes( array ( $this->shop_manager ) ),
393386
),
394387
),
395388
);
@@ -402,22 +395,14 @@ public function testCustomersQueryAndWhereArgs() {
402395
/**
403396
* Assertion Eight
404397
*
405-
* Tests "roleIn" where argument.
398+
* Tests "roleIn" where argument. Should
406399
*/
407400
$variables = array( 'roleIn' => array( 'SHOP_MANAGER' ) );
408401
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
409402
$expected = array(
410403
'data' => array(
411404
'customers' => array(
412-
'nodes' => $this->helper->print_nodes(
413-
$users,
414-
array(
415-
'filter' => function( $id ) {
416-
$customer = new \WC_Customer( $id );
417-
return 'shop_manager' === $customer->get_role();
418-
}
419-
)
420-
),
405+
'nodes' => $this->helper->print_nodes( array ( $this->shop_manager ) ),
421406
),
422407
),
423408
);
@@ -438,7 +423,12 @@ public function testCustomersQueryAndWhereArgs() {
438423
'data' => array(
439424
'customers' => array(
440425
'nodes' => $this->helper->print_nodes(
441-
$users,
426+
get_users(
427+
array(
428+
'role__not_in' => 'shop_manager',
429+
'fields' => 'ID',
430+
)
431+
),
442432
array(
443433
'filter' => function( $id ) {
444434
$customer = new \WC_Customer( $id );

0 commit comments

Comments
 (0)