Ticket #66: canonical.diff

File canonical.diff, 11.8 KB (added by nacin, 11 months ago)
  • wp-testcase/test_includes_canonical.php

     
    77 * 
    88 */ 
    99 
    10 class WP_Canonical extends _WPDataset2 { 
     10class WP_Test_Canonical extends WP_UnitTestCase { 
    1111 
    1212        // 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 
    1313        var $structure = '/%year%/%monthnum%/%day%/%postname%/'; 
    1414 
    15         function SetUp() { 
    16                 parent::SetUp(); 
     15        var $old_current_user; 
     16        var $user_id; 
     17        var $post_ids; 
     18        var $term_ids; 
    1719 
    18                 update_option('permalink_structure', $this->structure); 
    19                 update_option('comments_per_page', 5); 
     20        function setUp() { 
     21                parent::setUp(); 
    2022 
     23                update_option( 'comments_per_page', 5 ); 
     24 
    2125                global $wp_rewrite; 
    22                 $wp_rewrite->set_permalink_structure($this->structure); 
     26                $wp_rewrite->set_permalink_structure( $this->structure ); 
    2327                create_initial_taxonomies(); 
    2428 
    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 ); 
    2682        } 
    2783 
    2884        function tearDown() { 
    2985                parent::tearDown(); 
    30  
    31                 delete_option('permalink_structure'); 
    32  
     86                wp_set_current_user( $this->old_current_user ); 
    3387                global $wp_rewrite; 
    34                 $wp_rewrite->set_permalink_structure(''); 
    35                 $wp_rewrite->flush_rules(); 
    36  
    37                 $_GET = array(); 
     88                $wp_rewrite->init(); 
    3889        } 
    3990 
    4091        // URL's are relative to the site "front", ie. /category/uncategorized/ instead of http://site.../category.. 
    4192        // Return url's are full url's with the prepended home. 
    4293        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 ); 
    4695 
     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 
    47100                return $can_url; 
    48101        } 
    49102 
     
    64117                if ( !isset($expected['url']) && !isset($expected['qv']) ) 
    65118                        $this->markTestSkipped('No valid expected output was provided'); 
    66119 
    67                 $this->http( get_option('home') . $test_url ); 
     120                $this->go_to( home_url( $test_url ) ); 
    68121 
    69122                // Does the redirect match what's expected? 
    70123                $can_url = $this->get_canonical( $test_url ); 
     
    74127                if ( isset($expected['url']) ) 
    75128                        $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref ); 
    76129 
    77                 if ( isset($expected['qv']) ) { 
     130                if ( ! isset($expected['qv']) ) 
     131                        return; 
    78132 
    79                         // "make" that the request and check the query is correct 
    80                         $this->http( $can_url ); 
     133                // "make" that the request and check the query is correct 
     134                $this->go_to( $can_url ); 
    81135 
    82                         // Are all query vars accounted for, And correct? 
    83                         global $wp; 
     136                // Are all query vars accounted for, And correct? 
     137                global $wp; 
    84138 
    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); 
    88142 
    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 ); 
    91145 
    92                                 $query_vars = array_merge($query_vars, $_qv); 
    93                         } 
     146                        $query_vars = array_merge($query_vars, $_qv); 
     147                } 
    94148 
    95                         $this->assertEquals( $expected['qv'], $query_vars ); 
    96                 } //isset $expected['qv'] 
    97  
     149                $this->assertEquals( $expected['qv'], $query_vars ); 
    98150        } 
    99151 
    100152        function data() { 
     
    106158                 *      (string) expected redirect location 
    107159                 * [2]: (optional) The ticket the test refers to, Can be skipped if unknown. 
    108160                 */ 
    109  
     161                var_dump( $this->term_ids ); 
    110162                // 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. 
    111163                return array( 
    112164                        // 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( 
    116170                        array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ), 
    117171                        array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ), 
    118172                        array( '/category/uncategorized/?paged=2', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ), 
     
    217271        } 
    218272} 
    219273 
    220 class WP_Canonical_PageOnFront extends WP_Canonical { 
    221         var $special_pages = array(); 
    222  
    223         function SetUp() { 
     274class WP_Canonical_PageOnFront extends WP_Test_Canonical { 
     275        function setUp() { 
     276                parent::setUp(); 
    224277                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' ) ); 
    228278                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(); 
    232282        } 
    233283 
    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  
    251284        function data() { 
    252285                /* Format: 
    253286                 * [0]: $test_url, 
     
    267300        } 
    268301} 
    269302 
    270 class WP_Canonical_CustomRules extends WP_Canonical { 
    271         function SetUp() { 
     303class WP_Canonical_CustomRules extends WP_Test_Canonical { 
     304        function setUp() { 
     305                parent::setUp(); 
    272306                global $wp_rewrite; 
    273                 parent::SetUp(); 
    274307                // Add a custom Rewrite rule to test category redirections. 
    275308                $wp_rewrite->add_rule('ccr/(.+?)/sort/(asc|desc)', 'index.php?category_name=$matches[1]&order=$matches[2]', 'top'); // ccr = Custom_Cat_Rule 
    276309                $wp_rewrite->flush_rules(); 
     
    294327        } 
    295328} 
    296329 
    297 class WP_Canonical_NoRewrite extends WP_Canonical { 
     330class WP_Canonical_NoRewrite extends WP_Test_Canonical { 
    298331 
    299332        var $structure = ''; 
    300333 
  • wp-testlib/factory.php

     
    55                $this->post = new WP_UnitTest_Factory_For_Post( $this ); 
    66                $this->comment = new WP_UnitTest_Factory_For_Comment( $this ); 
    77                $this->user = new WP_UnitTest_Factory_For_User( $this ); 
     8                $this->term = new WP_UnitTest_Factory_For_Term( $this ); 
    89                if ( is_multisite() ) 
    910                        $this->blog = new WP_UnitTest_Factory_For_Blog( $this ); 
    1011        } 
     
    102103        function update_object( $blog_id, $fields ) {} 
    103104} 
    104105 
     106 
     107class 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 
    105133abstract class WP_UnitTest_Factory_For_Thing { 
    106134 
    107135        var $default_generation_definitions;