Ticket #28992: 28992.patch
File 28992.patch, 9.5 KB (added by , 10 years ago) |
---|
-
src/wp-includes/functions.php
21 21 * @param string $format Format of the date to return. 22 22 * @param string $date Date string to convert. 23 23 * @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. 25 25 */ 26 26 function 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' ); 28 30 return false; 31 } 29 32 30 if ( 'G' == $format )33 if ( 'G' === $format ) { 31 34 return strtotime( $date . ' +0000' ); 35 } 32 36 33 37 $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 ) { 36 42 return $i; 43 } 37 44 38 if ( $translate ) 45 if ( $translate ) { 39 46 return date_i18n( $format, $i ); 40 else47 } else { 41 48 return date( $format, $i ); 49 } 42 50 } 43 51 44 52 /** -
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 26 class 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
931 931 $this->assertFalse( get_post_modified_time( 'h:i:s', false, 9 ) ); 932 932 } 933 933 934 /**935 * @ticket 28310936 */937 function test_mysql2date_returns_false_with_no_date() {938 $this->assertFalse( mysql2date( 'F j, Y H:i:s', '' ) );939 }940 934 941 /**942 * @ticket 28310943 */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 }948 935 949 936 /** 950 937 * @ticket 25566 -
tests/phpunit/tests/query/conditionals.php
287 287 // 'search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]', 288 288 // 'search/(.+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]', 289 289 function test_search_feed() { 290 $this->setExpectedIncorrectUsage( 'mysql2date' ); 290 291 // check the long form 291 292 $types = array('feed', 'rdf', 'rss', 'rss2', 'atom'); 292 293 foreach ($types as $type) { … … 326 327 // 'category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]', 327 328 // 'category/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]', 328 329 function test_category_feed() { 330 $this->setExpectedIncorrectUsage( 'mysql2date' ); 329 331 $this->factory->term->create( array( 'name' => 'cat-a', 'taxonomy' => 'category' ) ); 330 332 // check the long form 331 333 $types = array('feed', 'rdf', 'rss', 'rss2', 'atom'); … … 359 361 // 'tag/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]', 360 362 // 'tag/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]', 361 363 function test_tag_feed() { 364 $this->setExpectedIncorrectUsage( 'mysql2date' ); 362 365 $this->factory->term->create( array( 'name' => 'tag-a', 'taxonomy' => 'post_tag' ) ); 363 366 // check the long form 364 367 $types = array('feed', 'rdf', 'rss', 'rss2', 'atom'); … … 405 408 // 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]', 406 409 // 'author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]', 407 410 function test_author_feed() { 411 $this->setExpectedIncorrectUsage( 'mysql2date' ); 408 412 $this->factory->user->create( array( 'user_login' => 'user-a' ) ); 409 413 // check the long form 410 414 $types = array('feed', 'rdf', 'rss', 'rss2', 'atom'); -
tests/phpunit/tests/xmlrpc/wp/newPost.php
299 299 * @ticket 28601 300 300 */ 301 301 function test_invalid_post_date_does_not_fatal() { 302 $this->setExpectedIncorrectUsage( 'mysql2date' ); 302 303 $this->make_user_by_role( 'author' ); 303 304 $date_string = 'invalid_date'; 304 305 $post = array( 'post_title' => 'test', 'post_content' => 'test', 'post_date' => $date_string ); … … 312 313 * @ticket 28601 313 314 */ 314 315 function test_invalid_post_date_gmt_does_not_fatal() { 316 $this->setExpectedIncorrectUsage( 'mysql2date' ); 315 317 $this->make_user_by_role( 'author' ); 316 318 $date_string = 'invalid date'; 317 319 $post = array( 'post_title' => 'test', 'post_content' => 'test', 'post_date_gmt' => $date_string );