wordpress-develop/tests/phpunit/tests/upload.php
Sergey Biryukov b9351465d6 Code Modernization: Replace strftime() and gmstrftime() usage in unit tests.
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
2021-08-09 19:08:09 +00:00

110 lines
3.7 KiB
PHP

<?php
/**
* @group upload
* @group media
*/
class Tests_Upload extends WP_UnitTestCase {
public $siteurl;
function set_up() {
parent::set_up();
$this->_reset_options();
}
function _reset_options() {
// System defaults.
update_option( 'upload_path', 'wp-content/uploads' );
update_option( 'upload_url_path', '' );
update_option( 'uploads_use_yearmonth_folders', 1 );
}
function test_upload_dir_default() {
// wp_upload_dir() with default parameters.
$info = wp_upload_dir();
$subdir = date_format( date_create( 'now' ), '/Y/m' );
$this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
$this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
$this->assertSame( $subdir, $info['subdir'] );
$this->assertFalse( $info['error'] );
}
function test_upload_dir_relative() {
// wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'.
update_option( 'upload_path', 'foo/bar' );
$info = _wp_upload_dir();
$subdir = date_format( date_create( 'now' ), '/Y/m' );
$this->assertSame( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] );
$this->assertSame( ABSPATH . 'foo/bar' . $subdir, $info['path'] );
$this->assertSame( $subdir, $info['subdir'] );
$this->assertFalse( $info['error'] );
}
/**
* @ticket 5953
*/
function test_upload_dir_absolute() {
$path = get_temp_dir() . 'wp-unit-test';
// wp_upload_dir() with an absolute upload path.
update_option( 'upload_path', $path );
// Doesn't make sense to use an absolute file path without setting the url path.
update_option( 'upload_url_path', '/baz' );
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
// It doesn't create the /year/month directories.
$info = _wp_upload_dir();
$subdir = date_format( date_create( 'now' ), '/Y/m' );
$this->assertSame( '/baz' . $subdir, $info['url'] );
$this->assertSame( $path . $subdir, $info['path'] );
$this->assertSame( $subdir, $info['subdir'] );
$this->assertFalse( $info['error'] );
}
function test_upload_dir_no_yearnum() {
update_option( 'uploads_use_yearmonth_folders', 0 );
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
$info = _wp_upload_dir();
$this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] );
$this->assertSame( ABSPATH . 'wp-content/uploads', $info['path'] );
$this->assertSame( '', $info['subdir'] );
$this->assertFalse( $info['error'] );
}
function test_upload_path_absolute() {
update_option( 'upload_url_path', 'http://' . WP_TESTS_DOMAIN . '/asdf' );
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
// It doesn't create the /year/month directories.
$info = _wp_upload_dir();
$subdir = date_format( date_create( 'now' ), '/Y/m' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/asdf' . $subdir, $info['url'] );
$this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
$this->assertSame( $subdir, $info['subdir'] );
$this->assertFalse( $info['error'] );
}
function test_upload_dir_empty() {
// Upload path setting is empty - it should default to 'wp-content/uploads'.
update_option( 'upload_path', '' );
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
// It doesn't create the /year/month directories.
$info = _wp_upload_dir();
$subdir = date_format( date_create( 'now' ), '/Y/m' );
$this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
$this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
$this->assertSame( $subdir, $info['subdir'] );
$this->assertFalse( $info['error'] );
}
}