category.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @group canonical
  4. */
  5. class Tests_Canonical_Category extends WP_Canonical_UnitTestCase {
  6. public $structure = '/%category%/%postname%/';
  7. public static $posts = array();
  8. public static $cats = array();
  9. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  10. self::$posts[0] = $factory->post->create( array( 'post_name' => 'post0' ) );
  11. self::$posts[1] = $factory->post->create( array( 'post_name' => 'post1' ) );
  12. self::$cats[0] = $factory->category->create( array( 'slug' => 'cat0' ) );
  13. self::$cats[1] = $factory->category->create( array( 'slug' => 'cat1' ) );
  14. self::$cats[2] = $factory->category->create( array( 'slug' => 'cat2' ) );
  15. wp_set_post_categories( self::$posts[0], self::$cats[2] );
  16. wp_set_post_categories( self::$posts[0], self::$cats[0] );
  17. wp_set_post_categories( self::$posts[1], self::$cats[1] );
  18. }
  19. /**
  20. * @dataProvider data_canonical_category
  21. */
  22. public function test_canonical_category( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
  23. $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
  24. }
  25. public function data_canonical_category() {
  26. /*
  27. * Data format:
  28. * [0]: Test URL.
  29. * [1]: Expected results: Any of the following can be used.
  30. * array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET );
  31. * array( expected query vars to be set, same as 'qv' above )
  32. * (string) expected redirect location
  33. * [2]: (optional) The ticket the test refers to, Can be skipped if unknown.
  34. * [3]: (optional) Array of class/function names expected to throw `_doing_it_wrong()` notices.
  35. */
  36. return array(
  37. // Valid category.
  38. array(
  39. '/cat0/post0/',
  40. array(
  41. 'url' => '/cat0/post0/',
  42. 'qv' => array(
  43. 'category_name' => 'cat0',
  44. 'name' => 'post0',
  45. 'page' => '',
  46. ),
  47. ),
  48. ),
  49. // Category other than the first one will redirect to first "canonical" category.
  50. array(
  51. '/cat2/post0/',
  52. array(
  53. 'url' => '/cat0/post0/',
  54. 'qv' => array(
  55. 'category_name' => 'cat0',
  56. 'name' => 'post0',
  57. 'page' => '',
  58. ),
  59. ),
  60. ),
  61. // Incorrect category will redirect to correct one.
  62. array(
  63. '/cat1/post0/',
  64. array(
  65. 'url' => '/cat0/post0/',
  66. 'qv' => array(
  67. 'category_name' => 'cat0',
  68. 'name' => 'post0',
  69. 'page' => '',
  70. ),
  71. ),
  72. ),
  73. // Nonexistent category will redirect to correct one.
  74. array(
  75. '/foo/post0/',
  76. array(
  77. 'url' => '/cat0/post0/',
  78. 'qv' => array(
  79. 'category_name' => 'cat0',
  80. 'name' => 'post0',
  81. 'page' => '',
  82. ),
  83. ),
  84. ),
  85. // Embed URLs should not redirect to post permalinks.
  86. array(
  87. '/cat0/post0/embed/',
  88. array(
  89. 'url' => '/cat0/post0/embed/',
  90. 'qv' => array(
  91. 'category_name' => 'cat0',
  92. 'name' => 'post0',
  93. 'embed' => 'true',
  94. ),
  95. ),
  96. ),
  97. );
  98. }
  99. }