Skip to content

Commit

Permalink
Updates and fixes to unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Oct 10, 2024
1 parent 415d5fe commit 3231754
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 55 deletions.
24 changes: 18 additions & 6 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,14 @@ public function get_datetime(): array {
* @return string The converted date in GMT (UTC) time zone in 'Y-m-d H:i:s' format.
*/
protected function get_gmt_datetime( string $date, DateTimeZone $timezone ): string {
if ( empty( $date ) ) {
return '';
}

$datetime = date_create( $date, $timezone );

if ( false === $datetime ) {
return '0000-00-00 00:00:00';
return '';
}

return $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( self::DATETIME_FORMAT );
Expand Down Expand Up @@ -640,10 +644,18 @@ public function get_calendar_description(): string {
public function save_datetimes( array $params ): bool {
global $wpdb;

$params['post_id'] = $this->event->ID;
$fields = array_filter(
$params = array_merge(
array(
'post_id' => $this->event->ID,
'datetime_start' => '',
'datetime_end' => '',
'timezone' => '',
),
$params
);
$fields = array_filter(
$params,
function ( $key ) {
static function ( $key ) {
return in_array(
$key,
array(
Expand All @@ -665,8 +677,8 @@ function ( $key ) {
$fields['timezone'] = ( ! empty( $fields['timezone'] ) ) ? $fields['timezone'] : wp_timezone_string();
$timezone = new DateTimeZone( $fields['timezone'] );

$fields['datetime_start_gmt'] = $this->get_gmt_datetime( $fields['datetime_start'], $timezone );
$fields['datetime_end_gmt'] = $this->get_gmt_datetime( $fields['datetime_end'], $timezone );
$fields['datetime_start_gmt'] = $this->get_gmt_datetime( (string) $fields['datetime_start'], $timezone );
$fields['datetime_end_gmt'] = $this->get_gmt_datetime( (string) $fields['datetime_end'], $timezone );

$table = sprintf( self::TABLE_FORMAT, $wpdb->prefix );

Expand Down
14 changes: 1 addition & 13 deletions includes/core/classes/class-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,11 @@ public function export(): void {
* @return void
*/
public function prepare( WP_Post $post ): void {
if ( $this->validate( $post ) ) {
if ( Validate::event_post_id( $post->ID ) ) {
add_post_meta( $post->ID, self::POST_META, true );
}
}

/**
* Checks if the currently exported post is of type 'gatherpress_event'.
*
* @since 1.0.0
*
* @param WP_Post $post Current meta key.
* @return bool True, when the currently exported post is of type 'gatherpress_event', false otherwise.
*/
protected function validate( WP_Post $post ): bool {
return ( Event::POST_TYPE === $post->post_type );
}

/**
* Extend WordPress' native Export
*
Expand Down
5 changes: 2 additions & 3 deletions test/unit/php/includes/core/classes/class-test-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,9 @@ public function test_get_gmt_datetime(): void {
)->get();
$event = new Event( $post->ID );
$timezone = new DateTimeZone( 'America/New_York' );
$this->assertSame(
'0000-00-00 00:00:00',
$this->assertEmpty(
Utility::invoke_hidden_method( $event, 'get_gmt_datetime', array( 'unit-test', $timezone ) ),
'Failed to assert that gmt datetime matches.'
'Failed to assert that gmt datetime is empty.'
);
}

Expand Down
29 changes: 0 additions & 29 deletions test/unit/php/includes/core/classes/class-test-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,6 @@ public function test_prepare(): void {
delete_post_meta( $post->ID, Export::POST_META );
}

/**
* Coverage for validate.
*
* @covers ::validate
*
* @return void
*/
public function test_validate(): void {
$instance = Export::get_instance();

$post = $this->mock->post()->get();
$this->assertFalse(
Utility::invoke_hidden_method( $instance, 'validate', array( $post ) ),
'Failed to assert that validation fails for non-validating post data.'
);

$post = $this->mock->post(
array(
'post_title' => 'Unit Test Event',
'post_type' => 'gatherpress_event',
'post_content' => 'Unit Test description.',
)
)->get();
$this->assertTrue(
Utility::invoke_hidden_method( $instance, 'validate', array( $post ) ),
'Failed to assert that validation passes for valid post data.'
);
}

/**
* Coverage for extend.
*
Expand Down
8 changes: 4 additions & 4 deletions test/unit/php/includes/core/classes/class-test-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ public function test_datetimes_callback(): void {
$instance->datetimes_callback( $post->ID, 0 );
$this->assertSame(
array(
'datetime_start' => '',
'datetime_start_gmt' => '',
'datetime_end' => '',
'datetime_end_gmt' => '',
'datetime_start' => '0000-00-00 00:00:00',
'datetime_start_gmt' => '0000-00-00 00:00:00',
'datetime_end' => '0000-00-00 00:00:00',
'datetime_end_gmt' => '0000-00-00 00:00:00',
'timezone' => '+00:00',
),
$event->get_datetime()
Expand Down

0 comments on commit 3231754

Please sign in to comment.