sitemaps.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. /**
  3. * Sitemaps: Sitemaps_Tests class
  4. *
  5. * Main test class.
  6. *
  7. * @package Sitemaps
  8. * @copyright 2019 The Core Sitemaps Contributors
  9. * @license GNU General Public License, version 2
  10. * @link https://github.com/GoogleChromeLabs/wp-sitemaps
  11. */
  12. /**
  13. * Core sitemaps test cases.
  14. *
  15. * @group sitemaps
  16. */
  17. class Test_Sitemaps extends WP_UnitTestCase {
  18. /**
  19. * List of user IDs.
  20. *
  21. * @var array
  22. */
  23. public static $users;
  24. /**
  25. * List of post_tag IDs.
  26. *
  27. * @var array
  28. */
  29. public static $post_tags;
  30. /**
  31. * List of category IDs.
  32. *
  33. * @var array
  34. */
  35. public static $cats;
  36. /**
  37. * List of post type post IDs.
  38. *
  39. * @var array
  40. */
  41. public static $posts;
  42. /**
  43. * List of post type page IDs.
  44. *
  45. * @var array
  46. */
  47. public static $pages;
  48. /**
  49. * Editor ID for use in some tests.
  50. *
  51. * @var int
  52. */
  53. public static $editor_id;
  54. /**
  55. * Test sitemap provider.
  56. *
  57. * @var WP_Sitemaps_Test_Provider
  58. */
  59. public static $test_provider;
  60. /**
  61. * Set up fixtures.
  62. *
  63. * @param WP_UnitTest_Factory $factory A WP_UnitTest_Factory object.
  64. */
  65. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  66. self::$users = $factory->user->create_many( 10 );
  67. self::$post_tags = $factory->term->create_many( 10 );
  68. self::$cats = $factory->term->create_many( 10, array( 'taxonomy' => 'category' ) );
  69. self::$pages = $factory->post->create_many( 10, array( 'post_type' => 'page' ) );
  70. // Create a set of posts pre-assigned to tags and authors.
  71. self::$posts = $factory->post->create_many(
  72. 10,
  73. array(
  74. 'tags_input' => self::$post_tags,
  75. 'post_author' => reset( self::$users ),
  76. )
  77. );
  78. // Create a user with an editor role to complete some tests.
  79. self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
  80. self::$test_provider = new WP_Sitemaps_Test_Provider();
  81. }
  82. /**
  83. * Helper function to get all sitemap entries data.
  84. *
  85. * @return array A list of sitemap entires.
  86. */
  87. public function _get_sitemap_entries() {
  88. $entries = array();
  89. $providers = wp_get_sitemap_providers();
  90. foreach ( $providers as $provider ) {
  91. // Using `array_push` is more efficient than `array_merge` in the loop.
  92. array_push( $entries, ...$provider->get_sitemap_entries() );
  93. }
  94. return $entries;
  95. }
  96. /**
  97. * Test default sitemap entries.
  98. */
  99. public function test_get_sitemap_entries() {
  100. $entries = $this->_get_sitemap_entries();
  101. $expected = array(
  102. array(
  103. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=post&paged=1',
  104. ),
  105. array(
  106. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=page&paged=1',
  107. ),
  108. array(
  109. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=category&paged=1',
  110. ),
  111. array(
  112. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1',
  113. ),
  114. array(
  115. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=users&paged=1',
  116. ),
  117. );
  118. $this->assertSame( $expected, $entries );
  119. }
  120. /**
  121. * Test default sitemap entries with permalinks on.
  122. */
  123. public function test_get_sitemap_entries_post_with_permalinks() {
  124. $this->set_permalink_structure( '/%year%/%postname%/' );
  125. $entries = $this->_get_sitemap_entries();
  126. $expected = array(
  127. array(
  128. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml',
  129. ),
  130. array(
  131. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml',
  132. ),
  133. array(
  134. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml',
  135. ),
  136. array(
  137. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml',
  138. ),
  139. array(
  140. 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml',
  141. ),
  142. );
  143. // Clean up permalinks.
  144. $this->set_permalink_structure();
  145. $this->assertSame( $expected, $entries );
  146. }
  147. /**
  148. * Test sitemap index entries with public and private custom post types.
  149. */
  150. public function test_get_sitemap_entries_custom_post_types() {
  151. // Register and create a public post type post.
  152. register_post_type( 'public_cpt', array( 'public' => true ) );
  153. self::factory()->post->create( array( 'post_type' => 'public_cpt' ) );
  154. // Register and create a private post type post.
  155. register_post_type( 'private_cpt', array( 'public' => false ) );
  156. self::factory()->post->create( array( 'post_type' => 'private_cpt' ) );
  157. $entries = wp_list_pluck( $this->_get_sitemap_entries(), 'loc' );
  158. // Clean up.
  159. unregister_post_type( 'public_cpt' );
  160. unregister_post_type( 'private_cpt' );
  161. $this->assertContains( 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=public_cpt&paged=1', $entries, 'Public CPTs are not in the index.' );
  162. $this->assertNotContains( 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=private_cpt&paged=1', $entries, 'Private CPTs are visible in the index.' );
  163. }
  164. /**
  165. * Test sitemap index entries with public and private custom post types.
  166. *
  167. * @ticket 50607
  168. */
  169. public function test_get_sitemap_entries_not_publicly_queryable_post_types() {
  170. register_post_type(
  171. 'non_queryable_cpt',
  172. array(
  173. 'public' => true,
  174. 'publicly_queryable' => false,
  175. )
  176. );
  177. self::factory()->post->create( array( 'post_type' => 'non_queryable_cpt' ) );
  178. $entries = wp_list_pluck( $this->_get_sitemap_entries(), 'loc' );
  179. // Clean up.
  180. unregister_post_type( 'non_queryable_cpt' );
  181. $this->assertNotContains( 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=non_queryable_cpt&paged=1', $entries, 'Non-publicly queryable CPTs are visible in the index.' );
  182. }
  183. /**
  184. * Tests getting a URL list for post type post.
  185. */
  186. public function test_get_url_list_post() {
  187. $providers = wp_get_sitemap_providers();
  188. $post_list = $providers['posts']->get_url_list( 1, 'post' );
  189. $expected = $this->_get_expected_url_list( 'post', self::$posts );
  190. $this->assertSame( $expected, $post_list );
  191. }
  192. /**
  193. * Tests getting a URL list for post type page.
  194. */
  195. public function test_get_url_list_page() {
  196. // Short circuit the show on front option.
  197. add_filter( 'pre_option_show_on_front', '__return_true' );
  198. $providers = wp_get_sitemap_providers();
  199. $post_list = $providers['posts']->get_url_list( 1, 'page' );
  200. $expected = $this->_get_expected_url_list( 'page', self::$pages );
  201. $this->assertSame( $expected, $post_list );
  202. }
  203. /**
  204. * Tests getting a URL list for post type page with included home page.
  205. */
  206. public function test_get_url_list_page_with_home() {
  207. $providers = wp_get_sitemap_providers();
  208. $post_list = $providers['posts']->get_url_list( 1, 'page' );
  209. $expected = $this->_get_expected_url_list( 'page', self::$pages );
  210. // Add the homepage to the front of the URL list.
  211. array_unshift(
  212. $expected,
  213. array(
  214. 'loc' => home_url( '/' ),
  215. )
  216. );
  217. $this->assertSame( $expected, $post_list );
  218. }
  219. /**
  220. * Tests getting a URL list for post with private post.
  221. */
  222. public function test_get_url_list_private_post() {
  223. wp_set_current_user( self::$editor_id );
  224. $providers = wp_get_sitemap_providers();
  225. $post_list_before = $providers['posts']->get_url_list( 1, 'post' );
  226. $private_post_id = self::factory()->post->create( array( 'post_status' => 'private' ) );
  227. $post_list_after = $providers['posts']->get_url_list( 1, 'post' );
  228. $private_post = array(
  229. 'loc' => get_permalink( $private_post_id ),
  230. );
  231. $this->assertNotContains( $private_post, $post_list_after );
  232. $this->assertSameSets( $post_list_before, $post_list_after );
  233. }
  234. /**
  235. * Tests getting a URL list for a custom post type.
  236. */
  237. public function test_get_url_list_cpt() {
  238. $post_type = 'custom_type';
  239. // Registered post types are private unless explicitly set to public.
  240. register_post_type( $post_type, array( 'public' => true ) );
  241. $ids = self::factory()->post->create_many( 10, array( 'post_type' => $post_type ) );
  242. $providers = wp_get_sitemap_providers();
  243. $post_list = $providers['posts']->get_url_list( 1, $post_type );
  244. $expected = $this->_get_expected_url_list( $post_type, $ids );
  245. // Clean up.
  246. unregister_post_type( $post_type );
  247. $this->assertSame( $expected, $post_list, 'Custom post type posts are not visible.' );
  248. }
  249. /**
  250. * Tests getting a URL list for a private custom post type.
  251. */
  252. public function test_get_url_list_cpt_private() {
  253. $post_type = 'private_type';
  254. // Create a private post type for testing against data leaking.
  255. register_post_type( $post_type, array( 'public' => false ) );
  256. self::factory()->post->create_many( 10, array( 'post_type' => $post_type ) );
  257. $providers = wp_get_sitemap_providers();
  258. $post_list = $providers['posts']->get_url_list( 1, $post_type );
  259. // Clean up.
  260. unregister_post_type( $post_type );
  261. $this->assertEmpty( $post_list, 'Private post types may be returned by the post provider.' );
  262. }
  263. /**
  264. * Tests getting a URL list for a private custom post type.
  265. *
  266. * @ticket 50607
  267. */
  268. public function test_get_url_list_cpt_not_publicly_queryable() {
  269. $post_type = 'non_queryable_cpt';
  270. register_post_type(
  271. $post_type,
  272. array(
  273. 'public' => true,
  274. 'publicly_queryable' => false,
  275. )
  276. );
  277. self::factory()->post->create_many( 10, array( 'post_type' => $post_type ) );
  278. $providers = wp_get_sitemap_providers();
  279. $post_list = $providers['posts']->get_url_list( 1, $post_type );
  280. // Clean up.
  281. unregister_post_type( $post_type );
  282. $this->assertEmpty( $post_list, 'Non-publicly queryable post types may be returned by the post provider.' );
  283. }
  284. /**
  285. * Helper function for building an expected url list.
  286. *
  287. * @param string $type An object sub type, e.g., post type.
  288. * @param array $ids Array of object IDs.
  289. * @return array A formed URL list.
  290. */
  291. public function _get_expected_url_list( $type, $ids ) {
  292. $posts = get_posts(
  293. array(
  294. 'include' => $ids,
  295. 'orderby' => 'ID',
  296. 'order' => 'ASC',
  297. 'post_type' => $type,
  298. )
  299. );
  300. return array_map(
  301. static function ( $post ) {
  302. return array(
  303. 'loc' => get_permalink( $post ),
  304. );
  305. },
  306. $posts
  307. );
  308. }
  309. /**
  310. * Test functionality that adds a new sitemap provider to the registry.
  311. */
  312. public function test_register_sitemap_provider() {
  313. wp_register_sitemap_provider( 'test_sitemap', self::$test_provider );
  314. $sitemaps = wp_get_sitemap_providers();
  315. $this->assertSame( $sitemaps['test_sitemap'], self::$test_provider, 'Can not confirm sitemap registration is working.' );
  316. }
  317. /**
  318. * Test robots.txt output.
  319. */
  320. public function test_robots_text() {
  321. // Get the text added to the default robots text output.
  322. $robots_text = apply_filters( 'robots_txt', '', true );
  323. $sitemap_string = 'Sitemap: http://' . WP_TESTS_DOMAIN . '/?sitemap=index';
  324. $this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not included in robots text.' );
  325. }
  326. /**
  327. * Test robots.txt output for a private site.
  328. */
  329. public function test_robots_text_private_site() {
  330. $robots_text = apply_filters( 'robots_txt', '', false );
  331. $sitemap_string = 'Sitemap: http://' . WP_TESTS_DOMAIN . '/?sitemap=index';
  332. $this->assertNotContains( $sitemap_string, $robots_text );
  333. }
  334. /**
  335. * Test robots.txt output with permalinks set.
  336. */
  337. public function test_robots_text_with_permalinks() {
  338. // Set permalinks for testing.
  339. $this->set_permalink_structure( '/%year%/%postname%/' );
  340. // Get the text added to the default robots text output.
  341. $robots_text = apply_filters( 'robots_txt', '', true );
  342. $sitemap_string = 'Sitemap: http://' . WP_TESTS_DOMAIN . '/wp-sitemap.xml';
  343. // Clean up permalinks.
  344. $this->set_permalink_structure();
  345. $this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not included in robots text.' );
  346. }
  347. /**
  348. * Test robots.txt output with line feed prefix.
  349. */
  350. public function test_robots_text_prefixed_with_line_feed() {
  351. // Get the text added to the default robots text output.
  352. $robots_text = apply_filters( 'robots_txt', '', true );
  353. $sitemap_string = "\nSitemap: ";
  354. $this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not prefixed with "\n".' );
  355. }
  356. /**
  357. * @ticket 50643
  358. */
  359. public function test_sitemaps_enabled() {
  360. $before = wp_sitemaps_get_server()->sitemaps_enabled();
  361. add_filter( 'wp_sitemaps_enabled', '__return_false' );
  362. $after = wp_sitemaps_get_server()->sitemaps_enabled();
  363. remove_filter( 'wp_sitemaps_enabled', '__return_false' );
  364. $this->assertTrue( $before );
  365. $this->assertFalse( $after );
  366. }
  367. /**
  368. * @ticket 50643
  369. * @runInSeparateProcess
  370. * @preserveGlobalState disabled
  371. */
  372. public function test_disable_sitemap_should_return_404() {
  373. add_filter( 'wp_sitemaps_enabled', '__return_false' );
  374. $this->go_to( home_url( '/?sitemap=index' ) );
  375. wp_sitemaps_get_server()->render_sitemaps();
  376. remove_filter( 'wp_sitemaps_enabled', '__return_false' );
  377. $this->assertTrue( is_404() );
  378. }
  379. /**
  380. * @ticket 50643
  381. * @runInSeparateProcess
  382. * @preserveGlobalState disabled
  383. */
  384. public function test_empty_url_list_should_return_404() {
  385. wp_register_sitemap_provider( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) );
  386. $this->go_to( home_url( '/?sitemap=foo' ) );
  387. wp_sitemaps_get_server()->render_sitemaps();
  388. $this->assertTrue( is_404() );
  389. }
  390. }