Changeset 904 in tests for trunk/tests/canonical/customRules.php
- Timestamp:
- 07/18/2012 07:01:41 PM (12 years ago)
- Location:
- trunk/tests/canonical
- Files:
-
- 1 added
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/canonical/customRules.php
r903 r904 1 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 canonical8 * @group rewrite9 * @group query10 */11 class WP_Test_Canonical extends WP_UnitTestCase {12 2 13 // 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 14 var $structure = '/%year%/%monthnum%/%day%/%postname%/'; 15 16 var $old_current_user; 17 var $author_id; 18 var $post_ids; 19 var $term_ids; 20 21 function setUp() { 22 parent::setUp(); 23 24 update_option( 'comments_per_page', 5 ); 25 update_option( 'posts_per_page', 5 ); 26 27 update_option( 'permalink_structure', $this->structure ); 28 create_initial_taxonomies(); 29 $GLOBALS['wp_rewrite']->init(); 30 flush_rewrite_rules(); 31 32 $this->old_current_user = get_current_user_id(); 33 $this->author_id = $this->factory->user->create( array( 'user_login' => 'canonical-author' ) ); 34 wp_set_current_user( $this->author_id ); 35 36 // Already created by install defaults: 37 // $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) ); 38 39 $this->term_ids = array(); 40 41 $this->factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02 00:00:00' ) ); 42 $post_id = $this->factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10 00:00:00' ) ); 43 $this->factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) ); 44 45 $this->factory->post->create( array( 46 'post_title' => 'images-test', 47 'post_date' => '2008-09-03 00:00:00', 48 'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3' 49 ) ); 50 51 $post_id = $this->factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03 00:00:00' ) ); 52 $this->factory->comment->create_post_comments( $post_id, 15 ); 53 54 $this->factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) ); 55 56 $this->factory->post->create( array( 'import_id' => 123 ) ); 57 $this->factory->post->create( array( 'import_id' => 1 ) ); 58 $this->factory->post->create( array( 'import_id' => 358 ) ); 59 60 $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) ); 61 $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) ); 62 $post_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) ); 63 $this->factory->post->create( 64 array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id, 65 ) ); 66 67 $this->term_ids['/category/parent/'] = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) ); 68 $this->term_ids['/category/parent/child-1/'] = $this->factory->term->create( array( 69 'taxonomy' => 'category', 'name' => 'child-1', 'parent' => $this->term_ids['/category/parent/'], 70 ) ); 71 $this->term_ids['/category/parent/child-1/child-2/'] = $this->factory->term->create( array( 72 'taxonomy' => 'category', 'name' => 'child-2', 'parent' => $this->term_ids['/category/parent/child-1/'], 73 ) ); 74 75 $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) ); 76 $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) ); 77 78 $this->factory->term->create( array( 'name' => 'post-formats' ) ); 79 } 80 81 function tearDown() { 82 parent::tearDown(); 83 wp_set_current_user( $this->old_current_user ); 84 85 $GLOBALS['wp_rewrite']->init(); 86 } 87 88 // URL's are relative to the site "front", ie. /category/uncategorized/ instead of http://site.../category.. 89 // Return url's are full url's with the prepended home. 90 function get_canonical($test_url) { 91 $test_url = home_url( $test_url ); 92 93 $can_url = redirect_canonical( $test_url, false ); 94 if ( ! $can_url ) 95 return $test_url; // No redirect will take place for this request 96 97 return $can_url; 98 } 99 100 /** 101 * @dataProvider data 102 */ 103 function test($test_url, $expected, $ticket = 0) { 104 if ( $ticket ) 105 $this->knownWPBug( $ticket ); 106 107 $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null; 108 109 if ( is_string($expected) ) 110 $expected = array('url' => $expected); 111 elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) ) 112 $expected = array( 'qv' => $expected ); 113 114 if ( !isset($expected['url']) && !isset($expected['qv']) ) 115 $this->markTestSkipped('No valid expected output was provided'); 116 117 if ( false !== strpos( $test_url, '%d' ) ) { 118 if ( false !== strpos( $test_url, '/?author=%d' ) ) 119 $test_url = sprintf( $test_url, $this->author_id ); 120 if ( false !== strpos( $test_url, '?cat=%d' ) ) 121 $test_url = sprintf( $test_url, $this->term_ids[ $expected['url'] ] ); 122 } 123 124 $this->go_to( home_url( $test_url ) ); 125 126 // Does the redirect match what's expected? 127 $can_url = $this->get_canonical( $test_url ); 128 $parsed_can_url = parse_url($can_url); 129 130 // Just test the Path and Query if present 131 if ( isset($expected['url']) ) 132 $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref ); 133 134 if ( ! isset($expected['qv']) ) 135 return; 136 137 // "make" that the request and check the query is correct 138 $this->go_to( $can_url ); 139 140 // Are all query vars accounted for, And correct? 141 global $wp; 142 143 $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars); 144 if ( !empty($parsed_can_url['query']) ) { 145 parse_str($parsed_can_url['query'], $_qv); 146 147 // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite) 148 $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref ); 149 150 $query_vars = array_merge($query_vars, $_qv); 151 } 152 153 $this->assertEquals( $expected['qv'], $query_vars ); 154 } 155 156 function data() { 157 /* Data format: 158 * [0]: $test_url, 159 * [1]: expected results: Any of the following can be used 160 * array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET ); 161 * array( expected query vars to be set, same as 'qv' above ) 162 * (string) expected redirect location 163 * [2]: (optional) The ticket the test refers to, Can be skipped if unknown. 164 */ 165 166 // 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. 167 return array( 168 // Categories 169 170 array( '?cat=%d', '/category/parent/', 15256 ), 171 array( '?cat=%d', '/category/parent/child-1/', 15256 ), 172 array( '?cat=%d', '/category/parent/child-1/child-2/' ), // no children 173 array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ), 174 array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ), 175 array( '/category/uncategorized/?paged=2', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ), 176 array( '/category/uncategorized/?paged=2&category_name=uncategorized', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ), 17174 ), 177 array( '/category/child-1/', '/category/parent/child-1/', 18734 ), 178 array( '/category/foo/child-1/', '/category/parent/child-1/', 18734 ), 179 180 // Categories & Intersections with other vars 181 array( '/category/uncategorized/?tag=post-formats', array( 'url' => '/category/uncategorized/?tag=post-formats', 'qv' => array('category_name' => 'uncategorized', 'tag' => 'post-formats') ) ), 182 array( '/?category_name=cat-a,cat-b', array( 'url' => '/?category_name=cat-a,cat-b', 'qv' => array( 'category_name' => 'cat-a,cat-b' ) ) ), 183 184 // Taxonomies with extra Query Vars 185 array( '/category/cat-a/page/1/?test=one%20two', '/category/cat-a/?test=one%20two', 18086), // Extra query vars should stay encoded 186 187 // Categories with Dates 188 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 ), 189 // array( '/2008/04/?cat=1', array( 'url' => '/2008/04/?cat=1', 'qv' => array('cat' => '1', 'year' => '2008', 'monthnum' => '04' ) ), 17661 ), 190 array( '/2008/04/?cat=1', array( 'url' => '/category/uncategorized/?year=2008&monthnum=04', 'qv' => array('category_name' => 'uncategorized', 'year' => '2008', 'monthnum' => '04' ) ), 17661 ), 191 // array( '/2008/?category_name=cat-a', array( 'url' => '/2008/?category_name=cat-a', 'qv' => array('category_name' => 'cat-a', 'year' => '2008' ) ) ), 192 array( '/2008/?category_name=cat-a', array( 'url' => '/category/cat-a/?year=2008', 'qv' => array('category_name' => 'cat-a', 'year' => '2008' ) ), 20386 ), 193 // array( '/category/uncategorized/?year=2008', array( 'url' => '/2008/?category_name=uncategorized', 'qv' => array('category_name' => 'uncategorized', 'year' => '2008' ) ), 17661 ), 194 array( '/category/uncategorized/?year=2008', array( 'url' => '/category/uncategorized/?year=2008', 'qv' => array('category_name' => 'uncategorized', 'year' => '2008' ) ), 17661 ), 195 196 // Pages 197 array( '/sample%20page/', array( 'url' => '/sample-page/', 'qv' => array('pagename' => 'sample-page', 'page' => '' ) ), 17653 ), // Page rules always set 'page' 198 array( '/sample------page/', array( 'url' => '/sample-page/', 'qv' => array('pagename' => 'sample-page', 'page' => '' ) ), 14773 ), 199 array( '/child-page-1/', '/parent-page/child-page-1/'), 200 array( '/?page_id=144', '/parent-page/child-page-1/'), 201 array( '/abo', '/about/' ), 202 203 // Posts 204 array( '?p=587', '/2008/06/02/post-format-test-audio/'), 205 array( '/?name=images-test', '/2008/09/03/images-test/'), 206 // Incomplete slug should resolve and remove the ?name= parameter 207 array( '/?name=images-te', '/2008/09/03/images-test/', 20374), 208 // Page slug should resolve to post slug and remove the ?pagename= parameter 209 array( '/?pagename=images-test', '/2008/09/03/images-test/', 20374), 210 211 array( '/2008/06/02/post-format-test-au/', '/2008/06/02/post-format-test-audio/'), 212 array( '/2008/06/post-format-test-au/', '/2008/06/02/post-format-test-audio/'), 213 array( '/2008/post-format-test-au/', '/2008/06/02/post-format-test-audio/'), 214 array( '/2010/post-format-test-au/', '/2008/06/02/post-format-test-audio/'), // A Year the post is not in 215 array( '/post-format-test-au/', '/2008/06/02/post-format-test-audio/'), 216 217 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 ?! 218 array( '/2008/09/03/images-test/8/', '/2008/09/03/images-test/4/', 11694 ), // post with 4 pages 219 array( '/2008/09/03/images-test/?page=3', '/2008/09/03/images-test/3/' ), 220 array( '/2008/09/03/images-te?page=3', '/2008/09/03/images-test/3/' ), 221 222 // Comments 223 array( '/2008/03/03/comment-test/?cpage=2', '/2008/03/03/comment-test/comment-page-2/', 20388 ), 224 array( '/2008/03/03/comment-test/comment-page-20/', '/2008/03/03/comment-test/comment-page-3/', 20388 ), // there's only 3 pages 225 array( '/2008/03/03/comment-test/?cpage=30', '/2008/03/03/comment-test/comment-page-3/', 20388 ), // there's only 3 pages 226 227 // Attachments 228 array( '/?attachment_id=611', '/2008/06/10/post-format-test-gallery/canola2/' ), 229 array( '/2008/06/10/post-format-test-gallery/?attachment_id=611', '/2008/06/10/post-format-test-gallery/canola2/' ), 230 231 // Dates 232 array( '/?m=2008', '/2008/' ), 233 array( '/?m=200809', '/2008/09/'), 234 array( '/?m=20080905', '/2008/09/05/'), 235 236 array( '/2008/?day=05', '/2008/?day=05'), // no redirect 237 array( '/2008/09/?day=05', '/2008/09/05/'), 238 array( '/2008/?monthnum=9', '/2008/09/'), 239 240 array( '/?year=2008', '/2008/'), 241 242 // Authors 243 array( '/?author=%d', '/author/canonical-author/' ), 244 // array( '/?author=%d&year=2008', '/2008/?author=3'), 245 array( '/?author=%d&year=2008', '/author/canonical-author/?year=2008', 17661 ), 246 // array( '/author/canonical-author/?year=2008', '/2008/?author=3'), //Either or, see previous testcase. 247 array( '/author/canonical-author/?year=2008', '/author/canonical-author/?year=2008', 17661 ), 248 249 // Feeds 250 array( '/?feed=atom', '/feed/atom/' ), 251 array( '/?feed=rss2', '/feed/' ), 252 array( '/?feed=comments-rss2', '/comments/feed/'), 253 array( '/?feed=comments-atom', '/comments/feed/atom/'), 254 255 // Feeds (per-post) 256 array( '/2008/03/03/comment-test/?feed=comments-atom', '/2008/03/03/comment-test/feed/atom/'), 257 array( '/?p=149&feed=comments-atom', '/2008/03/03/comment-test/feed/atom/'), 258 array( '/2008/03/03/comment-test/?feed=comments-atom', '/2008/03/03/comment-test/feed/atom/' ), 259 260 // Index 261 array( '/?paged=1', '/' ), 262 array( '/page/1/', '/' ), 263 array( '/page1/', '/' ), 264 array( '/?paged=2', '/page/2/' ), 265 array( '/page2/', '/page/2/' ), 266 267 // Misc 268 array( '/2008%20', '/2008' ), 269 array( '//2008////', '/2008/' ), 270 271 // Todo: Endpoints (feeds, trackbacks, etc), More fuzzed mixed query variables, comment paging, Home page (Static) 272 ); 273 } 274 } 3 require_once dirname( dirname( __FILE__ ) ) . '/canonical.php'; 275 4 276 5 /** … … 279 8 * @group query 280 9 */ 281 class WP_Canonical_PageOnFront extends WP_Test_Canonical { 282 function setUp() { 283 parent::setUp(); 284 global $wp_rewrite; 285 update_option( 'show_on_front', 'page' ); 286 update_option( 'page_for_posts', $this->factory->post->create( array( 'post_title' => 'blog-page', 'post_type' => 'page' ) ) ); 287 update_option( 'page_on_front', $this->factory->post->create( array( 'post_title' => 'front-page', 'post_type' => 'page' ) ) ); 288 $wp_rewrite->init(); 289 flush_rewrite_rules(); 290 } 291 292 function data() { 293 /* Format: 294 * [0]: $test_url, 295 * [1]: expected results: Any of the following can be used 296 * array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET ); 297 * array( expected query vars to be set, same as 'qv' above ) 298 * (string) expected redirect location 299 * [3]: (optional) The ticket the test refers to, Can be skipped if unknown. 300 */ 301 return array( 302 // Check against an odd redirect 303 array( '/page/2/', '/page/2/', 20385 ), 304 // The page designated as the front page should redirect to the front of the site 305 array( '/front-page/', '/' ), 306 array( '/blog-page/?paged=2', '/blog-page/page/2/' ), 307 ); 308 } 309 } 310 311 /** 312 * @group canonical 313 * @group rewrite 314 * @group query 315 */ 316 class WP_Canonical_CustomRules extends WP_Test_Canonical { 10 class Tests_Canonical_CustomRules extends Tests_Canonical { 317 11 function setUp() { 318 12 parent::setUp(); … … 340 34 } 341 35 } 342 343 /**344 * @group canonical345 * @group rewrite346 * @group query347 */348 class WP_Canonical_NoRewrite extends WP_Test_Canonical {349 350 var $structure = '';351 352 // These test cases are run against the test handler in WP_Canonical353 354 function data() {355 /* Format:356 * [0]: $test_url,357 * [1]: expected results: Any of the following can be used358 * array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET );359 * array( expected query vars to be set, same as 'qv' above )360 * (string) expected redirect location361 * [3]: (optional) The ticket the test refers to, Can be skipped if unknown.362 */363 return array(364 array( '/?p=123', '/?p=123' ),365 366 // This post_type arg should be stripped, because p=1 exists, and does not have post_type= in its query string367 array( '/?post_type=fake-cpt&p=1', '/?p=1' ),368 369 // Strip an existing but incorrect post_type arg370 array( '/?post_type=page&page_id=1', '/?p=1' ),371 372 array( '/?p=358 ', array('url' => '/?p=358', 'qv' => array('p' => '358') ) ), // Trailing spaces373 array( '/?p=358%20', array('url' => '/?p=358', 'qv' => array('p' => '358') ) ),374 375 array( '/?page_id=1', '/?p=1' ), // redirect page_id to p (should cover page_id|p|attachment_id to one another376 array( '/?page_id=1&post_type=revision', '/?p=1' ),377 378 );379 }380 }
Note: See TracChangeset
for help on using the changeset viewer.