-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathWP_Block_ProcessorTest.php
More file actions
621 lines (492 loc) · 20 KB
/
WP_Block_ProcessorTest.php
File metadata and controls
621 lines (492 loc) · 20 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<?php
/**
* Tests for WP_Block_Processor polyfill.
*
* @package WP_CLI\Entity\Compat
*/
namespace WP_CLI\Entity\Tests\Compat;
use PHPUnit\Framework\TestCase;
use WP_Block_Processor;
use WP_HTML_Span;
/**
* Test the WP_Block_Processor polyfill class.
*
* These tests verify that the polyfill behaves identically to the native
* WordPress implementation.
*/
class WP_Block_ProcessorTest extends TestCase {
/**
* Test next_block finds a simple paragraph block.
*/
public function test_next_block_finds_paragraph() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$this->assertTrue( $processor->next_block() );
$this->assertSame( 'core/paragraph', $processor->get_block_type() );
}
/**
* Test next_block finds void blocks.
*/
public function test_next_block_finds_void_block() {
$content = '<!-- wp:spacer {"height":"50px"} /-->';
$processor = new WP_Block_Processor( $content );
$this->assertTrue( $processor->next_block() );
$this->assertSame( 'core/spacer', $processor->get_block_type() );
$this->assertSame( WP_Block_Processor::VOID, $processor->get_delimiter_type() );
}
/**
* Test next_block with specific block type filter.
*/
public function test_next_block_with_type_filter() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->';
$processor = new WP_Block_Processor( $content );
$this->assertTrue( $processor->next_block( 'heading' ) );
$this->assertSame( 'core/heading', $processor->get_block_type() );
}
/**
* Test next_block returns false when no blocks found.
*/
public function test_next_block_returns_false_for_no_blocks() {
$content = '<p>Just regular HTML</p>';
$processor = new WP_Block_Processor( $content );
$this->assertFalse( $processor->next_block() );
}
/**
* Test next_block skips closers.
*/
public function test_next_block_skips_closers() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->';
$processor = new WP_Block_Processor( $content );
// First block is paragraph.
$this->assertTrue( $processor->next_block() );
$this->assertSame( 'core/paragraph', $processor->get_block_type() );
// Second block is heading (skips the paragraph closer).
$this->assertTrue( $processor->next_block() );
$this->assertSame( 'core/heading', $processor->get_block_type() );
// No more blocks.
$this->assertFalse( $processor->next_block() );
}
/**
* Test get_block_type returns null for freeform content.
*/
public function test_get_block_type_returns_null_for_freeform() {
$content = 'Just text';
$processor = new WP_Block_Processor( $content );
$processor->next_token();
$this->assertNull( $processor->get_block_type() );
$this->assertTrue( $processor->is_html() );
}
/**
* Test get_block_type normalizes implicit core namespace.
*/
public function test_get_block_type_normalizes_namespace() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertSame( 'core/paragraph', $processor->get_block_type() );
}
/**
* Test get_block_type preserves custom namespace.
*/
public function test_get_block_type_preserves_custom_namespace() {
$content = '<!-- wp:my-plugin/custom-block /-->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertSame( 'my-plugin/custom-block', $processor->get_block_type() );
}
/**
* Test extract_full_block_and_advance returns correct structure.
*/
public function test_extract_full_block_returns_correct_structure() {
$content = '<!-- wp:paragraph --><p>Hello World</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$block = $processor->extract_full_block_and_advance();
$this->assertIsArray( $block );
$this->assertSame( 'core/paragraph', $block['blockName'] );
$this->assertIsArray( $block['attrs'] );
$this->assertIsArray( $block['innerBlocks'] );
$this->assertArrayHasKey( 'innerHTML', $block );
$this->assertArrayHasKey( 'innerContent', $block );
$this->assertSame( '<p>Hello World</p>', $block['innerHTML'] );
}
/**
* Test extract_full_block_and_advance handles nested blocks.
*/
public function test_extract_full_block_handles_nested_blocks() {
$content = '<!-- wp:group --><div class="wp-block-group"><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --></div><!-- /wp:group -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$block = $processor->extract_full_block_and_advance();
$this->assertSame( 'core/group', $block['blockName'] );
$this->assertCount( 1, $block['innerBlocks'] );
$this->assertSame( 'core/paragraph', $block['innerBlocks'][0]['blockName'] );
}
/**
* Test allocate_and_return_parsed_attributes parses JSON correctly.
*/
public function test_allocate_and_return_parsed_attributes_parses_json() {
$content = '<!-- wp:heading {"level":2,"textAlign":"center"} --><h2>Title</h2><!-- /wp:heading -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$attrs = $processor->allocate_and_return_parsed_attributes();
$this->assertIsArray( $attrs );
$this->assertSame( 2, $attrs['level'] );
$this->assertSame( 'center', $attrs['textAlign'] );
}
/**
* Test allocate_and_return_parsed_attributes returns null for void blocks without attrs.
*/
public function test_allocate_and_return_parsed_attributes_returns_null_without_json() {
$content = '<!-- wp:separator /-->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$attrs = $processor->allocate_and_return_parsed_attributes();
$this->assertNull( $attrs );
}
/**
* Test get_span returns correct byte offsets.
*/
public function test_get_span_returns_correct_offsets() {
$content = 'Before<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$span = $processor->get_span();
$this->assertInstanceOf( WP_HTML_Span::class, $span );
$this->assertSame( 6, $span->start ); // After "Before".
$this->assertGreaterThan( 0, $span->length );
}
/**
* Test get_depth tracks nesting correctly.
*/
public function test_get_depth_tracks_nesting() {
$content = '<!-- wp:group --><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --><!-- /wp:group -->';
$processor = new WP_Block_Processor( $content );
// Before anything.
$this->assertSame( 0, $processor->get_depth() );
// After entering group.
$processor->next_block();
$this->assertSame( 1, $processor->get_depth() );
// After entering paragraph.
$processor->next_block();
$this->assertSame( 2, $processor->get_depth() );
}
/**
* Test get_breadcrumbs returns open block hierarchy.
*/
public function test_get_breadcrumbs_returns_hierarchy() {
$content = '<!-- wp:group --><!-- wp:columns --><!-- wp:column --><!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- /wp:column --><!-- /wp:columns --><!-- /wp:group -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block(); // group
$processor->next_block(); // columns
$processor->next_block(); // column
$processor->next_block(); // paragraph
$breadcrumbs = $processor->get_breadcrumbs();
$this->assertSame(
array( 'core/group', 'core/columns', 'core/column', 'core/paragraph' ),
$breadcrumbs
);
}
/**
* Test is_block_type with wildcard.
*/
public function test_is_block_type_with_wildcard() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertTrue( $processor->is_block_type( '*' ) );
}
/**
* Test is_block_type with shorthand name.
*/
public function test_is_block_type_with_shorthand() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertTrue( $processor->is_block_type( 'paragraph' ) );
$this->assertTrue( $processor->is_block_type( 'core/paragraph' ) );
}
/**
* Test opens_block detects block openers.
*/
public function test_opens_block_detects_openers() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertTrue( $processor->opens_block() );
$this->assertTrue( $processor->opens_block( 'paragraph' ) );
$this->assertFalse( $processor->opens_block( 'heading' ) );
}
/**
* Test get_delimiter_type returns correct types.
*/
public function test_get_delimiter_type_returns_correct_types() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_delimiter();
$this->assertSame( WP_Block_Processor::OPENER, $processor->get_delimiter_type() );
$processor->next_delimiter();
$this->assertSame( WP_Block_Processor::CLOSER, $processor->get_delimiter_type() );
}
/**
* Test normalize_block_type static method.
*/
public function test_normalize_block_type() {
$this->assertSame( 'core/paragraph', WP_Block_Processor::normalize_block_type( 'paragraph' ) );
$this->assertSame( 'core/paragraph', WP_Block_Processor::normalize_block_type( 'core/paragraph' ) );
$this->assertSame( 'my/block', WP_Block_Processor::normalize_block_type( 'my/block' ) );
}
/**
* Test empty content handling.
*/
public function test_empty_content_handling() {
$processor = new WP_Block_Processor( '' );
$this->assertFalse( $processor->next_block() );
$this->assertFalse( $processor->next_token() );
}
/**
* Test multiple blocks iteration.
*/
public function test_multiple_blocks_iteration() {
$content = '<!-- wp:paragraph --><p>One</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$count = 0;
while ( $processor->next_block() ) {
++$count;
}
$this->assertSame( 3, $count );
}
/**
* Test deeply nested blocks.
*/
public function test_deeply_nested_blocks() {
$content = '<!-- wp:group --><!-- wp:group --><!-- wp:group --><!-- wp:paragraph --><p>Deep</p><!-- /wp:paragraph --><!-- /wp:group --><!-- /wp:group --><!-- /wp:group -->';
$processor = new WP_Block_Processor( $content );
// Navigate to the deepest block.
$processor->next_block(); // First group.
$processor->next_block(); // Second group.
$processor->next_block(); // Third group.
$processor->next_block(); // Paragraph.
$this->assertSame( 4, $processor->get_depth() );
$this->assertSame( 'core/paragraph', $processor->get_block_type() );
}
/**
* Test freeform HTML content detection.
*/
public function test_freeform_html_content() {
$content = '<p>Freeform HTML</p><!-- wp:paragraph --><p>Block</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
// First token should be HTML.
$processor->next_token();
$this->assertTrue( $processor->is_html() );
// Second token should be the paragraph opener.
$processor->next_token();
$this->assertFalse( $processor->is_html() );
$this->assertSame( 'core/paragraph', $processor->get_block_type() );
}
/**
* Test is_non_whitespace_html distinguishes content.
*/
public function test_is_non_whitespace_html() {
$content = "\n\n<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->\n\n";
$processor = new WP_Block_Processor( $content );
// First token is whitespace HTML.
$processor->next_token();
$this->assertTrue( $processor->is_html() );
$this->assertFalse( $processor->is_non_whitespace_html() );
}
/**
* Test get_html_content returns correct content.
*/
public function test_get_html_content() {
$content = '<p>Freeform</p><!-- wp:paragraph --><p>Block</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_token();
$this->assertSame( '<p>Freeform</p>', $processor->get_html_content() );
}
// =========================================================================
// Additional Polyfill Verification Tests (Task 9)
// =========================================================================
/**
* Test next_block with type parameter filters correctly.
*/
public function test_next_block_type_filter_with_namespace() {
$content = '<!-- wp:paragraph --><p>Skip</p><!-- /wp:paragraph -->';
$content .= '<!-- wp:heading --><h2>Find</h2><!-- /wp:heading -->';
$processor = new WP_Block_Processor( $content );
// Should skip paragraph and find heading.
$this->assertTrue( $processor->next_block( 'core/heading' ) );
$this->assertSame( 'core/heading', $processor->get_block_type() );
}
/**
* Test next_block with shorthand type name.
*/
public function test_next_block_type_filter_shorthand() {
$content = '<!-- wp:paragraph --><p>Skip</p><!-- /wp:paragraph -->';
$content .= '<!-- wp:heading --><h2>Find</h2><!-- /wp:heading -->';
$processor = new WP_Block_Processor( $content );
// Shorthand should also work.
$this->assertTrue( $processor->next_block( 'heading' ) );
$this->assertSame( 'core/heading', $processor->get_block_type() );
}
/**
* Test get_breadcrumbs returns correct path.
*/
public function test_get_breadcrumbs_nested() {
$content = '<!-- wp:group --><!-- wp:columns --><!-- wp:column --><!-- wp:paragraph --><p>Deep</p><!-- /wp:paragraph --><!-- /wp:column --><!-- /wp:columns --><!-- /wp:group -->';
$processor = new WP_Block_Processor( $content );
// Navigate to paragraph.
while ( $processor->next_block() ) {
if ( 'core/paragraph' === $processor->get_block_type() ) {
break;
}
}
$breadcrumbs = $processor->get_breadcrumbs();
$this->assertContains( 'core/group', $breadcrumbs );
$this->assertContains( 'core/columns', $breadcrumbs );
$this->assertContains( 'core/column', $breadcrumbs );
}
/**
* Test is_block_type with custom namespace.
*/
public function test_is_block_type_custom_namespace() {
$content = '<!-- wp:my-plugin/custom-block /-->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertTrue( $processor->is_block_type( 'my-plugin/custom-block' ) );
$this->assertFalse( $processor->is_block_type( 'other-plugin/custom-block' ) );
$this->assertFalse( $processor->is_block_type( 'my-plugin/other-block' ) );
}
/**
* Test extract_full_block_and_advance returns correct structure.
*/
public function test_extract_full_block_structure() {
$content = '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center">Test</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$block = $processor->extract_full_block_and_advance();
$this->assertArrayHasKey( 'blockName', $block );
$this->assertArrayHasKey( 'attrs', $block );
$this->assertArrayHasKey( 'innerBlocks', $block );
$this->assertArrayHasKey( 'innerHTML', $block );
$this->assertArrayHasKey( 'innerContent', $block );
$this->assertSame( 'core/paragraph', $block['blockName'] );
$this->assertSame( 'center', $block['attrs']['align'] );
$this->assertEmpty( $block['innerBlocks'] );
$this->assertStringContainsString( 'Test', $block['innerHTML'] );
}
/**
* Test extract_full_block_and_advance with nested blocks.
*/
public function test_extract_full_block_with_nested() {
$content = '<!-- wp:group --><div><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --></div><!-- /wp:group -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$block = $processor->extract_full_block_and_advance();
$this->assertSame( 'core/group', $block['blockName'] );
$this->assertCount( 1, $block['innerBlocks'] );
$this->assertSame( 'core/paragraph', $block['innerBlocks'][0]['blockName'] );
}
/**
* Test handling of complex nested attributes.
*/
public function test_complex_nested_attributes() {
$content = '<!-- wp:gallery {"ids":[1,2,3],"nested":{"deep":{"value":true}}} /-->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$attrs = $processor->allocate_and_return_parsed_attributes();
$this->assertSame( [ 1, 2, 3 ], $attrs['ids'] );
$this->assertSame( true, $attrs['nested']['deep']['value'] );
}
/**
* Test that opener detection works correctly.
*/
public function test_opens_block_with_specific_type() {
$content = '<!-- wp:heading {"level":2} --><h2>Title</h2><!-- /wp:heading -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertTrue( $processor->opens_block() );
$this->assertTrue( $processor->opens_block( 'heading' ) );
$this->assertTrue( $processor->opens_block( 'core/heading' ) );
$this->assertFalse( $processor->opens_block( 'paragraph' ) );
}
/**
* Test multiple sequential blocks with extraction.
*/
public function test_multiple_blocks_extraction() {
$content = '<!-- wp:paragraph --><p>One</p><!-- /wp:paragraph -->';
$content .= '<!-- wp:heading --><h2>Two</h2><!-- /wp:heading -->';
$content .= '<!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$blocks = [];
while ( $processor->next_block() ) {
$blocks[] = $processor->extract_full_block_and_advance();
}
$this->assertCount( 3, $blocks );
$this->assertSame( 'core/paragraph', $blocks[0]['blockName'] );
$this->assertSame( 'core/heading', $blocks[1]['blockName'] );
$this->assertSame( 'core/paragraph', $blocks[2]['blockName'] );
}
/**
* Test depth tracking with multiple nested levels.
*/
public function test_depth_tracking_complex() {
$content = '<!-- wp:group --><!-- wp:columns --><!-- wp:column --><!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- /wp:column --><!-- /wp:columns --><!-- /wp:group -->';
$processor = new WP_Block_Processor( $content );
$max_depth = 0;
while ( $processor->next_block() ) {
$depth = $processor->get_depth();
if ( $depth > $max_depth ) {
$max_depth = $depth;
}
}
$this->assertSame( 4, $max_depth );
}
/**
* Test void block with complex attributes.
*/
public function test_void_block_with_complex_attrs() {
$content = '<!-- wp:image {"id":123,"sizeSlug":"large","linkDestination":"media","className":"is-style-rounded"} /-->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$this->assertSame( WP_Block_Processor::VOID, $processor->get_delimiter_type() );
$attrs = $processor->allocate_and_return_parsed_attributes();
$this->assertSame( 123, $attrs['id'] );
$this->assertSame( 'large', $attrs['sizeSlug'] );
$this->assertSame( 'media', $attrs['linkDestination'] );
$this->assertSame( 'is-style-rounded', $attrs['className'] );
}
/**
* Test handling of Unicode in block content.
*/
public function test_unicode_content_handling() {
$content = '<!-- wp:paragraph {"text":"日本語テスト"} --><p>日本語テスト</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$attrs = $processor->allocate_and_return_parsed_attributes();
$this->assertSame( '日本語テスト', $attrs['text'] );
}
/**
* Test span offsets for block in middle of content.
*/
public function test_span_offset_middle_block() {
$prefix = 'Some text before ';
$block = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$suffix = ' some text after';
$content = $prefix . $block . $suffix;
$processor = new WP_Block_Processor( $content );
$processor->next_block();
$span = $processor->get_span();
$this->assertSame( strlen( $prefix ), $span->start );
}
/**
* Test is_non_whitespace_html with actual content.
*/
public function test_is_non_whitespace_html_with_content() {
$content = '<p>Real content</p><!-- wp:paragraph --><p>Block</p><!-- /wp:paragraph -->';
$processor = new WP_Block_Processor( $content );
$processor->next_token();
$this->assertTrue( $processor->is_html() );
$this->assertTrue( $processor->is_non_whitespace_html() );
}
}