Make WordPress Core

Changeset 109 in tests


Ignore:
Timestamp:
12/04/2007 11:48:13 AM (18 years ago)
Author:
tellyworth
Message:

simplify query tests, add more

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_query.php

    r96 r109  
    33// test the is_*() functions in query.php across the URL structure
    44
    5 class TestWPQuery extends _WPSmallBlog {
     5class TestWPQuery extends _WPDataset1 {
     6    var $use_verbose_page_rules = true;
     7   
    68    function setUp() {
    79        parent::setUp();
    810        global $wp_rewrite;
    911        $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
     12        $wp_rewrite->flush_rules();
     13        $wp_rewrite->use_verbose_page_rules = $this->use_verbose_page_rules;
     14       
    1015    }
    1116
     
    1621    }
    1722
     23    // this will check each of the wp_query is_* functions/properties
     24    // any that are listed by name as parameters will be asserted as True; any others will be asserted False
     25    // e.g. assertQueryTrue('is_single', 'is_feed') means is_single() and is_feed() must be true, and
     26    // everything else must be false
     27    function assertQueryTrue(/* .. */) {
     28        $all = array(
     29            'is_admin', 'is_archive', 'is_attachment', 'is_author', 'is_category', 'is_tag', 'is_comments_popup', 'is_date',
     30            'is_day', 'is_feed', 'is_home', 'is_month', 'is_page', 'is_paged', 'is_plugin_page', 'is_preview', 'is_robots',
     31            'is_search', 'is_single', 'is_time', 'is_trackback', 'is_year', 'is_404', 'is_comment_feed',
     32            );
     33
     34        $true = func_get_args();
     35
     36        global $wp_query;
     37        foreach ($all as $query_thing) {
     38            if (is_callable($query_thing))
     39                $result = call_user_func($query_thing);
     40            else
     41                $result = $wp_query->$query_thing;
     42
     43            if (in_array($query_thing, $true))
     44                $this->assertTrue($result, "$query_thing should be true");
     45            else
     46                $this->assertFalse($result, "$query_thing should be false");
     47        }
     48    }
     49
    1850    function test_home() {
    1951        $this->http('/');
    2052
    21         $this->assertFalse( is_admin() );
    22         $this->assertFalse( is_archive() );
    23         $this->assertFalse( is_attachment() );
    24         $this->assertFalse( is_author() );
    25         $this->assertFalse( is_category() );
    26         $this->assertFalse( is_tag());
    27         $this->assertFalse( is_comments_popup() );
    28         $this->assertFalse( is_date() );
    29         $this->assertFalse( is_day() );
    30         $this->assertFalse( is_feed() );
    31         $this->assertTrue( is_home() );
    32         $this->assertFalse( is_month() );
    33         $this->assertFalse( is_page() );
    34         $this->assertFalse( is_paged() );
    35         $this->assertFalse( is_plugin_page() );
    36         $this->assertFalse( is_preview() );
    37         $this->assertFalse( is_robots() );
    38         $this->assertFalse( is_search() );
    39         $this->assertFalse( is_single() );
    40         $this->assertFalse( is_singular() );
    41         $this->assertFalse( is_time() );
    42         $this->assertFalse( is_trackback() );
    43         $this->assertFalse( is_year() );
    44         $this->assertFalse( is_404() );
    45 
    46         global $wp_query;
    47         $this->assertFalse( $wp_query->is_comment_feed );
     53        $this->assertQueryTrue('is_home');
    4854    }
    4955
     
    5157        $this->http('/'.rand_str());
    5258
    53         $this->assertFalse( is_admin() );
    54         $this->assertFalse( is_archive() );
    55         $this->assertFalse( is_attachment() );
    56         $this->assertFalse( is_author() );
    57         $this->assertFalse( is_category() );
    58         $this->assertFalse( is_tag());
    59         $this->assertFalse( is_comments_popup() );
    60         $this->assertFalse( is_date() );
    61         $this->assertFalse( is_day() );
    62         $this->assertFalse( is_feed() );
    63         $this->assertFalse( is_home() );
    64         $this->assertFalse( is_month() );
    65         $this->assertFalse( is_page() );
    66         $this->assertFalse( is_paged() );
    67         $this->assertFalse( is_plugin_page() );
    68         $this->assertFalse( is_preview() );
    69         $this->assertFalse( is_robots() );
    70         $this->assertFalse( is_search() );
    71         $this->assertFalse( is_single() );
    72         $this->assertFalse( is_singular() );
    73         $this->assertFalse( is_time() );
    74         $this->assertFalse( is_trackback() );
    75         $this->assertFalse( is_year() );
    76         $this->assertTrue( is_404() );
    77 
    78         global $wp_query;
    79         $this->assertFalse( $wp_query->is_comment_feed );
     59        $this->assertQueryTrue('is_404');
    8060    }
    8161
    8262    function test_permalink() {
    83         $this->http( get_permalink($this->post_ids[0]) );
     63        $this->http( get_permalink($this->_get_post_id_by_name('hello-world')) );
    8464
    85         $this->assertFalse( is_admin() );
    86         $this->assertFalse( is_archive() );
    87         $this->assertFalse( is_attachment() );
    88         $this->assertFalse( is_author() );
    89         $this->assertFalse( is_category() );
    90         $this->assertFalse( is_tag());
    91         $this->assertFalse( is_comments_popup() );
    92         $this->assertFalse( is_date() );
    93         $this->assertFalse( is_day() );
    94         $this->assertFalse( is_feed() );
    95         $this->assertFalse( is_home() );
    96         $this->assertFalse( is_month() );
    97         $this->assertFalse( is_page() );
    98         $this->assertFalse( is_paged() );
    99         $this->assertFalse( is_plugin_page() );
    100         $this->assertFalse( is_preview() );
    101         $this->assertFalse( is_robots() );
    102         $this->assertFalse( is_search() );
    103         $this->assertTrue( is_single() );
    104         $this->assertTrue( is_singular() );
    105         $this->assertFalse( is_time() );
    106         $this->assertFalse( is_trackback() );
    107         $this->assertFalse( is_year() );
    108         $this->assertFalse( is_404() );
    109 
    110         global $wp_query;
    111         $this->assertFalse( $wp_query->is_comment_feed );
     65        $this->assertQueryTrue('is_single', 'is_singular');
    11266    }
    11367
    11468    function test_main_feed() {
    115        
     69
    11670        $types = array('rss2', 'rss', 'atom');
    11771        foreach ($types as $type) {
    11872            $this->http(get_feed_link($type));
    119 
    120             $this->assertFalse( is_admin() );
    121             $this->assertFalse( is_archive() );
    122             $this->assertFalse( is_attachment() );
    123             $this->assertFalse( is_author() );
    124             $this->assertFalse( is_category() );
    125             $this->assertFalse( is_tag());
    126             $this->assertFalse( is_comments_popup() );
    127             $this->assertFalse( is_date() );
    128             $this->assertFalse( is_day() );
    129             $this->assertTrue( is_feed() );
    130             $this->assertFalse( is_home() );
    131             $this->assertFalse( is_month() );
    132             $this->assertFalse( is_page() );
    133             $this->assertFalse( is_paged() );
    134             $this->assertFalse( is_plugin_page() );
    135             $this->assertFalse( is_preview() );
    136             $this->assertFalse( is_robots() );
    137             $this->assertFalse( is_search() );
    138             $this->assertFalse( is_single() );
    139             $this->assertFalse( is_singular() );
    140             $this->assertFalse( is_time() );
    141             $this->assertFalse( is_trackback() );
    142             $this->assertFalse( is_year() );
    143             $this->assertFalse( is_404() );
    144 
    145             global $wp_query;
    146             $this->assertFalse( $wp_query->is_comment_feed );
     73            $this->assertQueryTrue('is_feed');
    14774        }
    14875    }
    14976
    15077    function test_post_comments_feed() {
    151         $GLOBALS['query_debug'] = true;
    152         $this->http(get_post_comments_feed_link($this->post_ids[0]));
    153         $GLOBALS['query_debug'] = false;
     78        $this->http(get_post_comments_feed_link($this->_get_post_id_by_name('hello-world')));
    15479
    155         $this->assertFalse( is_admin() );
    156         $this->assertFalse( is_archive() );
    157         $this->assertFalse( is_attachment() );
    158         $this->assertFalse( is_author() );
    159         $this->assertFalse( is_category() );
    160         $this->assertFalse( is_tag());
    161         $this->assertFalse( is_comments_popup() );
    162         $this->assertFalse( is_date() );
    163         $this->assertFalse( is_day() );
    164         $this->assertTrue( is_feed() );
    165         $this->assertFalse( is_home() );
    166         $this->assertFalse( is_month() );
    167         $this->assertFalse( is_page() );
    168         $this->assertFalse( is_paged() );
    169         $this->assertFalse( is_plugin_page() );
    170         $this->assertFalse( is_preview() );
    171         $this->assertFalse( is_robots() );
    172         $this->assertFalse( is_search() );
    173         $this->assertTrue( is_single() );
    174         $this->assertTrue( is_singular() );
    175         $this->assertFalse( is_time() );
    176         $this->assertFalse( is_trackback() );
    177         $this->assertFalse( is_year() );
    178         $this->assertFalse( is_404() );
    179 
    180         global $wp_query;
    181         $this->assertTrue( $wp_query->is_comment_feed );
     80        $this->assertQueryTrue('is_feed', 'is_single', 'is_singular', 'is_comment_feed');
    18281    }
    18382
     83    function _get_post_id_by_name($name) {
     84        global $wpdb;
     85        $name = $wpdb->escape($name);
     86        $page_id = $wpdb->get_var("SELECT ID from {$wpdb->posts} WHERE post_name = '{$name}' LIMIT 1");
     87        assert(is_numeric($page_id));
     88        return $page_id;
     89    }
     90
     91    function test_page() {
     92        $page_id = $this->_get_post_id_by_name('about');
     93        $this->http(get_permalink($page_id));
     94       
     95        $this->assertQueryTrue('is_page');
     96    }
     97
     98    function test_parent_page() {
     99        $page_id = $this->_get_post_id_by_name('parent-page');
     100        $this->http(get_permalink($page_id));
     101
     102        $this->assertQueryTrue('is_page');
     103    }
     104   
     105    function test_child_page_1() {
     106        $page_id = $this->_get_post_id_by_name('child-page-1');
     107        $this->http(get_permalink($page_id));
     108
     109        $this->assertQueryTrue('is_page');
     110    }
     111   
     112    function test_child_page_2() {
     113        $page_id = $this->_get_post_id_by_name('child-page-2');
     114        $this->http(get_permalink($page_id));
     115
     116        $this->assertQueryTrue('is_page');
     117    }
     118       
    184119}
    185120
     121class TestWPQueryShortPageRules extends TestWPQuery {
     122    var $use_verbose_page_rules = false;
     123}
     124
     125
    186126?>
Note: See TracChangeset for help on using the changeset viewer.