@@ -287,6 +287,174 @@ public function test_customize_preview_init() {
287
287
remove_filter ( 'acf/pre_load_reference ' , array ( $ form_customizer_with_settings , 'pre_load_reference ' ) );
288
288
}
289
289
290
+ /**
291
+ * Test the pre_load_value method.
292
+ */
293
+ public function test_pre_load_value () {
294
+ $ form_customizer = new ACF_Form_Customizer ();
295
+
296
+ // Set up preview values
297
+ $ form_customizer ->preview_values = array (
298
+ 'widget_test-1 ' => array (
299
+ 'field_123 ' => 'preview value 1 ' ,
300
+ 'field_456 ' => 'preview value 2 ' ,
301
+ ),
302
+ 'nav_menu_1 ' => array (
303
+ 'field_789 ' => 'menu preview value ' ,
304
+ ),
305
+ );
306
+
307
+ // Test case 1: Should return the preview value when it exists
308
+ $ post_id = 'widget_test-1 ' ;
309
+ $ field = array ( 'key ' => 'field_123 ' );
310
+ $ original_value = 'original value ' ;
311
+
312
+ $ result = $ form_customizer ->pre_load_value ( $ original_value , $ post_id , $ field );
313
+ $ this ->assertEquals ( 'preview value 1 ' , $ result , 'Should return the preview value when it exists ' );
314
+
315
+ // Test case 2: Should return the original value when post_id doesn't have preview values
316
+ $ post_id = 'non_existent_post ' ;
317
+ $ field = array ( 'key ' => 'field_123 ' );
318
+ $ original_value = 'original value ' ;
319
+
320
+ $ result = $ form_customizer ->pre_load_value ( $ original_value , $ post_id , $ field );
321
+ $ this ->assertEquals ( $ original_value , $ result , 'Should return the original value when post_id doesn \'t have preview values ' );
322
+
323
+ // Test case 3: Should return the original value when field key doesn't have preview values
324
+ $ post_id = 'widget_test-1 ' ;
325
+ $ field = array ( 'key ' => 'non_existent_field ' );
326
+ $ original_value = 'original value ' ;
327
+
328
+ $ result = $ form_customizer ->pre_load_value ( $ original_value , $ post_id , $ field );
329
+ $ this ->assertEquals ( $ original_value , $ result , 'Should return the original value when field key doesn \'t have preview values ' );
330
+
331
+ // Test case 4: Verify another field and post_id combination works
332
+ $ post_id = 'nav_menu_1 ' ;
333
+ $ field = array ( 'key ' => 'field_789 ' );
334
+ $ original_value = 'original menu value ' ;
335
+
336
+ $ result = $ form_customizer ->pre_load_value ( $ original_value , $ post_id , $ field );
337
+ $ this ->assertEquals ( 'menu preview value ' , $ result , 'Should correctly return preview values for different post_id and field combinations ' );
338
+ }
339
+
340
+ /**
341
+ * Test the pre_load_reference method.
342
+ */
343
+ public function test_pre_load_reference () {
344
+ $ form_customizer = new ACF_Form_Customizer ();
345
+
346
+ // Set up preview fields
347
+ $ form_customizer ->preview_fields = array (
348
+ 'widget_test-1 ' => array (
349
+ 'title_field ' => 'field_123 ' ,
350
+ 'content_field ' => 'field_456 ' ,
351
+ ),
352
+ 'nav_menu_1 ' => array (
353
+ 'menu_name ' => 'field_789 ' ,
354
+ ),
355
+ );
356
+
357
+ // Test case 1: Should return the preview field key when it exists
358
+ $ field_key = 'some_other_key ' ;
359
+ $ field_name = 'title_field ' ;
360
+ $ post_id = 'widget_test-1 ' ;
361
+
362
+ $ result = $ form_customizer ->pre_load_reference ( $ field_key , $ field_name , $ post_id );
363
+ $ this ->assertEquals ( 'field_123 ' , $ result , 'Should return the preview field key when it exists ' );
364
+
365
+ // Test case 2: Should return the original field key when post_id doesn't have preview fields
366
+ $ field_key = 'original_key ' ;
367
+ $ field_name = 'title_field ' ;
368
+ $ post_id = 'non_existent_post ' ;
369
+
370
+ $ result = $ form_customizer ->pre_load_reference ( $ field_key , $ field_name , $ post_id );
371
+ $ this ->assertEquals ( $ field_key , $ result , 'Should return the original field key when post_id doesn \'t have preview fields ' );
372
+
373
+ // Test case 3: Should return the original field key when field name doesn't have preview fields
374
+ $ field_key = 'original_key ' ;
375
+ $ field_name = 'non_existent_field ' ;
376
+ $ post_id = 'widget_test-1 ' ;
377
+
378
+ $ result = $ form_customizer ->pre_load_reference ( $ field_key , $ field_name , $ post_id );
379
+ $ this ->assertEquals ( $ field_key , $ result , 'Should return the original field key when field name doesn \'t have preview fields ' );
380
+
381
+ // Test case 4: Verify another field name and post_id combination works
382
+ $ field_key = 'some_menu_key ' ;
383
+ $ field_name = 'menu_name ' ;
384
+ $ post_id = 'nav_menu_1 ' ;
385
+
386
+ $ result = $ form_customizer ->pre_load_reference ( $ field_key , $ field_name , $ post_id );
387
+ $ this ->assertEquals ( 'field_789 ' , $ result , 'Should correctly return preview field keys for different post_id and field name combinations ' );
388
+ }
389
+
390
+ /**
391
+ * Test the customize_save method.
392
+ */
393
+ public function test_customize_save () {
394
+
395
+ $ form_customizer = new ACF_Form_Customizer ();
396
+
397
+ // Test case 1: Should do nothing when no settings exist
398
+ $ customizer_no_settings = $ this ->getMockBuilder ( stdClass::class )
399
+ ->getMock ();
400
+
401
+ // Mock the settings method to return false (no settings)
402
+ $ form_customizer_mock = $ this ->getMockBuilder ( 'ACF_Form_Customizer ' )
403
+ ->setMethods ( array ( 'settings ' ) )
404
+ ->getMock ();
405
+ $ form_customizer_mock ->method ( 'settings ' )->willReturn ( false );
406
+
407
+ // Run the method - should not produce errors
408
+ $ form_customizer_mock ->customize_save ( $ customizer_no_settings );
409
+
410
+ // Test case 2: Should save ACF data and add filters when settings exist
411
+ // Create mock settings with ACF data and id_data method
412
+ $ setting1 = $ this ->getMockBuilder ( stdClass::class )
413
+ ->addMethods ( array ( 'id_data ' ) )
414
+ ->getMock ();
415
+ $ setting1 ->id = 'widget_1 ' ;
416
+ $ setting1 ->acf = array (
417
+ 'post_id ' => 'widget_widget-1 ' ,
418
+ 'values ' => array ( 'field_123 ' => 'value1 ' ),
419
+ );
420
+ $ setting1 ->method ( 'id_data ' )->willReturn ( array ( 'base ' => 'widget_text ' ) );
421
+
422
+ $ setting2 = $ this ->getMockBuilder ( stdClass::class )
423
+ ->addMethods ( array ( 'id_data ' ) )
424
+ ->getMock ();
425
+ $ setting2 ->id = 'nav_menu_1 ' ;
426
+ $ setting2 ->acf = array (
427
+ 'post_id ' => 'nav_menu_nav-1 ' ,
428
+ 'values ' => array ( 'field_456 ' => 'value2 ' ),
429
+ );
430
+ $ setting2 ->method ( 'id_data ' )->willReturn ( array ( 'base ' => 'nav_menu_widgets ' ) );
431
+
432
+ // Set up form_customizer with mocked settings method
433
+ $ form_customizer_with_settings = $ this ->getMockBuilder ( 'ACF_Form_Customizer ' )
434
+ ->setMethods ( array ( 'settings ' ) )
435
+ ->getMock ();
436
+ $ form_customizer_with_settings ->method ( 'settings ' )
437
+ ->willReturn ( array ( $ setting1 , $ setting2 ) );
438
+
439
+ // Record filter state before
440
+ $ has_filter_widget_text_before = has_filter ( 'pre_update_option_widget_text ' , array ( $ form_customizer_with_settings , 'pre_update_option ' ) );
441
+ $ has_filter_nav_menu_before = has_filter ( 'pre_update_option_nav_menu_widgets ' , array ( $ form_customizer_with_settings , 'pre_update_option ' ) );
442
+
443
+ // Run the method
444
+ $ form_customizer_with_settings ->customize_save ( $ customizer_no_settings );
445
+
446
+ // Verify filters were added
447
+ $ has_filter_widget_text_after = has_filter ( 'pre_update_option_widget_text ' , array ( $ form_customizer_with_settings , 'pre_update_option ' ) );
448
+ $ has_filter_nav_menu_after = has_filter ( 'pre_update_option_nav_menu_widgets ' , array ( $ form_customizer_with_settings , 'pre_update_option ' ) );
449
+
450
+ $ this ->assertTrue ( false !== $ has_filter_widget_text_after , 'Filter should be added for widget_text ' );
451
+ $ this ->assertTrue ( false !== $ has_filter_nav_menu_after , 'Filter should be added for nav_menu_widgets ' );
452
+
453
+ // Clean up filters
454
+ remove_filter ( 'pre_update_option_widget_text ' , array ( $ form_customizer_with_settings , 'pre_update_option ' ) );
455
+ remove_filter ( 'pre_update_option_nav_menu_widgets ' , array ( $ form_customizer_with_settings , 'pre_update_option ' ) );
456
+ }
457
+
290
458
/**
291
459
* Helper method to create a mock setting object with a proper post_value method.
292
460
*
0 commit comments