forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-dynamic-blocks-render-test.php
167 lines (143 loc) · 3.51 KB
/
class-dynamic-blocks-render-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Dynamic blocks rendering Test
*
* @package Gutenberg
*/
/**
* Test do_blocks, WP_Block_Type::render
*/
class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
/**
* Dummy block instance number.
*
* @var int
*/
protected $dummy_block_instance_number = 0;
/**
* Dummy block rendering function.
*
* @param array $attributes Block attributes.
*
* @return string Block output.
*/
function render_dummy_block( $attributes ) {
$this->dummy_block_instance_number += 1;
return $this->dummy_block_instance_number . ':' . $attributes['value'];
}
/**
* Dummy block rendering function, returning numeric value.
*
* @return number Block output.
*/
function render_dummy_block_numeric() {
return 10;
}
/**
* Dummy block rendering function, creating a new WP_Query instance.
*
* @return string Block output.
*/
function render_dummy_block_wp_query() {
$content = '';
$recent = new WP_Query( array(
'numberposts' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true,
) );
while ( $recent->have_posts() ) {
$recent->the_post();
$content .= get_the_title();
}
wp_reset_postdata();
return $content;
}
/**
* Tear down.
*/
function tearDown() {
parent::tearDown();
$this->dummy_block_instance_number = 0;
$registry = WP_Block_Type_Registry::get_instance();
$registry->unregister( 'core/dummy' );
}
/**
* Test dynamic blocks that lack content, including void blocks.
*
* @covers ::do_blocks
*/
function test_dynamic_block_rendering() {
$settings = array(
'render_callback' => array(
$this,
'render_dummy_block',
),
);
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 {"value":"b2"} /-->' .
'after';
$updated_post_content = do_blocks( $post_content );
$this->assertEquals(
$updated_post_content,
'before' .
'1:b1' .
'2:b1' .
'between' .
'3:b2' .
'4:b2' .
'after'
);
}
/**
* Tests that do_blocks() maintains the global $post variable when dynamic
* blocks create new WP_Query instances in their callbacks.
*
* @covers ::do_blocks
*/
function test_global_post_persistence() {
global $post;
register_block_type(
'core/dummy',
array(
'render_callback' => array(
$this,
'render_dummy_block_wp_query',
),
)
);
$posts = self::factory()->post->create_many( 5 );
$post = get_post( end( $posts ) );
$global_post = $post;
do_blocks( '<!-- wp:core/dummy /-->' );
$this->assertEquals( $global_post, $post );
}
/**
* Test dynamic blocks return string value from render, even if render
* callback does not.
*
* @covers WP_Block_Type::render
*/
function test_dynamic_block_renders_string() {
$settings = array(
'render_callback' => array(
$this,
'render_dummy_block_numeric',
),
);
register_block_type( 'core/dummy', $settings );
$block_type = new WP_Block_Type( 'core/dummy', $settings );
$rendered = $block_type->render();
$this->assertSame( '10', $rendered );
$this->assertInternalType( 'string', $rendered );
}
}