Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add some tests to improve coverage
  • Loading branch information
CGastrell committed Nov 28, 2025
commit 7aee2f9b89cb1d63a6f69537138ad33e33f4289b
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,283 @@ public static function data_provider_test_register_child_blocks() {
),
);
}

/**
* Test that ::render_submit_button adds interactivity attributes to submit buttons.
*
* @dataProvider data_provider_test_render_submit_button
*/
#[DataProvider( 'data_provider_test_render_submit_button' )]
public function test_render_submit_button( $content, $parsed_block, $expected ) {
$result = Contact_Form_Block::render_submit_button( $content, $parsed_block );
$this->assertEquals( $expected, $result );
}

/**
* Data provider for test_render_submit_button.
*/
public static function data_provider_test_render_submit_button() {
// Skip HTML Tag Processor tests if the class doesn't exist (WordPress < 6.2)
if ( ! class_exists( '\WP_HTML_Tag_Processor' ) ) {
return array(
'no html tag processor' => array(
'<button>Submit</button>',
array( 'attrs' => array( 'className' => 'jetpack-form-submit-button' ) ),
'<button>Submit</button>',
),
);
}

return array(
'submit button with jetpack class' => array(
'<button class="wp-block-button__link jetpack-form-submit-button">Submit</button>',
array( 'attrs' => array( 'className' => 'jetpack-form-submit-button' ) ),
'<button data-wp-bind--aria-disabled="state.isAriaDisabled" data-wp-class--is-submitting="state.isSubmitting" class="wp-block-button__link jetpack-form-submit-button">Submit</button>',
),
'button without jetpack class' => array(
'<button class="wp-block-button__link">Submit</button>',
array( 'attrs' => array( 'className' => 'wp-block-button__link' ) ),
'<button class="wp-block-button__link">Submit</button>',
),
'submit button with multiple classes including jetpack' => array(
'<button class="my-custom-class jetpack-form-submit-button another-class">Submit</button>',
array( 'attrs' => array( 'className' => 'my-custom-class jetpack-form-submit-button another-class' ) ),
'<button data-wp-bind--aria-disabled="state.isAriaDisabled" data-wp-class--is-submitting="state.isSubmitting" class="my-custom-class jetpack-form-submit-button another-class">Submit</button>',
),
'block with no className attribute' => array(
'<button>Submit</button>',
array( 'attrs' => array() ),
'<button>Submit</button>',
),
'block with empty className' => array(
'<button>Submit</button>',
array( 'attrs' => array( 'className' => '' ) ),
'<button>Submit</button>',
),
'content without button tag' => array(
'<div class="jetpack-form-submit-button">Not a button</div>',
array( 'attrs' => array( 'className' => 'jetpack-form-submit-button' ) ),
'<div class="jetpack-form-submit-button">Not a button</div>',
),
);
}

/**
* Test that ::render_wrapped_html_block wraps HTML blocks with jetpack form parent.
*/
public function test_render_wrapped_html_block() {
$content = '<p>Some HTML content</p>';

// Test with hasJPFormParent flag
$parsed_block_with_parent = array( 'hasJPFormParent' => true );
$result = Contact_Form_Block::render_wrapped_html_block( $content, $parsed_block_with_parent );
$this->assertEquals( '<div><p>Some HTML content</p></div>', $result );

// Test without hasJPFormParent flag
$parsed_block_without_parent = array();
$result = Contact_Form_Block::render_wrapped_html_block( $content, $parsed_block_without_parent );
$this->assertEquals( '<p>Some HTML content</p>', $result );

// Test with hasJPFormParent set to false
$parsed_block_false_parent = array( 'hasJPFormParent' => false );
$result = Contact_Form_Block::render_wrapped_html_block( $content, $parsed_block_false_parent );
$this->assertEquals( '<p>Some HTML content</p>', $result );
}

