Skip to content

Commit 6cb7115

Browse files
committed
- update tests to support querying for a specific field via fragment on a node
- update test suite to add a published post by editor and published post by author so that more than 1 user are public in the environment - update textFieldTest to test querying the text field on a post - update UserFieldTest to test querying the user field (failing test)
1 parent 122d9ed commit 6cb7115

File tree

4 files changed

+119
-11
lines changed

4 files changed

+119
-11
lines changed

tests/_support/WPUnit/AcfFieldTestCase.php

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,22 @@ public function get_expected_block_fragment_response() {
105105
return null;
106106
}
107107

108-
/***
108+
/**
109+
* Test class should override this with the fragment to test with
110+
*
109111
* @return string
110112
*/
111113
public function get_query_fragment(): string {
112-
return '
113-
fragment QueryFragment on AcfTestGroup {
114-
testText
115-
}
116-
';
114+
return 'null';
115+
}
116+
117+
/**
118+
* Test class should override this with the expected value
119+
*
120+
* @return mixed
121+
*/
122+
public function get_expected_value() {
123+
return 'null';
117124
}
118125

119126
/**
@@ -971,12 +978,57 @@ public function testFieldWithNoGraphqlFieldNameAndNameThatStartsWithNumberDoesNo
971978
acf_remove_local_field( $field_key );
972979
}
973980

981+
public function testQueryFieldOnPostReturnsExpectedValue() {
982+
$field_key = $this->register_acf_field();
983+
984+
// Save data to the post
985+
update_field( $field_key, $this->get_data_to_store(), $this->published_post->ID );
986+
987+
$fragment = $this->get_query_fragment();
988+
989+
if ( 'null' === $fragment ) {
990+
$this->markTestIncomplete( 'get_query_fragment() not defined' );
991+
}
992+
993+
$expected_value = $this->get_expected_value();
994+
995+
if ( 'null' === $expected_value ) {
996+
$this->markTestIncomplete( 'get_expected_value() not defined' );
997+
}
998+
999+
$query = '
1000+
query AcfFieldOnPost ($id: ID!) {
1001+
post( id: $id idType: DATABASE_ID) {
1002+
databaseId
1003+
__typename
1004+
...on WithAcfAcfTestGroup {
1005+
acfTestGroup {
1006+
...AcfTestGroupFragment
1007+
}
1008+
}
1009+
}
1010+
}
1011+
' . $fragment;
1012+
1013+
$actual = $this->graphql([
1014+
'query' => $query,
1015+
'variables' => [
1016+
'id' => $this->published_post->ID,
1017+
]
1018+
]);
1019+
1020+
self::assertQuerySuccessful( $actual, [
1021+
$this->expectedField( 'post.databaseId', $this->published_post->ID ),
1022+
$this->expectedField( 'post.__typename', 'Post' ),
1023+
$this->expectedField( 'post.acfTestGroup.' . $this->get_formatted_field_name(), $this->get_expected_value() )
1024+
]);
9741025

9751026

1027+
}
1028+
9761029
/**
9771030
* @todo: implement the below tests
9781031
*/
979-
// abstract public function testQueryFieldOnPostReturnsExpectedValue();
9801032
// abstract public function testQueryFieldOnPageReturnsExpectedValue();
9811033
// abstract public function testQueryFieldOnCommentReturnsExpectedValue();
9821034
// abstract public function testQueryFieldOnTagReturnsExpectedValue();
@@ -1088,7 +1140,7 @@ public function get_expected_clone_value() {
10881140
*/
10891141
public function testQueryCloneFieldOnPost(): void {
10901142

1091-
$this->markTestIncomplete( 'Clone Fields are being refactored...' );
1143+
// $this->markTestIncomplete( 'Clone Fields are being refactored...' );
10921144

10931145
// if ACF PRO is not active, skip the test
10941146
if ( ! defined( 'ACF_PRO' ) ) {

tests/_support/WPUnit/WPGraphQLAcfTestCase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@ public function setUp(): void {
123123
],
124124
]);
125125

126+
$this->published_post_by_editor = self::factory()->post->create_and_get([
127+
'post_type' => 'post',
128+
'post_status' => 'publish',
129+
'post_author' => $this->editor->ID,
130+
'post_title' => 'Test post by editor title',
131+
'tax_input' => [
132+
'post_tag' => [ $this->tag->term_id ],
133+
'category' => [ $this->category->term_id ],
134+
],
135+
]);
136+
137+
$this->published_post_by_author = self::factory()->post->create_and_get([
138+
'post_type' => 'post',
139+
'post_status' => 'publish',
140+
'post_author' => $this->author->ID,
141+
'post_title' => 'Test post by author title',
142+
'tax_input' => [
143+
'post_tag' => [ $this->tag->term_id ],
144+
'category' => [ $this->category->term_id ],
145+
],
146+
]);
147+
126148
$this->published_page = self::factory()->post->create_and_get([
127149
'post_type' => 'page',
128150
'post_status' => 'publish',

tests/wpunit/FieldTypes/TextFieldTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,21 @@ public function get_block_query_fragment() {
5757
';
5858
}
5959

60-
public function get_block_data_to_store() {
60+
public function get_block_data_to_store(): string {
6161
return 'text value...';
6262
}
6363

64+
public function get_query_fragment(): string {
65+
return '
66+
fragment AcfTestGroupFragment on AcfTestGroup {
67+
testText
68+
}';
69+
}
70+
71+
public function get_expected_value() {
72+
return $this->get_data_to_store();
73+
}
74+
6475
public function get_expected_block_fragment_response() {
6576
return $this->get_block_data_to_store();
6677
}

tests/wpunit/FieldTypes/UserFieldTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function get_expected_field_resolve_kind(): ?string {
2828
return 'OBJECT';
2929
}
3030

31-
public function get_data_to_store():string {
32-
return $this->admin->ID;
31+
public function get_data_to_store() {
32+
return [ $this->admin->ID ];
3333
}
3434

3535
public function get_block_query_fragment() {
@@ -60,6 +60,29 @@ public function get_expected_block_fragment_response() {
6060
];
6161
}
6262

63+
public function get_query_fragment(): string {
64+
return '
65+
fragment AcfTestGroupFragment on AcfTestGroup {
66+
testUser {
67+
nodes {
68+
__typename
69+
databaseId
70+
}
71+
}
72+
}';
73+
}
74+
75+
public function get_expected_value() {
76+
return [
77+
'nodes' => [
78+
[
79+
'__typename' => 'User',
80+
'databaseId' => $this->admin->ID,
81+
]
82+
]
83+
];
84+
}
85+
6386
public function get_clone_value_to_save() {
6487
return $this->get_data_to_store();
6588
}

0 commit comments

Comments
 (0)