Skip to content

Commit

Permalink
Add tests to ensure that dynamic blocks are replaced one-by-one
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 19, 2017
1 parent 0b78c64 commit 08bcffa
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions phpunit/class-dynamic-blocks-render-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
* Test do_blocks
*/
class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {

/**
* Dummy block instance number.
*
* @var int
*/
protected $dummy_block_instance_number = 0;

/**
* Dummy block rendering function.
*
Expand All @@ -17,13 +25,23 @@ class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
* @return string Block output.
*/
function render_dummy_block( $attributes ) {
return $attributes['value'];
$this->dummy_block_instance_number += 1;
return $this->dummy_block_instance_number . ':' . $attributes['value'];
}

/**
* Tear down.
*/
function tearDown() {
$this->dummy_block_instance_number = 0;
$GLOBALS['wp_registered_blocks'] = array();
}

/**
* Test dynamic blocks that lack content, including void blocks.
*
* @covers do_blocks
*/
function test_dynamic_block_rendering() {
$settings = array(
'render' => array(
Expand All @@ -32,23 +50,34 @@ function test_dynamic_block_rendering() {
),
);
register_block_type( 'core/dummy', $settings );

// The duplicated dynamic blocks below are there to ensure that do_blocks() replaces each one-by-one.
$post_content =
'before' .
'<!-- wp:core/dummy value="b1" --><!-- /wp:core/dummy -->' .
'<!-- wp:core/dummy value="b1" --><!-- /wp:core/dummy -->' .
'between' .
'<!-- wp:core/dummy value="b2" --><!-- /wp:core/dummy -->' .
'<!-- wp:core/dummy value="b2" /-->' .
'<!-- wp:core/dummy value="b2" /-->' .
'after';

$updated_post_content = do_blocks( $post_content );
$this->assertEquals( $updated_post_content,
'before' .
'b1' .
'1:b1' .
'2:b1' .
'between' .
'b2' .
'3:b2' .
'4:b2' .
'after'
);
}

/**
* Test dynamic blocks that contain content.
*
* @covers do_blocks
*/
function test_dynamic_block_rendering_with_content() {
$settings = array(
'render' => array(
Expand All @@ -67,9 +96,9 @@ function test_dynamic_block_rendering_with_content() {
$updated_post_content = do_blocks( $post_content );
$this->assertEquals( $updated_post_content,
'before' .
'b1' .
'1:b1' .
'between' .
'b2' .
'2:b2' .
'after'
);
}
Expand Down

0 comments on commit 08bcffa

Please sign in to comment.