WordPress.org

Make WordPress Core

Ticket #24674: 24674.2.diff

File 24674.2.diff, 2.1 KB (added by boonebgorges, 5 years ago)
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 27d8c26..c705622 100644
    class WP_Query { 
    43704370
    43714371                $page = (array) $page;
    43724372
    4373                 if ( in_array( $page_obj->ID, $page ) ) {
     4373                if ( in_array( (string) $page_obj->ID, $page ) ) {
    43744374                        return true;
    43754375                } elseif ( in_array( $page_obj->post_title, $page ) ) {
    43764376                        return true;
  • tests/phpunit/tests/query/conditionals.php

    diff --git tests/phpunit/tests/query/conditionals.php tests/phpunit/tests/query/conditionals.php
    index 70d5a7d..e6ebab9 100644
    class Tests_Query_Conditionals extends WP_UnitTestCase { 
    809809                $this->assertTrue( is_attachment( $post->post_title ) );
    810810                $this->assertTrue( is_attachment( $post->post_name ) );
    811811        }
     812
     813        /**
     814         * @ticket 24674
     815         */
     816        public function test_is_page_with_page_id_zero_and_random_page_slug() {
     817                $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     818                $this->go_to( "/?page_id=$post_id" );
     819
     820                // override post ID to 0 temporarily for testing
     821                $_id = $GLOBALS['wp_query']->post->ID;
     822                $GLOBALS['wp_query']->post->ID = 0;
     823
     824                $post = get_queried_object();
     825                $q = $GLOBALS['wp_query'];
     826
     827                $this->assertTrue( $q->is_page() );
     828                $this->assertFalse( $q->is_page( 'sample-page' ) );
     829                $this->assertFalse( $q->is_page( 'random-page-slug' ) );
     830
     831                // revert $wp_query global change
     832                $GLOBALS['wp_query']->post->ID = $_id;
     833        }
     834
     835        /**
     836         * @ticket 24674
     837         */
     838        public function test_is_page_with_page_slug_that_begins_with_a_number_that_clashes_with_a_page_ID() {
     839                $p1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
     840
     841                $p2_name = $p1 . '-page';
     842                $p2 = $this->factory->post->create( array(
     843                        'post_type' => 'page',
     844                        'post_name' => $p2_name,
     845                ) );
     846
     847                $this->go_to( "/?page_id=$p1" );
     848
     849                $q = $GLOBALS['wp_query'];
     850
     851                $this->assertTrue( $q->is_page() );
     852                $this->assertTrue( $q->is_page( $p1 ) );
     853                $this->assertFalse( $q->is_page( $p2_name ) );
     854                $this->assertFalse( $q->is_page( $p2 ) );
     855        }
    812856}