Make WordPress Core

Ticket #21790: 21790.diff

File 21790.diff, 1.7 KB (added by collinsinternet, 10 years ago)

Unit test that catches PHP notice on pre_get_post when static front page is set.

  • tests/phpunit/tests/query.php

     
    22
    33class Tests_Query extends WP_UnitTestCase {
    44
     5        /**
     6         *
     7         * @var bool
     8         */
     9        var $test_error = false;
     10
    511        function setUp() {
    612                global $wp_rewrite;
    713                parent::setUp();
     
    133139
    134140                $this->assertContains( "ORDER BY $wpdb->posts.post_title DESC, $wpdb->posts.post_date DESC", $q->request );
    135141        }
     142       
     143        /**
     144         * Tests to see if the pre_get_posts filter causes PHP notices when run on a static front page.
     145         * @ticket 21790
     146         */
     147        function test_static_front_page_pre_get_posts() {
     148                add_filter( 'pre_get_posts', array( $this, 'filter_static_front_page_pre_get_posts' ) );
     149                $page_one        = $this->factory->post->create( array( 'post_type' => 'page' ) );
     150                $page_two        = $this->factory->post->create( array( 'post_type' => 'page' ) );
     151                update_option( 'show_on_front', 'page' );
     152                update_option( 'page_on_front', $page_one );
     153                $this->go_to( home_url() );
     154                $this->assertFalse( $this->test_error, 'PHP Notices on front page.' );
     155
     156                // Run same test on page that is not set as front page.
     157                $this->test_error = false;
     158                $this->go_to( get_permalink( $page_two ) );
     159                $this->assertFalse( $this->test_error, 'PHP Notices on another page.' );
    136160}
     161
     162        /**
     163         * Callback for pre_get_posts filter that tries to catch PHP notice.
     164         * @param WP_Query $query
     165         * @return WP_Query
     166         */
     167        function filter_static_front_page_pre_get_posts( $query ) {
     168                try {
     169                        if ( $query->is_main_query() && is_front_page() ) {
     170                               
     171                        }
     172                } catch ( Exception $e ) {
     173                        $this->test_error = true;
     174                }
     175
     176                return $query;
     177        }
     178
     179}