Make WordPress Core

Changeset 37464


Ignore:
Timestamp:
05/19/2016 02:22:59 AM (9 years ago)
Author:
boonebgorges
Message:

Move wp_dropdown_categories() tests into their own file.

See #31909.

Location:
trunk/tests/phpunit/tests
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/category.php

    r35242 r37464  
    243243        $this->assertNull( get_category_by_path( 'nocat/nocat/', false) );
    244244    }
    245 
    246     /**
    247      * @ticket 30306
    248      */
    249     public function test_wp_dropdown_categories_value_field_should_default_to_term_id() {
    250         // Create a test category.
    251         $cat_id = self::factory()->category->create( array(
    252             'name' => 'Test Category',
    253             'slug' => 'test_category',
    254         ) );
    255 
    256         // Get the default functionality of wp_dropdown_categories().
    257         $dropdown_default = wp_dropdown_categories( array(
    258             'echo' => 0,
    259             'hide_empty' => 0,
    260         ) );
    261 
    262         // Test to see if it returns the default with the category ID.
    263         $this->assertContains( 'value="' . $cat_id . '"', $dropdown_default );
    264     }
    265 
    266     /**
    267      * @ticket 30306
    268      */
    269     public function test_wp_dropdown_categories_value_field_term_id() {
    270         // Create a test category.
    271         $cat_id = self::factory()->category->create( array(
    272             'name' => 'Test Category',
    273             'slug' => 'test_category',
    274         ) );
    275 
    276         // Get the default functionality of wp_dropdown_categories().
    277         $found = wp_dropdown_categories( array(
    278             'echo' => 0,
    279             'hide_empty' => 0,
    280             'value_field' => 'term_id',
    281         ) );
    282 
    283         // Test to see if it returns the default with the category ID.
    284         $this->assertContains( 'value="' . $cat_id . '"', $found );
    285     }
    286 
    287     /**
    288      * @ticket 30306
    289      */
    290     public function test_wp_dropdown_categories_value_field_slug() {
    291         // Create a test category.
    292         $cat_id = self::factory()->category->create( array(
    293             'name' => 'Test Category',
    294             'slug' => 'test_category',
    295         ) );
    296 
    297         // Get the default functionality of wp_dropdown_categories().
    298         $found = wp_dropdown_categories( array(
    299             'echo' => 0,
    300             'hide_empty' => 0,
    301             'value_field' => 'slug',
    302         ) );
    303 
    304         // Test to see if it returns the default with the category slug.
    305         $this->assertContains( 'value="test_category"', $found );
    306     }
    307 
    308     /**
    309      * @ticket 30306
    310      */
    311     public function test_wp_dropdown_categories_value_field_should_fall_back_on_term_id_when_an_invalid_value_is_provided() {
    312         // Create a test category.
    313         $cat_id = self::factory()->category->create( array(
    314             'name' => 'Test Category',
    315             'slug' => 'test_category',
    316         ) );
    317 
    318         // Get the default functionality of wp_dropdown_categories().
    319         $found = wp_dropdown_categories( array(
    320             'echo' => 0,
    321             'hide_empty' => 0,
    322             'value_field' => 'foo',
    323         ) );
    324 
    325         // Test to see if it returns the default with the category slug.
    326         $this->assertContains( 'value="' . $cat_id . '"', $found );
    327     }
    328 
    329     /**
    330      * @ticket 32330
    331      */
    332     public function test_wp_dropdown_categories_selected_should_respect_custom_value_field() {
    333         $c1 = self::factory()->category->create( array(
    334             'name' => 'Test Category 1',
    335             'slug' => 'test_category_1',
    336         ) );
    337 
    338         $c2 = self::factory()->category->create( array(
    339             'name' => 'Test Category 2',
    340             'slug' => 'test_category_2',
    341         ) );
    342 
    343         $found = wp_dropdown_categories( array(
    344             'echo' => 0,
    345             'hide_empty' => 0,
    346             'value_field' => 'slug',
    347             'selected' => 'test_category_2',
    348         ) );
    349 
    350         $this->assertContains( "value=\"test_category_2\" selected=\"selected\"", $found );
    351     }
    352 
    353     /**
    354      * @ticket 33452
    355      */
    356     public function test_wp_dropdown_categories_show_option_all_should_be_selected_if_no_selected_value_is_explicitly_passed_and_value_field_does_not_have_string_values() {
    357         $cats = self::factory()->category->create_many( 3 );
    358 
    359         $found = wp_dropdown_categories( array(
    360             'echo' => 0,
    361             'hide_empty' => 0,
    362             'show_option_all' => 'Foo',
    363             'value_field' => 'slug',
    364         ) );
    365 
    366         $this->assertContains( "value='0' selected='selected'", $found );
    367 
    368         foreach ( $cats as $cat ) {
    369             $_cat = get_term( $cat, 'category' );
    370             $this->assertNotContains( 'value="' . $_cat->slug . '" selected="selected"', $found );
    371         }
    372     }
    373 
    374     /**
    375      * @ticket 33452
    376      */
    377     public function test_wp_dropdown_categories_show_option_all_should_be_selected_if_selected_value_of_0_string_is_explicitly_passed_and_value_field_does_not_have_string_values() {
    378         $cats = self::factory()->category->create_many( 3 );
    379 
    380         $found = wp_dropdown_categories( array(
    381             'echo' => 0,
    382             'hide_empty' => 0,
    383             'show_option_all' => 'Foo',
    384             'value_field' => 'slug',
    385             'selected' => '0',
    386         ) );
    387 
    388         $this->assertContains( "value='0' selected='selected'", $found );
    389 
    390         foreach ( $cats as $cat ) {
    391             $_cat = get_term( $cat, 'category' );
    392             $this->assertNotContains( 'value="' . $_cat->slug . '" selected="selected"', $found );
    393         }
    394     }
    395245}
  • trunk/tests/phpunit/tests/category/wpDropdownCategories.php

    r37463 r37464  
    11<?php
    22/**
    3  * Validate Category API
    4  *
    5  * Notes:
    6  * cat_is_ancestor_of is validated under test\term\term_is_ancestor_of
    7  *
     3 * @group taxonomy
    84 * @group category.php
    95 */
    10 class Tests_Category extends WP_UnitTestCase {
    11 
    12     function tearDown() {
    13         _unregister_taxonomy( 'test_tax_cat' );
    14         parent::tearDown();
    15     }
    16 
    17     /**
    18      * Validate get_all_category_ids
    19      *
    20      * @expectedDeprecated get_all_category_ids
    21      */
    22     function test_get_all_category_ids() {
    23         // create categories
    24         self::factory()->category->create_many( 2 );
    25 
    26         // create new taxonomy to ensure not included
    27         register_taxonomy( 'test_tax_cat', 'post' );
    28         wp_insert_term( "test1", 'test_tax_cat' );
    29 
    30         // Validate length is 1 + created due to uncategorized
    31         $cat_ids = get_all_category_ids();
    32         $this->assertEquals( 3, count($cat_ids));
    33     }
    34 
    35     /**
    36      * Validate get_category_by_slug function
    37      */
    38     function test_get_category_by_slug() {
    39 
    40         // create Test Categories
    41         $testcat = self::factory()->category->create_and_get(
    42             array(
    43                 'slug' => 'testcat',
    44                 'name' => 'Test Category 1'
    45             )
    46         );
    47         $testcat2 = self::factory()->category->create_and_get(
    48             array(
    49                 'slug' => 'testcat2',
    50                 'name' => 'Test Category 2'
    51             )
    52         );
    53 
    54         // validate category is returned by slug
    55         $ret_testcat = get_category_by_slug( 'testcat' );
    56         $this->assertEquals( $testcat->term_id, $ret_testcat->term_id );
    57         $ret_testcat = get_category_by_slug( 'TeStCaT' );
    58         $this->assertEquals( $testcat->term_id, $ret_testcat->term_id );
    59 
    60         // validate unknown category returns false
    61         $this->assertFalse( get_category_by_slug( 'testcat3' ) );
    62 
    63     }
    64 
    65     /**
    66      * Validate _make_cat_compat function
    67      */
    68     function test__make_cat_compat() {
    69 
    70         // create Test Categories and Array Representations
    71         $testcat_array = array(
    72             'slug' => 'testmcc',
    73             'name' => 'Test MCC',
    74             'description' => 'Category Test'
    75         );
    76         $testcat = self::factory()->category->create_and_get( $testcat_array );
    77         $testcat_array['term_id'] = $testcat->term_id;
    78 
    79         $testcat2_array = array(
    80             'slug' => 'testmcc',
    81             'name' => 'Test MCC',
    82             'description' => 'Category Test',
    83             'parent' => $testcat->term_id
    84         );
    85         $testcat2 = self::factory()->category->create_and_get( $testcat2_array );
    86         $testcat2_array['term_id'] = $testcat2->term_id;
    87 
    88         // unset properties to enable validation of object
    89         unset( $testcat->cat_ID );
    90         unset( $testcat->category_count );
    91         unset( $testcat->category_description );
    92         unset( $testcat->cat_name );
    93         unset( $testcat->category_nicename );
    94         unset( $testcat->category_parent );
    95 
    96         unset( $testcat2->cat_ID );
    97         unset( $testcat2->category_count );
    98         unset( $testcat2->category_description );
    99         unset( $testcat2->cat_name );
    100         unset( $testcat2->category_nicename );
    101         unset( $testcat2->category_parent );
    102 
    103         // make Compatible
    104         _make_cat_compat( $testcat );
    105         _make_cat_compat( $testcat2 );
    106         _make_cat_compat( $testcat_array );
    107         _make_cat_compat( $testcat2_array );
    108 
    109         // Validate Compatibility Object
    110         $this->assertEquals( $testcat->cat_ID, $testcat->term_id );
    111         $this->assertEquals( $testcat->category_count, $testcat->count );
    112         $this->assertEquals( $testcat->category_description, $testcat->description );
    113         $this->assertEquals( $testcat->cat_name, $testcat->name );
    114         $this->assertEquals( $testcat->category_nicename, $testcat->slug );
    115         $this->assertEquals( $testcat->category_parent, $testcat->parent );
    116 
    117         // Validate Compatibility Object with Parent
    118         $this->assertEquals( $testcat->cat_ID, $testcat->term_id );
    119         $this->assertEquals( $testcat->category_count, $testcat->count );
    120         $this->assertEquals( $testcat->category_description, $testcat->description );
    121         $this->assertEquals( $testcat->cat_name, $testcat->name );
    122         $this->assertEquals( $testcat->category_nicename, $testcat->slug );
    123         $this->assertEquals( $testcat->category_parent, $testcat->parent );
    124 
    125         // Validate Compatibility Array
    126         $this->assertEquals( $testcat_array['cat_ID'], $testcat_array['term_id'] );
    127         $this->assertEquals( $testcat_array['category_count'], $testcat_array['count'] );
    128         $this->assertEquals( $testcat_array['category_description'], $testcat_array['description'] );
    129         $this->assertEquals( $testcat_array['cat_name'], $testcat_array['name'] );
    130         $this->assertEquals( $testcat_array['category_nicename'], $testcat_array['slug'] );
    131         $this->assertEquals( $testcat_array['category_parent'], $testcat_array['parent'] );
    132 
    133         // Validate Compatibility Array with Parent
    134         $this->assertEquals( $testcat_array['cat_ID'], $testcat_array['term_id'] );
    135         $this->assertEquals( $testcat_array['category_count'], $testcat_array['count'] );
    136         $this->assertEquals( $testcat_array['category_description'], $testcat_array['description'] );
    137         $this->assertEquals( $testcat_array['cat_name'], $testcat_array['name'] );
    138         $this->assertEquals( $testcat_array['category_nicename'], $testcat_array['slug'] );
    139         $this->assertEquals( $testcat_array['category_parent'], $testcat_array['parent'] );
    140     }
    141 
    142     /**
    143      * Validate get_cat_name function
    144      */
    145     function test_get_cat_name() {
    146 
    147         // create Test Category
    148         $testcat = self::factory()->category->create_and_get(
    149             array(
    150                 'slug' => 'testcat',
    151                 'name' => 'Test Category 1'
    152             )
    153         );
    154 
    155         // Validate
    156         $this->assertEquals( $testcat->name, get_cat_name( $testcat->term_id ) );
    157         $this->assertEquals( '', get_cat_name( -1 ) );
    158         $this->assertEquals( '', get_cat_name( $testcat->term_id + 100 ) );
    159 
    160     }
    161 
    162     /**
    163      * Validate get_cat_name function
    164      */
    165     function test_get_cat_ID() {
    166 
    167         // create Test Category
    168         $testcat = self::factory()->category->create_and_get(
    169             array(
    170                 'slug' => 'testcat',
    171                 'name' => 'Test Category 1'
    172             )
    173         );
    174 
    175         // Validate
    176         $this->assertEquals( $testcat->term_id, get_cat_ID( $testcat->name ) );
    177         $this->assertEquals( 0, get_cat_ID( "NO CAT" ) );
    178         $this->assertEquals( 0, get_cat_ID( 12 ) );
    179 
    180     }
    181 
    182     /**
    183      * Validate get_category_by_path function
    184      */
    185     function test_get_category_by_path() {
    186 
    187         // create Test Categories
    188         $root_id = self::factory()->category->create(
    189             array(
    190                 'slug' => 'root',
    191             )
    192         );
    193         $root_cat_id = self::factory()->category->create(
    194             array(
    195                 'slug' => 'cat',
    196                 'parent' => $root_id
    197             )
    198         );
    199         $root_cat_cat_id = self::factory()->category->create(
    200             array(
    201                 'slug' => 'cat', //note this is modified on create
    202                 'parent' => $root_cat_id
    203             )
    204         );
    205         $root_path_id = self::factory()->category->create(
    206             array(
    207                 'slug' => 'path',
    208                 'parent' => $root_id
    209             )
    210         );
    211         $root_path_cat_id = self::factory()->category->create(
    212             array(
    213                 'slug' => 'cat', //note this is modified on create
    214                 'parent' => $root_path_id
    215             )
    216         );
    217         $root_level_id = self::factory()->category->create(
    218             array(
    219                 'slug' => 'level-1',
    220                 'parent' => $root_id
    221             )
    222         );
    223         $root_level_cat_id = self::factory()->category->create(
    224             array(
    225                 'slug' => 'cat', //note this is modified on create
    226                 'parent' => $root_level_id
    227             )
    228         );
    229 
    230         // Validate Full Match
    231         $ret_cat = get_category_by_path( '/root/level-1', true );
    232         $this->assertEquals( $root_level_id, $ret_cat->term_id );
    233         $this->assertNull( get_category_by_path( 'level-1', true ) );
    234         $this->assertNull( get_category_by_path( 'nocat/nocat/', true) );
    235 
    236         // Validate Partial Match
    237         $ret_cat = get_category_by_path( 'level-1', false );
    238         $this->assertEquals( $root_level_id, $ret_cat->term_id );
    239         $ret_cat = get_category_by_path( 'root/cat/level-1', false );
    240         $this->assertEquals( $root_level_id, $ret_cat->term_id );
    241         $ret_cat = get_category_by_path( 'root$2Fcat%20%2Flevel-1', false );
    242         $this->assertEquals( $root_level_id, $ret_cat->term_id );
    243         $this->assertNull( get_category_by_path( 'nocat/nocat/', false) );
    244     }
    245 
     6class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
    2467    /**
    2478     * @ticket 30306
Note: See TracChangeset for help on using the changeset viewer.