9
9
* Test do_blocks
10
10
*/
11
11
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
+
12
20
/**
13
21
* Dummy block rendering function.
14
22
*
@@ -17,13 +25,23 @@ class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
17
25
* @return string Block output.
18
26
*/
19
27
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 ' ];
21
30
}
22
31
32
+ /**
33
+ * Tear down.
34
+ */
23
35
function tearDown () {
36
+ $ this ->dummy_block_instance_number = 0 ;
24
37
$ GLOBALS ['wp_registered_blocks ' ] = array ();
25
38
}
26
39
40
+ /**
41
+ * Test dynamic blocks that lack content, including void blocks.
42
+ *
43
+ * @covers do_blocks
44
+ */
27
45
function test_dynamic_block_rendering () {
28
46
$ settings = array (
29
47
'render ' => array (
@@ -32,23 +50,34 @@ function test_dynamic_block_rendering() {
32
50
),
33
51
);
34
52
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.
35
55
$ post_content =
36
56
'before ' .
37
57
'<!-- wp:core/dummy value="b1" --><!-- /wp:core/dummy --> ' .
58
+ '<!-- wp:core/dummy value="b1" --><!-- /wp:core/dummy --> ' .
38
59
'between ' .
39
- '<!-- wp:core/dummy value="b2" --><!-- /wp:core/dummy --> ' .
60
+ '<!-- wp:core/dummy value="b2" /--> ' .
61
+ '<!-- wp:core/dummy value="b2" /--> ' .
40
62
'after ' ;
41
63
42
64
$ updated_post_content = do_blocks ( $ post_content );
43
65
$ this ->assertEquals ( $ updated_post_content ,
44
66
'before ' .
45
- 'b1 ' .
67
+ '1:b1 ' .
68
+ '2:b1 ' .
46
69
'between ' .
47
- 'b2 ' .
70
+ '3:b2 ' .
71
+ '4:b2 ' .
48
72
'after '
49
73
);
50
74
}
51
75
76
+ /**
77
+ * Test dynamic blocks that contain content.
78
+ *
79
+ * @covers do_blocks
80
+ */
52
81
function test_dynamic_block_rendering_with_content () {
53
82
$ settings = array (
54
83
'render ' => array (
@@ -67,9 +96,9 @@ function test_dynamic_block_rendering_with_content() {
67
96
$ updated_post_content = do_blocks ( $ post_content );
68
97
$ this ->assertEquals ( $ updated_post_content ,
69
98
'before ' .
70
- 'b1 ' .
99
+ '1: b1 ' .
71
100
'between ' .
72
- 'b2 ' .
101
+ '2: b2 ' .
73
102
'after '
74
103
);
75
104
}
0 commit comments