Make WordPress Core


Ignore:
Timestamp:
02/08/2024 08:55:18 AM (3 months ago)
Author:
gziolo
Message:

Editor: Introduce WP_Block_Bindings_Source class

Abstracts the block bindings source array into a well-defined object.

Fixes #60447.
See #60282.
Follow-up [57373].
Props czapla, santosguillamot, gziolo.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-bindings/register.php

    r57526 r57562  
    1212class Tests_Block_Bindings_Register extends WP_UnitTestCase {
    1313
    14     const TEST_SOURCE_NAME       = 'test/source';
    15     const TEST_SOURCE_PROPERTIES = array(
    16         'label' => 'Test source',
    17     );
     14    public static $test_source_name       = 'test/source';
     15    public static $test_source_properties = array();
     16
     17    /**
     18     * Set up before each test.
     19     *
     20     * @since 6.5.0
     21     */
     22    public function set_up() {
     23        parent::set_up();
     24
     25        self::$test_source_properties = array(
     26            'label'              => 'Test source',
     27            'get_value_callback' => function () {
     28                return 'test-value';
     29            },
     30        );
     31    }
    1832
    1933    /**
     
    4054     * @covers ::get_all_registered_block_bindings_sources
    4155     * @covers ::get_block_bindings_source
     56     * @covers WP_Block_Bindings_Source::__construct
    4257     */
    4358    public function test_get_all_registered() {
    4459        $source_one_name       = 'test/source-one';
    45         $source_one_properties = self::TEST_SOURCE_PROPERTIES;
     60        $source_one_properties = self::$test_source_properties;
    4661        register_block_bindings_source( $source_one_name, $source_one_properties );
    4762
    4863        $source_two_name       = 'test/source-two';
    49         $source_two_properties = self::TEST_SOURCE_PROPERTIES;
     64        $source_two_properties = self::$test_source_properties;
    5065        register_block_bindings_source( $source_two_name, $source_two_properties );
    5166
    5267        $source_three_name       = 'test/source-three';
    53         $source_three_properties = self::TEST_SOURCE_PROPERTIES;
     68        $source_three_properties = self::$test_source_properties;
    5469        register_block_bindings_source( $source_three_name, $source_three_properties );
    5570
    5671        $expected = array(
    57             $source_one_name         => array_merge( array( 'name' => $source_one_name ), $source_one_properties ),
    58             $source_two_name         => array_merge( array( 'name' => $source_two_name ), $source_two_properties ),
    59             $source_three_name       => array_merge( array( 'name' => $source_three_name ), $source_three_properties ),
     72            $source_one_name         => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ),
     73            $source_two_name         => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ),
     74            $source_three_name       => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ),
    6075            'core/post-meta'         => get_block_bindings_source( 'core/post-meta' ),
    6176            'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ),
     
    7388     * @covers ::register_block_bindings_source
    7489     * @covers ::unregister_block_bindings_source
     90     * @covers WP_Block_Bindings_Source::__construct
    7591     */
    7692    public function test_unregister_block_source() {
    77         register_block_bindings_source( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
     93        register_block_bindings_source( self::$test_source_name, self::$test_source_properties );
    7894
    79         $result = unregister_block_bindings_source( self::TEST_SOURCE_NAME );
    80         $this->assertSame(
    81             array_merge(
    82                 array( 'name' => self::TEST_SOURCE_NAME ),
    83                 self::TEST_SOURCE_PROPERTIES
     95        $result = unregister_block_bindings_source( self::$test_source_name );
     96        $this->assertEquals(
     97            new WP_Block_Bindings_Source(
     98                self::$test_source_name,
     99                self::$test_source_properties
    84100            ),
    85101            $result
Note: See TracChangeset for help on using the changeset viewer.