Skip to content

Commit 1e1f998

Browse files
authored
Tests: Add more tests for Form type classes (#347)
1 parent 48b7a29 commit 1e1f998

File tree

7 files changed

+452
-0
lines changed

7 files changed

+452
-0
lines changed

tests/php/includes/forms/test-form-comment.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,122 @@ public function test_comment_form_field_comment_appends_fields() {
200200
$this->assertStringContainsString( $html, $result, 'Should contain original html' );
201201
}
202202

203+
/**
204+
* Test admin_enqueue_scripts returns early on non-comment pages.
205+
*/
206+
public function test_admin_enqueue_scripts_bails_on_wrong_page() {
207+
$form_comment = new acf_form_comment();
208+
209+
// Set pagenow to a non-comment page.
210+
global $pagenow;
211+
$pagenow = 'edit.php'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Test requires setting global.
212+
213+
$form_comment->admin_enqueue_scripts();
214+
215+
// On wrong page, admin_footer action should NOT be added.
216+
$this->assertFalse(
217+
has_action( 'admin_footer', array( $form_comment, 'admin_footer' ) ),
218+
'admin_footer action should not be added on non-comment pages'
219+
);
220+
}
221+
222+
/**
223+
* Test admin_enqueue_scripts adds actions on comment.php.
224+
*/
225+
public function test_admin_enqueue_scripts_adds_actions_on_comment_page() {
226+
$form_comment = new acf_form_comment();
227+
228+
// Set pagenow to comment.php.
229+
global $pagenow;
230+
$pagenow = 'comment.php'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Test requires setting global.
231+
232+
$form_comment->admin_enqueue_scripts();
233+
234+
// On comment page, admin_footer action should be added.
235+
$this->assertNotFalse(
236+
has_action( 'admin_footer', array( $form_comment, 'admin_footer' ) ),
237+
'admin_footer action should be added on comment.php'
238+
);
239+
240+
// edit_comment action should be added.
241+
$this->assertNotFalse(
242+
has_action( 'add_meta_boxes_comment', array( $form_comment, 'edit_comment' ) ),
243+
'add_meta_boxes_comment action should be added on comment.php'
244+
);
245+
}
246+
247+
/**
248+
* Test edit_comment stores correct form data.
249+
*/
250+
public function test_edit_comment_renders_nothing_without_field_groups() {
251+
$form_comment = new acf_form_comment();
252+
253+
// Create a mock comment object.
254+
$comment = new stdClass();
255+
$comment->comment_ID = 123;
256+
$comment->comment_post_ID = 1;
257+
258+
// Ensure no field groups match to avoid rendering.
259+
add_filter(
260+
'acf/get_field_groups',
261+
function () {
262+
return array();
263+
}
264+
);
265+
266+
ob_start();
267+
$form_comment->edit_comment( $comment );
268+
$output = ob_get_clean();
269+
270+
// No output should be generated when no field groups match.
271+
$this->assertEmpty( $output, 'edit_comment should render nothing without field groups' );
272+
}
273+
274+
/**
275+
* Test edit_comment uses correct post_id format.
276+
*
277+
* When field groups exist, edit_comment should store form data with
278+
* post_id formatted as "comment_{comment_ID}".
279+
*/
280+
public function test_edit_comment_uses_correct_post_id_format() {
281+
$form_comment = new acf_form_comment();
282+
283+
// Create a mock comment object.
284+
$comment = new stdClass();
285+
$comment->comment_ID = 456;
286+
$comment->comment_post_ID = 1;
287+
288+
// Register a real field group to trigger the rendering path.
289+
$field_group = acf_update_field_group(
290+
array(
291+
'key' => 'group_comment_test',
292+
'title' => 'Comment Test Group',
293+
'location' => array(
294+
array(
295+
array(
296+
'param' => 'comment',
297+
'operator' => '==',
298+
'value' => 'all',
299+
),
300+
),
301+
),
302+
'instruction_placement' => 'label',
303+
)
304+
);
305+
306+
// Capture output to prevent it from polluting test output.
307+
ob_start();
308+
$form_comment->edit_comment( $comment );
309+
ob_get_clean();
310+
311+
// Verify form data was stored with correct post_id format.
312+
$form_data = acf_get_form_data( 'post_id' );
313+
$this->assertEquals( 'comment_456', $form_data, 'post_id should be formatted as comment_{id}' );
314+
315+
// Cleanup: delete the field group.
316+
acf_delete_field_group( $field_group['ID'] );
317+
}
318+
203319
/**
204320
* Test admin_footer outputs spinner JavaScript.
205321
*/

tests/php/includes/forms/test-form-front.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,135 @@ public function test_validate_form_defaults( $key, $expected ) {
451451

452452
$this->assertEquals( $expected, $args[ $key ], "$key should have correct default" );
453453
}
454+
455+
/**
456+
* Test enqueue_form triggers check_submit_form.
457+
*
458+
* When no form submission is present, enqueue_form should still complete
459+
* without errors and call check_submit_form internally.
460+
*/
461+
public function test_enqueue_form_calls_check_submit_form() {
462+
$form_front = new acf_form_front();
463+
464+
// Clear any POST data that might trigger form submission.
465+
unset( $_POST['_acf_nonce'] );
466+
unset( $_POST['_acf_form'] );
467+
468+
// Track if check_submit_form returns false (no form submitted).
469+
$check_result = $form_front->check_submit_form();
470+
471+
// Should return false when no form data is present.
472+
$this->assertFalse( $check_result, 'check_submit_form should return false without nonce' );
473+
}
474+
475+
/**
476+
* Test submit_form applies pre_submit_form filter.
477+
*/
478+
public function test_submit_form_applies_pre_submit_filter() {
479+
$form_front = new acf_form_front();
480+
481+
$filter_called = false;
482+
add_filter(
483+
'acf/pre_submit_form',
484+
function ( $form ) use ( &$filter_called ) {
485+
$filter_called = true;
486+
// Remove return to prevent redirect.
487+
$form['return'] = '';
488+
return $form;
489+
}
490+
);
491+
492+
// Use a real post to avoid issues.
493+
$post_id = wp_insert_post(
494+
array(
495+
'post_title' => 'Test Submit Form',
496+
'post_status' => 'draft',
497+
)
498+
);
499+
500+
$form = array(
501+
'post_id' => $post_id,
502+
'return' => '',
503+
);
504+
505+
$form_front->submit_form( $form );
506+
507+
$this->assertTrue( $filter_called, 'acf/pre_submit_form filter should be applied' );
508+
509+
// Cleanup.
510+
wp_delete_post( $post_id, true );
511+
}
512+
513+
/**
514+
* Test submit_form fires submit_form action.
515+
*/
516+
public function test_submit_form_fires_action() {
517+
$form_front = new acf_form_front();
518+
519+
$action_fired = false;
520+
$action_post_id = null;
521+
add_action(
522+
'acf/submit_form',
523+
function ( $form, $post_id ) use ( &$action_fired, &$action_post_id ) {
524+
$action_fired = true;
525+
$action_post_id = $post_id;
526+
},
527+
10,
528+
2
529+
);
530+
531+
// Use a real post.
532+
$post_id = wp_insert_post(
533+
array(
534+
'post_title' => 'Test Submit Form Action',
535+
'post_status' => 'draft',
536+
)
537+
);
538+
539+
$form = array(
540+
'post_id' => $post_id,
541+
'return' => '', // Empty to avoid redirect.
542+
);
543+
544+
$form_front->submit_form( $form );
545+
546+
$this->assertTrue( $action_fired, 'acf/submit_form action should fire' );
547+
$this->assertEquals( $post_id, $action_post_id, 'Action should receive correct post_id' );
548+
549+
// Cleanup.
550+
wp_delete_post( $post_id, true );
551+
}
552+
553+
/**
554+
* Test submit_form sets global acf_form variable.
555+
*/
556+
public function test_submit_form_sets_global() {
557+
$form_front = new acf_form_front();
558+
559+
// Clear any existing global.
560+
unset( $GLOBALS['acf_form'] );
561+
562+
// Use a real post.
563+
$post_id = wp_insert_post(
564+
array(
565+
'post_title' => 'Test Global Form',
566+
'post_status' => 'draft',
567+
)
568+
);
569+
570+
$form = array(
571+
'post_id' => $post_id,
572+
'return' => '',
573+
'custom_data' => 'test_value',
574+
);
575+
576+
$form_front->submit_form( $form );
577+
578+
$this->assertArrayHasKey( 'acf_form', $GLOBALS, 'Global acf_form should be set' );
579+
$this->assertEquals( 'test_value', $GLOBALS['acf_form']['custom_data'], 'Global should contain form data' );
580+
581+
// Cleanup.
582+
wp_delete_post( $post_id, true );
583+
unset( $GLOBALS['acf_form'] ); // @phpstan-ignore-line -- Cleanup global in test.
584+
}
454585
}

