Make WordPress Core

Changeset 29867


Ignore:
Timestamp:
10/09/2014 06:11:17 PM (12 years ago)
Author:
boonebgorges
Message:

Return an error when adding a term to a non-existent parent.

Parallels the logic of wp_insert_term(), introduced in [29196].

Props jesin.
Fixes #29614.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r29866 r29867  
    29342934                return new WP_Error('empty_term_name', __('A name is required for this term'));
    29352935
     2936        if ( $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) {
     2937                return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
     2938        }
     2939
    29362940        $empty_slug = false;
    29372941        if ( empty( $args['slug'] ) ) {
  • trunk/tests/phpunit/tests/term.php

    r29865 r29867  
    632632        }
    633633
     634        /**
     635         * @ticket 29614
     636         */
     637        function test_wp_update_term_parent_does_not_exist() {
     638                register_taxonomy( 'wptests_tax', array(
     639                        'hierarchical' => true,
     640                ) );
     641                $fake_term_id = 787878;
     642
     643                $this->assertNull( term_exists( $fake_term_id, 'wptests_tax' ) );
     644
     645                $t = $this->factory->term->create( array(
     646                        'taxonomy' => 'wptests_tax',
     647                ) );
     648
     649                $found = wp_update_term( $t, 'wptests_tax', array(
     650                        'parent' => $fake_term_id,
     651                ) );
     652
     653                $this->assertWPError( $found );
     654                $this->assertSame( 'missing_parent', $found->get_error_code() );
     655
     656                $term = get_term( $t, 'wptests_tax' );
     657                $this->assertEquals( 0, $term->parent );
     658        }
     659
    634660        public function test_wp_update_term_alias_of_no_term_group() {
    635661                register_taxonomy( 'wptests_tax', 'post' );
Note: See TracChangeset for help on using the changeset viewer.