-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathBlock_Processor_HelperTest.php
More file actions
1115 lines (927 loc) · 35 KB
/
Block_Processor_HelperTest.php
File metadata and controls
1115 lines (927 loc) · 35 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Tests for Block_Processor_Helper class.
*
* @package WP_CLI\Entity
*/
namespace WP_CLI\Entity\Tests;
use PHPUnit\Framework\TestCase;
use WP_CLI\Entity\Block_Processor_Helper;
/**
* Test the Block_Processor_Helper class.
*
* These tests verify the helper methods work correctly with the
* WP_Block_Processor polyfill.
*/
class Block_Processor_HelperTest extends TestCase {
/**
* Sample block content for testing.
*
* @var string
*/
private $sample_content;
/**
* Set up test fixtures.
*/
protected function setUp(): void {
parent::setUp();
// Create sample content with multiple blocks.
$this->sample_content = implode(
'',
[
'<!-- wp:paragraph --><p>First paragraph</p><!-- /wp:paragraph -->',
'<!-- wp:heading {"level":2} --><h2>My Heading</h2><!-- /wp:heading -->',
'<!-- wp:paragraph --><p>Second paragraph</p><!-- /wp:paragraph -->',
'<!-- wp:image {"id":123} /-->',
]
);
}
// =========================================================================
// Tests for parse_all()
// =========================================================================
/**
* Test parse_all returns correct structure for simple content.
*/
public function test_parse_all_returns_correct_structure() {
$content = '<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'core/paragraph', $blocks[0]['blockName'] );
$this->assertArrayHasKey( 'attrs', $blocks[0] );
$this->assertArrayHasKey( 'innerBlocks', $blocks[0] );
$this->assertArrayHasKey( 'innerHTML', $blocks[0] );
$this->assertArrayHasKey( 'innerContent', $blocks[0] );
}
/**
* Test parse_all handles multiple blocks.
*/
public function test_parse_all_handles_multiple_blocks() {
$blocks = Block_Processor_Helper::parse_all( $this->sample_content );
$this->assertCount( 4, $blocks );
$this->assertSame( 'core/paragraph', $blocks[0]['blockName'] );
$this->assertSame( 'core/heading', $blocks[1]['blockName'] );
$this->assertSame( 'core/paragraph', $blocks[2]['blockName'] );
$this->assertSame( 'core/image', $blocks[3]['blockName'] );
}
/**
* Test parse_all handles nested blocks.
*/
public function test_parse_all_handles_nested_blocks() {
$content = '<!-- wp:group --><div class="wp-block-group"><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --></div><!-- /wp:group -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'core/group', $blocks[0]['blockName'] );
$this->assertCount( 1, $blocks[0]['innerBlocks'] );
$this->assertSame( 'core/paragraph', $blocks[0]['innerBlocks'][0]['blockName'] );
}
/**
* Test parse_all handles void blocks.
*/
public function test_parse_all_handles_void_blocks() {
$content = '<!-- wp:separator /--><!-- wp:spacer {"height":"50px"} /-->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 2, $blocks );
$this->assertSame( 'core/separator', $blocks[0]['blockName'] );
$this->assertSame( 'core/spacer', $blocks[1]['blockName'] );
}
/**
* Test parse_all returns empty array for empty content.
*/
public function test_parse_all_returns_empty_for_empty_content() {
$blocks = Block_Processor_Helper::parse_all( '' );
$this->assertSame( [], $blocks );
}
/**
* Test parse_all parses attributes correctly.
*/
public function test_parse_all_parses_attributes() {
$content = '<!-- wp:heading {"level":3,"textAlign":"center"} --><h3>Title</h3><!-- /wp:heading -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 3, $blocks[0]['attrs']['level'] );
$this->assertSame( 'center', $blocks[0]['attrs']['textAlign'] );
}
// =========================================================================
// Tests for get_at_index()
// =========================================================================
/**
* Test get_at_index returns correct block.
*/
public function test_get_at_index_returns_correct_block() {
$block = Block_Processor_Helper::get_at_index( $this->sample_content, 1 );
$this->assertNotNull( $block );
$this->assertSame( 'core/heading', $block['blockName'] );
}
/**
* Test get_at_index returns first block at index 0.
*/
public function test_get_at_index_returns_first_block() {
$block = Block_Processor_Helper::get_at_index( $this->sample_content, 0 );
$this->assertNotNull( $block );
$this->assertSame( 'core/paragraph', $block['blockName'] );
}
/**
* Test get_at_index returns last block.
*/
public function test_get_at_index_returns_last_block() {
$block = Block_Processor_Helper::get_at_index( $this->sample_content, 3 );
$this->assertNotNull( $block );
$this->assertSame( 'core/image', $block['blockName'] );
}
/**
* Test get_at_index returns null for invalid index.
*/
public function test_get_at_index_returns_null_for_invalid_index() {
$block = Block_Processor_Helper::get_at_index( $this->sample_content, 10 );
$this->assertNull( $block );
}
/**
* Test get_at_index returns null for negative index.
*/
public function test_get_at_index_returns_null_for_negative_index() {
$block = Block_Processor_Helper::get_at_index( $this->sample_content, -1 );
$this->assertNull( $block );
}
/**
* Test get_at_index returns null for empty content.
*/
public function test_get_at_index_returns_null_for_empty_content() {
$block = Block_Processor_Helper::get_at_index( '', 0 );
$this->assertNull( $block );
}
// =========================================================================
// Tests for count_by_type()
// =========================================================================
/**
* Test count_by_type returns correct counts.
*/
public function test_count_by_type_returns_correct_counts() {
$counts = Block_Processor_Helper::count_by_type( $this->sample_content );
$this->assertSame( 2, $counts['core/paragraph'] );
$this->assertSame( 1, $counts['core/heading'] );
$this->assertSame( 1, $counts['core/image'] );
}
/**
* Test count_by_type with nested blocks excluded by default.
*/
public function test_count_by_type_excludes_nested_by_default() {
$content = '<!-- wp:group --><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --><!-- /wp:group -->';
$counts = Block_Processor_Helper::count_by_type( $content );
$this->assertSame( 1, $counts['core/group'] );
$this->assertArrayNotHasKey( 'core/paragraph', $counts );
}
/**
* Test count_by_type with nested blocks included.
*/
public function test_count_by_type_includes_nested_when_requested() {
$content = '<!-- wp:group --><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --><!-- /wp:group -->';
$counts = Block_Processor_Helper::count_by_type( $content, true );
$this->assertSame( 1, $counts['core/group'] );
$this->assertSame( 1, $counts['core/paragraph'] );
}
/**
* Test count_by_type returns empty array for empty content.
*/
public function test_count_by_type_returns_empty_for_empty_content() {
$counts = Block_Processor_Helper::count_by_type( '' );
$this->assertSame( [], $counts );
}
/**
* Test count_by_type returns empty array for plain HTML.
*/
public function test_count_by_type_returns_empty_for_plain_html() {
$counts = Block_Processor_Helper::count_by_type( '<p>Just HTML</p>' );
$this->assertSame( [], $counts );
}
// =========================================================================
// Tests for has_block()
// =========================================================================
/**
* Test has_block returns true when block exists.
*/
public function test_has_block_returns_true_when_exists() {
$this->assertTrue( Block_Processor_Helper::has_block( $this->sample_content, 'core/heading' ) );
}
/**
* Test has_block works with shorthand block names.
*/
public function test_has_block_works_with_shorthand() {
$this->assertTrue( Block_Processor_Helper::has_block( $this->sample_content, 'heading' ) );
$this->assertTrue( Block_Processor_Helper::has_block( $this->sample_content, 'paragraph' ) );
}
/**
* Test has_block returns false when block doesn't exist.
*/
public function test_has_block_returns_false_when_missing() {
$this->assertFalse( Block_Processor_Helper::has_block( $this->sample_content, 'core/quote' ) );
}
/**
* Test has_block returns false for empty content.
*/
public function test_has_block_returns_false_for_empty_content() {
$this->assertFalse( Block_Processor_Helper::has_block( '', 'paragraph' ) );
}
/**
* Test has_block finds nested blocks.
*/
public function test_has_block_finds_nested_blocks() {
$content = '<!-- wp:group --><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --><!-- /wp:group -->';
$this->assertTrue( Block_Processor_Helper::has_block( $content, 'paragraph' ) );
}
// =========================================================================
// Tests for get_block_count()
// =========================================================================
/**
* Test get_block_count returns correct count.
*/
public function test_get_block_count_returns_correct_count() {
$count = Block_Processor_Helper::get_block_count( $this->sample_content );
$this->assertSame( 4, $count );
}
/**
* Test get_block_count excludes nested by default.
*/
public function test_get_block_count_excludes_nested_by_default() {
$content = '<!-- wp:group --><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --><!-- /wp:group -->';
$count = Block_Processor_Helper::get_block_count( $content );
$this->assertSame( 1, $count );
}
/**
* Test get_block_count includes nested when requested.
*/
public function test_get_block_count_includes_nested_when_requested() {
$content = '<!-- wp:group --><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --><!-- /wp:group -->';
$count = Block_Processor_Helper::get_block_count( $content, true );
$this->assertSame( 2, $count );
}
/**
* Test get_block_count returns 0 for empty content.
*/
public function test_get_block_count_returns_zero_for_empty() {
$count = Block_Processor_Helper::get_block_count( '' );
$this->assertSame( 0, $count );
}
// =========================================================================
// Tests for has_blocks()
// =========================================================================
/**
* Test has_blocks returns true when blocks exist.
*/
public function test_has_blocks_returns_true_when_exists() {
$this->assertTrue( Block_Processor_Helper::has_blocks( $this->sample_content ) );
}
/**
* Test has_blocks returns false for empty content.
*/
public function test_has_blocks_returns_false_for_empty() {
$this->assertFalse( Block_Processor_Helper::has_blocks( '' ) );
}
/**
* Test has_blocks returns false for plain HTML.
*/
public function test_has_blocks_returns_false_for_plain_html() {
$this->assertFalse( Block_Processor_Helper::has_blocks( '<p>Just HTML</p>' ) );
}
// =========================================================================
// Tests for get_block_types()
// =========================================================================
/**
* Test get_block_types returns unique types.
*/
public function test_get_block_types_returns_unique_types() {
$types = Block_Processor_Helper::get_block_types( $this->sample_content );
$this->assertContains( 'core/paragraph', $types );
$this->assertContains( 'core/heading', $types );
$this->assertContains( 'core/image', $types );
$this->assertCount( 3, $types );
}
/**
* Test get_block_types returns empty array for empty content.
*/
public function test_get_block_types_returns_empty_for_empty() {
$types = Block_Processor_Helper::get_block_types( '' );
$this->assertSame( [], $types );
}
// =========================================================================
// Tests for extract_matching()
// =========================================================================
/**
* Test extract_matching finds blocks by type.
*/
public function test_extract_matching_finds_blocks_by_type() {
$blocks = Block_Processor_Helper::extract_matching(
$this->sample_content,
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Callback signature requires both params.
function ( $type, $attrs ) {
return 'core/paragraph' === $type;
}
);
$this->assertCount( 2, $blocks );
$this->assertSame( 'core/paragraph', $blocks[0]['blockName'] );
$this->assertSame( 'core/paragraph', $blocks[1]['blockName'] );
}
/**
* Test extract_matching finds blocks by attribute.
*/
public function test_extract_matching_finds_blocks_by_attribute() {
$blocks = Block_Processor_Helper::extract_matching(
$this->sample_content,
function ( $type, $attrs ) {
return isset( $attrs['level'] ) && 2 === $attrs['level'];
}
);
$this->assertCount( 1, $blocks );
$this->assertSame( 'core/heading', $blocks[0]['blockName'] );
}
/**
* Test extract_matching respects limit.
*/
public function test_extract_matching_respects_limit() {
$blocks = Block_Processor_Helper::extract_matching(
$this->sample_content,
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Callback signature requires both params.
function ( $type, $attrs ) {
return 'core/paragraph' === $type;
},
1
);
$this->assertCount( 1, $blocks );
}
/**
* Test extract_matching returns empty for no matches.
*/
public function test_extract_matching_returns_empty_for_no_matches() {
$blocks = Block_Processor_Helper::extract_matching(
$this->sample_content,
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Callback signature requires both params.
function ( $type, $attrs ) {
return 'core/quote' === $type;
}
);
$this->assertSame( [], $blocks );
}
// =========================================================================
// Tests for get_block_span()
// =========================================================================
/**
* Test get_block_span returns correct offsets.
*/
public function test_get_block_span_returns_correct_offsets() {
$content = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$span = Block_Processor_Helper::get_block_span( $content, 0 );
$this->assertNotNull( $span );
$this->assertArrayHasKey( 'start', $span );
$this->assertArrayHasKey( 'end', $span );
$this->assertSame( 0, $span['start'] );
$this->assertSame( strlen( $content ), $span['end'] );
}
/**
* Test get_block_span returns null for invalid index.
*/
public function test_get_block_span_returns_null_for_invalid_index() {
$span = Block_Processor_Helper::get_block_span( $this->sample_content, 100 );
$this->assertNull( $span );
}
/**
* Test get_block_span returns null for empty content.
*/
public function test_get_block_span_returns_null_for_empty() {
$span = Block_Processor_Helper::get_block_span( '', 0 );
$this->assertNull( $span );
}
// =========================================================================
// Tests for strip_inner_html()
// =========================================================================
/**
* Test strip_inner_html removes innerHTML.
*/
public function test_strip_inner_html_removes_content() {
$blocks = [
[
'blockName' => 'core/paragraph',
'attrs' => [],
'innerBlocks' => [],
'innerHTML' => '<p>Test</p>',
'innerContent' => [ '<p>Test</p>' ],
],
];
$stripped = Block_Processor_Helper::strip_inner_html( $blocks );
$this->assertArrayNotHasKey( 'innerHTML', $stripped[0] );
$this->assertArrayNotHasKey( 'innerContent', $stripped[0] );
$this->assertSame( 'core/paragraph', $stripped[0]['blockName'] );
}
/**
* Test strip_inner_html works recursively on nested blocks.
*/
public function test_strip_inner_html_works_recursively() {
$blocks = [
[
'blockName' => 'core/group',
'attrs' => [],
'innerHTML' => '<div></div>',
'innerContent' => [ '<div>', '</div>' ],
'innerBlocks' => [
[
'blockName' => 'core/paragraph',
'attrs' => [],
'innerHTML' => '<p>Inner</p>',
'innerContent' => [ '<p>Inner</p>' ],
'innerBlocks' => [],
],
],
],
];
$stripped = Block_Processor_Helper::strip_inner_html( $blocks );
$this->assertArrayNotHasKey( 'innerHTML', $stripped[0] );
$this->assertArrayNotHasKey( 'innerHTML', $stripped[0]['innerBlocks'][0] );
}
// =========================================================================
// Tests for filter_empty_blocks()
// =========================================================================
/**
* Test filter_empty_blocks removes null blockName entries.
*/
public function test_filter_empty_blocks_removes_empty() {
$blocks = [
[
'blockName' => 'core/paragraph',
'attrs' => [],
],
[
'blockName' => null,
'attrs' => [],
],
[
'blockName' => 'core/heading',
'attrs' => [],
],
[
'blockName' => '',
'attrs' => [],
],
];
$filtered = Block_Processor_Helper::filter_empty_blocks( $blocks );
$this->assertCount( 2, $filtered );
$this->assertSame( 'core/paragraph', $filtered[0]['blockName'] );
$this->assertSame( 'core/heading', $filtered[1]['blockName'] );
}
/**
* Test filter_empty_blocks re-indexes array.
*/
public function test_filter_empty_blocks_reindexes() {
$blocks = [
0 => [ 'blockName' => null ],
1 => [
'blockName' => 'core/paragraph',
'attrs' => [],
],
];
$filtered = Block_Processor_Helper::filter_empty_blocks( $blocks );
$this->assertArrayHasKey( 0, $filtered );
$this->assertArrayNotHasKey( 1, $filtered );
}
// =========================================================================
// Edge case tests
// =========================================================================
/**
* Test handling of 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 -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'core/group', $blocks[0]['blockName'] );
// Navigate to deepest block.
$inner = $blocks[0]['innerBlocks'][0]['innerBlocks'][0]['innerBlocks'][0];
$this->assertSame( 'core/paragraph', $inner['blockName'] );
}
/**
* Test handling of blocks with complex JSON attributes.
*/
public function test_complex_json_attributes() {
$content = '<!-- wp:gallery {"ids":[1,2,3],"columns":3,"linkTo":"media","nested":{"key":"value"}} /-->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( [ 1, 2, 3 ], $blocks[0]['attrs']['ids'] );
$this->assertSame( 3, $blocks[0]['attrs']['columns'] );
$this->assertSame( 'media', $blocks[0]['attrs']['linkTo'] );
$this->assertSame( [ 'key' => 'value' ], $blocks[0]['attrs']['nested'] );
}
/**
* Test handling of custom namespaced blocks.
*/
public function test_custom_namespace_blocks() {
$content = '<!-- wp:my-plugin/custom-block {"option":"value"} /-->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'my-plugin/custom-block', $blocks[0]['blockName'] );
$this->assertSame( 'value', $blocks[0]['attrs']['option'] );
}
// =========================================================================
// Serialization Round-Trip Tests (Task 1)
// =========================================================================
/**
* Normalize block structure for comparison (remove keys that may differ).
*
* @param array $blocks Blocks to normalize.
* @return array Normalized blocks.
*/
private function normalize_for_comparison( array $blocks ) {
return array_map(
function ( $block ) {
return [
'blockName' => $block['blockName'],
'attrs' => $block['attrs'] ?? [],
'innerBlocks' => isset( $block['innerBlocks'] )
? $this->normalize_for_comparison( $block['innerBlocks'] )
: [],
];
},
$blocks
);
}
/**
* Serialize an array of blocks to string (test helper).
*
* @param array $blocks Blocks to serialize.
* @return string Serialized content.
*/
private function serialize_blocks_for_test( array $blocks ) {
$output = '';
foreach ( $blocks as $block ) {
$output .= $this->serialize_block_for_test( $block );
}
return $output;
}
/**
* Serialize a single block to string (test helper).
*
* @param array $block Block to serialize.
* @return string Serialized content.
*/
private function serialize_block_for_test( array $block ) {
$block_name = $block['blockName'] ?? null;
if ( empty( $block_name ) ) {
return $block['innerHTML'] ?? '';
}
$name = $block_name;
if ( 0 === strpos( $name, 'core/' ) ) {
$name = substr( $name, 5 );
}
$attrs = '';
if ( ! empty( $block['attrs'] ) ) {
$attrs = ' ' . json_encode( $block['attrs'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}
if ( empty( $block['innerContent'] ) ) {
return "<!-- wp:{$name}{$attrs} /-->";
}
$output = "<!-- wp:{$name}{$attrs} -->";
$inner_index = 0;
foreach ( $block['innerContent'] as $chunk ) {
if ( null === $chunk ) {
if ( isset( $block['innerBlocks'][ $inner_index ] ) ) {
$output .= $this->serialize_block_for_test( $block['innerBlocks'][ $inner_index ] );
++$inner_index;
}
} else {
$output .= $chunk;
}
}
$output .= "<!-- /wp:{$name} -->";
return $output;
}
/**
* Test that parsed blocks can be serialized and re-parsed consistently.
*/
public function test_serialization_roundtrip_simple_paragraph() {
$original = '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->';
$parsed = Block_Processor_Helper::parse_all( $original );
$serialized = $this->serialize_blocks_for_test( $parsed );
$reparsed = Block_Processor_Helper::parse_all( $serialized );
$this->assertEquals(
$this->normalize_for_comparison( $parsed ),
$this->normalize_for_comparison( $reparsed )
);
}
/**
* Test serialization round-trip with attributes.
*/
public function test_serialization_roundtrip_with_attributes() {
$original = '<!-- wp:heading {"level":3,"textAlign":"center"} --><h3 class="has-text-align-center">Title</h3><!-- /wp:heading -->';
$parsed = Block_Processor_Helper::parse_all( $original );
$serialized = $this->serialize_blocks_for_test( $parsed );
$reparsed = Block_Processor_Helper::parse_all( $serialized );
$this->assertEquals(
$this->normalize_for_comparison( $parsed ),
$this->normalize_for_comparison( $reparsed )
);
}
/**
* Test serialization round-trip with nested blocks.
*/
public function test_serialization_roundtrip_nested_blocks() {
$original = '<!-- wp:group {"className":"test"} --><div class="wp-block-group test"><!-- wp:paragraph --><p>Inner</p><!-- /wp:paragraph --></div><!-- /wp:group -->';
$parsed = Block_Processor_Helper::parse_all( $original );
$serialized = $this->serialize_blocks_for_test( $parsed );
$reparsed = Block_Processor_Helper::parse_all( $serialized );
$this->assertEquals(
$this->normalize_for_comparison( $parsed ),
$this->normalize_for_comparison( $reparsed )
);
}
/**
* Test serialization round-trip with void blocks.
*/
public function test_serialization_roundtrip_void_blocks() {
$original = '<!-- wp:separator {"className":"is-style-wide"} /-->';
$parsed = Block_Processor_Helper::parse_all( $original );
$serialized = $this->serialize_blocks_for_test( $parsed );
$reparsed = Block_Processor_Helper::parse_all( $serialized );
$this->assertEquals(
$this->normalize_for_comparison( $parsed ),
$this->normalize_for_comparison( $reparsed )
);
}
/**
* Test serialization round-trip with multiple blocks.
*/
public function test_serialization_roundtrip_multiple_blocks() {
$original = '<!-- wp:paragraph --><p>One</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Three</h2><!-- /wp:heading -->';
$parsed = Block_Processor_Helper::parse_all( $original );
$serialized = $this->serialize_blocks_for_test( $parsed );
$reparsed = Block_Processor_Helper::parse_all( $serialized );
$this->assertEquals(
$this->normalize_for_comparison( $parsed ),
$this->normalize_for_comparison( $reparsed )
);
}
// =========================================================================
// Malformed Content Handling Tests (Task 2)
// =========================================================================
/**
* Test handling of unclosed block (missing closer).
*/
public function test_parse_all_handles_unclosed_block() {
$content = '<!-- wp:paragraph --><p>No closing tag';
$blocks = Block_Processor_Helper::parse_all( $content );
// Should not throw, may return empty or partial.
$this->assertIsArray( $blocks );
}
/**
* Test handling of orphaned closer (no opener).
*/
public function test_parse_all_handles_orphaned_closer() {
$content = '<p>Some text</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertIsArray( $blocks );
}
/**
* Test handling of mismatched block types.
*/
public function test_parse_all_handles_mismatched_blocks() {
$content = '<!-- wp:paragraph --><p>Text</p><!-- /wp:heading -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertIsArray( $blocks );
}
/**
* Test handling of invalid JSON in attributes.
*/
public function test_parse_all_handles_invalid_json_attrs() {
$content = '<!-- wp:paragraph {"broken: json} --><p>Test</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertIsArray( $blocks );
}
/**
* Test handling of truncated block comment.
*/
public function test_parse_all_handles_truncated_comment() {
$content = '<!-- wp:paragr';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertIsArray( $blocks );
$this->assertCount( 0, $blocks );
}
/**
* Test handling of empty block name.
*/
public function test_parse_all_handles_empty_block_name() {
$content = '<!-- wp: --><p>Test</p><!-- /wp: -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertIsArray( $blocks );
}
/**
* Test handling of block with only whitespace content.
*/
public function test_parse_all_handles_whitespace_only_content() {
$content = "<!-- wp:paragraph --> \n\n <!-- /wp:paragraph -->";
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'core/paragraph', $blocks[0]['blockName'] );
}
/**
* Test handling of deeply nested unclosed blocks.
*/
public function test_parse_all_handles_nested_unclosed() {
$content = '<!-- wp:group --><!-- wp:paragraph --><p>Unclosed nesting';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertIsArray( $blocks );
}
// =========================================================================
// Unicode and Special Character Tests (Task 3)
// =========================================================================
/**
* Test handling of emoji in attributes.
*/
public function test_parse_all_handles_emoji_in_attrs() {
$content = '<!-- wp:paragraph {"emoji":"🎉🚀💻"} --><p>Test</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( '🎉🚀💻', $blocks[0]['attrs']['emoji'] );
}
/**
* Test handling of Chinese characters in attributes.
*/
public function test_parse_all_handles_chinese_chars() {
$content = '<!-- wp:paragraph {"text":"中文测试"} --><p>中文测试</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( '中文测试', $blocks[0]['attrs']['text'] );
}
/**
* Test handling of RTL characters (Arabic/Hebrew).
*/
public function test_parse_all_handles_rtl_chars() {
$content = '<!-- wp:paragraph {"text":"مرحبا بالعالم"} --><p>مرحبا بالعالم</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'مرحبا بالعالم', $blocks[0]['attrs']['text'] );
}
/**
* Test handling of escaped quotes in attributes.
*/
public function test_parse_all_handles_escaped_quotes() {
$content = '<!-- wp:paragraph {"text":"He said \"hello\""} --><p>Test</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'He said "hello"', $blocks[0]['attrs']['text'] );
}
/**
* Test handling of newlines in attributes.
*/
public function test_parse_all_handles_newlines_in_attrs() {
$content = '<!-- wp:paragraph {"text":"Line1\\nLine2"} --><p>Test</p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertStringContainsString( "\n", $blocks[0]['attrs']['text'] );
}
/**
* Test handling of HTML entities in content.
*/
public function test_parse_all_handles_html_entities() {
$content = '<!-- wp:paragraph --><p><script>alert("xss")</script></p><!-- /wp:paragraph -->';
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertStringContainsString( '<script>', $blocks[0]['innerHTML'] );
}
/**
* Test handling of null bytes (should be stripped or handled).
*/
public function test_parse_all_handles_null_bytes() {
$content = "<!-- wp:paragraph --><p>Test\x00with\x00nulls</p><!-- /wp:paragraph -->";
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertIsArray( $blocks );
}
// =========================================================================
// Deep Nesting Tests (Task 4)
// =========================================================================
/**
* Test handling of 10-level deep nesting.
*/
public function test_parse_all_handles_10_level_nesting() {
$depth = 10;
$content = str_repeat( '<!-- wp:group --><div class="wp-block-group">', $depth );
$content .= '<!-- wp:paragraph --><p>Deep content</p><!-- /wp:paragraph -->';
$content .= str_repeat( '</div><!-- /wp:group -->', $depth );
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
$this->assertSame( 'core/group', $blocks[0]['blockName'] );
// Navigate to deepest block.
$current = $blocks[0];
for ( $i = 1; $i < $depth; $i++ ) {
$this->assertNotEmpty( $current['innerBlocks'] );
$current = $current['innerBlocks'][0];
}
$this->assertSame( 'core/paragraph', $current['innerBlocks'][0]['blockName'] );
}
/**
* Test handling of 50-level deep nesting (stress test).
*/
public function test_parse_all_handles_50_level_nesting() {
$depth = 50;
$content = str_repeat( '<!-- wp:group -->', $depth );
$content .= '<!-- wp:paragraph --><p>Very deep</p><!-- /wp:paragraph -->';
$content .= str_repeat( '<!-- /wp:group -->', $depth );
$blocks = Block_Processor_Helper::parse_all( $content );
$this->assertCount( 1, $blocks );
}
/**
* Test handling of wide nesting (many siblings at each level).
*/
public function test_parse_all_handles_wide_nesting() {
$siblings = 20;