tests/php/includes/forms/test-form-gutenberg.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,57 @@ public function test_acf_validate_save_post_scenarios( $value, $should_reset ) {
375375
unset( $_GET['meta-box-loader'] );
376376
acf_reset_validation_errors();
377377
}
378+
379+
/**
380+
* Test add_meta_boxes removes edit_form_after_title action.
381+
*/
382+
public function test_add_meta_boxes_removes_edit_form_after_title_action() {
383+
$form_gutenberg = new ACF_Form_Gutenberg();
384+
$form_post = acf_get_instance( 'ACF_Form_Post' );
385+
386+
// Add the action that should be removed.
387+
add_action( 'edit_form_after_title', array( $form_post, 'edit_form_after_title' ) );
388+
389+
// Verify it's added.
390+
$this->assertNotFalse(
391+
has_action( 'edit_form_after_title', array( $form_post, 'edit_form_after_title' ) ),
392+
'edit_form_after_title action should be added first'
393+
);
394+
395+
// Call add_meta_boxes which should remove it.
396+
$form_gutenberg->add_meta_boxes();
397+
398+
// Verify it's removed.
399+
$this->assertFalse(
400+
has_action( 'edit_form_after_title', array( $form_post, 'edit_form_after_title' ) ),
401+
'edit_form_after_title action should be removed by add_meta_boxes'
402+
);
403+
}
404+
405+
/**
406+
* Test block_editor_meta_box_hidden_fields relies on ACF_Form_Post.
407+
*
408+
* The block_editor_meta_box_hidden_fields method calls
409+
* ACF_Form_Post::edit_form_after_title() which requires complex WordPress
410+
* setup (current_screen, global $post, meta boxes). We verify the method
411+
* exists and is callable, while the actual behavior is tested through
412+
* integration tests or the ACF_Form_Post tests.
413+
*/
414+
public function test_block_editor_meta_box_hidden_fields_method_exists() {
415+
$form_gutenberg = new ACF_Form_Gutenberg();
416+
417+
// Verify the method exists and is callable.
418+
$this->assertTrue(
419+
method_exists( $form_gutenberg, 'block_editor_meta_box_hidden_fields' ),
420+
'block_editor_meta_box_hidden_fields method should exist'
421+
);
422+
423+
// Verify it's registered as an action via enqueue_block_editor_assets.
424+
$form_gutenberg->enqueue_block_editor_assets();
425+
426+
$this->assertNotFalse(
427+
has_action( 'block_editor_meta_box_hidden_fields', array( $form_gutenberg, 'block_editor_meta_box_hidden_fields' ) ),
428+
'block_editor_meta_box_hidden_fields should be registered as action'
429+
);
430+
}
378431
}

