Make WordPress Core

Changeset 56248


Ignore:
Timestamp:
07/17/2023 02:20:42 PM (3 years ago)
Author:
audrasjb
Message:

Editor: Fix a PHP notice appearing when adding a new template part.

This changeset fixes a PHP notice appearing when a template part is created in the site editor. It also adds a unit test case to cover
WP_REST_Templates_Controller::prepare_item_for_database.

Props wildworks, dunhakdis, Rahmohn, oglekler, audrasjb, mukesh27, costdev, dunhakdis.
Fixes #57851.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r56195 r56248  
    597597                        } elseif ( null !== $template && 'custom' !== $template->source && $template->area ) {
    598598                                $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area );
    599                         } elseif ( ! $template->area ) {
     599                        } elseif ( empty( $template->area ) ) {
    600600                                $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
    601601                        }
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r56051 r56248  
    846846                $this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html` when  ignore_empty is `true`.' );
    847847        }
     848
     849        /**
     850         * @ticket 57851
     851         *
     852         * @covers WP_REST_Templates_Controller::prepare_item_for_database
     853         */
     854        public function test_prepare_item_for_database() {
     855                $endpoint = new WP_REST_Templates_Controller( 'wp_template_part' );
     856
     857                $prepare_item_for_database = new ReflectionMethod( $endpoint, 'prepare_item_for_database' );
     858                $prepare_item_for_database->setAccessible( true );
     859
     860                $body_params = array(
     861                        'title'   => 'Untitled Template Part',
     862                        'slug'    => 'untitled-template-part',
     863                        'content' => '',
     864                );
     865
     866                $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' );
     867                $request->set_body_params( $body_params );
     868
     869                $prepared = $prepare_item_for_database->invoke( $endpoint, $request );
     870
     871                $this->assertInstanceOf( 'stdClass', $prepared, 'The item could not be prepared for the database.' );
     872
     873                $this->assertObjectHasAttribute( 'post_type', $prepared, 'The "post_type" was not included in the prepared template part.' );
     874                $this->assertObjectHasAttribute( 'post_status', $prepared, 'The "post_status" was not included in the prepared template part.' );
     875                $this->assertObjectHasAttribute( 'tax_input', $prepared, 'The "tax_input" was not included in the prepared template part.' );
     876                $this->assertArrayHasKey( 'wp_theme', $prepared->tax_input, 'The "wp_theme" tax was not included in the prepared template part.' );
     877                $this->assertArrayHasKey( 'wp_template_part_area', $prepared->tax_input, 'The "wp_template_part_area" tax was not included in the prepared template part.' );
     878                $this->assertObjectHasAttribute( 'post_content', $prepared, 'The "post_content" was not included in the prepared template part.' );
     879                $this->assertObjectHasAttribute( 'post_title', $prepared, 'The "post_title" was not included in the prepared template part.' );
     880
     881                $this->assertSame( 'wp_template_part', $prepared->post_type, 'The "post_type" in the prepared template part should be "wp_template_part".' );
     882                $this->assertSame( 'publish', $prepared->post_status, 'The post status in the prepared template part should be "publish".' );
     883                $this->assertSame( WP_TEMPLATE_PART_AREA_UNCATEGORIZED, $prepared->tax_input['wp_template_part_area'], 'The area in the prepared template part should be uncategorized.' );
     884                $this->assertSame( 'Untitled Template Part', $prepared->post_title, 'The title was not correct in the prepared template part.' );
     885
     886                $this->assertEmpty( $prepared->post_content, 'The content was not correct in the prepared template part.' );
     887        }
    848888}
Note: See TracChangeset for help on using the changeset viewer.