Skip to content

Commit dbd010c

Browse files
committed
- update AcfeDateRangePicker to get start and end values from the results of resolve_field
- update test suites - Add test for querying the daterangepicker
1 parent 122d9ed commit dbd010c

File tree

6 files changed

+183
-14
lines changed

6 files changed

+183
-14
lines changed

src/ThirdParty/AcfExtended/FieldType/AcfeDateRangePicker.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public static function register_field_type(): void {
1515
[
1616
'graphql_type' => 'ACFE_Date_Range',
1717
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
18-
$acf_field = $field_config->get_acf_field();
19-
$start_date = $field_config->resolve_field( $root, $args, $context, $info, [ 'name' => $acf_field['name'] . '_start' ] );
20-
$end_date = $field_config->resolve_field( $root, $args, $context, $info, [ 'name' => $acf_field['name'] . '_end' ] );
18+
$value = $field_config->resolve_field( $root, $args, $context, $info );
19+
$start_date = $value['start'] ?? null;
20+
$end_date = $value['end'] ?? null;
2121

2222
if ( ! empty( $start_date ) ) {
2323
$_start_date = \DateTime::createFromFormat( 'Ymd|', $start_date );

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/AcfeDateRangePickerFieldTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,65 @@ public function testFieldExists(): void {
6868
}
6969
}
7070

71+
public function get_query_fragment(): string {
72+
return '
73+
fragment AcfTestGroupFragment on AcfTestGroup {
74+
testAcfeDateRangePicker {
75+
startDate
76+
endDate
77+
}
78+
}';
79+
}
80+
81+
82+
public function testQueryDateRangePickerFieldOnPost() {
83+
84+
$this->register_acf_field();
85+
86+
$start_date = '20240205'; // feb 5 2024
87+
$end_date = '20240207'; // feb 7 2024
88+
89+
update_field( $this->get_field_name() . '_start', $start_date, $this->published_post->ID );
90+
update_field( $this->get_field_name() . '_end', $end_date, $this->published_post->ID );
91+
92+
$formatted_end_date = \DateTime::createFromFormat( 'Ymd|', $end_date );
93+
$formatted_end_date = $formatted_end_date->format( \DateTimeInterface::RFC3339 );
94+
95+
$formatted_start_date = \DateTime::createFromFormat( 'Ymd|', $start_date );
96+
$formatted_start_date = $formatted_start_date->format( \DateTimeInterface::RFC3339 );
97+
98+
$expected_value = [
99+
'startDate' => $formatted_start_date,
100+
'endDate' => $formatted_end_date
101+
];
102+
103+
$fragment = $this->get_query_fragment();
104+
105+
$query = '
106+
query getPostById( $id: ID! ) {
107+
post( id:$id idType:DATABASE_ID) {
108+
__typename
109+
databaseId
110+
acfTestGroup {
111+
...AcfTestGroupFragment
112+
}
113+
}
114+
}
115+
' . $fragment;
116+
117+
$actual = $this->graphql([
118+
'query' => $query,
119+
'variables' => [
120+
'id' => $this->published_post->ID,
121+
]
122+
]);
123+
124+
self::assertQuerySuccessful( $actual, [
125+
$this->expectedField( 'post.databaseId', $this->published_post->ID ),
126+
$this->expectedField( 'post.__typename', 'Post' ),
127+
$this->expectedField( 'post.acfTestGroup.testAcfeDateRangePicker', $expected_value )
128+
]);
129+
130+
}
131+
71132
}

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)