-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathpost-block.feature
More file actions
1920 lines (1646 loc) · 62.2 KB
/
post-block.feature
File metadata and controls
1920 lines (1646 loc) · 62.2 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
Feature: Manage blocks in post content
@require-wp-5.0
Scenario: Check if a post has blocks
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post has-blocks {POST_ID}`
Then STDOUT should contain:
"""
Success: Post {POST_ID} contains blocks.
"""
When I run `wp post create --post_title="Classic Post" --post_content="<p>Hello classic</p>" --porcelain`
Then save STDOUT as {CLASSIC_ID}
When I try `wp post has-blocks {CLASSIC_ID}`
Then STDERR should contain:
"""
Error: Post {CLASSIC_ID} does not contain blocks.
"""
And the return code should be 1
@require-wp-5.0
Scenario: Check if a post has a specific block
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post has-block {POST_ID} core/paragraph`
Then STDOUT should contain:
"""
Success: Post {POST_ID} contains block 'core/paragraph'.
"""
When I run `wp post has-block {POST_ID} core/heading`
Then STDOUT should contain:
"""
Success: Post {POST_ID} contains block 'core/heading'.
"""
When I try `wp post has-block {POST_ID} core/image`
Then STDERR should contain:
"""
Error: Post {POST_ID} does not contain block 'core/image'.
"""
And the return code should be 1
@require-wp-5.0
Scenario: Parse blocks in a post
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph {\"align\":\"center\"} --><p>Hello</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block parse {POST_ID}`
Then STDOUT should contain:
"""
"blockName": "core/paragraph"
"""
And STDOUT should contain:
"""
"align": "center"
"""
When I run `wp post block parse {POST_ID} --format=yaml`
Then STDOUT should contain:
"""
blockName: core/paragraph
"""
When I run `wp post block parse {POST_ID} --raw`
Then STDOUT should contain:
"""
innerHTML
"""
@require-wp-5.0
Scenario: List blocks in a post
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>One</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/paragraph | 2 |
| core/heading | 1 |
When I run `wp post block list {POST_ID} --format=json`
Then STDOUT should be JSON containing:
"""
[{"blockName":"core/paragraph","count":2}]
"""
When I run `wp post block list {POST_ID} --format=count`
Then STDOUT should be:
"""
2
"""
@require-wp-5.0
Scenario: List nested blocks
Given a WP install
When I run `wp post create --post_title="Nested Blocks" --post_content="<!-- wp:group --><!-- wp:paragraph --><p>Nested</p><!-- /wp:paragraph --><!-- /wp:group -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/group | 1 |
And STDOUT should not contain:
"""
core/paragraph
"""
When I run `wp post block list {POST_ID} --nested`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/group | 1 |
| core/paragraph | 1 |
@require-wp-5.0
Scenario: Render blocks to HTML
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Hello World</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block render {POST_ID}`
# In WordPress 7.0+ paragraph blocks are rendered with a class name.
# See https://github.com/WordPress/gutenberg/pull/71207.
Then STDOUT should contain:
"""
<p
"""
And STDOUT should contain:
"""
>Hello World</p>
"""
And STDOUT should contain:
"""
<h2
"""
And STDOUT should contain:
"""
Title</h2>
"""
When I run `wp post block render {POST_ID} --block=core/paragraph`
Then STDOUT should contain:
"""
<p
"""
And STDOUT should contain:
"""
>Hello World</p>
"""
And STDOUT should not contain:
"""
Title</h2>
"""
@require-wp-5.0
Scenario: Insert a block into a post
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block insert {POST_ID} core/paragraph --content="Added at end"`
Then STDOUT should contain:
"""
Success: Inserted block into post {POST_ID}.
"""
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
Added at end
"""
When I run `wp post block insert {POST_ID} core/heading --content="Title" --position=start`
Then STDOUT should contain:
"""
Success: Inserted block into post {POST_ID}.
"""
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/paragraph | 2 |
| core/heading | 1 |
@require-wp-5.0
Scenario: Insert a block with attributes
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block insert {POST_ID} core/heading --content="Title" --attrs='{"level":3}'`
Then STDOUT should contain:
"""
Success: Inserted block into post {POST_ID}.
"""
When I run `wp post block parse {POST_ID}`
Then STDOUT should contain:
"""
"level": 3
"""
@require-wp-5.0
Scenario: Remove a block by index
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block remove {POST_ID} --index=1`
Then STDOUT should contain:
"""
Success: Removed 1 block from post {POST_ID}.
"""
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
First
"""
And STDOUT should contain:
"""
Third
"""
And STDOUT should not contain:
"""
Second
"""
@require-wp-5.0
Scenario: Remove multiple blocks by indices
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block remove {POST_ID} --index=0,2`
Then STDOUT should contain:
"""
Success: Removed 2 blocks from post {POST_ID}.
"""
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
Second
"""
And STDOUT should not contain:
"""
First
"""
And STDOUT should not contain:
"""
Third
"""
@require-wp-5.0
Scenario: Remove blocks by name
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Para 1</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Heading</h2><!-- /wp:heading --><!-- wp:paragraph --><p>Para 2</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block remove {POST_ID} core/paragraph`
Then STDOUT should contain:
"""
Success: Removed 1 block from post {POST_ID}.
"""
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/paragraph | 1 |
| core/heading | 1 |
@require-wp-5.0
Scenario: Remove all blocks of a type
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Para 1</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Heading</h2><!-- /wp:heading --><!-- wp:paragraph --><p>Para 2</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block remove {POST_ID} core/paragraph --all`
Then STDOUT should contain:
"""
Success: Removed 2 blocks from post {POST_ID}.
"""
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/heading | 1 |
And STDOUT should not contain:
"""
core/paragraph
"""
@require-wp-5.0
Scenario: Replace blocks
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Content</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block replace {POST_ID} core/paragraph core/heading`
Then STDOUT should contain:
"""
Success: Replaced 1 block in post {POST_ID}.
"""
When I run `wp post has-block {POST_ID} core/heading`
Then STDOUT should contain:
"""
Success: Post {POST_ID} contains block 'core/heading'.
"""
When I try `wp post has-block {POST_ID} core/paragraph`
Then the return code should be 1
@require-wp-5.0
Scenario: Replace all blocks of a type
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Para 1</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Para 2</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block replace {POST_ID} core/paragraph core/verse --all`
Then STDOUT should contain:
"""
Success: Replaced 2 blocks in post {POST_ID}.
"""
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/verse | 2 |
And STDOUT should not contain:
"""
core/paragraph
"""
@require-wp-5.0
Scenario: Replace block with new attributes
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:heading {\"level\":2} --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block replace {POST_ID} core/heading core/heading --attrs='{"level":4}'`
Then STDOUT should contain:
"""
Success: Replaced 1 block in post {POST_ID}.
"""
When I run `wp post block parse {POST_ID}`
Then STDOUT should contain:
"""
"level": 4
"""
@require-wp-5.0
Scenario: Error handling for invalid post
Given a WP install
When I try `wp post has-blocks 999999`
Then STDERR should contain:
"""
Could not find the post
"""
And the return code should be 1
When I try `wp post block list 999999`
Then STDERR should contain:
"""
Could not find the post
"""
And the return code should be 1
@require-wp-5.0
Scenario: Error handling for remove without block name or index
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I try `wp post block remove {POST_ID}`
Then STDERR should contain:
"""
Error: You must specify either a block name or --index.
"""
And the return code should be 1
@require-wp-5.0
Scenario: Porcelain output for insert
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block insert {POST_ID} core/paragraph --content="New" --porcelain`
Then STDOUT should be:
"""
{POST_ID}
"""
@require-wp-5.0
Scenario: Porcelain output for remove
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block remove {POST_ID} --index=0 --porcelain`
Then STDOUT should be:
"""
1
"""
@require-wp-5.0
Scenario: Porcelain output for replace
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block replace {POST_ID} core/paragraph core/heading --porcelain`
Then STDOUT should be:
"""
1
"""
@require-wp-5.0
Scenario: Get a block by index
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph {\"align\":\"center\"} --><p>First</p><!-- /wp:paragraph --><!-- wp:heading {\"level\":2} --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block get {POST_ID} 0`
Then STDOUT should contain:
"""
"blockName": "core/paragraph"
"""
And STDOUT should contain:
"""
"align": "center"
"""
When I run `wp post block get {POST_ID} 1`
Then STDOUT should contain:
"""
"blockName": "core/heading"
"""
And STDOUT should contain:
"""
"level": 2
"""
When I run `wp post block get {POST_ID} 0 --format=yaml`
Then STDOUT should contain:
"""
blockName: core/paragraph
"""
When I run `wp post block get {POST_ID} 0 --raw`
Then STDOUT should contain:
"""
innerHTML
"""
@require-wp-5.0
Scenario: Error on invalid block index
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I try `wp post block get {POST_ID} 5`
Then STDERR should contain:
"""
Invalid index: 5
"""
And the return code should be 1
When I try `wp post block get {POST_ID} -1`
Then STDERR should contain:
"""
Invalid index: -1
"""
And the return code should be 1
@require-wp-5.0
Scenario: Update block attributes
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:heading {\"level\":2} --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block update {POST_ID} 0 --attrs='{"level":3}'`
Then STDOUT should contain:
"""
Success: Updated block at index 0 in post {POST_ID}.
"""
When I run `wp post block parse {POST_ID}`
Then STDOUT should contain:
"""
"level": 3
"""
@require-wp-5.0
Scenario: Update heading level syncs HTML tag
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:heading {\"level\":2} --><h2>Original Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block update {POST_ID} 0 --attrs='{"level":4}'`
Then STDOUT should contain:
"""
Success: Updated block at index 0 in post {POST_ID}.
"""
# Verify the attribute was updated
When I run `wp post block parse {POST_ID}`
Then STDOUT should contain:
"""
"level": 4
"""
# Verify the HTML tag was updated to match
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
<h4>Original Title</h4>
"""
And STDOUT should not contain:
"""
<h2>
"""
@require-wp-5.0
Scenario: Update list ordered attribute syncs HTML tag
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:list --><ul><li>Item 1</li><li>Item 2</li></ul><!-- /wp:list -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block update {POST_ID} 0 --attrs='{"ordered":true}'`
Then STDOUT should contain:
"""
Success: Updated block at index 0 in post {POST_ID}.
"""
# Verify the HTML tag was updated from ul to ol
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
<ol>
"""
And STDOUT should contain:
"""
</ol>
"""
And STDOUT should not contain:
"""
<ul>
"""
@require-wp-5.0
Scenario: Update block with custom HTML sync filter via --require
Given a WP install
And a custom-sync-filter.php file:
"""
<?php
WP_CLI::add_wp_hook( 'wp_cli_post_block_update_html', function( $block, $new_attrs, $block_name ) {
if ( 'core/paragraph' === $block_name && isset( $new_attrs['customClass'] ) ) {
$block['innerHTML'] = preg_replace(
'/<p([^>]*)>/',
'<p class="' . esc_attr( $new_attrs['customClass'] ) . '"$1>',
$block['innerHTML']
);
$block['innerContent'] = [ $block['innerHTML'] ];
}
return $block;
}, 10, 3 );
"""
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Hello World</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block update {POST_ID} 0 --attrs='{"customClass":"my-custom-class"}' --require=custom-sync-filter.php`
Then STDOUT should contain:
"""
Success: Updated block at index 0 in post {POST_ID}.
"""
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
<p class="my-custom-class">Hello World</p>
"""
@require-wp-5.0
Scenario: Update block content
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Old text</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block update {POST_ID} 0 --content="<p>New text</p>"`
Then STDOUT should contain:
"""
Success: Updated block at index 0 in post {POST_ID}.
"""
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
New text
"""
And STDOUT should not contain:
"""
Old text
"""
@require-wp-5.0
Scenario: Update block with replace-attrs flag
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:heading {\"level\":2,\"align\":\"center\"} --><h2 class=\"has-text-align-center\">Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block update {POST_ID} 0 --attrs='{"level":4}' --replace-attrs`
Then STDOUT should contain:
"""
Success: Updated block at index 0 in post {POST_ID}.
"""
When I run `wp post block parse {POST_ID}`
Then STDOUT should contain:
"""
"level": 4
"""
And STDOUT should not contain:
"""
"align"
"""
@require-wp-5.0
Scenario: Error when no attrs or content provided for update
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I try `wp post block update {POST_ID} 0`
Then STDERR should contain:
"""
You must specify either --attrs or --content.
"""
And the return code should be 1
@require-wp-5.0
Scenario: Porcelain output for update
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block update {POST_ID} 0 --content="<p>New</p>" --porcelain`
Then STDOUT should be:
"""
{POST_ID}
"""
@require-wp-5.0
Scenario: Move block forward
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block move {POST_ID} 0 2`
Then STDOUT should contain:
"""
Success: Moved block from index 0 to index 2 in post {POST_ID}.
"""
When I run `wp post block render {POST_ID}`
Then STDOUT should match /Second.*First/s
@require-wp-5.0
Scenario: Move block backward
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block move {POST_ID} 2 0`
Then STDOUT should contain:
"""
Success: Moved block from index 2 to index 0 in post {POST_ID}.
"""
When I run `wp post block render {POST_ID}`
Then STDOUT should match /Third.*First/s
@require-wp-5.0
Scenario: Move block same index warning
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I try `wp post block move {POST_ID} 0 0`
Then STDERR should contain:
"""
Source and destination indices are the same.
"""
And the return code should be 0
@require-wp-5.0
Scenario: Error on invalid move indices
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I try `wp post block move {POST_ID} 5 0`
Then STDERR should contain:
"""
Invalid from-index: 5
"""
And the return code should be 1
When I try `wp post block move {POST_ID} 0 10`
Then STDERR should contain:
"""
Invalid to-index: 10
"""
And the return code should be 1
@require-wp-5.0
Scenario: Porcelain output for move
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block move {POST_ID} 0 1 --porcelain`
Then STDOUT should be:
"""
{POST_ID}
"""
@require-wp-5.0
Scenario: Export blocks to STDOUT as JSON
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block export {POST_ID}`
Then STDOUT should contain:
"""
"version": "1.0"
"""
And STDOUT should contain:
"""
"generator": "wp-cli/entity-command"
"""
And STDOUT should contain:
"""
"blockName": "core/paragraph"
"""
@require-wp-5.0
Scenario: Export blocks as YAML
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block export {POST_ID} --format=yaml`
Then STDOUT should contain:
"""
version:
"""
And STDOUT should contain:
"""
generator: wp-cli/entity-command
"""
And STDOUT should contain:
"""
blockName: core/paragraph
"""
@require-wp-5.0
Scenario: Export blocks as HTML
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Hello World</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block export {POST_ID} --format=html`
# In WordPress 7.0+ paragraph blocks are rendered with a class name.
# See https://github.com/WordPress/gutenberg/pull/71207.
Then STDOUT should contain:
"""
<p
"""
And STDOUT should contain:
"""
>Hello World</p>
"""
@require-wp-5.0
Scenario: Export blocks to file
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block export {POST_ID} --file=blocks-export.json`
Then STDOUT should contain:
"""
Success: Exported 1 block to blocks-export.json
"""
And the blocks-export.json file should contain:
"""
"blockName": "core/paragraph"
"""
@require-wp-5.0
Scenario: Import blocks from file
Given a WP install
And a blocks-import.json file:
"""
{
"version": "1.0",
"blocks": [
{"blockName": "core/paragraph", "attrs": {}, "innerBlocks": [], "innerHTML": "<p>Imported</p>", "innerContent": ["<p>Imported</p>"]}
]
}
"""
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:heading --><h2>Original</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block import {POST_ID} --file=blocks-import.json`
Then STDOUT should contain:
"""
Success: Imported 1 block into post {POST_ID}.
"""
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/heading | 1 |
| core/paragraph | 1 |
@require-wp-5.0
Scenario: Import blocks at start
Given a WP install
And a blocks-import.json file:
"""
{
"blocks": [
{"blockName": "core/paragraph", "attrs": {}, "innerBlocks": [], "innerHTML": "<p>First</p>", "innerContent": ["<p>First</p>"]}
]
}
"""
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block import {POST_ID} --file=blocks-import.json --position=start`
Then STDOUT should contain:
"""
Success: Imported 1 block into post {POST_ID}.
"""
When I run `wp post block render {POST_ID}`
Then STDOUT should match /First.*Second/s
@require-wp-5.0
Scenario: Import blocks with replace
Given a WP install
And a blocks-import.json file:
"""
{
"blocks": [
{"blockName": "core/heading", "attrs": {"level": 2}, "innerBlocks": [], "innerHTML": "<h2>New Content</h2>", "innerContent": ["<h2>New Content</h2>"]}
]
}
"""
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Old</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block import {POST_ID} --file=blocks-import.json --replace`
Then STDOUT should contain:
"""
Success: Imported 1 block into post {POST_ID}.
"""
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/heading | 1 |
And STDOUT should not contain:
"""
core/paragraph
"""
@require-wp-5.0
Scenario: Import error on missing file
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I try `wp post block import {POST_ID} --file=nonexistent.json`
Then STDERR should contain:
"""
File not found: nonexistent.json
"""
And the return code should be 1
@require-wp-5.0
Scenario: Porcelain output for import
Given a WP install
And a blocks-import.json file:
"""
{
"blocks": [
{"blockName": "core/paragraph", "attrs": {}, "innerBlocks": [], "innerHTML": "<p>One</p>", "innerContent": ["<p>One</p>"]},
{"blockName": "core/paragraph", "attrs": {}, "innerBlocks": [], "innerHTML": "<p>Two</p>", "innerContent": ["<p>Two</p>"]}
]
}
"""
When I run `wp post create --post_title="Block Post" --post_content="" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block import {POST_ID} --file=blocks-import.json --porcelain`
Then STDOUT should be:
"""
2
"""
@require-wp-5.0
Scenario: Count blocks across posts
Given a WP install
When I run `wp post create --post_title="Post 1" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Test2</p><!-- /wp:paragraph -->" --post_status=publish --porcelain`
Then save STDOUT as {POST_1}
When I run `wp post create --post_title="Post 2" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --post_status=publish --porcelain`
Then save STDOUT as {POST_2}
When I run `wp post block count {POST_1} {POST_2}`
Then STDOUT should be a table containing rows:
| blockName | count | posts |
| core/paragraph | 3 | 2 |
| core/heading | 1 | 1 |
@require-wp-5.0
Scenario: Count specific block type
Given a WP install
When I run `wp post create --post_title="Post 1" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Test2</p><!-- /wp:paragraph -->" --post_status=publish --porcelain`
Then save STDOUT as {POST_1}
When I run `wp post block count {POST_1} --block=core/paragraph --format=count`
Then STDOUT should be:
"""
2
"""
@require-wp-5.0
Scenario: Count unique block types
Given a WP install
When I run `wp post create --post_title="Post 1" --post_content="<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --post_status=publish --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block count {POST_ID} --format=count`
Then STDOUT should be:
"""
2
"""
@require-wp-5.0
Scenario: Clone block with default position (after)
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block clone {POST_ID} 0`
Then STDOUT should contain:
"""
Success: Cloned block to index 1 in post {POST_ID}.
"""
When I run `wp post block list {POST_ID}`
Then STDOUT should be a table containing rows:
| blockName | count |
| core/paragraph | 3 |
@require-wp-5.0
Scenario: Clone block to end
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post block clone {POST_ID} 0 --position=end`
Then STDOUT should contain:
"""
Success: Cloned block to index 2 in post {POST_ID}.
"""
When I run `wp post block render {POST_ID}`
Then STDOUT should match /First.*Title.*First/s
@require-wp-5.0
Scenario: Clone block to start
Given a WP install
When I run `wp post create --post_title="Block Post" --post_content="<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->" --porcelain`