Make WordPress Core

Ticket #57416: 57416.2.diff

File 57416.2.diff, 2.1 KB (added by darthhexx, 20 months ago)

Add a condition to split_the_query for whether the call is for a single post and add unit tests.

  • src/wp-includes/class-wp-query.php

    diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php
    index a9ed269d72..8ceefc1baa 100644
    a b class WP_Query { 
    32833283
    32843284                if ( null === $this->posts ) {
    32853285                        $split_the_query = (
     3286                                $q['posts_per_page'] != 1
     3287                                &&
    32863288                                $is_unfiltered_query
    32873289                                && (
    32883290                                        wp_using_ext_object_cache()
  • tests/phpunit/tests/post/getPosts.php

    diff --git a/tests/phpunit/tests/post/getPosts.php b/tests/phpunit/tests/post/getPosts.php
    index dac071f1af..27f1930885 100644
    a b class Tests_Post_GetPosts extends WP_UnitTestCase { 
    163163
    164164                $this->assertSame( array( $p3 ), $found );
    165165        }
     166
     167        public function test_one_post_per_page_no_split_query() {
     168                $p1 = self::factory()->post->create(
     169                        array(
     170                                'post_date' => '2015-04-04 04:04:04',
     171                        )
     172                );
     173                $p2 = self::factory()->post->create(
     174                        array(
     175                                'post_date' => '2014-04-04 04:04:04',
     176                        )
     177                );
     178
     179                add_filter( 'split_the_query', array( $this, 'filter_split_the_query_is_false' ) );
     180
     181                $found = get_posts(
     182                        array(
     183                                'posts_per_page' => 1,
     184                        )
     185                );
     186
     187                remove_filter( 'split_the_query', array( $this, 'filter_split_the_query_is_false' ) );
     188        }
     189
     190        public function test_two_posts_per_page_split_query() {
     191                $p1 = self::factory()->post->create(
     192                        array(
     193                                'post_date' => '2015-04-04 04:04:04',
     194                        )
     195                );
     196                $p2 = self::factory()->post->create(
     197                        array(
     198                                'post_date' => '2014-04-04 04:04:04',
     199                        )
     200                );
     201
     202                add_filter( 'split_the_query', array( $this, 'filter_split_the_query_is_true' ) );
     203
     204                $found = get_posts(
     205                        array(
     206                                'posts_per_page' => 2,
     207                        )
     208                );
     209
     210                remove_filter( 'split_the_query', array( $this, 'filter_split_the_query_is_true' ) );
     211        }
     212
     213        public function filter_split_the_query_is_false( $split_the_query_default ) {
     214                $this->assertEquals( $split_the_query_default, false );
     215        }
     216
     217        public function filter_split_the_query_is_true( $split_the_query_default ) {
     218                $this->assertEquals( $split_the_query_default, true );
     219        }
    166220}