Make WordPress Core

Ticket #26798: 26798.3.diff

File 26798.3.diff, 2.8 KB (added by johnregan3, 15 months ago)

Refresh of the patch and Unit Tests

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 4ceb8dd3a6..b94b692b71 100644
    function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) { 
    49124912                }
    49134913        }
    49144914
    4915         // Validate the date.
    4916         $month = substr( $post_date, 5, 2 );
    4917         $day   = substr( $post_date, 8, 2 );
    4918         $year  = substr( $post_date, 0, 4 );
     4915        // Ensure we have a valid mysql date-formatted string (YYYY-MM-DD H:i:s).
     4916        preg_match( "/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])[\s](?:[0-1][0-9]|2[1-3]):[0-5][0-9]:[0-5][0-9]$/", $post_date, $matches );
     4917        if ( empty( $matches ) || ! is_array( $matches ) || count( $matches ) < 4 ) {
     4918                return false;
     4919        }
    49194920
    4920         $valid_date = wp_checkdate( $month, $day, $year, $post_date );
     4921        $valid_date = wp_checkdate( $matches[2], $matches[3], $matches[1], $post_date );
    49214922
    49224923        if ( ! $valid_date ) {
    49234924                return false;
    49244925        }
     4926
    49254927        return $post_date;
    49264928}
    49274929
  • tests/phpunit/tests/post.php

    diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
    index 108e6ef4a6..82a62cb3a4 100644
    class Tests_Post extends WP_UnitTestCase { 
    12201220                $this->assertSame( $post['post_date_gmt'], $out->post_date_gmt );
    12211221        }
    12221222
     1223        /**
     1224         * @ticket 26798
     1225         *
     1226         * On a deeper level, this is handled by test_wp_resolve_post_date.
     1227         */
     1228        public function test_wp_insert_post_reject_malformed_post_date() {
     1229                $post = array(
     1230                        'post_author'   => self::$editor_id,
     1231                        'post_status'   => 'publish',
     1232                        'post_content'  => 'content',
     1233                        'post_title'    => 'title',
     1234                        'post_date'     => '2012-01-8 12:00:00',
     1235                );
     1236
     1237                // Inserting the post should fail gracefully.
     1238                $id = wp_insert_post( $post );
     1239                $this->assertSame( 0, $id );
     1240        }
     1241
    12231242        public function test_wp_delete_post_reassign_hierarchical_post_type() {
    12241243                $grandparent_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
    12251244                $parent_page_id      = self::factory()->post->create(
    class Tests_Post extends WP_UnitTestCase { 
    16391658                $this->assertFalse( $resolved_post_date );
    16401659        }
    16411660
     1661        /**
     1662         * @ticket 26798
     1663         *
     1664         * Tests the regex inside of wp_resolve_post_date().
     1665         */
     1666        public function test_wp_resolve_post_date_regex() {
     1667                $invalid_dates = array(
     1668                        '2012-01-08',
     1669                        '201-01-08 00:00:00',
     1670                        '201a-01-08 00:00:00',
     1671                        '2012-1-08 00:00:00',
     1672                        '2012-31-08 00:00:00',
     1673                        '2012-01-8 00:00:00',
     1674                        '2012-01-48 00:00:00',
     1675                        '2012-01-08 0:00:00',
     1676                        '2012-01-08 24:00:00',
     1677                        '2012-01-08 00:0:00',
     1678                        '2012-01-08 00:60:00',
     1679                        '2012-01-08 00:00:0',
     1680                        '2012-01-08 00:00:60',
     1681                );
     1682                foreach( $invalid_dates as $date ) {
     1683                        $out = wp_resolve_post_date( $date );
     1684                        $this->assertFalse( $out );
     1685                }
     1686        }
     1687
    16421688        /**
    16431689         * Ensure sticking a post updates the `sticky_posts` option.
    16441690         *