Changeset 55921
- Timestamp:
- 06/14/2023 11:49:36 PM (18 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r55759 r55921 2738 2738 * @param string|int|array $terms A single term slug, single term ID, or array of either term slugs or IDs. 2739 2739 * Will replace all existing related terms in this taxonomy. Passing an 2740 * empty valuewill remove all related terms.2740 * empty array will remove all related terms. 2741 2741 * @param string $taxonomy The context in which to relate the term to the object. 2742 2742 * @param bool $append Optional. If false will delete difference of terms. Default false. … … 2752 2752 } 2753 2753 2754 if ( ! is_array( $terms ) ) { 2754 if ( empty( $terms ) ) { 2755 $terms = array(); 2756 } elseif ( ! is_array( $terms ) ) { 2755 2757 $terms = array( $terms ); 2756 2758 } -
trunk/tests/phpunit/tests/term/wpSetObjectTerms.php
r52389 r55921 5 5 */ 6 6 class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { 7 protected $taxonomy= 'category';7 protected static $taxonomy = 'category'; 8 8 protected static $post_ids = array(); 9 protected static $term_ids = array(); 9 10 10 11 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 11 12 self::$post_ids = $factory->post->create_many( 5 ); 13 self::$term_ids = $factory->term->create_many( 5, array( 'taxonomy' => self::$taxonomy ) ); 12 14 } 13 15 … … 106 108 for ( $i = 0; $i < 3; $i++ ) { 107 109 $term = "term_{$i}"; 108 $result = wp_insert_term( $term, $this->taxonomy );110 $result = wp_insert_term( $term, self::$taxonomy ); 109 111 $this->assertIsArray( $result ); 110 112 $term_id[ $term ] = $result['term_id']; … … 112 114 113 115 foreach ( $ids as $id ) { 114 $tt = wp_set_object_terms( $id, array_values( $term_id ), $this->taxonomy );116 $tt = wp_set_object_terms( $id, array_values( $term_id ), self::$taxonomy ); 115 117 // Should return three term taxonomy IDs. 116 118 $this->assertCount( 3, $tt ); … … 119 121 // Each term should be associated with every post. 120 122 foreach ( $term_id as $term => $id ) { 121 $actual = get_objects_in_term( $id, $this->taxonomy );123 $actual = get_objects_in_term( $id, self::$taxonomy ); 122 124 $this->assertSame( $ids, array_map( 'intval', $actual ) ); 123 125 } … … 125 127 // Each term should have a count of 5. 126 128 foreach ( array_keys( $term_id ) as $term ) { 127 $t = get_term_by( 'name', $term, $this->taxonomy );129 $t = get_term_by( 'name', $term, self::$taxonomy ); 128 130 $this->assertSame( 5, $t->count ); 129 131 } … … 140 142 141 143 foreach ( $ids as $id ) { 142 $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );144 $tt = wp_set_object_terms( $id, $terms, self::$taxonomy ); 143 145 // Should return three term taxonomy IDs. 144 146 $this->assertCount( 3, $tt ); 145 147 // Remember which term has which term_id. 146 148 for ( $i = 0; $i < 3; $i++ ) { 147 $term = get_term_by( 'name', $terms[ $i ], $this->taxonomy );149 $term = get_term_by( 'name', $terms[ $i ], self::$taxonomy ); 148 150 $term_id[ $terms[ $i ] ] = (int) $term->term_id; 149 151 } … … 152 154 // Each term should be associated with every post. 153 155 foreach ( $term_id as $term => $id ) { 154 $actual = get_objects_in_term( $id, $this->taxonomy );156 $actual = get_objects_in_term( $id, self::$taxonomy ); 155 157 $this->assertSame( $ids, array_map( 'intval', $actual ) ); 156 158 } … … 158 160 // Each term should have a count of 5. 159 161 foreach ( $terms as $term ) { 160 $t = get_term_by( 'name', $term, $this->taxonomy );162 $t = get_term_by( 'name', $term, self::$taxonomy ); 161 163 $this->assertSame( 5, $t->count ); 162 164 } … … 254 256 for ( $i = 0; $i < 3; $i++ ) { 255 257 $term = "term_{$i}"; 256 $result = wp_insert_term( $term, $this->taxonomy );258 $result = wp_insert_term( $term, self::$taxonomy ); 257 259 $this->assertIsArray( $result ); 258 260 $terms_1[ $i ] = $result['term_id']; … … 264 266 265 267 $term = 'term'; 266 $result = wp_insert_term( $term, $this->taxonomy );268 $result = wp_insert_term( $term, self::$taxonomy ); 267 269 $terms_2[1] = $result['term_id']; 268 270 269 271 // Set the initial terms. 270 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );272 $tt_1 = wp_set_object_terms( $post_id, $terms_1, self::$taxonomy ); 271 273 $this->assertCount( 3, $tt_1 ); 272 274 … … 274 276 $terms = wp_get_object_terms( 275 277 $post_id, 276 $this->taxonomy,278 self::$taxonomy, 277 279 array( 278 280 'fields' => 'ids', … … 283 285 284 286 // Change the terms. 285 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );287 $tt_2 = wp_set_object_terms( $post_id, $terms_2, self::$taxonomy ); 286 288 $this->assertCount( 2, $tt_2 ); 287 289 … … 289 291 $terms = wp_get_object_terms( 290 292 $post_id, 291 $this->taxonomy,293 self::$taxonomy, 292 294 array( 293 295 'fields' => 'ids', … … 312 314 313 315 // Set the initial terms. 314 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );316 $tt_1 = wp_set_object_terms( $post_id, $terms_1, self::$taxonomy ); 315 317 $this->assertCount( 3, $tt_1 ); 316 318 … … 318 320 $terms = wp_get_object_terms( 319 321 $post_id, 320 $this->taxonomy,322 self::$taxonomy, 321 323 array( 322 324 'fields' => 'names', … … 327 329 328 330 // Change the terms. 329 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );331 $tt_2 = wp_set_object_terms( $post_id, $terms_2, self::$taxonomy ); 330 332 $this->assertCount( 2, $tt_2 ); 331 333 … … 333 335 $terms = wp_get_object_terms( 334 336 $post_id, 335 $this->taxonomy,337 self::$taxonomy, 336 338 array( 337 339 'fields' => 'names', … … 431 433 $this->assertSame( array(), $tt_ids ); 432 434 } 435 436 /** 437 * Tests that empty values clear an object of all terms. 438 * 439 * @ticket 57923 440 * 441 * @dataProvider data_empty_value_should_clear_terms 442 * 443 * @param mixed $empty_value An empty value. 444 */ 445 public function test_empty_value_should_clear_terms( $empty_value ) { 446 $post_id = self::$post_ids[0]; 447 448 // Assign some terms. 449 wp_set_object_terms( $post_id, self::$term_ids, self::$taxonomy ); 450 451 // Make sure the terms are set. 452 $terms = wp_get_object_terms( $post_id, self::$taxonomy, array( 'fields' => 'names' ) ); 453 $this->assertNotEmpty( $terms, 'Terms should initially be applied to post object.' ); 454 455 // Remove terms by passing an empty value. 456 wp_set_object_terms( $post_id, $empty_value, self::$taxonomy ); 457 458 // Make sure the terms have been removed. 459 $terms = wp_get_object_terms( $post_id, self::$taxonomy, array( 'fields' => 'names' ) ); 460 $this->assertEmpty( $terms, 'An empty() value should clear terms from the post object.' ); 461 } 462 463 /** 464 * Data provider. 465 * 466 * @return array[] 467 */ 468 public function data_empty_value_should_clear_terms() { 469 return array( 470 '(bool) false' => array( false ), 471 'null' => array( null ), 472 '(int) 0' => array( 0 ), 473 '(float) 0.0' => array( 0.0 ), 474 'empty string' => array( '' ), 475 '(string) 0' => array( '0' ), 476 'empty array' => array( array() ), 477 ); 478 } 433 479 }
Note: See TracChangeset
for help on using the changeset viewer.