Skip to content

Commit b935146

Browse files
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: [https://github.com/php/php-src/blob/1cf4fb739f7a4fa8404a4c0958f13d04eae519d4/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
1 parent fff4242 commit b935146

21 files changed

+53
-51
lines changed

tests/phpunit/tests/canonical/postStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
3434
foreach ( $post_statuses as $post_status ) {
3535
$post_date = '';
3636
if ( 'future' === $post_status ) {
37-
$post_date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
37+
$post_date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
3838
}
3939

4040
self::$posts[ $post_status ] = $factory->post->create_and_get(

tests/phpunit/tests/comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,8 @@ public function test_close_comments_for_old_post() {
857857
// Close comments more than one day old.
858858
update_option( 'close_comments_days_old', 1 );
859859

860-
$old_date = strtotime( '-25 hours' );
861-
$old_post_id = self::factory()->post->create( array( 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $old_date ) ) );
860+
$old_date = date_create( '-25 hours' );
861+
$old_post_id = self::factory()->post->create( array( 'post_date' => date_format( $old_date, 'Y-m-d H:i:s' ) ) );
862862

863863
$old_post_comment_status = _close_comments_for_old_post( true, $old_post_id );
864864
$this->assertFalse( $old_post_comment_status );

tests/phpunit/tests/link.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function test_get_permalink_should_not_reveal_post_name_for_post_with_pos
108108
$p = self::factory()->post->create(
109109
array(
110110
'post_status' => 'publish',
111-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
111+
'post_date' => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
112112
)
113113
);
114114

@@ -131,7 +131,7 @@ public function test_get_permalink_should_not_reveal_post_name_for_cpt_with_post
131131
array(
132132
'post_status' => 'future',
133133
'post_type' => 'wptests_pt',
134-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
134+
'post_date' => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
135135
)
136136
);
137137

tests/phpunit/tests/media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2121
foreach ( $post_statuses as $post_status ) {
2222
$date = '';
2323
if ( 'future' === $post_status ) {
24-
strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
24+
date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
2525
}
2626

2727
self::$post_ids[ $post_status ] = $factory->post->create(

tests/phpunit/tests/multisite/ms-files-rewriting.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,22 @@ function test_switch_upload_dir() {
3535
$this->assertTrue( is_main_site() );
3636

3737
$site = get_current_site();
38+
$date = date_format( date_create( 'now' ), 'Y/m' );
3839

3940
$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
4041
$blog_id2 = self::factory()->blog->create( array( 'user_id' => $user_id ) );
4142
$info = wp_upload_dir();
42-
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['url'] );
43-
$this->assertSame( ABSPATH . 'wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['path'] );
44-
$this->assertSame( gmstrftime( '/%Y/%m' ), $info['subdir'] );
43+
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
44+
$this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
45+
$this->assertSame( '/' . $date, $info['subdir'] );
4546
$this->assertFalse( $info['error'] );
4647

4748
switch_to_blog( $blog_id2 );
4849
$info2 = wp_upload_dir();
4950
$this->assertNotEquals( $info, $info2 );
50-
$this->assertSame( get_option( 'siteurl' ) . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime( '%Y/%m' ), $info2['url'] );
51-
$this->assertSame( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime( '%Y/%m' ), $info2['path'] );
52-
$this->assertSame( gmstrftime( '/%Y/%m' ), $info2['subdir'] );
51+
$this->assertSame( get_option( 'siteurl' ) . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['url'] );
52+
$this->assertSame( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['path'] );
53+
$this->assertSame( '/' . $date, $info2['subdir'] );
5354
$this->assertFalse( $info2['error'] );
5455
restore_current_blog();
5556
}

tests/phpunit/tests/multisite/site.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -838,27 +838,28 @@ function test_switch_upload_dir() {
838838
$this->assertTrue( is_main_site() );
839839

840840
$site = get_current_site();
841+
$date = date_format( date_create( 'now' ), 'Y/m' );
841842

842843
$info = wp_upload_dir();
843-
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['url'] );
844-
$this->assertSame( ABSPATH . 'wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['path'] );
845-
$this->assertSame( gmstrftime( '/%Y/%m' ), $info['subdir'] );
844+
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
845+
$this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
846+
$this->assertSame( '/' . $date, $info['subdir'] );
846847
$this->assertFalse( $info['error'] );
847848

848849
$blog_id = self::factory()->blog->create();
849850

850851
switch_to_blog( $blog_id );
851852
$info = wp_upload_dir();
852-
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime( '%Y/%m' ), $info['url'] );
853-
$this->assertSame( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime( '%Y/%m' ), $info['path'] );
854-
$this->assertSame( gmstrftime( '/%Y/%m' ), $info['subdir'] );
853+
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['url'] );
854+
$this->assertSame( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['path'] );
855+
$this->assertSame( '/' . $date, $info['subdir'] );
855856
$this->assertFalse( $info['error'] );
856857
restore_current_blog();
857858

858859
$info = wp_upload_dir();
859-
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['url'] );
860-
$this->assertSame( ABSPATH . 'wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['path'] );
861-
$this->assertSame( gmstrftime( '/%Y/%m' ), $info['subdir'] );
860+
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
861+
$this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
862+
$this->assertSame( '/' . $date, $info['subdir'] );
862863
$this->assertFalse( $info['error'] );
863864
}
864865

tests/phpunit/tests/oembed/getResponseData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function test_get_oembed_response_data_with_scheduled_post() {
119119
$post = self::factory()->post->create_and_get(
120120
array(
121121
'post_status' => 'future',
122-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
122+
'post_date' => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
123123
)
124124
);
125125

tests/phpunit/tests/oembed/template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function test_oembed_output_scheduled_post() {
143143
'post_content' => 'Foo Bar',
144144
'post_excerpt' => 'Bar Baz',
145145
'post_status' => 'future',
146-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
146+
'post_date' => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
147147
)
148148
);
149149

tests/phpunit/tests/post.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function test_vb_insert_future() {
128128
'post_status' => 'publish',
129129
'post_content' => rand_str(),
130130
'post_title' => rand_str(),
131-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
131+
'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
132132
);
133133

134134
// Insert a post and make sure the ID is OK.
@@ -164,7 +164,7 @@ function test_vb_insert_future_over_dst() {
164164
'post_status' => 'publish',
165165
'post_content' => rand_str(),
166166
'post_title' => rand_str(),
167-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
167+
'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
168168
);
169169

170170
// Insert a post and make sure the ID is OK.
@@ -182,7 +182,7 @@ function test_vb_insert_future_over_dst() {
182182
// Now save it again with a date further in the future.
183183

184184
$post['ID'] = $id;
185-
$post['post_date'] = strftime( '%Y-%m-%d %H:%M:%S', $future_date_2 );
185+
$post['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
186186
$post['post_date_gmt'] = null;
187187
wp_update_post( $post );
188188

@@ -209,7 +209,7 @@ function test_vb_insert_future_edit_bug() {
209209
'post_status' => 'publish',
210210
'post_content' => rand_str(),
211211
'post_title' => rand_str(),
212-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
212+
'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
213213
);
214214

215215
// Insert a post and make sure the ID is OK.
@@ -227,7 +227,7 @@ function test_vb_insert_future_edit_bug() {
227227
// Now save it again with a date further in the future.
228228

229229
$post['ID'] = $id;
230-
$post['post_date'] = strftime( '%Y-%m-%d %H:%M:%S', $future_date_2 );
230+
$post['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
231231
$post['post_date_gmt'] = null;
232232
wp_update_post( $post );
233233

@@ -251,7 +251,7 @@ function test_vb_insert_future_draft() {
251251
'post_status' => 'draft',
252252
'post_content' => rand_str(),
253253
'post_title' => rand_str(),
254-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
254+
'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
255255
);
256256

257257
// Insert a post and make sure the ID is OK.
@@ -286,7 +286,7 @@ function test_vb_insert_future_change_to_draft() {
286286
'post_status' => 'publish',
287287
'post_content' => rand_str(),
288288
'post_title' => rand_str(),
289-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
289+
'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
290290
);
291291

292292
// Insert a post and make sure the ID is OK.
@@ -330,7 +330,7 @@ function test_vb_insert_future_change_status() {
330330
'post_status' => 'publish',
331331
'post_content' => rand_str(),
332332
'post_title' => rand_str(),
333-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
333+
'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
334334
);
335335

336336
// Insert a post and make sure the ID is OK.
@@ -372,7 +372,7 @@ function test_vb_insert_future_private() {
372372
'post_status' => 'private',
373373
'post_content' => rand_str(),
374374
'post_title' => rand_str(),
375-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
375+
'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
376376
);
377377

378378
// Insert a post and make sure the ID is OK.
@@ -429,7 +429,7 @@ function test_vb_insert_future_change_to_private() {
429429
'post_status' => 'publish',
430430
'post_content' => rand_str(),
431431
'post_title' => rand_str(),
432-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
432+
'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
433433
);
434434

435435
// Insert a post and make sure the ID is OK.
@@ -535,7 +535,7 @@ function test_delete_future_post_cron() {
535535
'post_status' => 'publish',
536536
'post_content' => rand_str(),
537537
'post_title' => rand_str(),
538-
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
538+
'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
539539
);
540540

541541
// Insert a post and make sure the ID is OK.

tests/phpunit/tests/post/getPostStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2121
$date = '';
2222
$actual_status = $post_status;
2323
if ( 'future' === $post_status ) {
24-
$date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
24+
$date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
2525
} elseif ( in_array( $post_status, array( 'trash', 'delete' ), true ) ) {
2626
$actual_status = 'publish';
2727
}

0 commit comments

Comments
 (0)