mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 07:40:07 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@57562 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -11,10 +11,24 @@
|
||||
*/
|
||||
class Tests_Block_Bindings_Register extends WP_UnitTestCase {
|
||||
|
||||
const TEST_SOURCE_NAME = 'test/source';
|
||||
const TEST_SOURCE_PROPERTIES = array(
|
||||
'label' => 'Test source',
|
||||
);
|
||||
public static $test_source_name = 'test/source';
|
||||
public static $test_source_properties = array();
|
||||
|
||||
/**
|
||||
* Set up before each test.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
|
||||
self::$test_source_properties = array(
|
||||
'label' => 'Test source',
|
||||
'get_value_callback' => function () {
|
||||
return 'test-value';
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down after each test.
|
||||
@@ -39,24 +53,25 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
|
||||
* @covers ::register_block_bindings_source
|
||||
* @covers ::get_all_registered_block_bindings_sources
|
||||
* @covers ::get_block_bindings_source
|
||||
* @covers WP_Block_Bindings_Source::__construct
|
||||
*/
|
||||
public function test_get_all_registered() {
|
||||
$source_one_name = 'test/source-one';
|
||||
$source_one_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_one_properties = self::$test_source_properties;
|
||||
register_block_bindings_source( $source_one_name, $source_one_properties );
|
||||
|
||||
$source_two_name = 'test/source-two';
|
||||
$source_two_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_two_properties = self::$test_source_properties;
|
||||
register_block_bindings_source( $source_two_name, $source_two_properties );
|
||||
|
||||
$source_three_name = 'test/source-three';
|
||||
$source_three_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_three_properties = self::$test_source_properties;
|
||||
register_block_bindings_source( $source_three_name, $source_three_properties );
|
||||
|
||||
$expected = array(
|
||||
$source_one_name => array_merge( array( 'name' => $source_one_name ), $source_one_properties ),
|
||||
$source_two_name => array_merge( array( 'name' => $source_two_name ), $source_two_properties ),
|
||||
$source_three_name => array_merge( array( 'name' => $source_three_name ), $source_three_properties ),
|
||||
$source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ),
|
||||
$source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ),
|
||||
$source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ),
|
||||
'core/post-meta' => get_block_bindings_source( 'core/post-meta' ),
|
||||
'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ),
|
||||
);
|
||||
@@ -72,15 +87,16 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
|
||||
*
|
||||
* @covers ::register_block_bindings_source
|
||||
* @covers ::unregister_block_bindings_source
|
||||
* @covers WP_Block_Bindings_Source::__construct
|
||||
*/
|
||||
public function test_unregister_block_source() {
|
||||
register_block_bindings_source( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
|
||||
register_block_bindings_source( self::$test_source_name, self::$test_source_properties );
|
||||
|
||||
$result = unregister_block_bindings_source( self::TEST_SOURCE_NAME );
|
||||
$this->assertSame(
|
||||
array_merge(
|
||||
array( 'name' => self::TEST_SOURCE_NAME ),
|
||||
self::TEST_SOURCE_PROPERTIES
|
||||
$result = unregister_block_bindings_source( self::$test_source_name );
|
||||
$this->assertEquals(
|
||||
new WP_Block_Bindings_Source(
|
||||
self::$test_source_name,
|
||||
self::$test_source_properties
|
||||
),
|
||||
$result
|
||||
);
|
||||
|
||||
@@ -13,10 +13,8 @@
|
||||
*/
|
||||
class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
|
||||
const TEST_SOURCE_NAME = 'test/source';
|
||||
const TEST_SOURCE_PROPERTIES = array(
|
||||
'label' => 'Test source',
|
||||
);
|
||||
public static $test_source_name = 'test/source';
|
||||
public static $test_source_properties = array();
|
||||
|
||||
/**
|
||||
* Fake block bindings registry.
|
||||
@@ -35,6 +33,13 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
parent::set_up();
|
||||
|
||||
$this->registry = new WP_Block_Bindings_Registry();
|
||||
|
||||
self::$test_source_properties = array(
|
||||
'label' => 'Test source',
|
||||
'get_value_callback' => function () {
|
||||
return 'test-value';
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,7 +63,7 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
|
||||
*/
|
||||
public function test_register_invalid_non_string_names() {
|
||||
$result = $this->registry->register( 1, self::TEST_SOURCE_PROPERTIES );
|
||||
$result = $this->registry->register( 1, self::$test_source_properties );
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
@@ -72,7 +77,7 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
|
||||
*/
|
||||
public function test_register_invalid_names_without_namespace() {
|
||||
$result = $this->registry->register( 'post-meta', self::TEST_SOURCE_PROPERTIES );
|
||||
$result = $this->registry->register( 'post-meta', self::$test_source_properties );
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
@@ -100,7 +105,60 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
|
||||
*/
|
||||
public function test_register_invalid_uppercase_characters() {
|
||||
$result = $this->registry->register( 'Core/PostMeta', self::TEST_SOURCE_PROPERTIES );
|
||||
$result = $this->registry->register( 'Core/PostMeta', self::$test_source_properties );
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Should reject block bindings registration without a label.
|
||||
*
|
||||
* @ticket 60282
|
||||
*
|
||||
* @covers WP_Block_Bindings_Registry::register
|
||||
*
|
||||
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
|
||||
*/
|
||||
public function test_register_invalid_missing_label() {
|
||||
|
||||
// Remove the label from the properties.
|
||||
unset( self::$test_source_properties['label'] );
|
||||
|
||||
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Should reject block bindings registration without a get_value_callback.
|
||||
*
|
||||
* @ticket 60282
|
||||
*
|
||||
* @covers WP_Block_Bindings_Registry::register
|
||||
*
|
||||
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
|
||||
*/
|
||||
public function test_register_invalid_missing_get_value_callback() {
|
||||
|
||||
// Remove the get_value_callback from the properties.
|
||||
unset( self::$test_source_properties['get_value_callback'] );
|
||||
|
||||
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Should reject block bindings registration if `get_value_callback` is not a callable.
|
||||
*
|
||||
* @ticket 60282
|
||||
*
|
||||
* @covers WP_Block_Bindings_Registry::register
|
||||
*
|
||||
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
|
||||
*/
|
||||
public function test_register_invalid_incorrect_callback_type() {
|
||||
|
||||
self::$test_source_properties['get_value_callback'] = 'not-a-callback';
|
||||
|
||||
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
@@ -110,13 +168,14 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
* @ticket 60282
|
||||
*
|
||||
* @covers WP_Block_Bindings_Registry::register
|
||||
* @covers WP_Block_Bindings_Source::__construct
|
||||
*/
|
||||
public function test_register_block_binding_source() {
|
||||
$result = $this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
|
||||
$this->assertSame(
|
||||
array_merge(
|
||||
array( 'name' => self::TEST_SOURCE_NAME ),
|
||||
self::TEST_SOURCE_PROPERTIES
|
||||
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
|
||||
$this->assertEquals(
|
||||
new WP_Block_Bindings_Source(
|
||||
self::$test_source_name,
|
||||
self::$test_source_properties
|
||||
),
|
||||
$result
|
||||
);
|
||||
@@ -143,15 +202,16 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
*
|
||||
* @covers WP_Block_Bindings_Registry::register
|
||||
* @covers WP_Block_Bindings_Registry::unregister
|
||||
* WP_Block_Bindings_Source::__construct
|
||||
*/
|
||||
public function test_unregister_block_source() {
|
||||
$this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
|
||||
$this->registry->register( self::$test_source_name, self::$test_source_properties );
|
||||
|
||||
$result = $this->registry->unregister( self::TEST_SOURCE_NAME );
|
||||
$this->assertSame(
|
||||
array_merge(
|
||||
array( 'name' => self::TEST_SOURCE_NAME ),
|
||||
self::TEST_SOURCE_PROPERTIES
|
||||
$result = $this->registry->unregister( self::$test_source_name );
|
||||
$this->assertEquals(
|
||||
new WP_Block_Bindings_Source(
|
||||
self::$test_source_name,
|
||||
self::$test_source_properties
|
||||
),
|
||||
$result
|
||||
);
|
||||
@@ -164,28 +224,29 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
*
|
||||
* @covers WP_Block_Bindings_Registry::register
|
||||
* @covers WP_Block_Bindings_Registry::get_all_registered
|
||||
* WP_Block_Bindings_Source::__construct
|
||||
*/
|
||||
public function test_get_all_registered() {
|
||||
$source_one_name = 'test/source-one';
|
||||
$source_one_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_one_properties = self::$test_source_properties;
|
||||
$this->registry->register( $source_one_name, $source_one_properties );
|
||||
|
||||
$source_two_name = 'test/source-two';
|
||||
$source_two_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_two_properties = self::$test_source_properties;
|
||||
$this->registry->register( $source_two_name, $source_two_properties );
|
||||
|
||||
$source_three_name = 'test/source-three';
|
||||
$source_three_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_three_properties = self::$test_source_properties;
|
||||
$this->registry->register( $source_three_name, $source_three_properties );
|
||||
|
||||
$expected = array(
|
||||
$source_one_name => array_merge( array( 'name' => $source_one_name ), $source_one_properties ),
|
||||
$source_two_name => array_merge( array( 'name' => $source_two_name ), $source_two_properties ),
|
||||
$source_three_name => array_merge( array( 'name' => $source_three_name ), $source_three_properties ),
|
||||
$source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ),
|
||||
$source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ),
|
||||
$source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ),
|
||||
);
|
||||
|
||||
$registered = $this->registry->get_all_registered();
|
||||
$this->assertSame( $expected, $registered );
|
||||
$this->assertEquals( $expected, $registered );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,7 +258,7 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
* @covers WP_Block_Bindings_Registry::get_registered
|
||||
*/
|
||||
public function test_get_registered_rejects_unknown_source_name() {
|
||||
$this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
|
||||
$this->registry->register( self::$test_source_name, self::$test_source_properties );
|
||||
|
||||
$source = $this->registry->get_registered( 'test/unknown-source' );
|
||||
$this->assertNull( $source );
|
||||
@@ -210,26 +271,26 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
*
|
||||
* @covers WP_Block_Bindings_Registry::register
|
||||
* @covers WP_Block_Bindings_Registry::get_registered
|
||||
* @covers WP_Block_Bindings_Source::__construct
|
||||
*/
|
||||
public function test_get_registered() {
|
||||
$source_one_name = 'test/source-one';
|
||||
$source_one_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_one_properties = self::$test_source_properties;
|
||||
$this->registry->register( $source_one_name, $source_one_properties );
|
||||
|
||||
$source_two_name = 'test/source-two';
|
||||
$source_two_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_two_properties = self::$test_source_properties;
|
||||
$this->registry->register( $source_two_name, $source_two_properties );
|
||||
|
||||
$source_three_name = 'test/source-three';
|
||||
$source_three_properties = self::TEST_SOURCE_PROPERTIES;
|
||||
$source_three_properties = self::$test_source_properties;
|
||||
$this->registry->register( $source_three_name, $source_three_properties );
|
||||
|
||||
$result = $this->registry->get_registered( 'test/source-two' );
|
||||
$this->assertSame(
|
||||
array_merge(
|
||||
array( 'name' => $source_two_name ),
|
||||
$source_two_properties
|
||||
),
|
||||
$expected = new WP_Block_Bindings_Source( $source_two_name, $source_two_properties );
|
||||
$result = $this->registry->get_registered( 'test/source-two' );
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$result
|
||||
);
|
||||
}
|
||||
@@ -255,9 +316,9 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
|
||||
* @covers WP_Block_Bindings_Registry::is_registered
|
||||
*/
|
||||
public function test_is_registered_for_known_source() {
|
||||
$this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
|
||||
$this->registry->register( self::$test_source_name, self::$test_source_properties );
|
||||
|
||||
$result = $this->registry->is_registered( self::TEST_SOURCE_NAME );
|
||||
$result = $this->registry->is_registered( self::$test_source_name );
|
||||
$this->assertTrue( $result );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user