Make WordPress Core


Ignore:
Timestamp:
09/04/2013 05:41:03 PM (11 years ago)
Author:
wonderboymusic
Message:

Allow int to be passed in lieu of array, add append arg to wp_set_post_categories(). Adds more extensive unit tests for wp_set_post_categories().

Props ptahdunbar for initial patch.
Fixes #16550.

File:
1 edited

Legend:

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

    r25106 r25234  
    467467        $this->assertEquals( 'This description is even more amazing!', $terms[0]->description );
    468468    }
     469
     470    function test_wp_set_post_categories() {
     471        $post_id = $this->factory->post->create();
     472        $post = get_post( $post_id );
     473
     474        $this->assertInternalType( 'array', $post->post_category );
     475        $this->assertEquals( 1, count( $post->post_category ) );
     476        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     477        $term1 = wp_insert_term( 'Foo', 'category' );
     478        $term2 = wp_insert_term( 'Bar', 'category' );
     479        $term3 = wp_insert_term( 'Baz', 'category' );
     480        wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
     481        $this->assertEquals( 2, count( $post->post_category ) );
     482        $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category );
     483
     484        wp_set_post_categories( $post_id, $term3['term_id'], true );
     485        $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category );
     486
     487        $term4 = wp_insert_term( 'Burrito', 'category' );
     488        wp_set_post_categories( $post_id, $term4['term_id'] );
     489        $this->assertEquals( array( $term4['term_id'] ), $post->post_category );
     490
     491        wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );
     492        $this->assertEquals( array( $term2['term_id'], $term4['term_id'],$term1['term_id'] ), $post->post_category );
     493
     494        wp_set_post_categories( $post_id, array(), true );
     495        $this->assertEquals( 1, count( $post->post_category ) );
     496        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     497
     498        wp_set_post_categories( $post_id, array() );
     499        $this->assertEquals( 1, count( $post->post_category ) );
     500        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     501    }
    469502}
Note: See TracChangeset for help on using the changeset viewer.