Changeset 849 in tests
- Timestamp:
- 07/02/2012 06:16:05 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-testcase/test_includes_canonical.php
r669 r849 8 8 */ 9 9 10 class WP_ Canonical extends _WPDataset2{10 class WP_Test_Canonical extends WP_UnitTestCase { 11 11 12 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 13 var $structure = '/%year%/%monthnum%/%day%/%postname%/'; 14 14 15 function SetUp() { 16 parent::SetUp(); 17 18 update_option('permalink_structure', $this->structure); 19 update_option('comments_per_page', 5); 20 21 global $wp_rewrite; 22 $wp_rewrite->set_permalink_structure($this->structure); 15 var $old_current_user; 16 var $author_id; 17 var $post_ids; 18 var $term_ids; 19 20 function setUp() { 21 parent::setUp(); 22 23 update_option( 'comments_per_page', 5 ); 24 update_option( 'posts_per_page', 5 ); 25 26 update_option( 'permalink_structure', $this->structure ); 23 27 create_initial_taxonomies(); 24 25 $wp_rewrite->flush_rules(); 28 $GLOBALS['wp_rewrite']->init(); 29 flush_rewrite_rules(); 30 31 $this->old_current_user = get_current_user_id(); 32 $this->author_id = $this->factory->user->create( array( 'user_login' => 'canonical-author' ) ); 33 wp_set_current_user( $this->author_id ); 34 35 // Already created by install defaults: 36 // $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) ); 37 38 $this->term_ids = array(); 39 40 $this->factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02' ) ); 41 $post_id = $this->factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10' ) ); 42 $this->factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) ); 43 44 $this->factory->post->create( array( 45 'post_title' => 'images-test', 46 'post_date' => '2008-09-03', 47 'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3' 48 ) ); 49 50 $post_id = $this->factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03' ) ); 51 $this->factory->comment->create_post_comments( $post_id, 15 ); 52 53 $this->factory->post->create( array( 'post_date' => '2008-09-05' ) ); 54 55 $this->factory->post->create( array( 'import_id' => 123 ) ); 56 $this->factory->post->create( array( 'import_id' => 1 ) ); 57 $this->factory->post->create( array( 'import_id' => 358 ) ); 58 59 $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) ); 60 $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) ); 61 $post_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) ); 62 $this->factory->post->create( 63 array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id, 64 ) ); 65 66 $this->term_ids['/category/parent/'] = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) ); 67 $this->term_ids['/category/parent/child-1/'] = $this->factory->term->create( array( 68 'taxonomy' => 'category', 'name' => 'child-1', 'parent' => $this->term_ids['/category/parent/'], 69 ) ); 70 $this->term_ids['/category/parent/child-1/child-2/'] = $this->factory->term->create( array( 71 'taxonomy' => 'category', 'name' => 'child-2', 'parent' => $this->term_ids['/category/parent/child-1/'], 72 ) ); 73 74 $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) ); 75 $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) ); 76 77 $this->factory->term->create( array( 'name' => 'post-formats' ) ); 26 78 } 27 79 28 80 function tearDown() { 29 81 parent::tearDown(); 30 31 delete_option('permalink_structure'); 32 33 global $wp_rewrite; 34 $wp_rewrite->set_permalink_structure(''); 35 $wp_rewrite->flush_rules(); 36 37 $_GET = array(); 82 wp_set_current_user( $this->old_current_user ); 83 84 $GLOBALS['wp_rewrite']->init(); 38 85 } 39 86 … … 41 88 // Return url's are full url's with the prepended home. 42 89 function get_canonical($test_url) { 43 $can_url = redirect_canonical( get_option('home') . $test_url, false); 44 if ( empty($can_url) ) 45 return get_option('home') . $test_url; // No redirect will take place for this request 90 $test_url = home_url( $test_url ); 91 92 $can_url = redirect_canonical( $test_url, false ); 93 if ( ! $can_url ) 94 return $test_url; // No redirect will take place for this request 46 95 47 96 return $can_url; … … 56 105 57 106 $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null; 58 107 59 108 if ( is_string($expected) ) 60 109 $expected = array('url' => $expected); … … 65 114 $this->markTestSkipped('No valid expected output was provided'); 66 115 67 $this->http( get_option('home') . $test_url ); 116 if ( false !== strpos( $test_url, '%d' ) ) { 117 if ( false !== strpos( $test_url, '/?author=%d' ) ) 118 $test_url = sprintf( $test_url, $this->author_id ); 119 if ( false !== strpos( $test_url, '?cat=%d' ) ) 120 $test_url = sprintf( $test_url, $this->term_ids[ $expected['url'] ] ); 121 } 122 123 $this->go_to( home_url( $test_url ) ); 68 124 69 125 // Does the redirect match what's expected? … … 75 131 $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref ); 76 132 77 if ( isset($expected['qv']) ) { 78 79 // "make" that the request and check the query is correct 80 $this->http( $can_url ); 81 82 // Are all query vars accounted for, And correct? 83 global $wp; 84 85 $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars); 86 if ( !empty($parsed_can_url['query']) ) { 87 parse_str($parsed_can_url['query'], $_qv); 88 89 // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite) 90 $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref ); 91 92 $query_vars = array_merge($query_vars, $_qv); 93 } 94 95 $this->assertEquals( $expected['qv'], $query_vars ); 96 } //isset $expected['qv'] 97 133 if ( ! isset($expected['qv']) ) 134 return; 135 136 // "make" that the request and check the query is correct 137 $this->go_to( $can_url ); 138 139 // Are all query vars accounted for, And correct? 140 global $wp; 141 142 $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars); 143 if ( !empty($parsed_can_url['query']) ) { 144 parse_str($parsed_can_url['query'], $_qv); 145 146 // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite) 147 $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref ); 148 149 $query_vars = array_merge($query_vars, $_qv); 150 } 151 152 $this->assertEquals( $expected['qv'], $query_vars ); 98 153 } 99 154 … … 111 166 return array( 112 167 // Categories 113 array( '?cat=32', '/category/parent/', 15256 ), 114 array( '?cat=50', '/category/parent/child-1/', 15256 ), 115 array( '?cat=51', '/category/parent/child-1/child-2/' ), // no children 168 169 array( '?cat=%d', '/category/parent/', 15256 ), 170 array( '?cat=%d', '/category/parent/child-1/', 15256 ), 171 array( '?cat=%d', '/category/parent/child-1/child-2/' ), // no children 116 172 array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ), 117 173 array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ), … … 184 240 185 241 // Authors 186 array( '/?author= 3', '/author/chip-bennett/' ),187 // array( '/?author= 3&year=2008', '/2008/?author=3'),188 array( '/?author= 3&year=2008', '/author/chip-bennett/?year=2008', 17661 ),189 // array( '/author/c hip-bennett/?year=2008', '/2008/?author=3'), //Either or, see previous testcase.190 array( '/author/c hip-bennett/?year=2008', '/author/chip-bennett/?year=2008', 17661 ),242 array( '/?author=%d', '/author/canonical-author/' ), 243 // array( '/?author=%d&year=2008', '/2008/?author=3'), 244 array( '/?author=%d&year=2008', '/author/canonical-author/?year=2008', 17661 ), 245 // array( '/author/canonical-author/?year=2008', '/2008/?author=3'), //Either or, see previous testcase. 246 array( '/author/canonical-author/?year=2008', '/author/canonical-author/?year=2008', 17661 ), 191 247 192 248 // Feeds … … 213 269 214 270 // Todo: Endpoints (feeds, trackbacks, etc), More fuzzed mixed query variables, comment paging, Home page (Static) 215 216 271 ); 217 272 } 218 273 } 219 274 220 class WP_Canonical_PageOnFront extends WP_Canonical { 221 var $special_pages = array(); 222 223 function SetUp() { 275 class WP_Canonical_PageOnFront extends WP_Test_Canonical { 276 function setUp() { 277 parent::setUp(); 224 278 global $wp_rewrite; 225 parent::SetUp();226 $this->special_pages['blog' ] = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => 'blog-page' ) );227 $this->special_pages['front'] = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => 'front-page' ) );228 279 update_option( 'show_on_front', 'page' ); 229 update_option( 'page_for_posts', $this->special_pages['blog'] ); 230 update_option( 'page_on_front', $this->special_pages['front'] ); 231 $wp_rewrite->flush_rules(); 232 } 233 234 function tearDown() { 235 parent::tearDown(); 236 update_option( 'show_on_front', 'posts' ); 237 238 delete_option( 'page_for_posts' ); 239 delete_option( 'page_on_front' ); 240 delete_option( 'permalink_structure' ); 241 242 foreach ( $this->special_pages as $p ) 243 wp_delete_post( $p ); 244 global $wp_rewrite; 245 $wp_rewrite->set_permalink_structure(''); 246 $wp_rewrite->flush_rules(); 247 248 $_GET = array(); 280 update_option( 'page_for_posts', $this->factory->post->create( array( 'post_title' => 'blog-page', 'post_type' => 'page' ) ) ); 281 update_option( 'page_on_front', $this->factory->post->create( array( 'post_title' => 'front-page', 'post_type' => 'page' ) ) ); 282 $wp_rewrite->init(); 283 flush_rewrite_rules(); 249 284 } 250 285 … … 259 294 */ 260 295 return array( 261 // Check against an odd redirect : #20385262 array( '/page/2/', '/page/2/' ),296 // Check against an odd redirect 297 array( '/page/2/', '/page/2/', 20385 ), 263 298 // The page designated as the front page should redirect to the front of the site 264 299 array( '/front-page/', '/' ), … … 268 303 } 269 304 270 class WP_Canonical_CustomRules extends WP_Canonical { 271 function SetUp() { 305 class WP_Canonical_CustomRules extends WP_Test_Canonical { 306 function setUp() { 307 parent::setUp(); 272 308 global $wp_rewrite; 273 parent::SetUp();274 309 // Add a custom Rewrite rule to test category redirections. 275 310 $wp_rewrite->add_rule('ccr/(.+?)/sort/(asc|desc)', 'index.php?category_name=$matches[1]&order=$matches[2]', 'top'); // ccr = Custom_Cat_Rule … … 295 330 } 296 331 297 class WP_Canonical_NoRewrite extends WP_ Canonical {332 class WP_Canonical_NoRewrite extends WP_Test_Canonical { 298 333 299 334 var $structure = '';
Note: See TracChangeset
for help on using the changeset viewer.