Make WordPress Core

Changeset 51587


Ignore:
Timestamp:
08/09/2021 07:08:09 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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: 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: 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
date_format( date_create( 'time phrase or timestamp' ), $format )

Other references:

Props jrf, SergeyBiryukov.
Fixes #53897.

Location:
trunk/tests/phpunit/tests
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/canonical/postStatus.php

    r51568 r51587  
    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
  • trunk/tests/phpunit/tests/comment.php

    r51568 r51587  
    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 );
  • trunk/tests/phpunit/tests/link.php

    r50132 r51587  
    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        );
     
    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        );
  • trunk/tests/phpunit/tests/media.php

    r51568 r51587  
    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
  • trunk/tests/phpunit/tests/multisite/ms-files-rewriting.php

    r51568 r51587  
    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
     
    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();
  • trunk/tests/phpunit/tests/multisite/site.php

    r51568 r51587  
    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
     
    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        }
  • trunk/tests/phpunit/tests/oembed/getResponseData.php

    r51568 r51587  
    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        );
  • trunk/tests/phpunit/tests/oembed/template.php

    r51462 r51587  
    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        );
  • trunk/tests/phpunit/tests/post.php

    r51568 r51587  
    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
     
    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
     
    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 );
     
    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
     
    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 );
     
    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
     
    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
     
    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
     
    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
     
    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
     
    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
  • trunk/tests/phpunit/tests/post/getPostStatus.php

    r49985 r51587  
    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';
  • trunk/tests/phpunit/tests/post/isPostPubliclyViewable.php

    r50130 r51587  
    1919            $actual_status = $post_status;
    2020            if ( 'future' === $post_status ) {
    21                 $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
     21                $date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
    2222            } elseif ( in_array( $post_status, array( 'trash', 'delete' ), true ) ) {
    2323                $actual_status = 'publish';
     
    5252        $date = '';
    5353        if ( 'future' === $post_status ) {
    54             $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
     54            $date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
    5555        }
    5656
  • trunk/tests/phpunit/tests/upload.php

    r51568 r51587  
    2323        // wp_upload_dir() with default parameters.
    2424        $info   = wp_upload_dir();
    25         $subdir = gmstrftime( '/%Y/%m' );
     25        $subdir = date_format( date_create( 'now' ), '/Y/m' );
    2626
    2727        $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
     
    3535        update_option( 'upload_path', 'foo/bar' );
    3636        $info   = _wp_upload_dir();
    37         $subdir = gmstrftime( '/%Y/%m' );
     37        $subdir = date_format( date_create( 'now' ), '/Y/m' );
    3838
    3939        $this->assertSame( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] );
     
    5858        // It doesn't create the /year/month directories.
    5959        $info   = _wp_upload_dir();
    60         $subdir = gmstrftime( '/%Y/%m' );
     60        $subdir = date_format( date_create( 'now' ), '/Y/m' );
    6161
    6262        $this->assertSame( '/baz' . $subdir, $info['url'] );
     
    8484        // It doesn't create the /year/month directories.
    8585        $info   = _wp_upload_dir();
    86         $subdir = gmstrftime( '/%Y/%m' );
     86        $subdir = date_format( date_create( 'now' ), '/Y/m' );
    8787
    8888        $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/asdf' . $subdir, $info['url'] );
     
    9999        // It doesn't create the /year/month directories.
    100100        $info   = _wp_upload_dir();
    101         $subdir = gmstrftime( '/%Y/%m' );
     101        $subdir = date_format( date_create( 'now' ), '/Y/m' );
    102102
    103103        $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
  • trunk/tests/phpunit/tests/xmlrpc/mw/editPost.php

    r51415 r51587  
    333333        $this->assertSame( 'future', $after->post_status );
    334334
    335         $future_date_string = strftime( '%Y-%m-%d %H:%M:%S', $future_time );
     335        $future_date_string = date_format( date_create( "@{$future_time}" ), 'Y-m-d H:i:s' );
    336336        $this->assertSame( $future_date_string, $after->post_date );
    337337    }
  • trunk/tests/phpunit/tests/xmlrpc/mw/getPost.php

    r51415 r51587  
    1717                    )
    1818                ),
    19                 'post_date'   => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
     19                'post_date'   => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
    2020            )
    2121        );
  • trunk/tests/phpunit/tests/xmlrpc/mw/getRecentPosts.php

    r51415 r51587  
    1818                    )
    1919                ),
    20                 'post_date'   => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
     20                'post_date'   => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
    2121            )
    2222        );
  • trunk/tests/phpunit/tests/xmlrpc/wp/editPost.php

    r51415 r51587  
    497497        $this->assertSame( 'future', $after->post_status );
    498498
    499         $future_date_string = strftime( '%Y-%m-%d %H:%M:%S', $future_time );
     499        $future_date_string = date_format( date_create( "@{$future_time}" ), 'Y-m-d H:i:s' );
    500500        $this->assertSame( $future_date_string, $after->post_date );
    501501    }
  • trunk/tests/phpunit/tests/xmlrpc/wp/getPage.php

    r51331 r51587  
    1818                    )
    1919                ),
    20                 'post_date'   => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
     20                'post_date'   => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
    2121            )
    2222        );
  • trunk/tests/phpunit/tests/xmlrpc/wp/getPageList.php

    r48937 r51587  
    1818                    )
    1919                ),
    20                 'post_date'   => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
     20                'post_date'   => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
    2121            )
    2222        );
  • trunk/tests/phpunit/tests/xmlrpc/wp/getPages.php

    r48937 r51587  
    1919                    )
    2020                ),
    21                 'post_date'   => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
     21                'post_date'   => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
    2222            )
    2323        );
  • trunk/tests/phpunit/tests/xmlrpc/wp/getPost.php

    r51568 r51587  
    1919            'post_excerpt' => rand_str( 100 ),
    2020            'post_author'  => $this->make_user_by_role( 'author' ),
    21             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $this->post_date_ts ),
     21            'post_date'    => date_format( date_create( "@{$this->post_date_ts}" ), 'Y-m-d H:i:s' ),
    2222        );
    2323        $this->post_id                 = wp_insert_post( $this->post_data );
  • trunk/tests/phpunit/tests/xmlrpc/wp/getUser.php

    r51568 r51587  
    6262            'role'            => 'author',
    6363            'aim'             => 'wordpress',
    64             'user_registered' => strftime( '%Y-%m-%d %H:%M:%S', $registered_date ),
     64            'user_registered' => date_format( date_create( "@{$registered_date}" ), 'Y-m-d H:i:s' ),
    6565        );
    6666        $user_id         = wp_insert_user( $user_data );
Note: See TracChangeset for help on using the changeset viewer.