Make WordPress Core


Ignore:
Timestamp:
10/28/2023 01:00:14 AM (7 months ago)
Author:
SergeyBiryukov
Message:

Blocks: Parse the arguments earlier in register_block_type_from_metadata().

This makes it possible to register a block by passing an array of arguments, without the presence of a block.json file.

Follow-up to [48141], [49948].

Props aristath, spacedmonkey, mukesh27, costdev, audrasjb, oglekler, felipeelia, hellofromTonya.
Fixes #56865.

File:
1 edited

Legend:

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

    r56607 r57026  
    601601
    602602    /**
     603     * Tests registering a block using arguments instead of a block.json file.
     604     *
     605     * @ticket 56865
     606     *
     607     * @covers ::register_block_type_from_metadata
     608     */
     609    public function test_register_block_type_from_metadata_with_arguments() {
     610        $result = register_block_type_from_metadata(
     611            '',
     612            array(
     613                'api_version' => 2,
     614                'name'        => 'tests/notice-from-array',
     615                'title'       => 'Notice from array',
     616                'category'    => 'common',
     617                'icon'        => 'star',
     618                'description' => 'Shows warning, error or success notices… (registered from an array)',
     619                'keywords'    => array(
     620                    'alert',
     621                    'message',
     622                ),
     623                'textdomain'  => 'notice-from-array',
     624            )
     625        );
     626
     627        $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' );
     628        $this->assertSame( 2, $result->api_version, 'The API version is incorrect' );
     629        $this->assertSame( 'tests/notice-from-array', $result->name, 'The block name is incorrect' );
     630        $this->assertSame( 'Notice from array', $result->title, 'The block title is incorrect' );
     631        $this->assertSame( 'common', $result->category, 'The block category is incorrect' );
     632        $this->assertSame( 'star', $result->icon, 'The block icon is incorrect' );
     633        $this->assertSame(
     634            'Shows warning, error or success notices… (registered from an array)',
     635            $result->description,
     636            'The block description is incorrect'
     637        );
     638        $this->assertSameSets( array( 'alert', 'message' ), $result->keywords, 'The block keywords are incorrect' );
     639    }
     640
     641    /**
     642     * Tests that defined $args can properly override the block.json file.
     643     *
     644     * @ticket 56865
     645     *
     646     * @covers ::register_block_type_from_metadata
     647     */
     648    public function test_block_registers_with_args_override() {
     649        $result = register_block_type_from_metadata(
     650            DIR_TESTDATA . '/blocks/notice',
     651            array(
     652                'name'  => 'tests/notice-with-overrides',
     653                'title' => 'Overriden title',
     654                'style' => array( 'tests-notice-style-overridden' ),
     655            )
     656        );
     657
     658        $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' );
     659        $this->assertSame( 2, $result->api_version, 'The API version is incorrect' );
     660        $this->assertSame( 'tests/notice-with-overrides', $result->name, 'The block name was not overridden' );
     661        $this->assertSame( 'Overriden title', $result->title, 'The block title was not overridden' );
     662        $this->assertSameSets(
     663            array( 'tests-notice-editor-script' ),
     664            $result->editor_script_handles,
     665            'The block editor script is incorrect'
     666        );
     667        $this->assertSameSets(
     668            array( 'tests-notice-style-overridden' ),
     669            $result->style_handles,
     670            'The block style was not overridden'
     671        );
     672        $this->assertIsCallable( $result->render_callback );
     673    }
     674
     675    /**
     676     * Tests that when the `name` is missing, `register_block_type_from_metadata()`
     677     * will return `false`.
     678     *
     679     * @ticket 56865
     680     *
     681     * @covers ::register_block_type_from_metadata
     682     *
     683     * @dataProvider data_register_block_registers_with_args_override_returns_false_when_name_is_missing
     684     *
     685     * @param string $file The metadata file.
     686     * @param array  $args Array of block type arguments.
     687     */
     688    public function test_block_registers_with_args_override_returns_false_when_name_is_missing( $file, $args ) {
     689        $this->assertFalse( register_block_type_from_metadata( $file, $args ) );
     690    }
     691
     692    /**
     693     * Data provider.
     694     *
     695     * @return array[]
     696     */
     697    public function data_register_block_registers_with_args_override_returns_false_when_name_is_missing() {
     698        return array(
     699            'no block.json file and no name argument' => array(
     700                'file' => '', // No block.json file.
     701                'args' => array(
     702                    'title' => 'Overriden title',
     703                    'style' => array( 'tests-notice-style-overridden' ),
     704                ),
     705            ),
     706            'existing file and args not an array'     => array(
     707                // A file that exists but is empty. This will bypass the file_exists() check.
     708                'file' => DIR_TESTDATA . '/blocks/notice/block.js',
     709                'args' => false,
     710            ),
     711            'existing file and args[name] missing'    => array(
     712                // A file that exists but is empty. This will bypass the file_exists() check.
     713                'file' => DIR_TESTDATA . '/blocks/notice/block.js',
     714                'args' => array(
     715                    'title' => 'Overriden title',
     716                    'style' => array( 'tests-notice-style-overridden' ),
     717                ),
     718            ),
     719        );
     720    }
     721
     722    /**
    603723     * Tests that the function returns the registered block when the `block.json`
    604724     * is found in the fixtures directory.
Note: See TracChangeset for help on using the changeset viewer.