| 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 | |