Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit test coverage for Validate class. #1012

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Changes from all commits
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
199 changes: 196 additions & 3 deletions test/unit/php/includes/core/classes/class-test-validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
* @coversDefaultClass \GatherPress\Core\Validate
*/
class Test_Validate extends Base {

/**
* Coverage for rsvp_status method.
*
* @since 1.0.0
* @covers ::rsvp_status
*
* @return void
Expand Down Expand Up @@ -52,7 +52,9 @@ public function test_rsvp_status(): void {
/**
* Data provider for send test.
*
* @return array[]
* @since 1.0.0
*
* @return array
*/
public function data_send(): array {
return array(
Expand Down Expand Up @@ -94,8 +96,8 @@ public function data_send(): array {
/**
* Coverage for send method.
*
* @since 1.0.0
* @dataProvider data_send
*
* @covers ::send
*
* @param mixed $params The parameters to send for validation.
Expand All @@ -110,6 +112,7 @@ public function test_send( $params, bool $expects ): void {
/**
* Coverage for event_post_id method.
*
* @since 1.0.0
* @covers ::event_post_id
* @covers ::number
*
Expand Down Expand Up @@ -140,10 +143,55 @@ public function test_event_post_id(): void {
'Failed to assert valid event post ID.'
);
}
/**
* Coverage for boolean method.
*
* @since 1.0.0
* @covers ::boolean
*
* @return void
*/
public function test_boolean(): void {
$this->assertTrue(
Validate::boolean( true ),
'Failed to assert valid boolean type.'
);

$this->assertTrue(
Validate::boolean( false ),
'Failed to assert valid boolean type.'
);

$this->assertTrue(
Validate::boolean( 1 ),
'Failed to assert valid boolean type.'
);

$this->assertTrue(
Validate::boolean( 0 ),
'Failed to assert valid boolean type.'
);

$this->assertTrue(
Validate::boolean( '1' ),
'Failed to assert valid boolean type.'
);

$this->assertTrue(
Validate::boolean( '0' ),
'Failed to assert valid boolean type.'
);

$this->assertFalse(
Validate::boolean( 'foobar' ),
'Failed to assert invalid boolean type.'
);
}

/**
* Coverage for event_list_type method.
*
* @since 1.0.0
* @covers ::event_list_type
*
* @return void
Expand All @@ -166,6 +214,7 @@ public function test_event_list_type(): void {
/**
* Coverage for datetime method.
*
* @since 1.0.0
* @covers ::datetime
*
* @return void
Expand All @@ -184,6 +233,7 @@ public function test_datetime(): void {
/**
* Coverage for timezone method.
*
* @since 1.0.0
* @covers ::timezone
*
* @return void
Expand All @@ -198,4 +248,147 @@ public function test_timezone(): void {
'Failed to assert valid timezone.'
);
}

/**
* Test valid block data with all required properties.
*
* @since 1.0.0
* @covers ::block_data
*
* @return void
*/
public function test_valid_block_data(): void {
$valid_data = wp_json_encode(
array(
'blockName' => 'core/paragraph',
'attrs' => array( 'align' => 'center' ),
'innerBlocks' => array(),
)
);

$this->assertTrue(
Validate::block_data( $valid_data )
);
}

/**
* Test invalid JSON string.
*
* @since 1.0.0
* @covers ::block_data
*
* @return void
*/
public function test_invalid_json(): void {
$invalid_json = '{invalid_json";';

$this->assertFalse(
Validate::block_data( $invalid_json )
);
}

/**
* Test missing required properties.
*
* @since 1.0.0
* @covers ::block_data
*
* @return void
*/
public function test_missing_required_properties(): void {
$missing_properties = wp_json_encode(
array(
'blockName' => 'core/paragraph',
'attrs' => array( 'align' => 'center' ),
// Missing innerBlocks.
)
);

$this->assertFalse(
Validate::block_data( $missing_properties )
);
}

/**
* Data provider for invalid property types test.
*
* @since 1.0.0
*
* @return array
*/
public function data_invalid_property_types_provider(): array {
return array(
'non_string_blockname' => array(
array(
'blockName' => array( 'not-a-string' ),
'attrs' => array(),
'innerBlocks' => array(),
),
),
'non_array_attrs' => array(
array(
'blockName' => 'core/paragraph',
'attrs' => 'not-an-array',
'innerBlocks' => array(),
),
),
'non_array_innerblocks' => array(
array(
'blockName' => 'core/paragraph',
'attrs' => array(),
'innerBlocks' => 'not-an-array',
),
),
);
}

/**
* Test invalid property types.
*
* @since 1.0.0
* @dataProvider data_invalid_property_types_provider
* @covers ::block_data
*
* @param array $data Array containing block data with invalid property types.
*
* @return void
*/
public function test_invalid_property_types( array $data ): void {
$invalid_data = wp_json_encode( $data );

$this->assertFalse(
Validate::block_data( $invalid_data )
);
}

/**
* Test complex nested block structure.
*
* @since 1.0.0
* @covers ::block_data
*
* @return void
*/
public function test_complex_nested_blocks(): void {
$complex_data = wp_json_encode(
array(
'blockName' => 'core/column',
'attrs' => array( 'width' => 50 ),
'innerBlocks' => array(
array(
'blockName' => 'core/paragraph',
'attrs' => array( 'dropCap' => true ),
'innerBlocks' => array(),
),
array(
'blockName' => 'core/image',
'attrs' => array( 'id' => 123 ),
'innerBlocks' => array(),
),
),
)
);

$this->assertTrue( Validate::block_data( $complex_data ) );
}
}
Loading