addRewriteEndpoint.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @group rewrite
  4. */
  5. class Tests_Rewrite_AddRewriteEndpoint extends WP_UnitTestCase {
  6. private $qvs;
  7. protected static $test_page_id;
  8. protected static $test_post_id;
  9. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  10. self::$test_page_id = $factory->post->create(
  11. array(
  12. 'post_type' => 'page',
  13. )
  14. );
  15. self::$test_post_id = $factory->post->create();
  16. }
  17. public function setUp() {
  18. parent::setUp();
  19. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  20. $this->qvs = $GLOBALS['wp']->public_query_vars;
  21. }
  22. public function tearDown() {
  23. $GLOBALS['wp']->public_query_vars = $this->qvs;
  24. parent::tearDown();
  25. }
  26. public function test_should_register_query_using_name_param_by_default() {
  27. add_rewrite_endpoint( 'foo', EP_ALL );
  28. $this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
  29. }
  30. public function test_should_register_query_using_name_param_if_null_is_passed_as_query_var() {
  31. add_rewrite_endpoint( 'foo', EP_ALL, null );
  32. $this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
  33. }
  34. public function test_should_register_query_using_query_var_param_if_not_null() {
  35. add_rewrite_endpoint( 'foo', EP_ALL, 'bar' );
  36. $this->assertContains( 'bar', $GLOBALS['wp']->public_query_vars );
  37. }
  38. /**
  39. * @ticket 25143
  40. */
  41. public function test_should_register_query_var_using_name_param_if_true_is_passed_as_query_var() {
  42. add_rewrite_endpoint( 'foo', EP_ALL, true );
  43. $this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
  44. }
  45. /**
  46. * @ticket 25143
  47. */
  48. public function test_should_not_register_query_var_if_query_var_param_is_false() {
  49. $qvs = $GLOBALS['wp']->public_query_vars;
  50. add_rewrite_endpoint( 'foo', EP_ALL, false );
  51. $this->assertSame( $qvs, $GLOBALS['wp']->public_query_vars );
  52. }
  53. /**
  54. * @ticket 25143
  55. */
  56. public function test_is_home_should_be_false_when_visiting_custom_endpoint_without_a_registered_query_var_and_page_on_front_is_set() {
  57. update_option( 'show_on_front', 'page' );
  58. update_option( 'page_on_front', self::$test_page_id );
  59. add_rewrite_endpoint( 'test', EP_ALL, false );
  60. flush_rewrite_rules();
  61. $this->go_to( home_url( '/test/1' ) );
  62. $this->assertQueryTrue( 'is_front_page', 'is_page', 'is_singular' );
  63. $this->assertFalse( is_home() );
  64. }
  65. public function test_permalink_endpoint_only_applies_on_permalink() {
  66. add_rewrite_endpoint( 'permalink_endpoint', EP_PERMALINK );
  67. flush_rewrite_rules();
  68. $this->go_to( get_permalink( self::$test_post_id ) . 'permalink_endpoint/foo/' );
  69. $this->assertTrue( is_single( self::$test_post_id ) );
  70. $this->assertSame( 'foo', get_query_var( 'permalink_endpoint' ) );
  71. $this->go_to( home_url( 'permalink_endpoint/foo/' ) );
  72. $this->assertTrue( is_404() );
  73. $this->assertSame( '', get_query_var( 'permalink_endpoint' ) );
  74. }
  75. public function test_page_endpoint_only_applies_on_page() {
  76. add_rewrite_endpoint( 'page_endpoint', EP_PAGES );
  77. flush_rewrite_rules();
  78. $this->go_to( get_permalink( self::$test_page_id ) . 'page_endpoint/foo/' );
  79. $this->assertTrue( is_page( self::$test_page_id ) );
  80. $this->assertSame( 'foo', get_query_var( 'page_endpoint' ) );
  81. $this->go_to( home_url( 'page_endpoint/foo/' ) );
  82. $this->assertTrue( is_404() );
  83. $this->assertSame( '', get_query_var( 'page_endpoint' ) );
  84. }
  85. }