From 3843238907b1a6501e723033f76a065304e542f9 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 7 Nov 2025 09:50:58 +0530 Subject: [PATCH 1/4] Refactor unit test --- tests/phpunit/tests/post.php | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index e5779946da066..3b81ddbd81507 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -816,13 +816,16 @@ public function test_use_block_editor_for_post() { } /** + * The purpose of this test is to ensure that invalid dates do not + * cause PHP errors when wp_insert_post() is called, and that the + * posts are not actually "inserted" (created). + * * @ticket 26798 * * @dataProvider data_wp_insert_post_handle_malformed_post_date * - * The purpose of this test is to ensure that invalid dates do not - * cause PHP errors when wp_insert_post() is called, and that the - * posts are not actually "inserted" (created). + * @param string $input The input post_date value. + * @param bool $expected Whether the post is expected to be inserted. */ public function test_wp_insert_post_handle_malformed_post_date( $input, $expected ) { $post = array( @@ -842,9 +845,16 @@ public function test_wp_insert_post_handle_malformed_post_date( $input, $expecte } /** - * @ticket 26798 + * Data provider for test_wp_insert_post_handle_malformed_post_date(). + * + * @return array[] { + * Arguments passed to test. + * + * @type string $input The input post_date value. + * @type bool $expected Whether the post is expected to be inserted. + * } */ - public function data_wp_insert_post_handle_malformed_post_date() { + public static function data_wp_insert_post_handle_malformed_post_date() { return array( array( '2012-01-01', @@ -940,12 +950,15 @@ public function data_wp_insert_post_handle_malformed_post_date() { } /** + * Tests the regex inside of wp_resolve_post_date(), with + * the emphasis on the date format (not the time). + * * @ticket 26798 * * @dataProvider data_wp_resolve_post_date_regex * - * Tests the regex inside of wp_resolve_post_date(), with - * the emphasis on the date format (not the time). + * @param string $date The input post_date value. + * @param string|false $expected The expected resolved post date, or false if invalid */ public function test_wp_resolve_post_date_regex( $date, $expected ) { // Attempt to resolve post date. @@ -956,9 +969,16 @@ public function test_wp_resolve_post_date_regex( $date, $expected ) { } /** - * @ticket 26798 + * Data provider for test_wp_resolve_post_date_regex(). + * + * @return array[] { + * Arguments passed to test. + * + * @type string $date The input post_date value. + * @type string|false $expected The expected resolved post date, or false if invalid + * } */ - public function data_wp_resolve_post_date_regex() { + public static function data_wp_resolve_post_date_regex() { return array( array( '2012-01-01', From 21a2c3ba8b94a93fd675f7c98def437fad552db6 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 7 Nov 2025 10:09:18 +0530 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Weston Ruter --- tests/phpunit/tests/post.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 3b81ddbd81507..b394c3e21f520 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -816,9 +816,8 @@ public function test_use_block_editor_for_post() { } /** - * The purpose of this test is to ensure that invalid dates do not - * cause PHP errors when wp_insert_post() is called, and that the - * posts are not actually "inserted" (created). + * Tests that invalid dates do not cause PHP errors when wp_insert_post() + * is called, and that the posts are not actually "inserted" (created). * * @ticket 26798 * From e06836ca7fc527dc9081999f07b673b45c758f36 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 7 Nov 2025 10:13:15 +0530 Subject: [PATCH 3/4] Address review feedbacks --- tests/phpunit/tests/post.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index b394c3e21f520..68dab01c1cd84 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -821,6 +821,8 @@ public function test_use_block_editor_for_post() { * * @ticket 26798 * + * @covers ::wp_insert_post + * * @dataProvider data_wp_insert_post_handle_malformed_post_date * * @param string $input The input post_date value. @@ -846,12 +848,7 @@ public function test_wp_insert_post_handle_malformed_post_date( $input, $expecte /** * Data provider for test_wp_insert_post_handle_malformed_post_date(). * - * @return array[] { - * Arguments passed to test. - * - * @type string $input The input post_date value. - * @type bool $expected Whether the post is expected to be inserted. - * } + * @return array */ public static function data_wp_insert_post_handle_malformed_post_date() { return array( @@ -954,6 +951,8 @@ public static function data_wp_insert_post_handle_malformed_post_date() { * * @ticket 26798 * + * @covers ::wp_resolve_post_date + * * @dataProvider data_wp_resolve_post_date_regex * * @param string $date The input post_date value. @@ -970,12 +969,7 @@ public function test_wp_resolve_post_date_regex( $date, $expected ) { /** * Data provider for test_wp_resolve_post_date_regex(). * - * @return array[] { - * Arguments passed to test. - * - * @type string $date The input post_date value. - * @type string|false $expected The expected resolved post date, or false if invalid - * } + * @return array */ public static function data_wp_resolve_post_date_regex() { return array( From 8faf812d68983a3238b520adc12d843d900d82d7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 6 Nov 2025 20:53:17 -0800 Subject: [PATCH 4/4] Fix return type and data format of data provider --- tests/phpunit/tests/post.php | 172 +++++++++++++++++------------------ 1 file changed, 86 insertions(+), 86 deletions(-) diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 68dab01c1cd84..25627c4e39f6a 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -848,99 +848,99 @@ public function test_wp_insert_post_handle_malformed_post_date( $input, $expecte /** * Data provider for test_wp_insert_post_handle_malformed_post_date(). * - * @return array + * @return array */ - public static function data_wp_insert_post_handle_malformed_post_date() { + public static function data_wp_insert_post_handle_malformed_post_date(): array { return array( array( - '2012-01-01', - true, + 'date' => '2012-01-01', + 'expected' => true, ), // 24-hour time format. array( - '2012-01-01 13:00:00', - true, + 'date' => '2012-01-01 13:00:00', + 'expected' => true, ), // ISO8601 date with timezone. array( - '2016-01-16T00:00:00Z', - true, + 'date' => '2016-01-16T00:00:00Z', + 'expected' => true, ), // ISO8601 date with timezone offset. array( - '2016-01-16T00:00:00+0100', - true, + 'date' => '2016-01-16T00:00:00+0100', + 'expected' => true, ), // RFC3339 Format. array( - '1970-01-01T01:00:00+01:00', - true, + 'date' => '1970-01-01T01:00:00+01:00', + 'expected' => true, ), // RSS Format array( - '1970-01-01T01:00:00+0100', - true, + 'date' => '1970-01-01T01:00:00+0100', + 'expected' => true, ), // Leap year. array( - '2012-02-29', - true, + 'date' => '2012-02-29', + 'expected' => true, ), // Strange formats. array( - '2012-01-01 0', - true, + 'date' => '2012-01-01 0', + 'expected' => true, ), array( - '2012-01-01 25:00:00', - true, + 'date' => '2012-01-01 25:00:00', + 'expected' => true, ), array( - '2012-01-01 00:60:00', - true, + 'date' => '2012-01-01 00:60:00', + 'expected' => true, ), // Dates without leading zeros (valid but malformed format). array( - '2012-08-1', - true, + 'date' => '2012-08-1', + 'expected' => true, ), array( - '2012-1-08 00:00:00', - true, + 'date' => '2012-1-08 00:00:00', + 'expected' => true, ), array( - '2012-01-8 00:00:00', - true, + 'date' => '2012-01-8 00:00:00', + 'expected' => true, ), // Failures. array( - '2012-08-0z', - false, + 'date' => '2012-08-0z', + 'expected' => false, ), array( - '201-01-08 00:00:00', - false, + 'date' => '201-01-08 00:00:00', + 'expected' => false, ), array( - '201-01-08 00:60:00', - false, + 'date' => '201-01-08 00:60:00', + 'expected' => false, ), array( - '201a-01-08 00:00:00', - false, + 'date' => '201a-01-08 00:00:00', + 'expected' => false, ), array( - '2012-31-08 00:00:00', - false, + 'date' => '2012-31-08 00:00:00', + 'expected' => false, ), array( - '2012-01-48 00:00:00', - false, + 'date' => '2012-01-48 00:00:00', + 'expected' => false, ), // Not a leap year. array( - '2011-02-29', - false, + 'date' => '2011-02-29', + 'expected' => false, ), ); } @@ -969,100 +969,100 @@ public function test_wp_resolve_post_date_regex( $date, $expected ) { /** * Data provider for test_wp_resolve_post_date_regex(). * - * @return array + * @return array */ - public static function data_wp_resolve_post_date_regex() { + public static function data_wp_resolve_post_date_regex(): array { return array( array( - '2012-01-01', - '2012-01-01', + 'date' => '2012-01-01', + 'expected' => '2012-01-01', ), array( - '2012-01-01 00:00:00', - '2012-01-01 00:00:00', + 'date' => '2012-01-01 00:00:00', + 'expected' => '2012-01-01 00:00:00', ), // ISO8601 date with timezone. array( - '2016-01-16T00:00:00Z', - '2016-01-16T00:00:00Z', + 'date' => '2016-01-16T00:00:00Z', + 'expected' => '2016-01-16T00:00:00Z', ), // ISO8601 date with timezone offset. array( - '2016-01-16T00:00:00+0100', - '2016-01-16T00:00:00+0100', + 'date' => '2016-01-16T00:00:00+0100', + 'expected' => '2016-01-16T00:00:00+0100', ), // RFC3339 Format. array( - '1970-01-01T01:00:00+01:00', - '1970-01-01T01:00:00+01:00', + 'date' => '1970-01-01T01:00:00+01:00', + 'expected' => '1970-01-01T01:00:00+01:00', ), // RSS Format array( - '1970-01-01T01:00:00+0100', - '1970-01-01T01:00:00+0100', + 'date' => '1970-01-01T01:00:00+0100', + 'expected' => '1970-01-01T01:00:00+0100', ), // 24-hour time format. array( - '2012-01-01 13:00:00', - '2012-01-01 13:00:00', + 'date' => '2012-01-01 13:00:00', + 'expected' => '2012-01-01 13:00:00', ), array( - '2016-01-16T00:0', - '2016-01-16T00:0', + 'date' => '2016-01-16T00:0', + 'expected' => '2016-01-16T00:0', ), array( - '2012-01-01 0', - '2012-01-01 0', + 'date' => '2012-01-01 0', + 'expected' => '2012-01-01 0', ), array( - '2012-01-01 00:00', - '2012-01-01 00:00', + 'date' => '2012-01-01 00:00', + 'expected' => '2012-01-01 00:00', ), array( - '2012-01-01 25:00:00', - '2012-01-01 25:00:00', + 'date' => '2012-01-01 25:00:00', + 'expected' => '2012-01-01 25:00:00', ), array( - '2012-01-01 00:60:00', - '2012-01-01 00:60:00', + 'date' => '2012-01-01 00:60:00', + 'expected' => '2012-01-01 00:60:00', ), array( - '2012-01-01 00:00:60', - '2012-01-01 00:00:60', + 'date' => '2012-01-01 00:00:60', + 'expected' => '2012-01-01 00:00:60', ), // Dates without leading zeros (valid but malformed format). array( - '2012-1-08', - '2012-1-08', + 'date' => '2012-1-08', + 'expected' => '2012-1-08', ), array( - '2012-01-8', - '2012-01-8', + 'date' => '2012-01-8', + 'expected' => '2012-01-8', ), array( - '201-01-08', - false, + 'date' => '201-01-08', + 'expected' => false, ), array( - '201a-01-08', - false, + 'date' => '201a-01-08', + 'expected' => false, ), array( - '2012-31-08', - false, + 'date' => '2012-31-08', + 'expected' => false, ), array( - '2012-01-48 00:00:00', - false, + 'date' => '2012-01-48 00:00:00', + 'expected' => false, ), // Leap year. array( - '2012-02-29', - '2012-02-29', + 'date' => '2012-02-29', + 'expected' => '2012-02-29', ), array( - '2011-02-29', - false, + 'date' => '2011-02-29', + 'expected' => false, ), ); }