🐛fix: Use feed publication date as post_date on import

Extracted the wp_insert_post/wp_update_post argument construction into a
private build_postarr() helper so the post_date/post_date_gmt derivation
from the feed timestamp can be exercised in isolation. Added smoke tests
that pin down the create-path behavior (feed timestamp becomes post_date)
and the fallback path (no feed date leaves post_date unset for WordPress
to fill in at import time). Added minimal get_date_from_gmt and
get_gmt_from_date stubs to the test bootstrap so the new tests run
without WordPress loaded.
This commit is contained in:
Keith Solomon
2026-08-24 10:14:50 -05:00
parent 82f955808d
commit 0a6cf92146
3 changed files with 149 additions and 14 deletions
+93
View File
@@ -40,4 +40,97 @@ final class SmokeTest extends TestCase {
$importer = new RSS2CPT\Import\Importer();
$this->assertSame( 300, $importer->filter_feed_cache_lifetime( 43200, 'https://example.test/feed.xml' ) );
}
/**
* Verify the imported post_date/post_date_gmt come from the feed timestamp
* on both create and update paths, with no timezone drift.
*/
public function test_postarr_uses_feed_timestamp_when_present(): void {
$importer = new RSS2CPT\Import\Importer();
$post_type = ( new \ReflectionClass( $importer ) )->getProperty( 'post_type' );
$post_type->setAccessible( true );
$post_type->setValue( $importer, 'podcast_episode' );
$method = ( new \ReflectionClass( $importer ) )->getMethod( 'build_postarr' );
$method->setAccessible( true );
$timestamp = 1700000000; // 2023-11-14T22:13:20Z, a known UTC moment.
$data = self::sample_item_data( $timestamp );
$expected_gmt = gmdate( 'Y-m-d H:i:s', $timestamp );
$postarr = $method->invoke(
$importer,
$data,
'https://example.test/feed.xml',
array(
'post_status' => 'publish',
'post_author' => 1,
)
);
$this->assertSame( $expected_gmt, $postarr['post_date_gmt'] );
$this->assertSame( get_date_from_gmt( $expected_gmt ), $postarr['post_date'] );
}
/**
* Verify that a feed item without a timestamp leaves post_date unset,
* letting WordPress default to the import time on insert/update.
*/
public function test_postarr_omits_post_date_when_feed_timestamp_missing(): void {
$importer = new RSS2CPT\Import\Importer();
$post_type = ( new \ReflectionClass( $importer ) )->getProperty( 'post_type' );
$post_type->setAccessible( true );
$post_type->setValue( $importer, 'podcast_episode' );
$method = ( new \ReflectionClass( $importer ) )->getMethod( 'build_postarr' );
$method->setAccessible( true );
$postarr = $method->invoke(
$importer,
self::sample_item_data( 0 ),
'https://example.test/feed.xml',
array(
'post_status' => 'publish',
'post_author' => 1,
)
);
$this->assertArrayNotHasKey( 'post_date', $postarr );
$this->assertArrayNotHasKey( 'post_date_gmt', $postarr );
}
/**
* Build a complete sample item-data array.
*
* Both build_postarr() and the helper it calls, build_meta_input(), read
* every field that map_item() produces. Supplying the full set keeps the
* tests focused on the post_date behavior we are pinning down.
*
* @param int $timestamp Optional feed timestamp (zero means "no date").
* @return array<string,mixed>
*/
private static function sample_item_data( int $timestamp ): array {
return array(
'title' => 'Episode',
'content' => 'Body',
'excerpt' => 'Teaser',
'timestamp' => $timestamp,
'link' => 'https://example.test/episode',
'audio_url' => 'https://example.test/episode.mp3',
'audio_type' => 'audio/mpeg',
'audio_length' => 0,
'duration' => '',
'duration_seconds' => 0,
'season' => 0,
'episode' => 0,
'episode_type' => '',
'explicit' => '0',
'source_id' => 'src-1',
'source_key' => str_repeat( 'a', 64 ),
'creator' => '',
'fingerprint' => str_repeat( 'b', 64 ),
);
}
}