WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/21/2019 06:58:50 PM (2 years ago)
Author:
kadamwhite
Message:

REST API: Avoid undefined-property notice when setting parent term to 0.

Only try to access term_id once $parent_term is known to be a WP_Term.

Props dlh, earnjam.
Fixes #44983.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r44510 r44965  
    860860    }
    861861
     862    public function test_create_item_with_no_parent() {
     863        wp_set_current_user( self::$administrator );
     864        $parent  = 0;
     865        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
     866        $request->set_param( 'name', 'My Awesome Term' );
     867        $request->set_param( 'parent', $parent );
     868        $response = rest_get_server()->dispatch( $request );
     869        $this->assertEquals( 201, $response->get_status() );
     870        $data = $response->get_data();
     871        $this->assertEquals( $parent, $data['parent'] );
     872    }
     873
    862874    public function test_update_item() {
    863875        wp_set_current_user( self::$administrator );
     
    928940        $data = $response->get_data();
    929941        $this->assertEquals( $parent->term_id, $data['parent'] );
     942    }
     943
     944    public function test_update_item_remove_parent() {
     945        wp_set_current_user( self::$administrator );
     946
     947        $old_parent_term = get_term_by( 'id', $this->factory->category->create(), 'category' );
     948        $new_parent_id   = 0;
     949
     950        $term = get_term_by(
     951            'id',
     952            $this->factory->category->create(
     953                [
     954                    'parent' => $old_parent_term->term_id,
     955                ]
     956            ),
     957            'category'
     958        );
     959
     960        $this->assertEquals( $old_parent_term->term_id, $term->parent );
     961
     962        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
     963        $request->set_param( 'parent', $new_parent_id );
     964        $response = rest_get_server()->dispatch( $request );
     965        $this->assertEquals( 200, $response->get_status() );
     966
     967        $data = $response->get_data();
     968        $this->assertEquals( $new_parent_id, $data['parent'] );
    930969    }
    931970
Note: See TracChangeset for help on using the changeset viewer.