Make WordPress Core

Ticket #44983: 44983.2.diff

File 44983.2.diff, 3.2 KB (added by kadamwhite, 7 years ago)

Adjust patch based on feedback from @earnjam and fix PHPCS spacing issue

  • src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
    index ff399cc60e..527a8bc199 100644
    class WP_REST_Terms_Controller extends WP_REST_Controller { 
    685685                }
    686686
    687687                if ( isset( $request['parent'] ) && ! empty( $schema['properties']['parent'] ) ) {
    688                         $parent_term_id = 0;
    689                         $parent_term    = get_term( (int) $request['parent'], $this->taxonomy );
     688                        $parent_term_id   = 0;
     689                        $requested_parent = (int) $request['parent'];
    690690
    691                         if ( $parent_term ) {
    692                                 $parent_term_id = $parent_term->term_id;
     691                        if ( $requested_parent ) {
     692                                $parent_term = get_term( $requested_parent, $this->taxonomy );
     693
     694                                if ( $parent_term instanceof WP_Term ) {
     695                                        $parent_term_id = $parent_term->term_id;
     696                                }
    693697                        }
    694698
    695699                        $prepared_term->parent = $parent_term_id;
  • tests/phpunit/tests/rest-api/rest-categories-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-categories-controller.php tests/phpunit/tests/rest-api/rest-categories-controller.php
    index 3c9543833b..8d44e282db 100644
    class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 
    859859                $this->assertErrorResponse( 'rest_term_invalid', $response, 400 );
    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 );
    864876                $orig_args = array(
    class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 
    929941                $this->assertEquals( $parent->term_id, $data['parent'] );
    930942        }
    931943
     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'] );
     969        }
     970
    932971        public function test_update_item_invalid_parent() {
    933972                wp_set_current_user( self::$administrator );
    934973                $term = get_term_by( 'id', $this->factory->category->create(), 'category' );