Make WordPress Core


Ignore:
Timestamp:
06/23/2020 03:43:19 PM (5 years ago)
Author:
gziolo
Message:

Editor: Introduce new API method that register block from block.json metadata file

Backports changes added to Gutenberg in:

register_block_type_from_metadata function is going to be used to register all blocks on the server using block.json metadata files.

Props ocean90, azaozz, aduth, mcsf, jorgefilipecosta, spacedmonkey, nosolosw, swissspidy and noahtallen.
Fixes #50263.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/register.php

    r46586 r48141  
    104104
    105105    /**
     106     * @ticket 50263
     107     */
     108    function test_does_not_remove_block_asset_path_prefix() {
     109        $result = remove_block_asset_path_prefix( 'script-handle' );
     110
     111        $this->assertSame( 'script-handle', $result );
     112    }
     113
     114    /**
     115     * @ticket 50263
     116     */
     117    function test_removes_block_asset_path_prefix() {
     118        $result = remove_block_asset_path_prefix( 'file:./block.js' );
     119
     120        $this->assertSame( './block.js', $result );
     121    }
     122
     123    /**
     124     * @ticket 50263
     125     */
     126    function test_generate_block_asset_handle() {
     127        $block_name = 'unit-tests/my-block';
     128
     129        $this->assertSame(
     130            'unit-tests-my-block-editor-script',
     131            generate_block_asset_handle( $block_name, 'editorScript' )
     132        );
     133        $this->assertSame(
     134            'unit-tests-my-block-script',
     135            generate_block_asset_handle( $block_name, 'script' )
     136        );
     137        $this->assertSame(
     138            'unit-tests-my-block-editor-style',
     139            generate_block_asset_handle( $block_name, 'editorStyle' )
     140        );
     141        $this->assertSame(
     142            'unit-tests-my-block-style',
     143            generate_block_asset_handle( $block_name, 'style' )
     144        );
     145    }
     146
     147    /**
     148     * @ticket 50263
     149     */
     150    function test_field_not_found_register_block_script_handle() {
     151        $result = register_block_script_handle( array(), 'script' );
     152
     153        $this->assertFalse( $result );
     154    }
     155
     156    /**
     157     * @ticket 50263
     158     */
     159    function test_empty_value_register_block_script_handle() {
     160        $metadata = array( 'script' => '' );
     161        $result   = register_block_script_handle( $metadata, 'script' );
     162
     163        $this->assertFalse( $result );
     164    }
     165
     166    /**
     167     * @expectedIncorrectUsage register_block_script_handle
     168     * @ticket 50263
     169     */
     170    function test_missing_asset_file_register_block_script_handle() {
     171        $metadata = array(
     172            'file'   => __FILE__,
     173            'name'   => 'unit-tests/test-block',
     174            'script' => 'file:./fixtures/missing-asset.js',
     175        );
     176        $result   = register_block_script_handle( $metadata, 'script' );
     177
     178        $this->assertFalse( $result );
     179    }
     180
     181    /**
     182     * @ticket 50263
     183     */
     184    function test_handle_passed_register_block_script_handle() {
     185        $metadata = array(
     186            'editorScript' => 'test-script-handle',
     187        );
     188        $result   = register_block_script_handle( $metadata, 'editorScript' );
     189
     190        $this->assertSame( 'test-script-handle', $result );
     191    }
     192
     193    /**
     194     * @ticket 50263
     195     */
     196    function test_success_register_block_script_handle() {
     197        $metadata = array(
     198            'file'   => __FILE__,
     199            'name'   => 'unit-tests/test-block',
     200            'script' => 'file:./fixtures/block.js',
     201        );
     202        $result   = register_block_script_handle( $metadata, 'script' );
     203
     204        $this->assertSame( 'unit-tests-test-block-script', $result );
     205    }
     206
     207    /**
     208     * @ticket 50263
     209     */
     210    function test_field_not_found_register_block_style_handle() {
     211        $result = register_block_style_handle( array(), 'style' );
     212
     213        $this->assertFalse( $result );
     214    }
     215
     216    /**
     217     * @ticket 50263
     218     */
     219    function test_empty_value_found_register_block_style_handle() {
     220        $metadata = array( 'style' => '' );
     221        $result   = register_block_style_handle( $metadata, 'style' );
     222
     223        $this->assertFalse( $result );
     224    }
     225
     226    /**
     227     * @ticket 50263
     228     */
     229    function test_handle_passed_register_block_style_handle() {
     230        $metadata = array(
     231            'style' => 'test-style-handle',
     232        );
     233        $result   = register_block_style_handle( $metadata, 'style' );
     234
     235        $this->assertSame( 'test-style-handle', $result );
     236    }
     237
     238    /**
     239     * @ticket 50263
     240     */
     241    function test_success_register_block_style_handle() {
     242        $metadata = array(
     243            'file'  => __FILE__,
     244            'name'  => 'unit-tests/test-block',
     245            'style' => 'file:./fixtures/block.css',
     246        );
     247        $result   = register_block_style_handle( $metadata, 'style' );
     248
     249        $this->assertSame( 'unit-tests-test-block-style', $result );
     250    }
     251
     252    /**
     253     * Tests that the function returns false when the `block.json` is not found
     254     * in the WordPress core.
     255     *
     256     * @ticket 50263
     257     */
     258    function test_metadata_not_found_in_wordpress_core() {
     259        $result = register_block_type_from_metadata( 'unknown' );
     260
     261        $this->assertFalse( $result );
     262    }
     263
     264    /**
     265     * Tests that the function returns false when the `block.json` is not found
     266     * in the current directory.
     267     *
     268     * @ticket 50263
     269     */
     270    function test_metadata_not_found_in_the_current_directory() {
     271        $result = register_block_type_from_metadata( __DIR__ );
     272
     273        $this->assertFalse( $result );
     274    }
     275
     276    /**
     277     * Tests that the function returns the registered block when the `block.json`
     278     * is found in the fixtures directory.
     279     *
     280     * @ticket 50263
     281     */
     282    function test_block_registers_with_metadata_fixture() {
     283        $result = register_block_type_from_metadata(
     284            __DIR__ . '/fixtures'
     285        );
     286
     287        $this->assertInstanceOf( 'WP_Block_Type', $result );
     288        $this->assertSame( 'my-plugin/notice', $result->name );
     289        $this->assertSame( 'Notice', $result->title );
     290        $this->assertSame( 'common', $result->category );
     291        $this->assertEqualSets( array( 'core/group' ), $result->parent );
     292        $this->assertSame( 'star', $result->icon );
     293        $this->assertSame( 'Shows warning, error or success notices…', $result->description );
     294        $this->assertEqualSets( array( 'alert', 'message' ), $result->keywords );
     295        $this->assertEquals(
     296            array(
     297                'message' => array(
     298                    'type'     => 'string',
     299                    'source'   => 'html',
     300                    'selector' => '.message',
     301                ),
     302            ),
     303            $result->attributes
     304        );
     305        $this->assertEquals(
     306            array(
     307                'my-plugin/message' => 'message',
     308            ),
     309            $result->provides_context
     310        );
     311        $this->assertEqualSets( array( 'groupId' ), $result->uses_context );
     312        $this->assertEquals(
     313            array(
     314                'align'             => true,
     315                'lightBlockWrapper' => true,
     316            ),
     317            $result->supports
     318        );
     319        $this->assertEquals(
     320            array(
     321                array(
     322                    'name'      => 'default',
     323                    'label'     => 'Default',
     324                    'isDefault' => true,
     325                ),
     326                array(
     327                    'name'  => 'other',
     328                    'label' => 'Other',
     329                ),
     330            ),
     331            $result->styles
     332        );
     333        $this->assertEquals(
     334            array(
     335                'attributes' => array(
     336                    'message' => 'This is a notice!',
     337                ),
     338            ),
     339            $result->example
     340        );
     341        $this->assertSame( 'my-plugin-notice-editor-script', $result->editor_script );
     342        $this->assertSame( 'my-plugin-notice-script', $result->script );
     343        $this->assertSame( 'my-plugin-notice-editor-style', $result->editor_style );
     344        $this->assertSame( 'my-plugin-notice-style', $result->style );
     345    }
     346
     347    /**
    106348     * @ticket 45109
    107349     */
Note: See TracChangeset for help on using the changeset viewer.