From 40acd786f101532b2d408b86e6bf0d9f78271be6 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Mon, 13 May 2024 12:46:44 +0300 Subject: [PATCH] fix: change item_date to convert to site timezone on import References: #832 --- includes/admin/feedzy-rss-feeds-import.php | 6 +- tests/test-import.php | 66 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index f9e036d5..f2babfe5 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -1413,7 +1413,7 @@ private function run_job( $job, $max ) { } // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $item_date = date( get_option( 'date_format' ) . ' at ' . get_option( 'time_format' ), $item['item_date'] ); + $item_date = wp_date( get_option( 'date_format' ) . ' at ' . get_option( 'time_format' ), $item['item_date'] ); $item_date = $item['item_date_formatted']; // Transform any structure like [[{"value":"[#item_title]"}]] to [#item_title]. @@ -1576,9 +1576,9 @@ private function run_job( $job, $max ) { } // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $item_date = date( 'Y-m-d H:i:s', $item['item_date'] ); + $item_date = wp_date( 'Y-m-d H:i:s', $item['item_date'] ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $now = date( 'Y-m-d H:i:s' ); + $now = wp_date( 'Y-m-d H:i:s' ); if ( trim( $import_date ) === '' ) { $post_date = $now; } diff --git a/tests/test-import.php b/tests/test-import.php index 38a9f204..542b3d67 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -12,6 +12,8 @@ class Test_Feedzy_Import extends WP_UnitTestCase { private $import_limit = 1; + private $original_dates = array(); + /** * Sets up the test methods. */ @@ -303,6 +305,70 @@ public function test_attachement_import() { $this->assertTrue( $attachments[0]->post_mime_type === 'image/jpeg' ); } + /** + * Utility method to capture feed dates before import. + * + * @param array $feed_items The feed items. + * @param array $options The options. + * @param mixed $feed The feed. + * @param string $feedURL The feed URL. + * @param int $sizes The sizes. + * + * @return array + */ + public function mock_feed_dates( $feed_items, $options, $feed, $feedURL, $sizes ) { + $formated_date = date( 'Y-m-d H:i:s', $feed_items[0]['item_date'] ); + $this->original_dates[] = $formated_date; + return $feed_items; + } + + /** + * Test date import with UTC zero. + */ + public function test_date_import_with_utc_zero() { + update_option( 'gmt_offset', 0 ); // ensure UTC is 0 + + add_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 ); + + $post = $this->test_feedzy_imports( $this->get_rand_name(), $this->get_rand_name(), $this->get_two_rand_feeds(), '[#item_content]', false, 'post' ); + + $imported_date = $post->post_date; + + // check if the date is in the original dates + $this->assertTrue( in_array( $imported_date, $this->original_dates, true ) ); + + remove_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 ); + } + + /** + * Test date import with UTC +2. + */ + public function test_date_import_with_utc_plus_two() { + update_option( 'gmt_offset', 2 ); // ensure UTC is +2 + + add_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 ); + + $post = $this->test_feedzy_imports( $this->get_rand_name(), $this->get_rand_name(), $this->get_two_rand_feeds(), '[#item_content]', false, 'post' ); + + $imported_date = $post->post_date; + + // check if the date is not in the original dates + $this->assertTrue( ! in_array( $imported_date, $this->original_dates, true ) ); + + // shift original dates by 2 hours + $shifted_dates = array_map( + function( $date ) { + return date( 'Y-m-d H:i:s', strtotime( $date . ' +2 hours' ) ); + }, + $this->original_dates + ); + + // check if the imported date is in the shifted dates + $this->assertTrue( in_array( $imported_date, $shifted_dates, true ) ); + + remove_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 ); + update_option( 'gmt_offset', 0 ); // reset UTC + } /** * Utility method to generate a random 5 char string.