tests/php/includes/forms/test-form-nav-menu.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,23 @@ public function test_wp_edit_nav_menu_walker_scenarios( $walker_class, $menu_id,
316316
$this->assertEquals( $expected, $result, 'Should return walker class unchanged' );
317317
$this->assertEquals( $menu_id, acf_get_data( 'nav_menu_id' ), 'Should store menu ID' );
318318
}
319+
320+
/**
321+
* Test admin_enqueue_scripts returns early when not on nav-menus screen.
322+
*
323+
* In test environment, get_current_screen() returns null, so acf_is_screen()
324+
* returns false, triggering the early return path.
325+
*/
326+
public function test_admin_enqueue_scripts_bails_when_not_on_nav_menus_screen() {
327+
$form_nav_menu = new acf_form_nav_menu();
328+
329+
// Call admin_enqueue_scripts - should return early since no screen is set.
330+
$form_nav_menu->admin_enqueue_scripts();
331+
332+
// admin_footer action should NOT be added when not on nav-menus screen.
333+
$this->assertFalse(
334+
has_action( 'admin_footer', array( $form_nav_menu, 'admin_footer' ) ),
335+
'admin_footer action should not be added when not on nav-menus screen'
336+
);
337+
}
319338
}

tests/php/includes/forms/test-form-taxonomy.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,25 @@ public function test_validate_page_scenarios( $page, $expected ) {
265265
$this->assertEquals( $expected, $result, "validate_page should return $expected for $page" );
266266
}
267267

268+
/**
269+
* Test admin_enqueue_scripts returns early on non-taxonomy pages.
270+
*/
271+
public function test_admin_enqueue_scripts_bails_on_wrong_page() {
272+
$form_taxonomy = new acf_form_taxonomy();
273+
274+
// Set pagenow to a non-taxonomy page.
275+
global $pagenow;
276+
$pagenow = 'edit.php'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Test requires setting global.
277+
278+
$form_taxonomy->admin_enqueue_scripts();
279+
280+
// On wrong page, admin_footer action should NOT be added.
281+
$this->assertFalse(
282+
has_action( 'admin_footer', array( $form_taxonomy, 'admin_footer' ) ),
283+
'admin_footer action should not be added on non-taxonomy pages'
284+
);
285+
}
286+
268287
/**
269288
* Data provider for view states.
270289
*

0 commit comments

Comments
 (0)