/**
* Test that ::register_feature adds multistep-form feature.
*/
public function test_register_feature() {
$input_features = array( 'existing-feature' => true );

// We can't easily mock static methods, so we'll test the structure
$result = Contact_Form_Block::register_feature( $input_features );

// Should preserve existing features
$this->assertTrue( $result['existing-feature'] );

// Should add multistep-form feature
$this->assertArrayHasKey( 'multistep-form', $result );
$this->assertIsBool( $result['multistep-form'] );
}

/**
* Test form step counting functionality.
*
* @dataProvider data_provider_test_form_step_counting
*/
#[DataProvider( 'data_provider_test_form_step_counting' )]
public function test_form_step_counting( $block_structure, $expected_steps ) {
// Use reflection to access private method
$reflection = new \ReflectionClass( Contact_Form_Block::class );
$count_method = $reflection->getMethod( 'count_form_steps_in_block' );
$count_method->setAccessible( true );

$result = $count_method->invoke( null, $block_structure );
$this->assertEquals( $expected_steps, $result );
}

/**
* Data provider for form step counting tests.
*/
public static function data_provider_test_form_step_counting() {
return array(
'no inner blocks' => array(
array(
'blockName' => 'jetpack/contact-form',
'innerBlocks' => array(),
),
0,
),
'single form step' => array(
array(
'blockName' => 'jetpack/contact-form',
'innerBlocks' => array(
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
),
),
1,
),
'multiple form steps' => array(
array(
'blockName' => 'jetpack/contact-form',
'innerBlocks' => array(
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
),
),
3,
),
'nested form steps in container' => array(
array(
'blockName' => 'jetpack/contact-form',
'innerBlocks' => array(
array(
'blockName' => 'jetpack/form-step-container',
'innerBlocks' => array(
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
),
),
),
),
2,
),
'mixed blocks with form steps' => array(
array(
'blockName' => 'jetpack/contact-form',
'innerBlocks' => array(
array(
'blockName' => 'jetpack/field-text',
'innerBlocks' => array(),
),
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
array(
'blockName' => 'jetpack/field-email',
'innerBlocks' => array(),
),
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
),
),
2,
),
);
}

/**
* Test pre_render_contact_form hook processing.
*/
public function test_pre_render_contact_form() {
$contact_form_block = array(
'blockName' => 'jetpack/contact-form',
'innerBlocks' => array(
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
array(
'blockName' => 'jetpack/form-step',
'innerBlocks' => array(),
),
),
);

$other_block = array(
'blockName' => 'core/paragraph',
'innerBlocks' => array(),
);

// Test that it returns null for non-contact-form blocks
$result = Contact_Form_Block::pre_render_contact_form( null, $other_block );
$this->assertNull( $result );

// Test that it processes contact form blocks and returns null (lets normal rendering continue)
$result = Contact_Form_Block::pre_render_contact_form( null, $contact_form_block );
$this->assertNull( $result );

// Test that step count is updated after processing
$step_count = Contact_Form_Block::get_form_step_count();
$this->assertEquals( 2, $step_count );
}

/**
* Test get_form_step_count method.
*/
public function test_get_form_step_count() {
// Use reflection to set the private static property for testing
$reflection = new \ReflectionClass( Contact_Form_Block::class );
$step_count_property = $reflection->getProperty( 'form_step_count' );
$step_count_property->setAccessible( true );
$step_count_property->setValue( null, 5 );

$result = Contact_Form_Block::get_form_step_count();
$this->assertEquals( 5, $result );

// Reset to default
$step_count_property->setValue( null, 1 );
}

/**
* Test can_manage_block method behavior.
*/
public function test_can_manage_block() {
// Test the filter override
add_filter( 'jetpack_contact_form_can_manage_block', '__return_true' );
$this->assertTrue( Contact_Form_Block::can_manage_block() );
remove_filter( 'jetpack_contact_form_can_manage_block', '__return_true' );

add_filter( 'jetpack_contact_form_can_manage_block', '__return_false' );

// When not in Jetpack context (class doesn't exist), should return true
if ( ! class_exists( 'Jetpack' ) ) {
$this->assertTrue( Contact_Form_Block::can_manage_block() );
}

remove_filter( 'jetpack_contact_form_can_manage_block', '__return_false' );
}
}