sitemaps-index.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @group sitemaps
  4. */
  5. class Test_WP_Sitemaps_Index extends WP_UnitTestCase {
  6. public function test_get_sitemap_list() {
  7. $registry = new WP_Sitemaps_Registry();
  8. /*
  9. * The test provider has 3 subtypes.
  10. * Each subtype has 4 pages with results.
  11. * There are 2 providers registered.
  12. * Hence, 3*4*2=24.
  13. */
  14. $registry->add_provider( 'foo', new WP_Sitemaps_Test_Provider( 'foo' ) );
  15. $registry->add_provider( 'bar', new WP_Sitemaps_Test_Provider( 'bar' ) );
  16. $sitemap_index = new WP_Sitemaps_Index( $registry );
  17. $this->assertCount( 24, $sitemap_index->get_sitemap_list() );
  18. }
  19. /**
  20. * Test that a sitemap index won't contain more than 50000 sitemaps.
  21. *
  22. * @ticket 50666
  23. */
  24. public function test_get_sitemap_list_limit() {
  25. $registry = new WP_Sitemaps_Registry();
  26. // add 3 providers, which combined produce more than the maximum 50000 sitemaps in the index.
  27. $registry->add_provider( 'provider_1', new WP_Sitemaps_Large_Test_Provider( 25000 ) );
  28. $registry->add_provider( 'provider_2', new WP_Sitemaps_Large_Test_Provider( 25000 ) );
  29. $registry->add_provider( 'provider_3', new WP_Sitemaps_Large_Test_Provider( 25000 ) );
  30. $count = 0;
  31. foreach ( $registry->get_providers() as $provider ) {
  32. $count += count( $provider->get_url_list( 1 ) );
  33. }
  34. $this->assertGreaterThan( 50000, $count );
  35. $sitemap_index = new WP_Sitemaps_Index( $registry );
  36. $this->assertCount( 50000, $sitemap_index->get_sitemap_list() );
  37. }
  38. public function test_get_sitemap_list_no_entries() {
  39. $registry = new WP_Sitemaps_Registry();
  40. $registry->add_provider( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) );
  41. $sitemap_index = new WP_Sitemaps_Index( $registry );
  42. $this->assertCount( 0, $sitemap_index->get_sitemap_list() );
  43. }
  44. public function test_get_index_url() {
  45. $sitemap_index = new WP_Sitemaps_Index( new WP_Sitemaps_Registry() );
  46. $index_url = $sitemap_index->get_index_url();
  47. $this->assertStringEndsWith( '/?sitemap=index', $index_url );
  48. }
  49. public function test_get_index_url_pretty_permalinks() {
  50. // Set permalinks for testing.
  51. $this->set_permalink_structure( '/%year%/%postname%/' );
  52. $sitemap_index = new WP_Sitemaps_Index( new WP_Sitemaps_Registry() );
  53. $index_url = $sitemap_index->get_index_url();
  54. // Clean up permalinks.
  55. $this->set_permalink_structure();
  56. $this->assertStringEndsWith( '/wp-sitemap.xml', $index_url );
  57. }
  58. }