Ticket #66: canonical.diff
| File canonical.diff, 11.8 KB (added by nacin, 11 months ago) |
|---|
-
wp-testcase/test_includes_canonical.php
7 7 * 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(); 15 var $old_current_user; 16 var $user_id; 17 var $post_ids; 18 var $term_ids; 17 19 18 update_option('permalink_structure', $this->structure);19 update_option('comments_per_page', 5);20 function setUp() { 21 parent::setUp(); 20 22 23 update_option( 'comments_per_page', 5 ); 24 21 25 global $wp_rewrite; 22 $wp_rewrite->set_permalink_structure( $this->structure);26 $wp_rewrite->set_permalink_structure( $this->structure ); 23 27 create_initial_taxonomies(); 24 28 25 $wp_rewrite->flush_rules(); 29 flush_rewrite_rules(); 30 31 $this->old_current_user = get_current_user_id(); 32 $this->user_id = $this->factory->user->create( array( 'user_login' => 'canonical-author' ) ); 33 wp_set_current_user( $user_id ); 34 35 $this->factory->term->create( array( 36 'taxonomy' => 'category', 'name' => 'uncategorized', 37 ) ); 38 39 $this->post_ids = $this->term_ids = array(); 40 $this->post_ids['post-format-test-audio'] = $this->factory->post->create( array( 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02' ) ); 41 $this->post_ids['post-format-test-gallery'] = $this->factory->post->create( array( 'post_title' => 'post-format-test-gallery' ) ); 42 $this->factory->post->create( array( 43 'post_title' => 'images-test', 44 'post_date' => '2008-09-03', 45 'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3' 46 ) ); 47 $this->post_ids['comment-test'] = $this->factory->post->create( array( 'post_title' => 'comment-test', 'post_date' => '2008-03-03' ) ); 48 $this->factory->post->create( array( 'post_date' => '2008-09-05' ) ); 49 50 $this->post_ids['p123'] = $this->factory->post->create(); 51 $this->post_ids['p1'] = $this->factory->post->create(); 52 $this->post_ids['p358'] = $this->factory->post->create(); 53 54 $this->factory->comment->create_post_comments( $post_ids['comment_test'], 15 ); 55 56 $this->post_ids['canola2'] = $this->factory->post->create( array( 57 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_ids['post-format-test-gallery'], 58 ) ); 59 60 $this->post_ids['sample-page'] = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) ); 61 $this->post_ids['parent-page'] = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) ); 62 $this->post_ids['about'] = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) ); 63 $this->post_ids['child-page-1'] = $this->factory->post->create( 64 array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $this->post_ids['parent-page'], 65 ) ); 66 67 $this->term_ids['parent'] = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) ); 68 $this->term_ids['child-1'] = $this->factory->term->create( array( 69 'taxonomy' => 'category', 'name' => 'child-1', 'parent' => $this->term_ids['parent']['term_id'], 70 ) ); 71 $this->term_ids['child-2'] = $this->factory->term->create( array( 72 'taxonomy' => 'category', 'name' => 'child-2', 'parent' => $this->term_ids['child-1']['term_id'], 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 foreach ( $this->term_ids as &$term_id ) 80 $term_id = $term_id['term_id']; 81 unset( $term_id ); 26 82 } 27 83 28 84 function tearDown() { 29 85 parent::tearDown(); 30 31 delete_option('permalink_structure'); 32 86 wp_set_current_user( $this->old_current_user ); 33 87 global $wp_rewrite; 34 $wp_rewrite->set_permalink_structure(''); 35 $wp_rewrite->flush_rules(); 36 37 $_GET = array(); 88 $wp_rewrite->init(); 38 89 } 39 90 40 91 // URL's are relative to the site "front", ie. /category/uncategorized/ instead of http://site.../category.. 41 92 // Return url's are full url's with the prepended home. 42 93 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 94 $test_url = home_url( $test_url ); 46 95 96 $can_url = redirect_canonical( $test_url, false ); 97 if ( ! $can_url ) 98 return $test_url; // No redirect will take place for this request 99 47 100 return $can_url; 48 101 } 49 102 … … 64 117 if ( !isset($expected['url']) && !isset($expected['qv']) ) 65 118 $this->markTestSkipped('No valid expected output was provided'); 66 119 67 $this-> http( get_option('home') . $test_url);120 $this->go_to( home_url( $test_url ) ); 68 121 69 122 // Does the redirect match what's expected? 70 123 $can_url = $this->get_canonical( $test_url ); … … 74 127 if ( isset($expected['url']) ) 75 128 $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref ); 76 129 77 if ( isset($expected['qv']) ) { 130 if ( ! isset($expected['qv']) ) 131 return; 78 132 79 // "make" that the request and check the query is correct80 $this->http( $can_url );133 // "make" that the request and check the query is correct 134 $this->go_to( $can_url ); 81 135 82 // Are all query vars accounted for, And correct?83 global $wp;136 // Are all query vars accounted for, And correct? 137 global $wp; 84 138 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);139 $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars); 140 if ( !empty($parsed_can_url['query']) ) { 141 parse_str($parsed_can_url['query'], $_qv); 88 142 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 );143 // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite) 144 $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref ); 91 145 92 $query_vars = array_merge($query_vars, $_qv);93 }146 $query_vars = array_merge($query_vars, $_qv); 147 } 94 148 95 $this->assertEquals( $expected['qv'], $query_vars ); 96 } //isset $expected['qv'] 97 149 $this->assertEquals( $expected['qv'], $query_vars ); 98 150 } 99 151 100 152 function data() { … … 106 158 * (string) expected redirect location 107 159 * [2]: (optional) The ticket the test refers to, Can be skipped if unknown. 108 160 */ 109 161 var_dump( $this->term_ids ); 110 162 // 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. 111 163 return array( 112 164 // 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 165 array( '?cat=' . $this->term_ids['parent'], '/category/parent/', 15256 ), 166 array( '?cat=' . $this->term_ids['child-1'], '/category/parent/child-1/', 15256 ), 167 array( '?cat=' . $this->term_ids['child-2'], '/category/parent/child-1/child-2/' ), // no children 168 ); 169 return array( 116 170 array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ), 117 171 array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ), 118 172 array( '/category/uncategorized/?paged=2', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ), … … 217 271 } 218 272 } 219 273 220 class WP_Canonical_PageOnFront extends WP_Canonical { 221 var $special_pages = array(); 222 223 function SetUp() { 274 class WP_Canonical_PageOnFront extends WP_Test_Canonical { 275 function setUp() { 276 parent::setUp(); 224 277 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 278 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();279 update_option( 'page_for_posts', $this->factory->post->create( array( 'post_title' => 'blog-page' ) ) ); 280 update_option( 'page_on_front', $this->factory->post->create( array( 'post_title' => 'front-page' ) ) ); 281 flush_rewrite_rules(); 232 282 } 233 283 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();249 }250 251 284 function data() { 252 285 /* Format: 253 286 * [0]: $test_url, … … 267 300 } 268 301 } 269 302 270 class WP_Canonical_CustomRules extends WP_Canonical { 271 function SetUp() { 303 class WP_Canonical_CustomRules extends WP_Test_Canonical { 304 function setUp() { 305 parent::setUp(); 272 306 global $wp_rewrite; 273 parent::SetUp();274 307 // Add a custom Rewrite rule to test category redirections. 275 308 $wp_rewrite->add_rule('ccr/(.+?)/sort/(asc|desc)', 'index.php?category_name=$matches[1]&order=$matches[2]', 'top'); // ccr = Custom_Cat_Rule 276 309 $wp_rewrite->flush_rules(); … … 294 327 } 295 328 } 296 329 297 class WP_Canonical_NoRewrite extends WP_ Canonical {330 class WP_Canonical_NoRewrite extends WP_Test_Canonical { 298 331 299 332 var $structure = ''; 300 333 -
wp-testlib/factory.php
5 5 $this->post = new WP_UnitTest_Factory_For_Post( $this ); 6 6 $this->comment = new WP_UnitTest_Factory_For_Comment( $this ); 7 7 $this->user = new WP_UnitTest_Factory_For_User( $this ); 8 $this->term = new WP_UnitTest_Factory_For_Term( $this ); 8 9 if ( is_multisite() ) 9 10 $this->blog = new WP_UnitTest_Factory_For_Blog( $this ); 10 11 } … … 102 103 function update_object( $blog_id, $fields ) {} 103 104 } 104 105 106 107 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing { 108 109 function __construct( $factory = null ) { 110 parent::__construct( $factory ); 111 $this->default_generation_definitions = array( 112 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ), 113 'taxonomy' => 'post_tag', 114 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ), 115 ); 116 } 117 118 function create_object( $args ) { 119 return wp_insert_term( $args['name'], $args['taxonomy'], $args ); 120 } 121 122 function update_object( $term, $fields ) { 123 if ( is_object( $term ) ) 124 $taxonomy = $term->taxonomy; 125 elseif ( isset( $fields['taxonomy'] ) ) 126 $taxonomy = $fields['taxonomy']; 127 else 128 $taxonomy = 'post_tag'; 129 return wp_update_term( $term, $taxonomy, $fields ); 130 } 131 } 132 105 133 abstract class WP_UnitTest_Factory_For_Thing { 106 134 107 135 var $default_generation_definitions;
