WordPress.org

Make WordPress Core

Ticket #26570: 26570.2.diff

File 26570.2.diff, 4.2 KB (added by DrewAPicture, 6 years ago)

+ unit tests

  • src/wp-includes/taxonomy.php

     
    26172617 * @since 2.3.0
    26182618 * @uses wp_remove_object_terms()
    26192619 *
    2620  * @param int $object_id The object to relate to.
    2621  * @param array|int|string $terms The slug or id of the term, will replace all existing
    2622  * related terms in this taxonomy.
    2623  * @param array|string $taxonomy The context in which to relate the term to the object.
    2624  * @param bool $append If false will delete difference of terms.
    2625  * @return array|WP_Error Affected Term IDs
     2620 * @param int              $object_id The object to relate to.
     2621 * @param array|int|string $terms     A single term slug (if the taxonomy is hierarchical), single term id,
     2622 *                                    or array of either term slugs or ids. Will replace all existing related
     2623 *                                    terms in this taxonomy.
     2624 * @param array|string     $taxonomy  The context in which to relate the term to the object.
     2625 * @param bool             $append    If false will delete difference of terms.
     2626 * @return array|WP_Error Affected Term IDs.
    26262627 */
    2627 function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {
     2628function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
    26282629        global $wpdb;
    26292630
    26302631        $object_id = (int) $object_id;
  • tests/phpunit/tests/taxonomy.php

     
    296296                $term20 = $this->factory->tag->create( array( 'name' => 'A--' ) );
    297297                $this->assertFalse( is_wp_error( $term20 ) );
    298298        }
     299
     300        /**
     301         * @ticket 26570
     302         */
     303        function test_wp_set_object_terms() {
     304                $non_hier = rand_str( 10 );
     305                $hier     = rand_str( 10 );
     306
     307                // Register taxonomies
     308                register_taxonomy( $non_hier, array() );
     309                register_taxonomy( $hier, array( 'hierarchical' => true ) );
     310
     311                // Create a post.
     312                $post_id = $this->factory->post->create();
     313
     314                /*
     315                 * Set a single term (non-hierarchical) by ID.
     316                 */
     317                $tag = wp_insert_term( 'Foo', $non_hier );
     318                $this->assertFalse( has_term( $tag['term_id'], $non_hier, $post_id ) );
     319
     320                wp_set_object_terms( $post_id, $tag['term_id'], $non_hier );
     321                $this->assertTrue( has_term( $tag['term_id'], $non_hier, $post_id ) );
     322
     323                /*
     324                 * Set a single term (hierarchical) by slug.
     325                 */
     326                $cat = wp_insert_term( 'Bar', $hier );
     327                $cat = get_term( $cat['term_id'], $hier );
     328
     329                $this->assertFalse( has_term( $cat->slug, $hier, $post_id ) );
     330
     331                wp_set_object_terms( $post_id, $cat->slug, $hier );
     332                $this->assertTrue( has_term( $cat->slug, $hier, $post_id ) );
     333
     334                /*
     335                 * Set an array of term IDs (non-hierarchical) by ID.
     336                 */
     337                $tag1 = wp_insert_term( '_tag1', $non_hier );
     338                $this->assertFalse( has_term( $tag1['term_id'], $non_hier, $post_id ) );
     339
     340                $tag2 = wp_insert_term( '_tag2', $non_hier );
     341                $this->assertFalse( has_term( $tag2['term_id'], $non_hier, $post_id ) );
     342
     343                $tag3 = wp_insert_term( '_tag3', $non_hier );
     344                $this->assertFalse( has_term( $tag3['term_id'], $non_hier, $post_id ) );
     345
     346                wp_set_object_terms( $post_id, array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier );
     347                $this->assertTrue( has_term( array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier, $post_id ) );
     348
     349                /**
     350                 * Set an array of term slugs (hierarchical) by slug.
     351                 */
     352                $cat1 = wp_insert_term( '_cat1', $hier );
     353                $cat1 = get_term( $cat1['term_id'], $hier );
     354                $this->assertFalse( has_term( $cat1->slug, $hier, $post_id ) );
     355
     356                $cat2 = wp_insert_term( '_cat2', $hier );
     357                $cat2 = get_term( $cat2['term_id'], $hier );
     358                $this->assertFalse( has_term( $cat2->slug, $hier, $post_id ) );
     359
     360                $cat3 = wp_insert_term( '_cat3', $hier );
     361                $cat3 = get_term( $cat3['term_id'], $hier );
     362                $this->assertFalse( has_term( $cat3->slug, $hier, $post_id ) );
     363
     364                wp_set_object_terms( $post_id, array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier );
     365                $this->assertTrue( has_term( array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier, $post_id ) );
     366        }
    299367}