Make WordPress Core

Ticket #50620: 50620.1.diff

File 50620.1.diff, 4.9 KB (added by manooweb, 5 years ago)

Add unit test

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

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php
    index 3388078d05..926bcec4b2 100644
    a b class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { 
    5454                                                        'description'       => __( 'Attributes for the block' ),
    5555                                                        'type'              => 'object',
    5656                                                        'default'           => array(),
     57                                                        'sanitize_callback' => static function ( $value, $request ) {
     58                                                                $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );
     59
     60                                                                if ( ! $block ) {
     61                                                                        // This will get rejected in ::get_item().
     62                                                                        return true;
     63                                                                }
     64
     65                                                                $schema = array(
     66                                                                        'type'                 => 'object',
     67                                                                        'properties'           => $block->get_attributes(),
     68                                                                        'additionalProperties' => false,
     69                                                                );
     70
     71                                                                return rest_sanitize_value_from_schema( $value, $schema );
     72                                                        },
    5773                                                        'validate_callback' => static function ( $value, $request ) {
    5874                                                                $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );
    5975
  • tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php
    index 423d4a4947..c047fa268e 100644
    a b class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca 
    5555         */
    5656        protected static $non_dynamic_block_name = 'core/non-dynamic';
    5757
     58        /**
     59         * Dynamic block with boolean attributes block name.
     60         *
     61         * @since 5.5.0
     62         *
     63         * @var string
     64         */
     65        protected static $dynamic_block_with_boolean_attributes_block_name = 'core/dynamic-block-with-boolean-attributes';
     66
    5867        /**
    5968         * Test API user's ID.
    6069         *
    class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca 
    127136                $this->register_test_block();
    128137                $this->register_post_context_test_block();
    129138                $this->register_non_dynamic_block();
     139                $this->register_dynamic_block_with_boolean_attributes();
    130140                parent::setUp();
    131141        }
    132142
    class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca 
    139149                WP_Block_Type_Registry::get_instance()->unregister( self::$block_name );
    140150                WP_Block_Type_Registry::get_instance()->unregister( self::$context_block_name );
    141151                WP_Block_Type_Registry::get_instance()->unregister( self::$non_dynamic_block_name );
     152                WP_Block_Type_Registry::get_instance()->unregister( self::$dynamic_block_with_boolean_attributes_block_name );
    142153                parent::tearDown();
    143154        }
    144155
    class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca 
    195206                register_block_type( self::$non_dynamic_block_name );
    196207        }
    197208
     209        /**
     210         * Registers the dynamic with boolean attributes block name.
     211         *
     212         * @since 5.5.0
     213         */
     214        protected function register_dynamic_block_with_boolean_attributes() {
     215                register_block_type(
     216                        self::$dynamic_block_with_boolean_attributes_block_name,
     217                        array(
     218                                'attributes'      => array(
     219                                        'boolean_true_attribute'  => array(
     220                                                'type'    => 'boolean',
     221                                                'default' => true,
     222                                        ),
     223                                        'boolean_false_attribute' => array(
     224                                                'type'    => 'boolean',
     225                                                'default' => false,
     226                                        ),
     227                                ),
     228                                'render_callback' => array( $this, 'render_test_block' ),
     229                        )
     230                );
     231        }
     232
    198233        /**
    199234         * Test render callback.
    200235         *
    class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca 
    522557                $this->assertErrorResponse( 'block_invalid', $response, 404 );
    523558        }
    524559
     560        /**
     561         * @ticket 50620
     562         */
     563        public function test_get_sanitized_attributes_for_dynamic_block_with_boolean_attributes() {
     564                wp_set_current_user( self::$user_id );
     565
     566                $request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$dynamic_block_with_boolean_attributes_block_name );
     567
     568                $attributes = array(
     569                        'boolean_true_attribute'  => 'true',
     570                        'boolean_false_attribute' => 'false',
     571                );
     572
     573                $expected_attributes                            = $attributes;
     574                $expected_attributes['boolean_true_attribute']  = filter_var( $attributes['boolean_true_attribute'], FILTER_VALIDATE_BOOLEAN );
     575                $expected_attributes['boolean_false_attribute'] = filter_var( $attributes['boolean_false_attribute'], FILTER_VALIDATE_BOOLEAN );
     576
     577                $request->set_param( 'context', 'edit' );
     578                $request->set_param( 'attributes', $attributes );
     579                $response = rest_get_server()->dispatch( $request );
     580                $this->assertEquals( 200, $response->get_status() );
     581                $data = $response->get_data();
     582
     583                $this->assertEquals( $expected_attributes, json_decode( $data['rendered'], true ) );
     584        }
     585
    525586        /**
    526587         * Get item schema.
    527588         *