Changeset 42343 for trunk/tests/phpunit/tests/term/wpSetObjectTerms.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term/wpSetObjectTerms.php
r39174 r42343 5 5 */ 6 6 class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { 7 protected $taxonomy = 'category';7 protected $taxonomy = 'category'; 8 8 protected static $post_ids = array(); 9 9 … … 104 104 105 105 $terms = array(); 106 for ( $i=0; $i<3; $i++ ) {107 $term = "term_{$i}";106 for ( $i = 0; $i < 3; $i++ ) { 107 $term = "term_{$i}"; 108 108 $result = wp_insert_term( $term, $this->taxonomy ); 109 109 $this->assertInternalType( 'array', $result ); 110 $term_id[ $term] = $result['term_id'];111 } 112 113 foreach ( $ids as $id) {114 $tt = wp_set_object_terms( $id, array_values( $term_id), $this->taxonomy );110 $term_id[ $term ] = $result['term_id']; 111 } 112 113 foreach ( $ids as $id ) { 114 $tt = wp_set_object_terms( $id, array_values( $term_id ), $this->taxonomy ); 115 115 // should return three term taxonomy ids 116 $this->assertEquals( 3, count( $tt) );116 $this->assertEquals( 3, count( $tt ) ); 117 117 } 118 118 119 119 // each term should be associated with every post 120 foreach ( $term_id as $term=>$id) {121 $actual = get_objects_in_term( $id, $this->taxonomy);122 $this->assertEquals( $ids, array_map( 'intval', $actual) );120 foreach ( $term_id as $term => $id ) { 121 $actual = get_objects_in_term( $id, $this->taxonomy ); 122 $this->assertEquals( $ids, array_map( 'intval', $actual ) ); 123 123 } 124 124 125 125 // each term should have a count of 5 126 foreach ( array_keys($term_id) as $term) {127 $t = get_term_by( 'name', $term, $this->taxonomy);126 foreach ( array_keys( $term_id ) as $term ) { 127 $t = get_term_by( 'name', $term, $this->taxonomy ); 128 128 $this->assertEquals( 5, $t->count ); 129 129 } … … 136 136 rand_str(), 137 137 rand_str(), 138 rand_str() 139 ); 140 141 foreach ( $ids as $id) {138 rand_str(), 139 ); 140 141 foreach ( $ids as $id ) { 142 142 $tt = wp_set_object_terms( $id, $terms, $this->taxonomy ); 143 143 // should return three term taxonomy ids 144 $this->assertEquals( 3, count( $tt) );144 $this->assertEquals( 3, count( $tt ) ); 145 145 // remember which term has which term_id 146 for ( $i=0; $i<3; $i++) {147 $term = get_term_by('name', $terms[$i], $this->taxonomy);148 $term_id[ $terms[$i]] = intval($term->term_id);146 for ( $i = 0; $i < 3; $i++ ) { 147 $term = get_term_by( 'name', $terms[ $i ], $this->taxonomy ); 148 $term_id[ $terms[ $i ] ] = intval( $term->term_id ); 149 149 } 150 150 } 151 151 152 152 // each term should be associated with every post 153 foreach ( $term_id as $term=>$id) {154 $actual = get_objects_in_term( $id, $this->taxonomy);155 $this->assertEquals( $ids, array_map( 'intval', $actual) );153 foreach ( $term_id as $term => $id ) { 154 $actual = get_objects_in_term( $id, $this->taxonomy ); 155 $this->assertEquals( $ids, array_map( 'intval', $actual ) ); 156 156 } 157 157 158 158 // each term should have a count of 5 159 foreach ( $terms as $term) {160 $t = get_term_by( 'name', $term, $this->taxonomy);159 foreach ( $terms as $term ) { 160 $t = get_term_by( 'name', $term, $this->taxonomy ); 161 161 $this->assertEquals( 5, $t->count ); 162 162 } … … 165 165 function test_set_object_terms_invalid() { 166 166 // bogus taxonomy 167 $result = wp_set_object_terms( self::$post_ids[0], array( rand_str()), rand_str() );167 $result = wp_set_object_terms( self::$post_ids[0], array( rand_str() ), rand_str() ); 168 168 $this->assertWPError( $result ); 169 169 } … … 171 171 public function test_wp_set_object_terms_append_true() { 172 172 register_taxonomy( 'wptests_tax', 'post' ); 173 $p = self::$post_ids[0]; 174 $t1 = self::factory()->term->create( array( 175 'taxonomy' => 'wptests_tax', 176 ) ); 177 $t2 = self::factory()->term->create( array( 178 'taxonomy' => 'wptests_tax', 179 ) ); 173 $p = self::$post_ids[0]; 174 $t1 = self::factory()->term->create( 175 array( 176 'taxonomy' => 'wptests_tax', 177 ) 178 ); 179 $t2 = self::factory()->term->create( 180 array( 181 'taxonomy' => 'wptests_tax', 182 ) 183 ); 180 184 181 185 $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' ); … … 192 196 public function test_wp_set_object_terms_append_false() { 193 197 register_taxonomy( 'wptests_tax', 'post' ); 194 $p = self::$post_ids[0]; 195 $t1 = self::factory()->term->create( array( 196 'taxonomy' => 'wptests_tax', 197 ) ); 198 $t2 = self::factory()->term->create( array( 199 'taxonomy' => 'wptests_tax', 200 ) ); 198 $p = self::$post_ids[0]; 199 $t1 = self::factory()->term->create( 200 array( 201 'taxonomy' => 'wptests_tax', 202 ) 203 ); 204 $t2 = self::factory()->term->create( 205 array( 206 'taxonomy' => 'wptests_tax', 207 ) 208 ); 201 209 202 210 $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' ); … … 213 221 public function test_wp_set_object_terms_append_default_to_false() { 214 222 register_taxonomy( 'wptests_tax', 'post' ); 215 $p = self::$post_ids[0]; 216 $t1 = self::factory()->term->create( array( 217 'taxonomy' => 'wptests_tax', 218 ) ); 219 $t2 = self::factory()->term->create( array( 220 'taxonomy' => 'wptests_tax', 221 ) ); 223 $p = self::$post_ids[0]; 224 $t1 = self::factory()->term->create( 225 array( 226 'taxonomy' => 'wptests_tax', 227 ) 228 ); 229 $t2 = self::factory()->term->create( 230 array( 231 'taxonomy' => 'wptests_tax', 232 ) 233 ); 222 234 223 235 $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' ); … … 239 251 // first set: 3 terms 240 252 $terms_1 = array(); 241 for ( $i=0; $i<3; $i++ ) {242 $term = "term_{$i}";253 for ( $i = 0; $i < 3; $i++ ) { 254 $term = "term_{$i}"; 243 255 $result = wp_insert_term( $term, $this->taxonomy ); 244 256 $this->assertInternalType( 'array', $result ); 245 $terms_1[ $i] = $result['term_id'];257 $terms_1[ $i ] = $result['term_id']; 246 258 } 247 259 248 260 // second set: one of the original terms, plus one new term 249 $terms_2 = array();261 $terms_2 = array(); 250 262 $terms_2[0] = $terms_1[1]; 251 263 252 $term = 'term';253 $result = wp_insert_term( $term, $this->taxonomy );264 $term = 'term'; 265 $result = wp_insert_term( $term, $this->taxonomy ); 254 266 $terms_2[1] = $result['term_id']; 255 256 267 257 268 // set the initial terms 258 269 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 259 $this->assertEquals( 3, count( $tt_1) );270 $this->assertEquals( 3, count( $tt_1 ) ); 260 271 261 272 // make sure they're correct 262 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id')); 273 $terms = wp_get_object_terms( 274 $post_id, $this->taxonomy, array( 275 'fields' => 'ids', 276 'orderby' => 'term_id', 277 ) 278 ); 263 279 $this->assertEquals( $terms_1, $terms ); 264 280 265 281 // change the terms 266 282 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 267 $this->assertEquals( 2, count( $tt_2) );283 $this->assertEquals( 2, count( $tt_2 ) ); 268 284 269 285 // make sure they're correct 270 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id')); 286 $terms = wp_get_object_terms( 287 $post_id, $this->taxonomy, array( 288 'fields' => 'ids', 289 'orderby' => 'term_id', 290 ) 291 ); 271 292 $this->assertEquals( $terms_2, $terms ); 272 293 … … 281 302 $post_id = self::$post_ids[0]; 282 303 283 $terms_1 = array( 'foo', 'bar', 'baz');284 $terms_2 = array( 'bar', 'bing');304 $terms_1 = array( 'foo', 'bar', 'baz' ); 305 $terms_2 = array( 'bar', 'bing' ); 285 306 286 307 // set the initial terms 287 308 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 288 $this->assertEquals( 3, count( $tt_1) );309 $this->assertEquals( 3, count( $tt_1 ) ); 289 310 290 311 // make sure they're correct 291 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id')); 312 $terms = wp_get_object_terms( 313 $post_id, $this->taxonomy, array( 314 'fields' => 'names', 315 'orderby' => 'term_id', 316 ) 317 ); 292 318 $this->assertEquals( $terms_1, $terms ); 293 319 294 320 // change the terms 295 321 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 296 $this->assertEquals( 2, count( $tt_2) );322 $this->assertEquals( 2, count( $tt_2 ) ); 297 323 298 324 // make sure they're correct 299 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id')); 325 $terms = wp_get_object_terms( 326 $post_id, $this->taxonomy, array( 327 'fields' => 'names', 328 'orderby' => 'term_id', 329 ) 330 ); 300 331 $this->assertEquals( $terms_2, $terms ); 301 332 … … 321 352 register_taxonomy( 'wptests_tax', 'post' ); 322 353 323 $t = self::factory()->term->create( array( 324 'taxonomy' => 'wptests_tax', 325 'slug' => 'foo', 326 'name' => 'Bar', 327 ) ); 354 $t = self::factory()->term->create( 355 array( 356 'taxonomy' => 'wptests_tax', 357 'slug' => 'foo', 358 'name' => 'Bar', 359 ) 360 ); 328 361 329 362 $tt_ids = wp_set_object_terms( self::$post_ids[0], 'foo', 'wptests_tax' ); … … 338 371 register_taxonomy( 'wptests_tax', 'post' ); 339 372 340 $t = self::factory()->term->create( array( 341 'taxonomy' => 'wptests_tax', 342 'slug' => 'foo', 343 'name' => 'Bar', 344 ) ); 373 $t = self::factory()->term->create( 374 array( 375 'taxonomy' => 'wptests_tax', 376 'slug' => 'foo', 377 'name' => 'Bar', 378 ) 379 ); 345 380 346 381 $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' ); … … 355 390 register_taxonomy( 'wptests_tax', 'post' ); 356 391 357 $t1 = self::factory()->term->create( array( 358 'taxonomy' => 'wptests_tax', 359 'slug' => 'foo', 360 'name' => 'Bar', 361 ) ); 362 363 $t2 = self::factory()->term->create( array( 364 'taxonomy' => 'wptests_tax', 365 'slug' => 'bar', 366 'name' => 'Foo', 367 ) ); 392 $t1 = self::factory()->term->create( 393 array( 394 'taxonomy' => 'wptests_tax', 395 'slug' => 'foo', 396 'name' => 'Bar', 397 ) 398 ); 399 400 $t2 = self::factory()->term->create( 401 array( 402 'taxonomy' => 'wptests_tax', 403 'slug' => 'bar', 404 'name' => 'Foo', 405 ) 406 ); 368 407 369 408 $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' );
Note: See TracChangeset
for help on using the changeset viewer.