Skip to content

Commit

Permalink
Merge pull request #937 from carstingaxion/fix/unit-test-migrate-classes
Browse files Browse the repository at this point in the history
NEW unit tests for the Import & Export classes
  • Loading branch information
mauteri authored Oct 10, 2024
2 parents d692360 + 5483640 commit a0518fa
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 24 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
20 changes: 6 additions & 14 deletions includes/core/classes/class-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ 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 );
}
}
Expand Down Expand Up @@ -137,18 +137,6 @@ public function extend( bool $skip, string $meta_key, object $meta ): bool {
return $skip;
}

/**
* 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 );
}

/**
* Exports all custom data.
*
Expand Down Expand Up @@ -185,7 +173,11 @@ public function run( WP_Post $post ): void {
* @since 1.0.0
*/
public function render( array $callbacks, string $key, WP_Post $post ) {
if ( ! isset( $callbacks['export_callback'] ) || ! is_callable( $callbacks['export_callback'] ) ) {
if (
! isset( $callbacks['export_callback'] ) ||
! is_callable( $callbacks['export_callback'] ) ||
! function_exists( 'wxr_cdata' )
) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion includes/core/classes/class-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,6 @@ public function run( ?bool $check, int $object_id, string $meta_key, $meta_value
public function datetimes_callback( int $post_id, $data ): void {
$event = new Event( $post_id );

$event->save_datetimes( maybe_unserialize( $data ) );
$event->save_datetimes( (array) maybe_unserialize( $data ) );
}
}
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
171 changes: 171 additions & 0 deletions test/unit/php/includes/core/classes/class-test-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use GatherPress\Core\Event;
use GatherPress\Core\Export;
use PMC\Unit_Test\Base;
use PMC\Unit_Test\Utility;

/**
* Class Test_Export.
*
* @coversDefaultClass \GatherPress\Core\Export
* @group migrate
*/
class Test_Export extends Base {
/**
Expand All @@ -40,6 +42,175 @@ public function test_setup_hooks(): void {
$this->assert_hooks( $hooks, $instance );
}

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

$this->assertFalse(
has_action( 'the_post', array( $instance, 'prepare' ) ),
'Failed to assert that the "the_post" action is not already added.'
);
$this->assertFalse(
has_filter( 'wxr_export_skip_postmeta', array( $instance, 'extend' ) ),
'Failed to assert that the "wxr_export_skip_postmeta" filter is not already added.'
);

$instance->export();

$this->assertSame(
10,
has_action( 'the_post', array( $instance, 'prepare' ) ),
'Failed to assert that the "the_post" action was added.'
);

$this->assertSame(
10,
has_filter( 'wxr_export_skip_postmeta', array( $instance, 'extend' ) ),
'Failed to assert that the "wxr_export_skip_postmeta" filter was added.'
);
}

/**
* Coverage for prepare.
*
* @covers ::prepare
*
* @return void
*/
public function test_prepare(): void {
$instance = Export::get_instance();
$post = $this->mock->post()->get();

$this->assertEmpty(
get_post_meta( $post->ID, Export::POST_META, true ),
'Failed to assert the post meta "gatherpress_extend_export" didn\'t exist yet.'
);

// Run method under test with a post.
$instance->prepare( $post );

$this->assertEmpty(
get_post_meta( $post->ID, Export::POST_META, true ),
'Failed to assert the post meta "gatherpress_extend_export" wasn\'t saved for a regular post.'
);

$post = $this->mock->post(
array(
'post_title' => 'Unit Test Event',
'post_type' => 'gatherpress_event',
'post_content' => 'Unit Test description.',
)
)->get();

// Run method under test with a gatherpress_event.
$instance->prepare( $post );

$this->assertSame(
'1',
get_post_meta( $post->ID, Export::POST_META, true ),
'Failed to assert the post meta "gatherpress_extend_export" was saved.'
);

// Clean up for later tests.
delete_post_meta( $post->ID, Export::POST_META );
}

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

$post_id = $this->mock->post()->get()->post_id;
$meta_key = '';
$meta = (object) array(
'post_id' => $post_id,
);
$this->assertTrue(
$instance->extend( true, $meta_key, $meta ),
'Failed to assert the method accepts whether to "skip" saving the current post meta, independent from the data to save.'
);
$this->assertFalse(
$instance->extend( false, $meta_key, $meta ),
'Failed to assert the method accepts whether to "skip" saving the current post meta, independent from the data to save.'
);

$skip = false;
$meta_key = Export::POST_META;

$this->assertSame(
'gatherpress_extend_export',
$meta_key,
'Failed to assert the post meta key hasn\'t changed.'
);

// Add temporary marker.
add_post_meta( $post_id, $meta_key, 'temp-unit-test' );

$this->assertTrue(
$instance->extend( $skip, $meta_key, $meta ),
'Failed to assert the method returns true, even with false given, because the "meta_key" matches.'
);
$this->assertFalse(
get_post_meta( $post_id, $meta_key ),
'Failed to assert the temporary marker was deleted from post meta.'
);
}

/**
* Coverage for run & render.
*
* @covers ::run
* @covers ::render
*
* @return void
*/
public function test_run_and_render(): void {
$export = Export::get_instance();
$post = $this->mock->post(
array(
'post_title' => 'Unit Test Event',
'post_type' => 'gatherpress_event',
'post_content' => 'Unit Test description.',
)
)->get();
$event = new Event( $post->ID );
$params = array(
'datetime_start' => '2020-05-11 15:00:00',
'datetime_end' => '2020-05-12 17:00:00',
'timezone' => 'America/New_York',
);
$event->save_datetimes( $params );

if ( ! function_exists( 'export_wp' ) ) {
require_once ABSPATH . 'wp-admin/includes/export.php';
}

$output = Utility::buffer_and_return( 'export_wp', array( array( 'content' => 'gatherpress_event' ) ) );

$this->assertStringContainsString( '<wp:post_name><![CDATA[unit-test-event]]></wp:post_name>', $output );
$this->assertStringContainsString( '<wp:post_type><![CDATA[gatherpress_event]]></wp:post_type>', $output );

$this->assertStringContainsString( '<wp:meta_key><![CDATA[gatherpress_datetime_start]]></wp:meta_key>', $output );
$this->assertStringContainsString( '<wp:meta_value><![CDATA[2020-05-11 15:00:00]]></wp:meta_value>', $output );

$this->assertStringContainsString( '<wp:meta_key><![CDATA[gatherpress_datetime_end]]></wp:meta_key>', $output );
$this->assertStringContainsString( '<wp:meta_value><![CDATA[2020-05-12 17:00:00]]></wp:meta_value>', $output );

$this->assertStringContainsString( '<wp:meta_key><![CDATA[gatherpress_datetimes]]></wp:meta_key>', $output );
$this->assertStringContainsString( '<wp:meta_value><![CDATA[a:5:{s:14:"datetime_start";s:19:"2020-05-11 15:00:00";s:18:"datetime_start_gmt";s:19:"2020-05-11 19:00:00";s:12:"datetime_end";s:19:"2020-05-12 17:00:00";s:16:"datetime_end_gmt";s:19:"2020-05-12 21:00:00";s:8:"timezone";s:16:"America/New_York";}]]></wp:meta_value>', $output );
}

/**
* Coverage for datetime_callback method.
*
Expand Down
Loading

0 comments on commit a0518fa

Please sign in to comment.