Make WordPress Core


Ignore:
Timestamp:
07/22/2022 01:22:04 PM (21 months ago)
Author:
spacedmonkey
Message:

REST API: Use wp_get_lastest_revision_id_and_total_count function in WP_REST_Posts_Controller class.

Add new function called wp_get_lastest_revision_id_and_total_count, that performs an optimized query to get the last revision and total and use it in WP_REST_Posts_Controller class.

Props Spacedmonkey, timothyblynjacobs, furi3r, peterwilsoncc.
Fixes #55857.

File:
1 edited

Legend:

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

    r52389 r53759  
    657657
    658658    /**
     659     * Tests that wp_get_lastest_revision_id_and_total_count() returns last revision id and total count.
     660     *
     661     * @ticket 55857
     662     * @dataProvider data_wp_get_post_revisions_url
     663     */
     664    public function test_wp_get_last_revision_id_and_total_count( $revisions ) {
     665        $post_id = self::factory()->post->create();
     666        for ( $i = 0; $i < $revisions; ++$i ) {
     667            wp_update_post(
     668                array(
     669                    'ID'         => $post_id,
     670                    'post_title' => 'Some Post',
     671                )
     672            );
     673        }
     674
     675        $post_revisions     = wp_get_post_revisions( $post_id );
     676        $last_post_revision = current( $post_revisions );
     677        $revision           = wp_get_lastest_revision_id_and_total_count( $post_id );
     678
     679        $this->assertSame(
     680            $last_post_revision->ID,
     681            $revision['revision'],
     682            'Failed asserting latest revision id.'
     683        );
     684
     685        $this->assertSame(
     686            count( $post_revisions ),
     687            $revision['count'],
     688            'Failed asserting total count of revision.'
     689        );
     690    }
     691
     692    /**
     693     * Tests that wp_get_lastest_revision_id_and_total_count() when no revisions.
     694     *
     695     * @ticket 55857
     696     */
     697    public function test_wp_get_last_revision_id_and_total_count_no_revisions() {
     698        $revision = wp_get_lastest_revision_id_and_total_count( null );
     699        $this->assertWPError( $revision, 'Invalid Post, non existing revisions.' );
     700        $this->assertSame( $revision->get_error_message(), 'Invalid post.' );
     701
     702        add_filter( 'wp_revisions_to_keep', '__return_zero' );
     703        $post_id  = self::factory()->post->create();
     704        $revision = wp_get_lastest_revision_id_and_total_count( $post_id );
     705        $this->assertWPError( $revision, 'Revisions should be not enabled.' );
     706        $this->assertSame( $revision->get_error_message(), 'Revisions not enabled.' );
     707    }
     708
     709    /**
    659710     * Tests that wp_get_post_revisions_url() returns the revisions URL.
    660711     *
Note: See TracChangeset for help on using the changeset viewer.