Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 104 additions & 91 deletions tests/phpunit/tests/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,13 +816,17 @@ public function test_use_block_editor_for_post() {
}

/**
* 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
*
* @covers ::wp_insert_post
*
* @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(
Expand All @@ -842,110 +846,117 @@ 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<array{ date: string, expected: bool }>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would be:

Suggested change
* @return array<array{ date: string, expected: bool }>
* @return array<string, array{ date: string, expected: bool }>

Where the array keys in the data provider below are strings in an associative array. What are currently comments like "24-hour time format" should rather be part of the array key. This would make it much easier to locate the data set for a failing test.

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

/**
* Tests the regex inside of wp_resolve_post_date(), with
* the emphasis on the date format (not the time).
*
* @ticket 26798
*
* @covers ::wp_resolve_post_date
*
* @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.
Expand All @@ -956,100 +967,102 @@ public function test_wp_resolve_post_date_regex( $date, $expected ) {
}

/**
* @ticket 26798
* Data provider for test_wp_resolve_post_date_regex().
*
* @return array<array{ date: string, expected: string|false }>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto above:

Suggested change
* @return array<array{ date: string, expected: string|false }>
* @return array<string, array{ date: string, expected: string|false }>

But this would require coming up with names for all of the tests to use in the keys.

*/
public 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,
),
);
}
Expand Down
Loading