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