Blocks: Introduce register_block_type(), unregister_block_type(), and get_dynamic_blocks() functions.

These helper functions allow easy access to the global block registry.

Merges [43743] from the 5.0 branch to trunk.

See #45109.


git-svn-id: https://develop.svn.wordpress.org/trunk@44109 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-12-13 09:53:10 +00:00
parent 9254ae4a72
commit 738609ae9c
4 changed files with 236 additions and 0 deletions
@@ -0,0 +1,17 @@
<p>First Auto Paragraph</p>
<!--more-->
<p>First Gutenberg Paragraph</p>
<p>Second Auto Paragraph</p>
<p>Third Gutenberg Paragraph</p>
<p>Third Auto Paragraph</p>
<p>[someshortcode]</p>
<p>And some content?!</p>
<p>[/someshortcode]</p>
@@ -0,0 +1,25 @@
<p>First Auto Paragraph</p>
<!--more-->
<!-- wp:core/paragraph -->
<p>First Gutenberg Paragraph</p>
<!-- /wp:core/paragraph -->
<p>Second Auto Paragraph</p>
<!-- wp:core/test-self-closing /-->
<!-- wp:core/paragraph -->
<p>Third Gutenberg Paragraph</p>
<!-- /wp:core/paragraph -->
<p>Third Auto Paragraph</p>
<!-- wp:core/shortcode -->
[someshortcode]
And some content?!
[/someshortcode]
<!-- /wp:core/shortcode -->
+141
View File
@@ -0,0 +1,141 @@
<?php
/**
* Block registry tests.
*
* @package WordPress
* @subpackage Blocks
* @since 5.0.0
*/
/**
* Tests for register_block_type(), unregister_block_type(), get_dynamic_block_names()
*
* @since 5.0.0
*
* @group blocks
*/
class WP_Test_Block_Register extends WP_UnitTestCase {
/**
* ID for a test post.
*
* @since 5.0.0
* @var int
*/
protected static $post_id;
/**
* Set up before class.
*
* @since 5.0.0
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$post_id = $factory->post->create(
array(
'post_content' => file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-original.html' ),
)
);
}
/**
* Tear down after class.
*
* @since 5.0.0
*/
public static function wpTearDownAfterClass() {
// Also deletes revisions.
wp_delete_post( self::$post_id, true );
}
/**
* Empty render function for tests to use.
*/
function render_stub() {}
/**
* Tear down after each test.
*
* @since 5.0.0
*/
function tearDown() {
parent::tearDown();
$registry = WP_Block_Type_Registry::get_instance();
foreach ( array( 'test-static', 'test-dynamic' ) as $block_name ) {
$block_name = 'core/' . $block_name;
if ( $registry->is_registered( $block_name ) ) {
$registry->unregister( $block_name );
}
}
}
/**
* @ticket 45109
*/
function test_register_affects_main_registry() {
$name = 'core/test-static';
$settings = array(
'icon' => 'text',
);
register_block_type( $name, $settings );
$registry = WP_Block_Type_Registry::get_instance();
$this->assertTrue( $registry->is_registered( $name ) );
}
/**
* @ticket 45109
*/
function test_unregister_affects_main_registry() {
$name = 'core/test-static';
$settings = array(
'icon' => 'text',
);
register_block_type( $name, $settings );
unregister_block_type( $name );
$registry = WP_Block_Type_Registry::get_instance();
$this->assertFalse( $registry->is_registered( $name ) );
}
/**
* @ticket 45109
*/
function test_get_dynamic_block_names() {
register_block_type( 'core/test-static', array() );
register_block_type( 'core/test-dynamic', array( 'render_callback' => array( $this, 'render_stub' ) ) );
$dynamic_block_names = get_dynamic_block_names();
$this->assertContains( 'core/test-dynamic', $dynamic_block_names );
$this->assertNotContains( 'core/test-static', $dynamic_block_names );
}
/**
* @ticket 45109
*/
function test_has_blocks() {
// Test with passing post ID.
$this->assertTrue( has_blocks( self::$post_id ) );
// Test with passing WP_Post object.
$this->assertTrue( has_blocks( get_post( self::$post_id ) ) );
// Test with passing content string.
$this->assertTrue( has_blocks( get_post( self::$post_id ) ) );
// Test default.
$this->assertFalse( has_blocks() );
$query = new WP_Query( array( 'post__in' => array( self::$post_id ) ) );
$query->the_post();
$this->assertTrue( has_blocks() );
// Test string (without blocks).
$content = file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-expected.html' );
$this->assertFalse( has_blocks( $content ) );
}
}