Skip to content

Commit ada326e

Browse files
committed
fix: UsersConnectionSearchColumnEnum values should be prefixed by user_.
1 parent afe6a52 commit ada326e

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed

src/Type/Enum/UsersConnectionSearchColumnEnum.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ public static function register_type() {
2020
'description' => __( 'The globally unique ID.', 'wp-graphql' ),
2121
],
2222
'LOGIN' => [
23-
'value' => 'login',
23+
'value' => 'user_login',
2424
'description' => __( 'The username the User uses to login with.', 'wp-graphql' ),
2525
],
2626
'NICENAME' => [
27-
'value' => 'nicename',
27+
'value' => 'user_nicename',
2828
'description' => __( 'A URL-friendly name for the user. The default is the user\'s username.', 'wp-graphql' ),
2929
],
3030
'EMAIL' => [
31-
'value' => 'email',
31+
'value' => 'user_email',
3232
'description' => __( 'The user\'s email address.', 'wp-graphql' ),
3333
],
3434
'URL' => [
35-
'value' => 'url',
35+
'value' => 'user_url',
3636
'description' => __( 'The URL of the user\'s website.', 'wp-graphql' ),
3737
],
3838
],

tests/wpunit/UserConnectionQueriesTest.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,4 +943,142 @@ public function testWithHasPublishedPostsFilter() {
943943
$this->assertArrayNotHasKey( 'errors', $actual );
944944
$this->assertEmpty( $actual['data']['users']['edges'] );
945945
}
946+
947+
public function testWithSearchColumns() {
948+
$admin_id = $this->factory()->user->create(
949+
[
950+
'role' => 'administrator',
951+
]
952+
);
953+
954+
$user_one_id = $this->factory()->user->create(
955+
[
956+
'user_login' => 'keyword',
957+
]
958+
);
959+
960+
$user_two_id = $this->factory()->user->create(
961+
[
962+
'user_email' => '[email protected]',
963+
]
964+
);
965+
966+
$user_three_id = $this->factory()->user->create(
967+
[
968+
'user_url' => 'https://keyword.com',
969+
]
970+
);
971+
972+
$query = '
973+
query UsersWithSearchColumns( $search: String, $searchColumns: [UsersConnectionSearchColumnEnum] ){
974+
users(first:100 where: { search: $search, searchColumns: $searchColumns } ) {
975+
edges{
976+
node{
977+
databaseId
978+
}
979+
}
980+
}
981+
}
982+
';
983+
984+
// Test search by user_login
985+
$variables = [
986+
'search' => 'keyword',
987+
'searchColumns' => 'LOGIN'
988+
];
989+
990+
wp_set_current_user( $admin_id );
991+
992+
$actual = $this->graphql( compact( 'query', 'variables' ) );
993+
994+
$this->assertArrayNotHasKey( 'errors', $actual );
995+
$this->assertCount( 1, $actual['data']['users']['edges'] );
996+
$this->assertEquals( $user_one_id, $actual['data']['users']['edges'][0]['node']['databaseId'] );
997+
998+
// Test search by user_email
999+
$variables = [
1000+
'search' => 'keyword',
1001+
'searchColumns' => 'EMAIL'
1002+
];
1003+
1004+
$actual = $this->graphql( compact( 'query', 'variables' ) );
1005+
1006+
$this->assertArrayNotHasKey( 'errors', $actual );
1007+
$this->assertCount( 1, $actual['data']['users']['edges'] );
1008+
1009+
$this->assertEquals( $user_two_id, $actual['data']['users']['edges'][0]['node']['databaseId'] );
1010+
1011+
// Test search by user_url
1012+
$variables = [
1013+
'search' => 'keyword',
1014+
'searchColumns' => 'URL'
1015+
];
1016+
1017+
$actual = $this->graphql( compact( 'query', 'variables' ) );
1018+
1019+
$this->assertArrayNotHasKey( 'errors', $actual );
1020+
$this->assertCount( 1, $actual['data']['users']['edges'] );
1021+
1022+
$this->assertEquals( $user_three_id, $actual['data']['users']['edges'][0]['node']['databaseId'] );
1023+
1024+
// Test search by all columns
1025+
$variables = [
1026+
'search' => 'keyword',
1027+
'searchColumns' => [ 'LOGIN', 'EMAIL', 'URL' ]
1028+
];
1029+
1030+
$actual = $this->graphql( compact( 'query', 'variables' ) );
1031+
1032+
$this->assertArrayNotHasKey( 'errors', $actual );
1033+
$this->assertCount( 3, $actual['data']['users']['edges'] );
1034+
1035+
$this->assertEquals( $user_one_id, $actual['data']['users']['edges'][0]['node']['databaseId'] );
1036+
$this->assertEquals( $user_two_id, $actual['data']['users']['edges'][1]['node']['databaseId'] );
1037+
$this->assertEquals( $user_three_id, $actual['data']['users']['edges'][2]['node']['databaseId'] );
1038+
1039+
// Test search by two columns
1040+
$variables = [
1041+
'search' => 'keyword',
1042+
'searchColumns' => [ 'LOGIN', 'EMAIL' ]
1043+
];
1044+
1045+
$actual = $this->graphql( compact( 'query', 'variables' ) );
1046+
1047+
$this->assertArrayNotHasKey( 'errors', $actual );
1048+
$this->assertCount( 2, $actual['data']['users']['edges'] );
1049+
1050+
$this->assertEquals( $user_one_id, $actual['data']['users']['edges'][0]['node']['databaseId'] );
1051+
$this->assertEquals( $user_two_id, $actual['data']['users']['edges'][1]['node']['databaseId'] );
1052+
1053+
// And a different two columns
1054+
$variables = [
1055+
'search' => 'keyword',
1056+
'searchColumns' => [ 'NICENAME', 'URL' ]
1057+
];
1058+
1059+
$actual = $this->graphql( compact( 'query', 'variables' ) );
1060+
1061+
$this->assertArrayNotHasKey( 'errors', $actual );
1062+
$this->assertCount( 2, $actual['data']['users']['edges'] );
1063+
1064+
$this->assertEquals( $user_one_id, $actual['data']['users']['edges'][0]['node']['databaseId'] );
1065+
$this->assertEquals( $user_three_id, $actual['data']['users']['edges'][1]['node']['databaseId'] );
1066+
1067+
// Test bad keyword returns no results
1068+
$variables = [
1069+
'search' => 'badkeyword',
1070+
'searchColumns' => [ 'LOGIN', 'EMAIL', 'URL' ]
1071+
];
1072+
1073+
$actual = $this->graphql( compact( 'query', 'variables' ) );
1074+
1075+
$this->assertArrayNotHasKey( 'errors', $actual );
1076+
$this->assertEmpty( $actual['data']['users']['edges'] );
1077+
1078+
// Cleanup.
1079+
wp_delete_user( $admin_id );
1080+
wp_delete_user( $user_one_id );
1081+
wp_delete_user( $user_two_id );
1082+
wp_delete_user( $user_three_id );
1083+
}
9461084
}

0 commit comments

Comments
 (0)