canonical.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Tests Canonical redirections.
  4. *
  5. * In the process of doing so, it also tests WP, WP_Rewrite and WP_Query, A fail here may show a bug in any one of these areas.
  6. *
  7. * @group canonical
  8. * @group rewrite
  9. * @group query
  10. */
  11. class Tests_Canonical extends WP_UnitTestCase {
  12. // This can be defined in a subclass of this class which contains it's own data() method, those tests will be run against the specified permastruct
  13. var $structure = '/%year%/%monthnum%/%day%/%postname%/';
  14. var $old_current_user;
  15. var $author_id;
  16. var $post_ids;
  17. var $term_ids;
  18. function setUp() {
  19. parent::setUp();
  20. update_option( 'page_comments', true );
  21. update_option( 'comments_per_page', 5 );
  22. update_option( 'posts_per_page', 5 );
  23. update_option( 'permalink_structure', $this->structure );
  24. create_initial_taxonomies();
  25. $GLOBALS['wp_rewrite']->init();
  26. flush_rewrite_rules();
  27. $this->old_current_user = get_current_user_id();
  28. $this->author_id = $this->factory->user->create( array( 'user_login' => 'canonical-author' ) );
  29. wp_set_current_user( $this->author_id );
  30. // Already created by install defaults:
  31. // $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) );
  32. $this->term_ids = array();
  33. $this->factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02 00:00:00' ) );
  34. $post_id = $this->factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10 00:00:00' ) );
  35. $this->factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) );
  36. $this->factory->post->create( array(
  37. 'post_title' => 'images-test',
  38. 'post_date' => '2008-09-03 00:00:00',
  39. 'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3'
  40. ) );
  41. $post_id = $this->factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03 00:00:00' ) );
  42. $this->factory->comment->create_post_comments( $post_id, 15 );
  43. $this->factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) );
  44. $this->factory->post->create( array( 'import_id' => 123 ) );
  45. $this->factory->post->create( array( 'import_id' => 1 ) );
  46. $this->factory->post->create( array( 'import_id' => 358 ) );
  47. $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) );
  48. $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) );
  49. $post_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  50. $this->factory->post->create(
  51. array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id,
  52. ) );
  53. $this->term_ids['/category/parent/'] = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) );
  54. $this->term_ids['/category/parent/child-1/'] = $this->factory->term->create( array(
  55. 'taxonomy' => 'category', 'name' => 'child-1', 'parent' => $this->term_ids['/category/parent/'],
  56. ) );
  57. $this->term_ids['/category/parent/child-1/child-2/'] = $this->factory->term->create( array(
  58. 'taxonomy' => 'category', 'name' => 'child-2', 'parent' => $this->term_ids['/category/parent/child-1/'],
  59. ) );
  60. $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
  61. $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
  62. $this->factory->term->create( array( 'name' => 'post-formats' ) );
  63. }
  64. function tearDown() {
  65. parent::tearDown();
  66. wp_set_current_user( $this->old_current_user );
  67. $GLOBALS['wp_rewrite']->init();
  68. }
  69. // URL's are relative to the site "front", ie. /category/uncategorized/ instead of http://site.../category..
  70. // Return url's are full url's with the prepended home.
  71. function get_canonical($test_url) {
  72. $test_url = home_url( $test_url );
  73. $can_url = redirect_canonical( $test_url, false );
  74. if ( ! $can_url )
  75. return $test_url; // No redirect will take place for this request
  76. return $can_url;
  77. }
  78. /**
  79. * @dataProvider data
  80. */
  81. function test($test_url, $expected, $ticket = 0) {
  82. if ( $ticket )
  83. $this->knownWPBug( $ticket );
  84. $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
  85. if ( is_string($expected) )
  86. $expected = array('url' => $expected);
  87. elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
  88. $expected = array( 'qv' => $expected );
  89. if ( !isset($expected['url']) && !isset($expected['qv']) )
  90. $this->markTestSkipped('No valid expected output was provided');
  91. if ( false !== strpos( $test_url, '%d' ) ) {
  92. if ( false !== strpos( $test_url, '/?author=%d' ) )
  93. $test_url = sprintf( $test_url, $this->author_id );
  94. if ( false !== strpos( $test_url, '?cat=%d' ) )
  95. $test_url = sprintf( $test_url, $this->term_ids[ $expected['url'] ] );
  96. }
  97. $this->go_to( home_url( $test_url ) );
  98. // Does the redirect match what's expected?
  99. $can_url = $this->get_canonical( $test_url );
  100. $parsed_can_url = parse_url($can_url);
  101. // Just test the Path and Query if present
  102. if ( isset($expected['url']) )
  103. $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
  104. if ( ! isset($expected['qv']) )
  105. return;
  106. // "make" that the request and check the query is correct
  107. $this->go_to( $can_url );
  108. // Are all query vars accounted for, And correct?
  109. global $wp;
  110. $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
  111. if ( !empty($parsed_can_url['query']) ) {
  112. parse_str($parsed_can_url['query'], $_qv);
  113. // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
  114. $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
  115. $query_vars = array_merge($query_vars, $_qv);
  116. }
  117. $this->assertEquals( $expected['qv'], $query_vars );
  118. }
  119. function data() {
  120. /* Data format:
  121. * [0]: $test_url,
  122. * [1]: expected results: Any of the following can be used
  123. * array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET );
  124. * array( expected query vars to be set, same as 'qv' above )
  125. * (string) expected redirect location
  126. * [2]: (optional) The ticket the test refers to, Can be skipped if unknown.
  127. */
  128. // Please Note: A few test cases are commented out below, Look at the test case following it, in most cases it's simple showing 2 options for the "proper" redirect.
  129. return array(
  130. // Categories
  131. array( '?cat=%d', '/category/parent/', 15256 ),
  132. array( '?cat=%d', '/category/parent/child-1/', 15256 ),
  133. array( '?cat=%d', '/category/parent/child-1/child-2/' ), // no children
  134. array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ),
  135. array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
  136. array( '/category/uncategorized/?paged=2', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
  137. array( '/category/uncategorized/?paged=2&category_name=uncategorized', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ), 17174 ),
  138. array( '/category/child-1/', '/category/parent/child-1/', 18734 ),
  139. array( '/category/foo/child-1/', '/category/parent/child-1/', 18734 ),
  140. // Categories & Intersections with other vars
  141. array( '/category/uncategorized/?tag=post-formats', array( 'url' => '/category/uncategorized/?tag=post-formats', 'qv' => array('category_name' => 'uncategorized', 'tag' => 'post-formats') ) ),
  142. array( '/?category_name=cat-a,cat-b', array( 'url' => '/?category_name=cat-a,cat-b', 'qv' => array( 'category_name' => 'cat-a,cat-b' ) ) ),
  143. // Taxonomies with extra Query Vars
  144. array( '/category/cat-a/page/1/?test=one%20two', '/category/cat-a/?test=one%20two', 18086), // Extra query vars should stay encoded
  145. // Categories with Dates
  146. array( '/category/uncategorized/?paged=2&year=2008', array( 'url' => '/category/uncategorized/page/2/?year=2008', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2, 'year' => 2008) ), 17661 ),
  147. // array( '/2008/04/?cat=1', array( 'url' => '/2008/04/?cat=1', 'qv' => array('cat' => '1', 'year' => '2008', 'monthnum' => '04' ) ), 17661 ),
  148. array( '/2008/04/?cat=1', array( 'url' => '/category/uncategorized/?year=2008&monthnum=04', 'qv' => array('category_name' => 'uncategorized', 'year' => '2008', 'monthnum' => '04' ) ), 17661 ),
  149. // array( '/2008/?category_name=cat-a', array( 'url' => '/2008/?category_name=cat-a', 'qv' => array('category_name' => 'cat-a', 'year' => '2008' ) ) ),
  150. array( '/2008/?category_name=cat-a', array( 'url' => '/category/cat-a/?year=2008', 'qv' => array('category_name' => 'cat-a', 'year' => '2008' ) ), 20386 ),
  151. // array( '/category/uncategorized/?year=2008', array( 'url' => '/2008/?category_name=uncategorized', 'qv' => array('category_name' => 'uncategorized', 'year' => '2008' ) ), 17661 ),
  152. array( '/category/uncategorized/?year=2008', array( 'url' => '/category/uncategorized/?year=2008', 'qv' => array('category_name' => 'uncategorized', 'year' => '2008' ) ), 17661 ),
  153. // Pages
  154. array( '/sample%20page/', array( 'url' => '/sample-page/', 'qv' => array('pagename' => 'sample-page', 'page' => '' ) ), 17653 ), // Page rules always set 'page'
  155. array( '/sample------page/', array( 'url' => '/sample-page/', 'qv' => array('pagename' => 'sample-page', 'page' => '' ) ), 14773 ),
  156. array( '/child-page-1/', '/parent-page/child-page-1/'),
  157. array( '/?page_id=144', '/parent-page/child-page-1/'),
  158. array( '/abo', '/about/' ),
  159. // Posts
  160. array( '?p=587', '/2008/06/02/post-format-test-audio/'),
  161. array( '/?name=images-test', '/2008/09/03/images-test/'),
  162. // Incomplete slug should resolve and remove the ?name= parameter
  163. array( '/?name=images-te', '/2008/09/03/images-test/', 20374),
  164. // Page slug should resolve to post slug and remove the ?pagename= parameter
  165. array( '/?pagename=images-test', '/2008/09/03/images-test/', 20374),
  166. array( '/2008/06/02/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
  167. array( '/2008/06/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
  168. array( '/2008/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
  169. array( '/2010/post-format-test-au/', '/2008/06/02/post-format-test-audio/'), // A Year the post is not in
  170. array( '/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
  171. array( '/2008/09/03/images-test/3/', array( 'url' => '/2008/09/03/images-test/3/', 'qv' => array( 'name' => 'images-test', 'year' => '2008', 'monthnum' => '09', 'day' => '03', 'page' => '/3' ) ) ), // page = /3 ?!
  172. array( '/2008/09/03/images-test/8/', '/2008/09/03/images-test/4/', 11694 ), // post with 4 pages
  173. array( '/2008/09/03/images-test/?page=3', '/2008/09/03/images-test/3/' ),
  174. array( '/2008/09/03/images-te?page=3', '/2008/09/03/images-test/3/' ),
  175. // Comments
  176. array( '/2008/03/03/comment-test/?cpage=2', '/2008/03/03/comment-test/comment-page-2/' ),
  177. array( '/2008/03/03/comment-test/comment-page-20/', '/2008/03/03/comment-test/comment-page-3/', 20388 ), // there's only 3 pages
  178. array( '/2008/03/03/comment-test/?cpage=30', '/2008/03/03/comment-test/comment-page-3/', 20388 ), // there's only 3 pages
  179. // Attachments
  180. array( '/?attachment_id=611', '/2008/06/10/post-format-test-gallery/canola2/' ),
  181. array( '/2008/06/10/post-format-test-gallery/?attachment_id=611', '/2008/06/10/post-format-test-gallery/canola2/' ),
  182. // Dates
  183. array( '/?m=2008', '/2008/' ),
  184. array( '/?m=200809', '/2008/09/'),
  185. array( '/?m=20080905', '/2008/09/05/'),
  186. array( '/2008/?day=05', '/2008/?day=05'), // no redirect
  187. array( '/2008/09/?day=05', '/2008/09/05/'),
  188. array( '/2008/?monthnum=9', '/2008/09/'),
  189. array( '/?year=2008', '/2008/'),
  190. array( '/2012/13/', '/2012/'),
  191. array( '/2012/11/51/', '/2012/11/'),
  192. // Authors
  193. array( '/?author=%d', '/author/canonical-author/' ),
  194. // array( '/?author=%d&year=2008', '/2008/?author=3'),
  195. array( '/?author=%d&year=2008', '/author/canonical-author/?year=2008', 17661 ),
  196. // array( '/author/canonical-author/?year=2008', '/2008/?author=3'), //Either or, see previous testcase.
  197. array( '/author/canonical-author/?year=2008', '/author/canonical-author/?year=2008', 17661 ),
  198. // Feeds
  199. array( '/?feed=atom', '/feed/atom/' ),
  200. array( '/?feed=rss2', '/feed/' ),
  201. array( '/?feed=comments-rss2', '/comments/feed/'),
  202. array( '/?feed=comments-atom', '/comments/feed/atom/'),
  203. // Feeds (per-post)
  204. array( '/2008/03/03/comment-test/?feed=comments-atom', '/2008/03/03/comment-test/feed/atom/'),
  205. array( '/?p=149&feed=comments-atom', '/2008/03/03/comment-test/feed/atom/'),
  206. array( '/2008/03/03/comment-test/?feed=comments-atom', '/2008/03/03/comment-test/feed/atom/' ),
  207. // Index
  208. array( '/?paged=1', '/' ),
  209. array( '/page/1/', '/' ),
  210. array( '/page1/', '/' ),
  211. array( '/?paged=2', '/page/2/' ),
  212. array( '/page2/', '/page/2/' ),
  213. // Misc
  214. array( '/2008%20', '/2008' ),
  215. array( '//2008////', '/2008/' ),
  216. // Todo: Endpoints (feeds, trackbacks, etc), More fuzzed mixed query variables, comment paging, Home page (Static)
  217. );
  218. }
  219. }