rewrite.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * A set of unit tests for functions in wp-includes/rewrite.php
  4. *
  5. * @group rewrite
  6. */
  7. class Tests_Rewrite extends WP_UnitTestCase {
  8. function setUp() {
  9. parent::setUp();
  10. // Need rewrite rules in place to use url_to_postid
  11. global $wp_rewrite;
  12. update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
  13. create_initial_taxonomies();
  14. $GLOBALS['wp_rewrite']->init();
  15. flush_rewrite_rules();
  16. }
  17. function tearDown() {
  18. parent::tearDown();
  19. $GLOBALS['wp_rewrite']->init();
  20. }
  21. function test_url_to_postid() {
  22. $id = $this->factory->post->create();
  23. $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
  24. $id = $this->factory->post->create( array( 'post_type' => 'page' ) );
  25. $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
  26. }
  27. function test_url_to_postid_custom_post_type() {
  28. delete_option( 'rewrite_rules' );
  29. $post_type = rand_str( 12 );
  30. register_post_type( $post_type, array( 'public' => true ) );
  31. $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
  32. $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
  33. _unregister_post_type( $post_type );
  34. }
  35. function test_url_to_postid_hierarchical() {
  36. $parent_id = $this->factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) );
  37. $child_id = $this->factory->post->create( array( 'post_title' => 'Child', 'post_type' => 'page', 'post_parent' => $parent_id ) );
  38. $this->assertEquals( $parent_id, url_to_postid( get_permalink( $parent_id ) ) );
  39. $this->assertEquals( $child_id, url_to_postid( get_permalink( $child_id ) ) );
  40. }
  41. function test_url_to_postid_home_has_path() {
  42. update_option( 'home', home_url( '/example/' ) );
  43. $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'examp' ) );
  44. $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
  45. $this->assertEquals( $id, url_to_postid( site_url('/example/examp' ) ) );
  46. $this->assertEquals( $id, url_to_postid( '/example/examp/' ) );
  47. $this->assertEquals( $id, url_to_postid( '/example/examp' ) );
  48. $this->assertEquals( 0, url_to_postid( site_url( '/example/ex' ) ) );
  49. $this->assertEquals( 0, url_to_postid( '/example/ex' ) );
  50. $this->assertEquals( 0, url_to_postid( '/example/ex/' ) );
  51. $this->assertEquals( 0, url_to_postid( '/example-page/example/' ) );
  52. $this->assertEquals( 0, url_to_postid( '/example-page/ex/' ) );
  53. }
  54. function test_url_to_postid_dupe_path() {
  55. update_option( 'home', home_url('/example/') );
  56. $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'example' ) );
  57. $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
  58. $this->assertEquals( $id, url_to_postid( site_url( '/example/example/' ) ) );
  59. $this->assertEquals( $id, url_to_postid( '/example/example/' ) );
  60. $this->assertEquals( $id, url_to_postid( '/example/example' ) );
  61. }
  62. /**
  63. * Reveals bug introduced in WP 3.0
  64. */
  65. function test_url_to_postid_home_url_collision() {
  66. update_option( 'home', home_url( '/example' ) );
  67. $this->factory->post->create( array( 'post_title' => 'Collision', 'post_type' => 'page', 'post_name' => 'collision' ) );
  68. // This url should NOT return a post ID
  69. $badurl = site_url( '/example-collision' );
  70. $this->assertEquals( 0, url_to_postid( $badurl ) );
  71. }
  72. /**
  73. * Reveals bug introduced in WP 3.0
  74. *
  75. * Run tests using multisite `phpunit -c multisite`
  76. */
  77. function test_url_to_postid_ms_home_url_collision() {
  78. if ( ! is_multisite() ) {
  79. $this->markTestSkipped( 'test_url_to_postid_ms_home_url_collision requires multisite' );
  80. return false;
  81. }
  82. $blog_id = $this->factory->blog->create( array( 'path' => '/example' ) );
  83. switch_to_blog( $blog_id );
  84. $this->factory->post->create( array( 'post_title' => 'Collision ', 'post_type' => 'page' ) );
  85. // This url should NOT return a post ID
  86. $badurl = network_home_url( '/example-collision' );
  87. $this->assertEquals( 0, url_to_postid( $badurl ) );
  88. restore_current_blog();
  89. }
  90. }