22
33class OptionsPageTest extends \Tests \WPGraphQL \Acf \WPUnit \WPGraphQLAcfTestCase {
44
5-
65 public function setUp ():void {
6+
77 if ( ! function_exists ( 'acf_add_options_page ' ) ) {
88 $ this ->markTestSkipped ( 'ACF Options Pages are not available in this test environment ' );
99 }
@@ -15,13 +15,14 @@ public function tearDown(): void {
1515 parent ::tearDown ();
1616 }
1717
18- public function registerOptionsPage ( $ title = 'My Options Page ' , $ config = [] ) {
1918
2019
20+ public function registerOptionsPage ( $ config = [] ) {
21+
2122 // register options page
2223 acf_add_options_page (
2324 array_merge ( [
24- 'page_title ' => $ title ,
25+ 'page_title ' => ' My Options Page ' ,
2526 'menu_title ' => __ ( 'My Options Page ' ),
2627 'menu_slug ' => 'my-options-page ' ,
2728 'capability ' => 'edit_posts ' ,
@@ -35,15 +36,7 @@ public function testAcfOptionsPageIsQueryableInSchema() {
3536
3637 $ this ->registerOptionsPage ();
3738
38- $ expected_value = uniqid ( 'test ' , true );
39-
40- // Save a value to the ACF Option Field
41- // see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
42- if ( function_exists ( 'update_field ' ) ) {
43- update_field ( 'text ' , $ expected_value , 'option ' );
44- }
45-
46- $ this ->register_acf_field ( [], [
39+ $ field_key = $ this ->register_acf_field ( [], [
4740 'graphql_field_name ' => 'OptionsFields ' ,
4841 'location ' => [
4942 [
@@ -56,6 +49,16 @@ public function testAcfOptionsPageIsQueryableInSchema() {
5649 ],
5750 ]);
5851
52+ $ expected_value = uniqid ( 'test ' , true );
53+
54+ // Save a value to the ACF Option Field
55+ // see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
56+ if ( function_exists ( 'update_field ' ) ) {
57+ update_field ( $ field_key , $ expected_value , 'options ' );
58+ }
59+
60+
61+
5962 $ query = '
6063 {
6164 myOptionsPage {
@@ -103,7 +106,7 @@ public function testAcfOptionsPageIsQueryableInSchema() {
103106 */
104107 public function testOptionsPageNotInSchemaIfShowInGraphqlIsFalse () {
105108
106- acf_add_options_page (
109+ $ this -> registerOptionsPage (
107110 [
108111 'page_title ' => 'ShowInGraphQLFalse ' ,
109112 'menu_title ' => __ ( 'Show in GraphQL False ' ),
@@ -175,26 +178,20 @@ public function testOptionsPageNotInSchemaIfShowInGraphqlIsFalse() {
175178
176179 // - options page shows with different name if "graphql_field_name" is set
177180 public function testOptionsPageRespectsGraphqlFieldName () {
178- acf_add_options_page (
181+
182+ $ this ->registerOptionsPage (
179183 [
180184 'page_title ' => 'CustomGraphqlName ' ,
181- 'menu_title ' => __ ( 'Show in GraphQL False ' ),
185+ 'menu_title ' => __ ( 'Custom GraphQL Name ' ),
182186 'menu_slug ' => 'custom-graphql-name ' ,
183187 'capability ' => 'edit_posts ' ,
188+ 'post_id ' => 'custom-graphql-name ' ,
184189 // options pages will show in the Schema unless set to false
185190 'graphql_type_name ' => 'MyCustomOptionsName ' ,
186191 ]
187192 );
188193
189- $ expected_value = uniqid ( 'test ' , true );
190-
191- // Save a value to the ACF Option Field
192- // see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
193- if ( function_exists ( 'update_field ' ) ) {
194- update_field ( 'text ' , $ expected_value , 'option ' );
195- }
196-
197- $ this ->register_acf_field ( [], [
194+ $ acf_field = $ this ->register_acf_field ( [], [
198195 'graphql_field_name ' => 'OptionsFields ' ,
199196 'location ' => [
200197 [
@@ -207,10 +204,21 @@ public function testOptionsPageRespectsGraphqlFieldName() {
207204 ],
208205 ]);
209206
207+ $ expected_value = 'test value ' ;
208+
209+ // Save a value to the ACF Option Field
210+ // see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
211+ if ( ! function_exists ( 'update_field ' ) ) {
212+ $ this ->markTestSkipped ( 'update_field not available... ' );
213+ }
214+
215+ // update the field using the field key of the registered field
216+ update_field ( $ acf_field , $ expected_value , 'custom-graphql-name ' );
217+
210218 $ query = '
211219 {
212- myCustomOptionsName {
213- optionsFields {
220+ myCustomOptionsName { # this is the name of the options page based on graphql_type_name
221+ optionsFields { # this is the field name for the field group
214222 text
215223 }
216224 }
@@ -221,6 +229,12 @@ public function testOptionsPageRespectsGraphqlFieldName() {
221229 'query ' => $ query ,
222230 ]);
223231
232+ codecept_debug ( [
233+ '$get_field ' => get_field ( 'text ' , 'custom-graphql-name ' ),
234+ 'options_pages ' => acf_get_options_pages (),
235+ '$acf_field ' => $ acf_field ,
236+ ]);
237+
224238 self ::assertQuerySuccessful ( $ actual , [
225239 // the instructions should be used for the description
226240 $ this ->expectedField ( 'myCustomOptionsName.optionsFields.text ' , $ expected_value ),
@@ -248,7 +262,7 @@ public function testOptionsPageRespectsGraphqlFieldName() {
248262
249263 public function testQueryOptionsPageAsNode () {
250264
251- acf_add_options_page (
265+ $ this -> registerOptionsPage (
252266 [
253267 'page_title ' => 'OptionsPageNode ' ,
254268 'menu_title ' => __ ( 'Options Page Node ' ),
0 commit comments