sitemaps.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @group canonical
  4. * @group rewrite
  5. * @group query
  6. * @group sitemaps
  7. */
  8. class Tests_Canonical_Sitemaps extends WP_Canonical_UnitTestCase {
  9. public function setUp() {
  10. parent::setUp();
  11. $wp_sitemaps = new WP_Sitemaps();
  12. $wp_sitemaps->init();
  13. }
  14. public function test_remove_trailing_slashes_for_sitemap_index_requests() {
  15. $this->set_permalink_structure( '/%postname%/' );
  16. $this->assertCanonical( '/wp-sitemap.xml', '/wp-sitemap.xml' );
  17. $this->assertCanonical( '/wp-sitemap.xml/', '/wp-sitemap.xml' );
  18. }
  19. public function test_remove_trailing_slashes_for_sitemap_index_stylesheet_requests() {
  20. $this->set_permalink_structure( '/%postname%/' );
  21. $this->assertCanonical( '/wp-sitemap-index.xsl', '/wp-sitemap-index.xsl' );
  22. $this->assertCanonical( '/wp-sitemap-index.xsl/', '/wp-sitemap-index.xsl' );
  23. }
  24. public function test_remove_trailing_slashes_for_sitemap_requests() {
  25. $this->set_permalink_structure( '/%postname%/' );
  26. $this->assertCanonical( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' );
  27. $this->assertCanonical( '/wp-sitemap-posts-post-1.xml/', '/wp-sitemap-posts-post-1.xml' );
  28. $this->assertCanonical( '/wp-sitemap-users-1.xml', '/wp-sitemap-users-1.xml' );
  29. $this->assertCanonical( '/wp-sitemap-users-1.xml/', '/wp-sitemap-users-1.xml' );
  30. }
  31. public function test_remove_trailing_slashes_for_sitemap_stylesheet_requests() {
  32. $this->set_permalink_structure( '/%postname%/' );
  33. $this->assertCanonical( '/wp-sitemap.xsl', '/wp-sitemap.xsl' );
  34. $this->assertCanonical( '/wp-sitemap.xsl/', '/wp-sitemap.xsl' );
  35. }
  36. /**
  37. * Ensure sitemaps redirects work as expected with pretty permalinks.
  38. *
  39. * @dataProvider data_sitemaps_canonical_pretty_redirects
  40. * @ticket 50910
  41. */
  42. public function test_sitemaps_canonical_pretty_redirects( $test_url, $expected ) {
  43. $this->set_permalink_structure( '/%postname%/' );
  44. $this->assertCanonical( $test_url, $expected, 50910 );
  45. }
  46. /**
  47. * Data provider for test_sitemaps_canonical_pretty_redirects.
  48. *
  49. * @return array[] {
  50. * Data to test with.
  51. *
  52. * @type string $0 The test URL.
  53. * @type string $1 The expected canonical URL.
  54. * }
  55. */
  56. public function data_sitemaps_canonical_pretty_redirects() {
  57. return array(
  58. // Ugly/incorrect versions redirect correctly.
  59. array( '/?sitemap=index', '/wp-sitemap.xml' ),
  60. array( '/wp-sitemap.xml/', '/wp-sitemap.xml' ),
  61. array( '/?sitemap=posts&sitemap-subtype=post', '/wp-sitemap-posts-post-1.xml' ),
  62. array( '/?sitemap=posts&sitemap-subtype=post&paged=2', '/wp-sitemap-posts-post-2.xml' ),
  63. array( '/?sitemap=taxonomies&sitemap-subtype=category', '/wp-sitemap-taxonomies-category-1.xml' ),
  64. array( '/?sitemap=taxonomies&sitemap-subtype=category&paged=2', '/wp-sitemap-taxonomies-category-2.xml' ),
  65. // Pretty versions don't redirect incorrectly.
  66. array( '/wp-sitemap.xml', '/wp-sitemap.xml' ),
  67. array( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' ),
  68. array( '/wp-sitemap-posts-post-2.xml', '/wp-sitemap-posts-post-2.xml' ),
  69. array( '/wp-sitemap-taxonomies-category-1.xml', '/wp-sitemap-taxonomies-category-1.xml' ),
  70. array( '/wp-sitemap-taxonomies-category-2.xml', '/wp-sitemap-taxonomies-category-2.xml' ),
  71. );
  72. }
  73. /**
  74. * Ensure sitemaps redirects work as expected with ugly permalinks.
  75. *
  76. * @dataProvider data_sitemaps_canonical_ugly_redirects
  77. * @ticket 50910
  78. */
  79. public function test_sitemaps_canonical_ugly_redirects( $test_url, $expected ) {
  80. $this->set_permalink_structure( '' );
  81. $this->assertCanonical( $test_url, $expected, 50910 );
  82. }
  83. /**
  84. * Data provider for test_sitemaps_canonical_ugly_redirects.
  85. *
  86. * @return array[] {
  87. * Data to test with.
  88. *
  89. * @type string $0 The test URL.
  90. * @type string $1 The expected canonical URL.
  91. * }
  92. */
  93. public function data_sitemaps_canonical_ugly_redirects() {
  94. return array(
  95. // Ugly permalinks remain ugly.
  96. array( '/?sitemap=index', '/?sitemap=index' ),
  97. array( '/?sitemap=posts&sitemap-subtype=post', '/?sitemap=posts&sitemap-subtype=post' ),
  98. array( '/?sitemap=posts&sitemap-subtype=post&paged=2', '/?sitemap=posts&sitemap-subtype=post&paged=2' ),
  99. array( '/?sitemap=taxonomies&sitemap-subtype=category', '/?sitemap=taxonomies&sitemap-subtype=category' ),
  100. array( '/?sitemap=taxonomies&sitemap-subtype=category&paged=2', '/?sitemap=taxonomies&sitemap-subtype=category&paged=2' ),
  101. );
  102. }
  103. }