Skip to content

Commit 08bcffa

Browse files
committed
Add tests to ensure that dynamic blocks are replaced one-by-one
1 parent 0b78c64 commit 08bcffa

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

phpunit/class-dynamic-blocks-render-test.php

+35-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
* Test do_blocks
1010
*/
1111
class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
12+
13+
/**
14+
* Dummy block instance number.
15+
*
16+
* @var int
17+
*/
18+
protected $dummy_block_instance_number = 0;
19+
1220
/**
1321
* Dummy block rendering function.
1422
*
@@ -17,13 +25,23 @@ class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
1725
* @return string Block output.
1826
*/
1927
function render_dummy_block( $attributes ) {
20-
return $attributes['value'];
28+
$this->dummy_block_instance_number += 1;
29+
return $this->dummy_block_instance_number . ':' . $attributes['value'];
2130
}
2231

32+
/**
33+
* Tear down.
34+
*/
2335
function tearDown() {
36+
$this->dummy_block_instance_number = 0;
2437
$GLOBALS['wp_registered_blocks'] = array();
2538
}
2639

40+
/**
41+
* Test dynamic blocks that lack content, including void blocks.
42+
*
43+
* @covers do_blocks
44+
*/
2745
function test_dynamic_block_rendering() {
2846
$settings = array(
2947
'render' => array(
@@ -32,23 +50,34 @@ function test_dynamic_block_rendering() {
3250
),
3351
);
3452
register_block_type( 'core/dummy', $settings );
53+
54+
// The duplicated dynamic blocks below are there to ensure that do_blocks() replaces each one-by-one.
3555
$post_content =
3656
'before' .
3757
'<!-- wp:core/dummy value="b1" --><!-- /wp:core/dummy -->' .
58+
'<!-- wp:core/dummy value="b1" --><!-- /wp:core/dummy -->' .
3859
'between' .
39-
'<!-- wp:core/dummy value="b2" --><!-- /wp:core/dummy -->' .
60+
'<!-- wp:core/dummy value="b2" /-->' .
61+
'<!-- wp:core/dummy value="b2" /-->' .
4062
'after';
4163

4264
$updated_post_content = do_blocks( $post_content );
4365
$this->assertEquals( $updated_post_content,
4466
'before' .
45-
'b1' .
67+
'1:b1' .
68+
'2:b1' .
4669
'between' .
47-
'b2' .
70+
'3:b2' .
71+
'4:b2' .
4872
'after'
4973
);
5074
}
5175

76+
/**
77+
* Test dynamic blocks that contain content.
78+
*
79+
* @covers do_blocks
80+
*/
5281
function test_dynamic_block_rendering_with_content() {
5382
$settings = array(
5483
'render' => array(
@@ -67,9 +96,9 @@ function test_dynamic_block_rendering_with_content() {
6796
$updated_post_content = do_blocks( $post_content );
6897
$this->assertEquals( $updated_post_content,
6998
'before' .
70-
'b1' .
99+
'1:b1' .
71100
'between' .
72-
'b2' .
101+
'2:b2' .
73102
'after'
74103
);
75104
}

0 commit comments

Comments
 (0)