Make WordPress Core

Ticket #28992: 28992.patch

File 28992.patch, 9.5 KB (added by pbearne, 10 years ago)

complete patch for mysql2date() and Unit tests adding _doing_it_wrong call to warning about bad output

  • src/wp-includes/functions.php

     
    2121 * @param string $format    Format of the date to return.
    2222 * @param string $date      Date string to convert.
    2323 * @param bool   $translate Whether the return date should be translated. Default true.
    24  * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
     24 * @return string|int|bool|false Formatted date string or Unix timestamp. False if $date is empty.
    2525 */
    2626function mysql2date( $format, $date, $translate = true ) {
    27         if ( empty( $date ) )
     27
     28        if ( empty( $date ) ) {
     29                _doing_it_wrong( 'mysql2date' , 'empty string passed', '4.4.0' );
    2830                return false;
     31        }
    2932
    30         if ( 'G' == $format )
     33        if ( 'G' === $format ) {
    3134                return strtotime( $date . ' +0000' );
     35        }
    3236
    3337        $i = strtotime( $date );
    34 
    35         if ( 'U' == $format )
     38        if ( false === $i ) {
     39                _doing_it_wrong( 'mysql2date' , 'bad date passed!', '4.4.0' );
     40        }
     41        if ( 'U' === $format ) {
    3642                return $i;
     43        }
    3744
    38         if ( $translate )
     45        if ( $translate ) {
    3946                return date_i18n( $format, $i );
    40         else
     47        } else {
    4148                return date( $format, $i );
     49        }
    4250}
    4351
    4452/**
  • tests/phpunit/tests/functions/mysql2date.php

     
     1<?php
     2
     3/**
     4 * @group functions.php
     5 * @group 28559
     6 */
     7/**
     8 * Convert given date string into a different format.
     9 *
     10 * $format should be either a PHP date format string, e.g. 'U' for a Unix
     11 * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
     12 *
     13 * If $translate is true then the given date and format string will
     14 * be passed to date_i18n() for translation.
     15 *
     16 * @since 0.71
     17 *
     18 * @param string $format Format of the date to return.
     19 * @param string $date Date string to convert.
     20 * @param bool $translate Whether the return date should be translated. Default true.
     21 *
     22 * @return string|int Formatted date string, or Unix timestamp.
     23 */
     24
     25
     26class Tests_Functions_mysql2date extends WP_UnitTestCase {
     27
     28
     29
     30        /**
     31         *
     32         */
     33        function test_mysql2date_pass_empty_date() {
     34                $this->setExpectedIncorrectUsage( 'mysql2date' );
     35                $this->assertFalse( mysql2date( '', '' ) , 'Pass empty strings' );
     36                $this->assertFalse( mysql2date( 'Y-m-d', '' ) , 'Pass Y-m-d and empty string' );
     37        }
     38        /**
     39         *
     40         */
     41        function test_mysql2date_pass_bad_date() {
     42                $current_date = date( 'Y-m-d' );
     43
     44                $this->setExpectedIncorrectUsage( 'mysql2date' );
     45                // invalid date return current date
     46                $this->assertEquals( $current_date, mysql2date( 'Y-m-d', '42351234523541345143534534535314' ), 'passed Y-m-d, 42351234523541345143534534535314' );
     47                $this->assertEquals( $current_date, mysql2date( 'Y-m-d', '1' ) );
     48                $this->assertEquals( $current_date, mysql2date( 'Y-m-d', '1355270400' ) ); // unix date does work
     49                $this->assertEquals( $current_date, mysql2date( 'Y-m-d', 'i am not a date' ) );
     50
     51                $this->assertFalse( mysql2date( 'U', '42351234523541345143534534535314' ), 'passed Y-m-d, 42351234523541345143534534535314' );
     52                $this->assertFalse( mysql2date( 'U', '1' ) );
     53                $this->assertFalse( mysql2date( 'U', '1355270400' ) ); // unix date does work
     54                $this->assertFalse( mysql2date( 'U', 'i am not a date' ) );
     55
     56                $this->assertFalse( mysql2date( 'G', '42351234523541345143534534535314' ), 'passed Y-m-d, 42351234523541345143534534535314' );
     57                $this->assertFalse( mysql2date( 'G', '1' ) );
     58                $this->assertFalse( mysql2date( 'G', '1355270400' ) ); // unix date does work
     59                $this->assertFalse( mysql2date( 'G', 'i am not a date' ) );
     60
     61        }
     62
     63        /**
     64         * moved from posts.php tests
     65         * @ticket 28310
     66         */
     67        function test_mysql2date_returns_false_with_no_date() {
     68                $this->setExpectedIncorrectUsage( 'mysql2date' );
     69                $this->assertFalse( mysql2date( 'F j, Y H:i:s', '' ) );
     70        }
     71
     72        /**
     73         * moved from posts.php tests
     74         * @ticket 28310
     75         */
     76        function test_mysql2date_returns_gmt_or_unix_timestamp() {
     77                $this->assertEquals( '441013392', mysql2date( 'G', '1983-12-23 07:43:12' ) );
     78                $this->assertEquals( '441013392', mysql2date( 'U', '1983-12-23 07:43:12' ) );
     79        }
     80
     81
     82        /**
     83        *
     84        */
     85        function test_mysql2date() {
     86
     87                $this->assertEquals( '2012-12-30', mysql2date( 'Y-m-d', '2012-12-30' ) );
     88                $this->assertEquals( '12-12-30', mysql2date( 'y-m-d', '2012-12-30' ) );
     89                $this->assertEquals( '1356825600', mysql2date( 'G', '2012-12-30' ) );
     90                $this->assertEquals( '1356825600', mysql2date( 'U', '2012-12-30' ) );
     91
     92                // false is th default
     93                $this->assertEquals( '2012-12-30', mysql2date( 'Y-m-d', '2012-12-30', false ) );
     94
     95                $this->assertEquals( '2012-12-30', mysql2date( 'Y-m-d', 'December 30, 2012, 12:00 am' ) );
     96
     97                $this->assertEquals( 'December 30, 2012, 12:00 am', mysql2date( 'F j, Y, g:i a', '2012-12-30' ) );
     98                // no change on the phpunit as testing in english
     99                $this->assertEquals( 'December 30, 2012, 12:00 am', mysql2date( 'F j, Y, g:i a', '2012-12-30' ), true );
     100
     101        }
     102
     103        /**
     104         *
     105        */
     106        function test_mysql2date_format_matches() {
     107                $formated_now = date( 'Y-m-d' );
     108                $this->assertNotEquals( $formated_now, mysql2date( 'Y-m-d', 'tomorrow' ) );
     109                $this->assertEquals( $formated_now, mysql2date( 'Y-m-d', 'today' ) );
     110
     111        }
     112
     113        /**
     114         *
     115         */
     116        function test_mysql2date_transulation() {
     117
     118                add_filter( 'date_i18n', array( __class__, 'retrurn_date_i18n_ran' ), 1, 99999 );
     119
     120                $this->assertEquals( 'date_i18n_ran', mysql2date( 'F j, Y, g:i a', '2012-12-30', true ) );
     121                $this->assertEquals( 'date_i18n_ran', mysql2date( 'Y-m-d', '2012-12-30', true ) );
     122
     123                remove_filter( 'date_i18n', array( __class__, 'retrurn_date_i18n_ran' ), 1 );
     124
     125        }
     126
     127        public static function retrurn_date_i18n_ran( $date ) {
     128                return 'date_i18n_ran';
     129        }
     130
     131}
  • tests/phpunit/tests/post.php

     
    931931                $this->assertFalse( get_post_modified_time( 'h:i:s', false, 9 ) );
    932932        }
    933933
    934         /**
    935          * @ticket 28310
    936          */
    937         function test_mysql2date_returns_false_with_no_date() {
    938                 $this->assertFalse( mysql2date( 'F j, Y H:i:s', '' ) );
    939         }
    940934
    941         /**
    942          * @ticket 28310
    943          */
    944         function test_mysql2date_returns_gmt_or_unix_timestamp() {
    945                 $this->assertEquals( '441013392', mysql2date( 'G', '1983-12-23 07:43:12' ) );
    946                 $this->assertEquals( '441013392', mysql2date( 'U', '1983-12-23 07:43:12' ) );
    947         }
    948935
    949936        /**
    950937         * @ticket 25566
  • tests/phpunit/tests/query/conditionals.php

     
    287287        // 'search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]',
    288288        // 'search/(.+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]',
    289289        function test_search_feed() {
     290                $this->setExpectedIncorrectUsage( 'mysql2date' );
    290291                // check the long form
    291292                $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
    292293                foreach ($types as $type) {
     
    326327        // 'category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]',
    327328        // 'category/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]',
    328329        function test_category_feed() {
     330                $this->setExpectedIncorrectUsage( 'mysql2date' );
    329331                $this->factory->term->create( array( 'name' => 'cat-a', 'taxonomy' => 'category' ) );
    330332                // check the long form
    331333                $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
     
    359361        // 'tag/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]',
    360362        // 'tag/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]',
    361363        function test_tag_feed() {
     364                $this->setExpectedIncorrectUsage( 'mysql2date' );
    362365                $this->factory->term->create( array( 'name' => 'tag-a', 'taxonomy' => 'post_tag' ) );
    363366                // check the long form
    364367                $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
     
    405408        // 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
    406409        // 'author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
    407410        function test_author_feed() {
     411                $this->setExpectedIncorrectUsage( 'mysql2date' );
    408412                $this->factory->user->create( array( 'user_login' => 'user-a' ) );
    409413                // check the long form
    410414                $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  • tests/phpunit/tests/xmlrpc/wp/newPost.php

     
    299299         * @ticket 28601
    300300         */
    301301        function test_invalid_post_date_does_not_fatal() {
     302                $this->setExpectedIncorrectUsage( 'mysql2date' );
    302303                $this->make_user_by_role( 'author' );
    303304                $date_string = 'invalid_date';
    304305                $post = array( 'post_title' => 'test', 'post_content' => 'test', 'post_date' => $date_string );
     
    312313         * @ticket 28601
    313314         */
    314315        function test_invalid_post_date_gmt_does_not_fatal() {
     316                $this->setExpectedIncorrectUsage( 'mysql2date' );
    315317                $this->make_user_by_role( 'author' );
    316318                $date_string = 'invalid date';
    317319                $post = array( 'post_title' => 'test', 'post_content' => 'test', 'post_date_gmt' => $date_string );