Make WordPress Core

Ticket #44983: 44983.patch

File 44983.patch, 3.2 KB (added by dlh, 7 years ago)
  • 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 884e5f60f3..a9565d91d3 100644
    class WP_REST_Terms_Controller extends WP_REST_Controller { 
    669669                }
    670670
    671671                if ( isset( $request['parent'] ) && ! empty( $schema['properties']['parent'] ) ) {
    672                         $parent_term_id = 0;
    673                         $parent_term    = get_term( (int) $request['parent'], $this->taxonomy );
     672                        $parent_term_id   = 0;
     673                        $requested_parent = (int) $request['parent'];
    674674
    675                         if ( $parent_term ) {
    676                                 $parent_term_id = $parent_term->term_id;
     675                        if ( 0 === $requested_parent ) {
     676                                $parent_term_id = $requested_parent;
     677                        }
     678
     679                        if ( $requested_parent ) {
     680                                $parent_term = get_term( $requested_parent, $this->taxonomy );
     681
     682                                if ( $parent_term instanceof WP_Term ) {
     683                                        $parent_term_id = $parent_term->term_id;
     684                                }
    677685                        }
    678686
    679687                        $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 79add82cde..6d9a29dfc0 100644
    class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 
    854854                $this->assertErrorResponse( 'rest_term_invalid', $response, 400 );
    855855        }
    856856
     857        public function test_create_item_with_no_parent() {
     858                wp_set_current_user( self::$administrator );
     859                $parent = 0;
     860                $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
     861                $request->set_param( 'name', 'My Awesome Term' );
     862                $request->set_param( 'parent', $parent );
     863                $response = rest_get_server()->dispatch( $request );
     864                $this->assertEquals( 201, $response->get_status() );
     865                $data = $response->get_data();
     866                $this->assertEquals( $parent, $data['parent'] );
     867        }
     868
    857869        public function test_update_item() {
    858870                wp_set_current_user( self::$administrator );
    859871                $orig_args = array(
    class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 
    924936                $this->assertEquals( $parent->term_id, $data['parent'] );
    925937        }
    926938
     939        public function test_update_item_remove_parent() {
     940                wp_set_current_user( self::$administrator );
     941
     942                $old_parent_term = get_term_by( 'id', $this->factory->category->create(), 'category' );
     943                $new_parent_id   = 0;
     944
     945                $term = get_term_by(
     946                        'id',
     947                        $this->factory->category->create(
     948                                [
     949                                        'parent' => $old_parent_term->term_id,
     950                                ]
     951                        ),
     952                        'category'
     953                );
     954
     955                $this->assertEquals( $old_parent_term->term_id, $term->parent );
     956
     957                $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
     958                $request->set_param( 'parent', $new_parent_id );
     959                $response = rest_get_server()->dispatch( $request );
     960                $this->assertEquals( 200, $response->get_status() );
     961
     962                $data = $response->get_data();
     963                $this->assertEquals( $new_parent_id, $data['parent'] );
     964        }
     965
    927966        public function test_update_item_invalid_parent() {
    928967                wp_set_current_user( self::$administrator );
    929968                $term = get_term_by( 'id', $this->factory->category->create(), 'category' );