Editor: Refactor the way block bindings sources are handled

It fixes the coding style issues reported. It goes further and improves the code quality it other places where the logic for block bindings was added.

Follow-up for [57514].
Props: gziolo, mukesh27, youknowriad, santosguillamot.
See #60282.



git-svn-id: https://develop.svn.wordpress.org/trunk@57526 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Greg Ziółkowski
2024-02-02 20:22:11 +00:00
parent 7e34c4b7d3
commit 235cb39891
10 changed files with 188 additions and 167 deletions
+10 -18
View File
@@ -16,19 +16,6 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
'label' => 'Test source',
);
/**
* Set up before each test.
*
* @since 6.5.0
*/
public function set_up() {
foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) {
unregister_block_bindings_source( $source_name );
}
parent::set_up();
}
/**
* Tear down after each test.
*
@@ -36,7 +23,9 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
*/
public function tear_down() {
foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) {
unregister_block_bindings_source( $source_name );
if ( str_starts_with( $source_name, 'test/' ) ) {
unregister_block_bindings_source( $source_name );
}
}
parent::tear_down();
@@ -49,6 +38,7 @@ 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
*/
public function test_get_all_registered() {
$source_one_name = 'test/source-one';
@@ -64,13 +54,15 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
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 => 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 ),
'core/post-meta' => get_block_bindings_source( 'core/post-meta' ),
'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ),
);
$registered = get_all_registered_block_bindings_sources();
$this->assertSame( $expected, $registered );
$this->assertEquals( $expected, $registered );
}
/**
@@ -1,6 +1,6 @@
<?php
/**
* Unit tests covering the Block Bindings public API.
* Tests for Block Bindings integration with block rendering.
*
* @package WordPress
* @subpackage Blocks
@@ -8,10 +8,8 @@
*
* @group blocks
* @group block-bindings
*
* @covers register_block_bindings_source
*/
class WP_Block_Bindings_Test extends WP_UnitTestCase {
class WP_Block_Bindings_Render extends WP_UnitTestCase {
const SOURCE_NAME = 'test/source';
const SOURCE_LABEL = array(
@@ -19,18 +17,18 @@ class WP_Block_Bindings_Test extends WP_UnitTestCase {
);
/**
* Set up before each test.
* Tear down after each test.
*
* @since 6.5.0
*/
public function set_up() {
public function tear_down() {
foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) {
if ( str_starts_with( $source_name, 'test/' ) ) {
unregister_block_bindings_source( $source_name );
}
}
parent::set_up();
parent::tear_down();
}
/**
@@ -38,7 +36,7 @@ class WP_Block_Bindings_Test extends WP_UnitTestCase {
*
* @ticket 60282
*
* @covers register_block_bindings_source
* @covers ::register_block_bindings_source
*/
public function test_update_block_with_value_from_source() {
$get_value_callback = function () {
@@ -63,7 +61,6 @@ HTML;
$expected = '<p>test source value</p>';
$result = $block->render();
// Check if the block content was updated correctly.
$this->assertEquals( $expected, $result, 'The block content should be updated with the value returned by the source.' );
}
@@ -72,7 +69,7 @@ HTML;
*
* @ticket 60282
*
* @covers register_block_bindings_source
* @covers ::register_block_bindings_source
*/
public function test_passing_arguments_to_source() {
$get_value_callback = function ( $source_args, $block_instance, $attribute_name ) {
@@ -88,19 +85,17 @@ HTML;
)
);
$key = 'test';
$value = 'test';
$block_content = <<<HTML
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "$key"}}}}} --><p>This should not appear</p><!-- /wp:paragraph -->
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "$value"}}}}} --><p>This should not appear</p><!-- /wp:paragraph -->
HTML;
$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block( $parsed_blocks[0] );
$expected = "<p>The attribute name is 'content' and its binding has argument 'key' with value 'test'.</p>";
$expected = "<p>The attribute name is 'content' and its binding has argument 'key' with value '$value'.</p>";
$result = $block->render();
// Check if the block content was updated correctly.
$this->assertEquals( $expected, $result, 'The block content should be updated with the value returned by the source.' );
}
}