WordPress.org

Make WordPress Core

Changeset 42597


Ignore:
Timestamp:
01/24/2018 09:43:17 PM (3 years ago)
Author:
jorbin
Message:

Query: Fix warning on counting non countable

Merges [42581], [42585], and [42594] to the 4.9 branch.

Adds tests to continue the behavior for both null and strings. Skip the tests on PHP 5.2 as they require ReflectionMethod.

See https://wiki.php.net/rfc/counting_non_countables for information on the PHP change.

Fixes #42860.
Props dd32 for test skipping and janak007 and ayeshrajans for initial patches.

Location:
branches/4.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r41688 r42597  
    30333033            $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
    30343034        } else {
    3035             $this->found_posts = count( $this->posts );
     3035            if ( is_array( $this->posts ) ) {
     3036                $this->found_posts = count( $this->posts );
     3037            } else {
     3038                if ( null === $this->posts ) { 
     3039                    $this->found_posts = 0;
     3040                } else {
     3041                    $this->found_posts = 1;
     3042                }
     3043            }
    30363044        }
    30373045
  • branches/4.9/tests/phpunit/tests/post/query.php

    r40278 r42597  
    536536        $this->assertEquals( 2, $q->max_num_pages );
    537537    }
     538
     539    public function set_found_posts_provider() {
     540        // count return 0 for null, but 1 for other data you may not expect
     541        return array(
     542            array( null, 0 ),
     543            array( '', 1 ),
     544            array( "To life, to life, l'chaim", 1 ),
     545            array( false, 1 ),
     546        );
     547    }
     548
     549    /**
     550     * @ticket 42860
     551     *
     552     * @dataProvider set_found_posts_provider
     553     */
     554    public function test_set_found_posts_not_posts_as_an_array( $posts, $expected ) {
     555        if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
     556            $this->markTestSkipped( 'ReflectionMethod::setAccessible is only available in PHP 5.3+' );
     557            return;
     558        }
     559
     560        $q = new WP_Query(
     561            array(
     562                'post_type'      => 'wptests_pt',
     563                'posts_per_page' => 1,
     564            )
     565        );
     566
     567        $q->posts = $posts;
     568
     569        $methd = new \ReflectionMethod( 'WP_Query', 'set_found_posts' );
     570        $methd->setAccessible( true );
     571        $methd->invoke( $q, array( 'no_found_rows' => false ), array() );
     572
     573        $this->assertEquals( $expected, $q->found_posts );
     574    }
     575
    538576}
Note: See TracChangeset for help on using the changeset viewer.