mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
render.php
503 lines (417 loc) · 12.1 KB
/
render.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
/**
* Block rendering tests
*
* @package WordPress
* @subpackage Blocks
* @since 5.0.0
*/
/**
* Tests for block rendering functions.
*
* @since 5.0.0
*
* @group blocks
*/
class Tests_Blocks_Render extends WP_UnitTestCase {
/**
* The location of the fixtures to test with.
*
* @since 5.0.0
* @var string
*/
protected static $fixtures_dir;
/**
* Test block instance number.
*
* @since 5.0.0
*
* @var int
*/
protected $test_block_instance_number = 0;
/**
* Tear down after each test.
*
* @since 5.0.0
*/
public function tear_down() {
$this->test_block_instance_number = 0;
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( 'core/test' ) ) {
$registry->unregister( 'core/test' );
}
if ( $registry->is_registered( 'core/dynamic' ) ) {
$registry->unregister( 'core/dynamic' );
}
parent::tear_down();
}
/**
* @ticket 45109
*/
public function test_do_blocks_removes_comments() {
$original_html = file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-original.html' );
$expected_html = file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-expected.html' );
$actual_html = do_blocks( $original_html );
$this->assertSameIgnoreEOL( $expected_html, $actual_html );
}
/**
* @ticket 45109
*/
public function test_the_content() {
add_shortcode( 'someshortcode', array( $this, 'handle_shortcode' ) );
$classic_content = "Foo\n\n[someshortcode]\n\nBar\n\n[/someshortcode]\n\nBaz";
$block_content = "<!-- wp:core/paragraph -->\n<p>Foo</p>\n<!-- /wp:core/paragraph -->\n\n<!-- wp:core/shortcode -->[someshortcode]\n\nBar\n\n[/someshortcode]<!-- /wp:core/shortcode -->\n\n<!-- wp:core/paragraph -->\n<p>Baz</p>\n<!-- /wp:core/paragraph -->";
$classic_filtered_content = apply_filters( 'the_content', $classic_content );
$block_filtered_content = apply_filters( 'the_content', $block_content );
// Block rendering add some extra blank lines, but we're not worried about them.
$block_filtered_content = preg_replace( "/\n{2,}/", "\n", $block_filtered_content );
remove_shortcode( 'someshortcode' );
$this->assertSame( trim( $classic_filtered_content ), trim( $block_filtered_content ) );
}
public function handle_shortcode( $atts, $content ) {
return $content;
}
/**
* @ticket 45495
*/
public function test_nested_calls_to_the_content() {
register_block_type(
'core/test',
array(
'render_callback' => array(
$this,
'dynamic_the_content_call',
),
)
);
$content = "foo\n\nbar";
$the_content = apply_filters( 'the_content', '<!-- wp:core/test -->' . $content . '<!-- /wp:core/test -->' );
$this->assertSame( $content, $the_content );
}
public function dynamic_the_content_call( $attrs, $content ) {
apply_filters( 'the_content', '' );
return $content;
}
public function test_can_nest_at_least_so_deep() {
$minimum_depth = 99;
$content = 'deep inside';
for ( $i = 0; $i < $minimum_depth; $i++ ) {
$content = '<!-- wp:core/test -->' . $content . '<!-- /wp:core/test -->';
}
$this->assertSame( 'deep inside', do_blocks( $content ) );
}
public function test_can_nest_at_least_so_deep_with_dynamic_blocks() {
$minimum_depth = 99;
$content = '0';
for ( $i = 0; $i < $minimum_depth; $i++ ) {
$content = '<!-- wp:core/test -->' . $content . '<!-- /wp:core/test -->';
}
register_block_type(
'core/test',
array(
'render_callback' => array(
$this,
'render_dynamic_incrementer',
),
)
);
$this->assertSame( $minimum_depth, (int) do_blocks( $content ) );
}
public function render_dynamic_incrementer( $attrs, $content ) {
return (string) ( 1 + (int) $content );
}
/**
* @ticket 45290
*/
public function test_blocks_arent_autopeed() {
$expected_content = 'test';
$test_content = "<!-- wp:fake/block -->\n$expected_content\n<!-- /wp:fake/block -->";
$current_priority = has_action( 'the_content', 'wpautop' );
$filtered_content = trim( apply_filters( 'the_content', $test_content ) );
$this->assertSame( $expected_content, $filtered_content );
// Check that wpautop() is still defined in the same place.
$this->assertSame( $current_priority, has_action( 'the_content', 'wpautop' ) );
// ... and that the restore function has removed itself.
$this->assertFalse( has_action( 'the_content', '_restore_wpautop_hook' ) );
$test_content = 'test';
$expected_content = "<p>$test_content</p>";
$current_priority = has_action( 'the_content', 'wpautop' );
$filtered_content = trim( apply_filters( 'the_content', $test_content ) );
$this->assertSame( $expected_content, $filtered_content );
$this->assertSame( $current_priority, has_action( 'the_content', 'wpautop' ) );
$this->assertFalse( has_action( 'the_content', '_restore_wpautop_hook' ) );
}
/**
* @ticket 45109
*/
public function data_do_block_test_filenames() {
self::$fixtures_dir = DIR_TESTDATA . '/blocks/fixtures';
$fixture_filenames = array_merge(
glob( self::$fixtures_dir . '/*.json' ),
glob( self::$fixtures_dir . '/*.html' )
);
$fixture_filenames = array_values(
array_unique(
array_map(
array( $this, 'clean_fixture_filename' ),
$fixture_filenames
)
)
);
return array_map(
array( $this, 'pass_parser_fixture_filenames' ),
$fixture_filenames
); }
/**
* @dataProvider data_do_block_test_filenames
* @ticket 45109
*/
public function test_do_block_output( $html_filename, $server_html_filename ) {
$html_path = self::$fixtures_dir . '/' . $html_filename;
$server_html_path = self::$fixtures_dir . '/' . $server_html_filename;
foreach ( array( $html_path, $server_html_path ) as $filename ) {
if ( ! file_exists( $filename ) ) {
throw new Exception( "Missing fixture file: '$filename'" );
}
}
$html = do_blocks( self::strip_r( file_get_contents( $html_path ) ) );
$expected_html = self::strip_r( file_get_contents( $server_html_path ) );
$this->assertSame(
$expected_html,
$html,
"File '$html_path' does not match expected value"
);
}
/**
* @ticket 45109
*/
public function test_dynamic_block_rendering() {
$settings = array(
'render_callback' => array(
$this,
'render_test_block',
),
);
register_block_type( 'core/test', $settings );
// The duplicated dynamic blocks below are there to ensure that do_blocks() replaces each one-by-one.
$post_content =
'before' .
'<!-- wp:core/test {"value":"b1"} --><!-- /wp:core/test -->' .
'<!-- wp:core/test {"value":"b1"} --><!-- /wp:core/test -->' .
'between' .
'<!-- wp:core/test {"value":"b2"} /-->' .
'<!-- wp:core/test {"value":"b2"} /-->' .
'after';
$updated_post_content = do_blocks( $post_content );
$this->assertSame(
$updated_post_content,
'before' .
'1:b1' .
'2:b1' .
'between' .
'3:b2' .
'4:b2' .
'after'
);
}
/**
* @ticket 45109
*/
public function test_global_post_persistence() {
global $post;
register_block_type(
'core/test',
array(
'render_callback' => array(
$this,
'render_test_block_wp_query',
),
)
);
$posts = self::factory()->post->create_many( 5 );
$post = get_post( end( $posts ) );
$global_post = $post;
do_blocks( '<!-- wp:core/test /-->' );
$this->assertSame( $global_post, $post );
}
public function test_render_latest_comments_on_password_protected_post() {
$post_id = self::factory()->post->create(
array(
'post_password' => 'password',
)
);
$comment_text = wp_generate_password( 10, false );
self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_content' => $comment_text,
)
);
$comments = do_blocks( '<!-- wp:latest-comments {"commentsToShow":1,"displayExcerpt":true} /-->' );
$this->assertStringNotContainsString( $comment_text, $comments );
}
/**
* @ticket 45109
*/
public function test_dynamic_block_renders_string() {
$settings = array(
'render_callback' => array(
$this,
'render_test_block_numeric',
),
);
register_block_type( 'core/test', $settings );
$block_type = new WP_Block_Type( 'core/test', $settings );
$rendered = $block_type->render();
$this->assertSame( '10', $rendered );
$this->assertIsString( $rendered );
}
public function test_dynamic_block_gets_inner_html() {
register_block_type(
'core/dynamic',
array(
'render_callback' => array(
$this,
'render_serialize_dynamic_block',
),
)
);
$output = do_blocks( '<!-- wp:dynamic -->inner<!-- /wp:dynamic -->' );
$data = unserialize( base64_decode( $output ) );
$this->assertSame( 'inner', $data[1] );
}
public function test_dynamic_block_gets_rendered_inner_blocks() {
register_block_type(
'core/test',
array(
'render_callback' => array(
$this,
'render_test_block_numeric',
),
)
);
register_block_type(
'core/dynamic',
array(
'render_callback' => array(
$this,
'render_serialize_dynamic_block',
),
)
);
$output = do_blocks( '<!-- wp:dynamic -->before<!-- wp:test /-->after<!-- /wp:dynamic -->' );
$data = unserialize( base64_decode( $output ) );
$this->assertSame( 'before10after', $data[1] );
}
public function test_dynamic_block_gets_rendered_inner_dynamic_blocks() {
register_block_type(
'core/dynamic',
array(
'render_callback' => array(
$this,
'render_serialize_dynamic_block',
),
)
);
$output = do_blocks( '<!-- wp:dynamic -->before<!-- wp:dynamic -->deep inner<!-- /wp:dynamic -->after<!-- /wp:dynamic -->' );
$data = unserialize( base64_decode( $output ) );
$inner = $this->render_serialize_dynamic_block( array(), 'deep inner' );
$this->assertSame( $data[1], 'before' . $inner . 'after' );
}
/**
* Helper function to remove relative paths and extension from a filename, leaving just the fixture name.
*
* @since 5.0.0
*
* @param string $filename The filename to clean.
* @return string The cleaned fixture name.
*/
protected function clean_fixture_filename( $filename ) {
$filename = wp_basename( $filename );
$filename = preg_replace( '/\..+$/', '', $filename );
return $filename;
}
/**
* Helper function to return the filenames needed to test the parser output.
*
* @since 5.0.0
*
* @param string $filename The cleaned fixture name.
* @return array The input and expected output filenames for that fixture.
*/
protected function pass_parser_fixture_filenames( $filename ) {
return array(
"$filename.html",
"$filename.server.html",
);
}
/**
* Helper function to remove '\r' characters from a string.
*
* @since 5.0.0
*
* @param string $input The string to remove '\r' from.
* @return string The input string, with '\r' characters removed.
*/
protected function strip_r( $input ) {
return str_replace( "\r", '', $input );
}
/**
* Test block rendering function.
*
* @since 5.0.0
*
* @param array $attributes Block attributes.
* @return string Block output.
*/
public function render_test_block( $attributes ) {
$this->test_block_instance_number += 1;
return $this->test_block_instance_number . ':' . $attributes['value'];
}
/**
* Test block rendering function, returning numeric value.
*
* @since 5.0.0
*
* @return int Block output.
*/
public function render_test_block_numeric() {
return 10;
}
/**
* Test block rendering function, returning base64 encoded serialised value.
*
* @since 5.0.0
*
* @return string Block output.
*/
public function render_serialize_dynamic_block( $attributes, $content ) {
return base64_encode( serialize( array( $attributes, $content ) ) );
}
/**
* Test block rendering function, creating a new WP_Query instance.
*
* @since 5.0.0
*
* @return string Block output.
*/
public function render_test_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;
}
}