WordPress.org

Make WordPress Core

Ticket #30210: 30210.7.patch

File 30210.7.patch, 9.6 KB (added by stevenkword, 4 years ago)

Updates tests to include coverage for valid feeds

  • src/wp-includes/functions.php

     
    12211221function do_feed() {
    12221222        global $wp_query;
    12231223
     1224        // Determine if we are looking at the main comment feed
     1225        $is_main_comments_feed = ( $wp_query->is_comment_feed() && ! $wp_query->is_singular() ) ? true : false;
     1226
     1227        /*
     1228         * Check the queried object for the existence of posts if it is not a feed for an archive,
     1229         * search result, or main comments. By checking for the absense of posts we can prevent rendering the feed
     1230         * templates at invalid endpoints. e.g.) /wp-content/plugins/feed/
     1231         *
     1232         */
     1233        if ( ! $wp_query->have_posts() && ! ( $wp_query->is_archive() || $wp_query->is_post_type_archive() || $wp_query->is_search() || $is_main_comments_feed ) ) {
     1234                wp_die( __( 'ERROR: This is not a valid feed.' ), '', array( 'response' => 404 ) );
     1235        }
     1236
    12241237        $feed = get_query_var( 'feed' );
    12251238
    12261239        // Remove the pad, if present.
  • tests/phpunit/tests/feed/rss2.php

     
    1212        static $user_id;
    1313        static $posts;
    1414        static $category;
     15        static $post_date;
    1516
    1617        /**
    1718         * Setup a new user and attribute some posts.
     
    2526                ) );
    2627
    2728                // Create a taxonomy
    28                 self::$category = self::factory()->category->create_and_get( array(
    29                         'name' => 'Test Category',
    30                         'slug' => 'test-cat',
     29                self::$category = $factory->category->create_and_get( array(
     30                        'name' => 'Foo Category',
     31                        'slug' => 'foo',
    3132                ) );
    3233
     34                // Set a predictable time for testing date archives.
     35                self::$post_date = '2003-05-27 10:07:53';
     36
    3337                // Create a few posts
    3438                self::$posts = $factory->post->create_many( 42, array(
    3539                        'post_author'  => self::$user_id,
     40                        'post_date'    => self::$post_date,
    3641                        'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec velit massa, ultrices eu est suscipit, mattis posuere est. Donec vitae purus lacus. Cras vitae odio odio.',
    3742                        'post_excerpt' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
    3843                ) );
     
    5358                $this->excerpt_only = get_option( 'rss_use_excerpt' );
    5459                // this seems to break something
    5560                update_option( 'use_smilies', false );
     61
     62                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     63                create_initial_taxonomies();
    5664        }
    5765
    5866        /**
     
    248256                remove_filter( 'comments_open', '__return_false' );
    249257        }
    250258
     259        /*
     260         * Check to make sure we are rendering feed templates for the home feed.
     261         * e.g.) https://example.com/feed/
     262         *
     263         * @ticket 30210
     264         */
     265        function test_valid_home_feed_endpoint() {
     266                // An example of a valid home feed endpoint.
     267                $this->go_to( 'feed/' );
     268
     269                // This must be globalized after calling `WP_UnitTestCase::go_to` which references $GLOBALS['wp_query'] and not $wp_query.
     270                global $wp_query;
     271
     272                // Verify the query object is a feed.
     273                $this->assertQueryTrue( 'is_feed' );
     274
     275                // Queries performed on valid feed endpoints should contain posts.
     276                $this->assertTrue( $wp_query->have_posts() );
     277
     278                // Check to see if we have the expected XML output from the feed template.
     279                $feed = $this->do_rss2();
     280
     281                $xml = xml_to_array( $feed );
     282
     283                // Get the <rss> child element of <xml>.
     284                $rss = xml_find( $xml, 'rss' );
     285
     286                // There should only be one <rss> child element.
     287                $this->assertEquals( 1, count( $rss ) );
     288        }
     289
     290        /*
     291         * Check to make sure we are rendering feed templates for the taxonomy feeds.
     292         * e.g.) https://example.com/category/foo/feed/
     293         *
     294         * @ticket 30210
     295         */
     296        function test_valid_taxonomy_feed_endpoint() {
     297                // An example of an valid taxonomy feed endpoint.
     298                $this->go_to( 'category/foo/feed/' );
     299
     300                // This must be globalized after calling `WP_UnitTestCase::go_to` which references $GLOBALS['wp_query'] and not $wp_query.
     301                global $wp_query;
     302
     303                // Verify the query object is a feed.
     304                $this->assertQueryTrue( 'is_feed', 'is_archive', 'is_category' );
     305
     306                // Queries performed on valid feed endpoints should contain posts.
     307                $this->assertTrue( $wp_query->have_posts() );
     308
     309                // Check to see if we have the expected XML output from the feed template.
     310                $feed = $this->do_rss2();
     311
     312                // Check to see if we have the expected XML output from the feed template.
     313                $xml = xml_to_array( $feed );
     314
     315                // Get the <rss> child element of <xml>.
     316                $rss = xml_find( $xml, 'rss' );
     317
     318                // There should only be one <rss> child element.
     319                $this->assertEquals( 1, count( $rss ) );
     320        }
     321
     322        /*
     323         * Check to make sure we are rendering feed templates for the main comment feed.
     324         * e.g.) https://example.com/comments/feed/
     325         *
     326         * @ticket 30210
     327         */
     328        function test_valid_main_comment_feed_endpoint() {
     329                // Assign a category to those posts
     330                foreach ( self::$posts as $post ) {
     331                        self::factory()->comment->create_post_comments( $post, 3 );
     332                }
     333
     334                // An example of an valid main comment feed endpoint.
     335                $this->go_to( 'comments/feed/' );
     336
     337                // This must be globalized after calling `WP_UnitTestCase::go_to` which references $GLOBALS['wp_query'] and not $wp_query.
     338                global $wp_query;
     339
     340                // Verify the query object is a feed.
     341                $this->assertQueryTrue( 'is_feed', 'is_comment_feed' );
     342
     343                // Queries performed on valid feed endpoints should contain comments.
     344                $this->assertTrue( $wp_query->have_comments() );
     345
     346                // Check to see if we have the expected XML output from the feed template.
     347                $feed = $this->do_rss2();
     348
     349                // Check to see if we have the expected XML output from the feed template.
     350                $xml = xml_to_array( $feed );
     351
     352                // Get the <rss> child element of <xml>.
     353                $rss = xml_find( $xml, 'rss' );
     354
     355                // There should only be one <rss> child element.
     356                $this->assertEquals( 1, count( $rss ) );
     357        }
     358
     359        /*
     360         * Check to make sure we are rendering feed templates for the date archive feeds.
     361         * e.g.) https://example.com/2003/05/27/feed/
     362         *
     363         * @ticket 30210
     364         */
     365        function test_valid_archive_feed_endpoint() {
     366                // An example of an valid date archive feed endpoint.
     367                $this->go_to( '2003/05/27/feed/' );
     368
     369                // This must be globalized after calling `WP_UnitTestCase::go_to` which references $GLOBALS['wp_query'] and not $wp_query.
     370                global $wp_query;
     371
     372                // Verify the query object is a feed.
     373                $this->assertQueryTrue( 'is_feed', 'is_archive', 'is_day', 'is_date' );
     374
     375                // Queries performed on valid feed endpoints should contain posts.
     376                $this->assertTrue( $wp_query->have_posts() );
     377
     378                // Check to see if we have the expected XML output from the feed template.
     379                $feed = $this->do_rss2();
     380
     381                // Check to see if we have the expected XML output from the feed template.
     382                $xml = xml_to_array( $feed );
     383
     384                // Get the <rss> child element of <xml>.
     385                $rss = xml_find( $xml, 'rss' );
     386
     387                // There should only be one <rss> child element.
     388                $this->assertEquals( 1, count( $rss ) );
     389        }
     390
     391
     392        /*
     393         * Check to make sure we are rendering feed templates for the search archive feeds.
     394         * e.g.) https://example.com/?s=Lorem&feed=rss
     395         *
     396         * @ticket 30210
     397         */
     398        function test_valid_search_feed_endpoint() {
     399                // An example of an valid search feed endpoint
     400                $this->go_to( '?s=Lorem&feed=rss' );
     401
     402                // This must be globalized after calling `WP_UnitTestCase::go_to` which references $GLOBALS['wp_query'] and not $wp_query.
     403                global $wp_query;
     404
     405                // Verify the query object is a feed.
     406                $this->assertQueryTrue( 'is_feed', 'is_search' );
     407
     408                // Queries performed on valid feed endpoints should contain posts.
     409                $this->assertTrue( $wp_query->have_posts() );
     410
     411                // Check to see if we have the expected XML output from the feed template.
     412                $feed = $this->do_rss2();
     413
     414                // Check to see if we have the expected XML output from the feed template.
     415                $xml = xml_to_array( $feed );
     416
     417                // Get the <rss> child element of <xml>.
     418                $rss = xml_find( $xml, 'rss' );
     419
     420                // There should only be one <rss> child element.
     421                $this->assertEquals( 1, count( $rss ) );
     422        }
     423
     424        /*
     425         * Check to make sure we are not rendering feed templates for invalid feed endpoints.
     426         * e.g.) https://example.com/wp-content/feed/
     427         *
     428         * @ticket 30210
     429         */
     430        function test_invalid_feed_endpoint() {
     431                // An example of an invalid feed endpoint
     432                $this->go_to( 'wp-content/feed/' );
     433
     434                // This must be globalized after calling `WP_UnitTestCase::go_to` which references $GLOBALS['wp_query'] and not $wp_query.
     435                global $wp_query;
     436
     437                // Queries performed on invalid feed endpoints should never contain posts.
     438                $this->assertFalse( $wp_query->have_posts() );
     439
     440                // This is the assertion.  Once the exception is thrown in do_feed, execution stops, preventing futher assertions.
     441                $this->setExpectedException( 'WPDieException', 'ERROR: This is not a valid feed.' );
     442                do_feed();
     443        }
     444
     445        /*
     446         * Make sure the requested feed is registered before rendering the requested template.
     447         *
     448         * @ticket 30210
     449         */
     450        function test_nonexistent_feeds() {
     451                global $wp_rewrite;
     452                $feedname = 'badfeed';
     453
     454                // Unregister the feed if it exists
     455                if ( in_array( $feedname, $wp_rewrite->feeds ) ) {
     456                        unset( $wp_rewrite->feeds->feedname );
     457                        $hook = 'do_feed_' . $feedname;
     458                        // Remove default function hook
     459                        remove_action( $hook, $hook );
     460                }
     461
     462                $this->go_to( '/?feed=' . $feedname );
     463
     464                // This must be globalized after calling `WP_UnitTestCase::go_to` which references $GLOBALS['wp_query'] and not $wp_query.
     465                global $wp_query;
     466
     467                // This is the assertion.  Once the exception is thrown in do_feed, execution stops, preventing futher assertions.
     468                $this->setExpectedException( 'WPDieException', 'ERROR: This is not a valid feed template.' );
     469                do_feed();
     470        }
     471
    251472}