mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-05-23 04:34:41 +00:00
Since PHP 8.1, the `strftime()` and `gmstrftime()` functions are deprecated:
> The `strftime()` and `gmstrftime()` functions exhibit similar issues as `strptime()`, in that the formats they support, as well as their behavior, is platform-dependent. Unlike `strptime()`, these functions are available on Windows, though with a different feature set than on Linux. Musl-based distributions like Alpine do not support timezone-related format specifiers correctly. These functions are also locale-based, and as such may exhibit thread-safety issues.
>
> `date()` or `DateTime::format()` provide portable alternatives, and `IntlDateFormatter::format()` provides a more sophisticated, localization-aware alternative.
Reference: [https://wiki.php.net/rfc/deprecations_php_8_1#strftime_and_gmstrftime PHP RFC: Deprecations for PHP 8.1: strftime() and gmstrftime()]
> The `strftime()` and `gmstrftime()` functions have been deprecated in favor of
> `date()/DateTime::format()` (for locale-independent formatting) or
> `IntlDateFormatter::format()` (for locale-dependent formatting).
Reference: [1cf4fb739f/UPGRADING (L379-L381) PHP 8.1 Upgrade Notes].
Aside from one instance in SimplePie, the `strftime()` and `gmstrftime()` functions are only used within the test suite of WordPress to create formatted timestamps.
As the function is used in test code, this leads to test warnings like this on PHP 8.1:
{{{
Deprecated: Function strftime() is deprecated in path/to/tests/phpunit/tests/canonical/postStatus.php on line 37
}}}
These calls can all be safely converted to use a pattern along the lines of:
{{{#!php
<?php
date_format( date_create( 'time phrase or timestamp' ), $format )
}}}
Other references:
* [https://www.php.net/manual/en/function.strftime.php PHP Manual: strftime()] (for the old format string characters)
* [https://www.php.net/manual/en/datetime.format.php PHP Manual: DateTime::format()] (for the new format string characters)
* [https://www.php.net/manual/en/datetime.construct.php PHP Manual: DateTime::__construct()] (see Example 2 for a Unix timestamp code sample)
Props jrf, SergeyBiryukov.
Fixes #53897.
git-svn-id: https://develop.svn.wordpress.org/trunk@51587 602fd350-edb4-49c9-b593-d223f7449a82
168 lines
5.6 KiB
PHP
168 lines
5.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group post
|
|
*/
|
|
class Tests_Post_GetPostStatus extends WP_UnitTestCase {
|
|
|
|
/**
|
|
* Array of post IDs.
|
|
*
|
|
* @var int[]
|
|
*/
|
|
public static $post_ids;
|
|
|
|
/**
|
|
* Create shared fixtures.
|
|
*/
|
|
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
|
$post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash', 'private', 'delete' );
|
|
foreach ( $post_statuses as $post_status ) {
|
|
$date = '';
|
|
$actual_status = $post_status;
|
|
if ( 'future' === $post_status ) {
|
|
$date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
|
|
} elseif ( in_array( $post_status, array( 'trash', 'delete' ), true ) ) {
|
|
$actual_status = 'publish';
|
|
}
|
|
|
|
self::$post_ids[ $post_status ] = $factory->post->create(
|
|
array(
|
|
'post_status' => $actual_status,
|
|
'post_date' => $date,
|
|
'post_name' => "$post_status-post",
|
|
)
|
|
);
|
|
|
|
// Attachments without parent or media.
|
|
self::$post_ids[ "$post_status-attachment-no-parent" ] = $factory->attachment->create_object(
|
|
array(
|
|
'post_status' => $actual_status,
|
|
'post_name' => "$post_status-attachment-no-parent",
|
|
'post_date' => $date,
|
|
)
|
|
);
|
|
|
|
// Attachments without media.
|
|
self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
|
|
array(
|
|
'post_parent' => self::$post_ids[ $post_status ],
|
|
'post_status' => 'inherit',
|
|
'post_name' => "$post_status-attachment",
|
|
'post_date' => $date,
|
|
)
|
|
);
|
|
}
|
|
|
|
// Attachment with incorrect parent ID.
|
|
self::$post_ids['badly-parented-attachment'] = $factory->attachment->create_object(
|
|
array(
|
|
'post_parent' => PHP_INT_MAX, // Impossibly large number.
|
|
'post_status' => 'inherit',
|
|
'post_name' => "$post_status-attachment",
|
|
'post_date' => $date,
|
|
)
|
|
);
|
|
|
|
// Trash the trash post and attachment.
|
|
wp_trash_post( self::$post_ids['trash'] );
|
|
wp_trash_post( self::$post_ids['trash-attachment-no-parent'] );
|
|
|
|
// Force delete parent and unattached post objects.
|
|
wp_delete_post( self::$post_ids['delete'], true );
|
|
wp_delete_post( self::$post_ids['delete-attachment-no-parent'], true );
|
|
}
|
|
|
|
/**
|
|
* Ensure `get_post_status()` resolves correctly for posts and attachments.
|
|
*
|
|
* @ticket 52326
|
|
* @dataProvider data_get_post_status_resolves
|
|
*
|
|
* @param string $post_key The post key in self::$post_ids.
|
|
* @param string $expected The expected get_post_status() return value.
|
|
*/
|
|
public function test_get_post_status_resolves( $post_key, $expected ) {
|
|
$this->assertSame( $expected, get_post_status( self::$post_ids[ $post_key ] ) );
|
|
}
|
|
|
|
/**
|
|
* Data provider for test_get_post_status_resolves().
|
|
*
|
|
* @return array[] {
|
|
* @type string $post_key The post key in self::$post_ids.
|
|
* @type string $expected The expected get_post_status() return value.
|
|
* }
|
|
*/
|
|
public function data_get_post_status_resolves() {
|
|
return array(
|
|
array( 'publish', 'publish' ),
|
|
array( 'future', 'future' ),
|
|
array( 'draft', 'draft' ),
|
|
array( 'auto-draft', 'auto-draft' ),
|
|
array( 'trash', 'trash' ),
|
|
array( 'private', 'private' ),
|
|
array( 'delete', false ),
|
|
|
|
// Attachment with `inherit` status from parent.
|
|
array( 'publish-attachment', 'publish' ),
|
|
array( 'future-attachment', 'future' ),
|
|
array( 'draft-attachment', 'draft' ),
|
|
array( 'auto-draft-attachment', 'auto-draft' ),
|
|
array( 'trash-attachment', 'publish' ),
|
|
array( 'private-attachment', 'private' ),
|
|
array( 'delete-attachment', 'publish' ),
|
|
|
|
// Attachment with native status (rather than inheriting from parent).
|
|
array( 'publish-attachment-no-parent', 'publish' ),
|
|
array( 'future-attachment-no-parent', 'publish' ), // Attachments can't have future status.
|
|
array( 'draft-attachment-no-parent', 'publish' ), // Attachments can't have draft status.
|
|
array( 'auto-draft-attachment-no-parent', 'auto-draft' ),
|
|
array( 'trash-attachment-no-parent', 'trash' ),
|
|
array( 'private-attachment-no-parent', 'private' ),
|
|
array( 'delete-attachment-no-parent', false ),
|
|
|
|
// Attachment attempting to inherit from an invalid parent number.
|
|
array( 'badly-parented-attachment', 'publish' ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Ensure post status resolves after trashing parent posts.
|
|
*
|
|
* @ticket 52326
|
|
* @dataProvider data_get_post_status_after_trashing
|
|
*
|
|
* @param string $post_to_test The post key in self::$post_ids.
|
|
* @param string $post_to_trash The post key to trash then delete in self::$post_ids.
|
|
* @param string $expected The expected result after trashing the post.
|
|
*/
|
|
public function test_get_post_status_after_trashing( $post_to_test, $post_to_trash, $expected ) {
|
|
wp_trash_post( self::$post_ids[ $post_to_trash ] );
|
|
$this->assertSame( $expected, get_post_status( self::$post_ids[ $post_to_test ] ) );
|
|
|
|
// Now delete the post, expect publish.
|
|
wp_delete_post( self::$post_ids[ $post_to_trash ], true );
|
|
$this->assertSame( 'publish', get_post_status( self::$post_ids[ $post_to_test ] ) );
|
|
}
|
|
|
|
/**
|
|
* Data provider for test_get_post_status_after_trashing().
|
|
* @return array[] {
|
|
* @type string $post_to_test The post key in self::$post_ids.
|
|
* @type string $post_to_trash The post key to trash then delete in self::$post_ids.
|
|
* @type string $expected The expected result after trashing the post.
|
|
* }
|
|
*/
|
|
public function data_get_post_status_after_trashing() {
|
|
return array(
|
|
array( 'publish-attachment', 'publish', 'publish' ),
|
|
array( 'future-attachment', 'future', 'future' ),
|
|
array( 'draft-attachment', 'draft', 'draft' ),
|
|
array( 'auto-draft-attachment', 'auto-draft', 'auto-draft' ),
|
|
array( 'private-attachment', 'private', 'private' ),
|
|
array( 'delete-attachment', 'publish', 'publish' ),
|
|
);
|
|
}
|
|
}
|