WordPress.org

Make WordPress Core

Changeset 30511


Ignore:
Timestamp:
11/22/2014 02:17:21 PM (6 years ago)
Author:
boonebgorges
Message:

Improve performance of post revision order test.

test_revision_order() was written ([28541], #26042) to ensure that revision
order was properly preserved in two different cases: (1) where the post_date
varied (in which case the revisions would be sorted by post_date DESC) and
(2) where the post_date was the same (in which case sorting would fall back on
ID DESC). In an attempt to ensure that both of these scenarios arose in the
context of a single test, 100 posts were created. We can make the process far
more efficient by manually creating the revisions with the post_dates
explicitly declared, and splitting the two different cases into two separate
test methods.

This test was previously the single worst offender in the entire suite, taking
upwards of 15 seconds to run. All that most maddens and torments; all that stirs
up the lees of things; all truth with malice in it; all that cracks the sinews
and cakes the brain; all the subtle demonisms of life and thought; all evil, to
crazy Boone, were visibly personified, and made practically assailable in
test_revision_order().

See #30017.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/revisions.php

    r28962 r30511  
    343343     * @ticket 26042
    344344     */
    345     function test_revision_order() {
    346         $ok = 0;
    347         $reversed = 0;
    348 
    349         for ( $i = 0; $i < 100; $i++ ) {
    350             $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
    351 
    352             for ( $j = 1; $j < 3; $j++ ) {
    353                 wp_update_post( array( 'post_content' => 'updated post' . $j , 'ID' => $post_id ) );
    354             }
    355 
    356             $revisions = wp_get_post_revisions( $post_id );
    357             $first = array_shift( $revisions );
    358             $last = array_pop( $revisions );
    359 
    360             if ( $first->ID < $last->ID ) {
    361                 $reversed++;
    362             } else {
    363                 $ok++;
    364             }
    365         }
    366 
    367         $this->assertEquals( 100, $ok );
    368         $this->assertEquals( 0, $reversed );
     345    function test_wp_get_post_revisions_should_order_by_post_date() {
     346        global $wpdb;
     347
     348        $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
     349
     350        $post = (array) $post;
     351        $post_revision_fields = _wp_post_revision_fields( $post );
     352        $post_revision_fields = wp_slash( $post_revision_fields );
     353
     354        $revision_ids = array();
     355        $now = time();
     356        for ( $j = 1; $j < 3; $j++ ) {
     357            // Manually modify dates to ensure they're different.
     358            $date = date( 'Y-m-d H:i:s', $now - ( $j * 10 ) );
     359            $post_revision_fields['post_date'] = $date;
     360            $post_revision_fields['post_date_gmt'] = $date;
     361
     362            $revision_id = wp_insert_post( $post_revision_fields );
     363
     364            $revision_ids[] = $revision_id;
     365        }
     366
     367        $revisions = wp_get_post_revisions( $post['ID'] );
     368
     369        $this->assertEquals( $revision_ids, array_values( wp_list_pluck( $revisions, 'ID' ) ) );
     370    }
     371
     372    /**
     373     * @ticket 26042
     374     */
     375    function test_wp_get_post_revisions_should_order_by_ID_when_post_date_matches() {
     376        global $wpdb;
     377
     378        $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
     379
     380        $post = (array) $post;
     381        $post_revision_fields = _wp_post_revision_fields( $post );
     382        $post_revision_fields = wp_slash( $post_revision_fields );
     383
     384        $revision_ids = array();
     385        $date = date( 'Y-m-d H:i:s', time() - 10 );
     386        for ( $j = 1; $j < 3; $j++ ) {
     387            // Manually modify dates to ensure they're the same.
     388            $post_revision_fields['post_date'] = $date;
     389            $post_revision_fields['post_date_gmt'] = $date;
     390
     391            $revision_id = wp_insert_post( $post_revision_fields );
     392
     393            $revision_ids[] = $revision_id;
     394        }
     395
     396        rsort( $revision_ids );
     397
     398        $revisions = wp_get_post_revisions( $post['ID'] );
     399
     400        $this->assertEquals( $revision_ids, array_values( wp_list_pluck( $revisions, 'ID' ) ) );
    369401    }
    370402}
Note: See TracChangeset for help on using the changeset viewer.