-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsearch-replace.feature
More file actions
1637 lines (1430 loc) · 56.1 KB
/
search-replace.feature
File metadata and controls
1637 lines (1430 loc) · 56.1 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: Do global search/replace
@require-mysql
Scenario: Basic search/replace
Given a WP install
When I run `wp search-replace foo bar`
Then STDOUT should contain:
"""
guid
"""
When I run `wp search-replace foo bar --skip-tables=wp_posts`
Then STDOUT should not contain:
"""
wp_posts
"""
When I run `wp search-replace foo bar --skip-tables=wp_post\*`
Then STDOUT should not contain:
"""
wp_posts
"""
And STDOUT should not contain:
"""
wp_postmeta
"""
And STDOUT should contain:
"""
wp_users
"""
When I run `wp search-replace foo bar --skip-columns=guid`
Then STDOUT should not contain:
"""
guid
"""
When I run `wp search-replace foo bar --include-columns=post_content`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 0 | SQL |
When I run `wp search-replace foo bar --skip-columns=wp_posts.guid`
Then STDOUT should not contain:
"""
guid
"""
When I run `wp search-replace foo bar --include-columns=wp_posts.post_content`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 0 | SQL |
@require-mysql
Scenario: Multisite search/replace
Given a WP multisite install
And I run `wp site create --slug="foo" --title="foo" --email="foo@example.com"`
And I run `wp search-replace foo bar --network`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_2_options | option_value | 4 | PHP |
| wp_blogs | path | 1 | SQL |
@require-mysql
Scenario: Don't run on unregistered tables by default
Given a WP install
And I run `wp db query "CREATE TABLE wp_awesome ( id int(11) unsigned NOT NULL AUTO_INCREMENT, awesome_stuff TEXT, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"`
When I run `wp search-replace foo bar`
Then STDOUT should not contain:
"""
wp_awesome
"""
When I run `wp search-replace foo bar --all-tables-with-prefix`
Then STDOUT should contain:
"""
wp_awesome
"""
@require-mysql
Scenario: Skip views during search/replace
Given a WP install
And I run `wp db query "CREATE VIEW wp_posts_view AS SELECT ID, post_title FROM wp_posts;"`
When I run `wp search-replace foo bar --all-tables-with-prefix`
Then STDOUT should contain:
"""
wp_posts_view
"""
And STDOUT should contain:
"""
skipped (view)
"""
When I run `wp search-replace foo bar --all-tables`
Then STDOUT should contain:
"""
wp_posts_view
"""
And STDOUT should contain:
"""
skipped (view)
"""
@require-mysql
Scenario: Run on unregistered, unprefixed tables with --all-tables flag
Given a WP install
And I run `wp db query "CREATE TABLE awesome_table ( id int(11) unsigned NOT NULL AUTO_INCREMENT, awesome_stuff TEXT, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"`
When I run `wp search-replace foo bar`
Then STDOUT should not contain:
"""
awesome_table
"""
When I run `wp search-replace foo bar --all-tables`
Then STDOUT should contain:
"""
awesome_table
"""
@require-mysql
Scenario: Run on all tables matching string with wildcard
Given a WP install
When I run `wp option set bar fooz`
And I run `wp option get bar`
Then STDOUT should be:
"""
fooz
"""
When I run `wp post create --post_title=bar --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post meta add {POST_ID} fooz bar`
Then STDOUT should not be empty
When I run `wp search-replace bar burrito wp_post\?`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_title | 1 | SQL |
And STDOUT should not contain:
"""
wp_options
"""
When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
burrito
"""
When I run `wp post meta get {POST_ID} fooz`
Then STDOUT should be:
"""
bar
"""
When I run `wp option get bar`
Then STDOUT should be:
"""
fooz
"""
When I try `wp search-replace fooz burrito wp_opt\*on`
Then STDERR should be:
"""
Error: Couldn't find any tables matching: wp_opt*on
"""
And the return code should be 1
When I run `wp search-replace fooz burrito wp_opt\* wp_postme\*`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_key | 1 | SQL |
And STDOUT should not contain:
"""
wp_posts
"""
When I run `wp option get bar`
Then STDOUT should be:
"""
burrito
"""
When I run `wp post meta get {POST_ID} burrito`
Then STDOUT should be:
"""
bar
"""
@require-mysql
Scenario: Quiet search/replace
Given a WP install
When I run `wp search-replace foo bar --quiet`
Then STDOUT should be empty
@require-mysql
Scenario: Verbose search/replace
Given a WP install
And I run `wp post create --post_title='Replace this text' --porcelain`
And save STDOUT as {POSTID}
When I run `wp search-replace 'Replace' 'Replaced' --verbose`
Then STDOUT should contain:
"""
Checking: wp_posts.post_title
1 rows affected
"""
When I run `wp search-replace 'Replace' 'Replaced' --verbose --precise`
Then STDOUT should contain:
"""
Checking: wp_posts.post_title
1 rows affected
"""
# See https://github.com/wp-cli/search-replace-command/issues/190
Scenario: Regex search/replace
Given a WP install
When I run `wp search-replace '(Hello)\s(world)' '$2, $1' --regex`
Then STDOUT should contain:
"""
wp_posts
"""
When I run `wp post list --fields=post_title`
Then STDOUT should contain:
"""
world, Hello
"""
Scenario: Regex search/replace with a incorrect `--regex-flags`
Given a WP install
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-flags='kppr'`
Then STDERR should be:
"""
Error: The regex pattern '(Hello)\s(world)' with default delimiter 'chr(1)' and flags 'kppr' fails.
preg_match(): Unknown modifier 'k'.
"""
And the return code should be 1
@require-mysql
Scenario: Search and replace within theme mods
Given a WP install
And a setup-theme-mod.php file:
"""
<?php
set_theme_mod( 'header_image_data', (object) array( 'url' => 'https://subdomain.example.com/foo.jpg' ) );
"""
And I run `wp eval-file setup-theme-mod.php`
When I run `wp theme mod get header_image_data`
Then STDOUT should be a table containing rows:
| key | value |
| header_image_data | {"url":"https:\/\/subdomain.example.com\/foo.jpg"} |
When I run `wp search-replace subdomain.example.com example.com --no-recurse-objects`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 0 | PHP |
When I run `wp search-replace subdomain.example.com example.com`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 1 | PHP |
When I run `wp theme mod get header_image_data`
Then STDOUT should be a table containing rows:
| key | value |
| header_image_data | {"url":"https:\/\/example.com\/foo.jpg"} |
@require-mysql
Scenario: Search and replace handles JSON-encoded URLs in post content
Given a WP install
When I run `wp post create --post_content='{"src":"http:\/\/example.com\/wp-content\/uploads\/fonts\/test.woff2","fontWeight":"400"}' --post_status=publish --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
http:\/\/example.com
"""
When I run `wp search-replace 'http://example.com' 'http://newdomain.com' wp_posts --include-columns=post_content`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 1 | SQL |
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
http:\/\/newdomain.com
"""
And STDOUT should not contain:
"""
http:\/\/example.com
"""
When I run `wp search-replace 'http://newdomain.com' 'http://example.com' wp_posts --include-columns=post_content --precise`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 1 | PHP |
When I run `wp post get {POST_ID} --field=post_content`
Then STDOUT should contain:
"""
http:\/\/example.com
"""
@require-mysql
Scenario: Search and replace with quoted strings
Given a WP install
When I run `wp post create --post_content='<a href="https://apple.com">Apple</a>' --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post get {POST_ID} --field=content`
Then STDOUT should be:
"""
<a href="https://apple.com">Apple</a>
"""
When I run `wp search-replace '<a href="https://apple.com">Apple</a>' '<a href="https://google.com">Google</a>' --dry-run`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 1 | SQL |
When I run `wp search-replace '<a href="https://apple.com">Apple</a>' '<a href="https://google.com">Google</a>'`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 1 | SQL |
When I run `wp search-replace '<a href="https://google.com">Google</a>' '<a href="https://apple.com">Apple</a>' --dry-run`
Then STDOUT should contain:
"""
1 replacement to be made.
"""
When I run `wp post get {POST_ID} --field=content`
Then STDOUT should be:
"""
<a href="https://google.com">Google</a>
"""
Scenario: Search and replace with the same terms
Given a WP install
When I try `wp search-replace foo foo`
Then STDERR should be:
"""
Warning: Replacement value 'foo' is identical to search value 'foo'. Skipping operation.
"""
And STDOUT should be empty
And the return code should be 0
@require-mysql
Scenario: Search and replace a table that has a multi-column primary key
Given a WP install
And I run `wp db query "CREATE TABLE wp_multicol ( "id" bigint(20) NOT NULL AUTO_INCREMENT,"name" varchar(60) NOT NULL,"value" text NOT NULL,PRIMARY KEY ("id","name"),UNIQUE KEY "name" ("name") ) ENGINE=InnoDB DEFAULT CHARSET=utf8 "`
And I run `wp db query "INSERT INTO wp_multicol VALUES (1, 'foo', 'bar')"`
And I run `wp db query "INSERT INTO wp_multicol VALUES (2, 'bar', 'foo')"`
When I run `wp search-replace bar replaced wp_multicol --all-tables`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_multicol | name | 1 | SQL |
| wp_multicol | value | 1 | SQL |
# Skip on 5.0 for now due to difficulties introduced by https://core.trac.wordpress.org/changeset/42981
@less-than-wp-5.0
Scenario Outline: Large guid search/replace where replacement contains search (or not)
Given a WP install
And I run `wp option get siteurl`
And save STDOUT as {SITEURL}
And I run `wp site empty --yes`
And I run `wp post generate --count=20`
When I run `wp search-replace <flags> {SITEURL} <replacement>`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | guid | 20 | SQL |
Examples:
| replacement | flags |
| {SITEURL}/subdir | |
| https://newdomain.com | |
| https://newdomain.com | --dry-run |
@require-mysql
Scenario Outline: Choose replacement method (PHP or MySQL/MariaDB) given proper flags or data.
Given a WP install
And I run `wp option get siteurl`
And save STDOUT as {SITEURL}
When I run `wp search-replace <flags> {SITEURL} https://wordpress.org`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 2 | <serial> |
| wp_posts | post_title | 0 | <noserial> |
Examples:
| flags | serial | noserial |
| | PHP | SQL |
| --precise | PHP | PHP |
@require-mysql
Scenario Outline: Ensure search and replace uses PHP (precise) mode when serialized data is found
Given a WP install
And I run `wp post create --post_content='<input>' --porcelain`
And save STDOUT as {CONTROLPOST}
And I run `wp search-replace --precise foo bar`
And I run `wp post get {CONTROLPOST} --field=content`
And save STDOUT as {CONTROL}
And I run `wp post create --post_content='<input>' --porcelain`
And save STDOUT as {TESTPOST}
And I run `wp search-replace foo bar`
When I run `wp post get {TESTPOST} --field=content`
Then STDOUT should be:
"""
{CONTROL}
"""
Examples:
| input |
| a:1:{s:3:"bar";s:3:"foo";} |
| O:8:"stdClass":1:{s:1:"a";s:3:"foo";} |
@require-mysql
Scenario: Search replace with a regex flag
Given a WP install
When I run `wp search-replace 'EXAMPLE.com' 'BAXAMPLE.com' wp_options --regex`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 0 | PHP |
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.com
"""
When I run `wp search-replace 'EXAMPLE.com' 'BAXAMPLE.com' wp_options --regex --regex-flags=i`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 5 | PHP |
When I run `wp option get home`
Then STDOUT should be:
"""
https://BAXAMPLE.com
"""
@require-mysql
Scenario: Search replace with a regex delimiter
Given a WP install
When I run `wp search-replace 'HTTPS://EXAMPLE.COM' 'https://example.jp/' wp_options --regex --regex-flags=i --regex-delimiter='#'`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 2 | PHP |
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.jp
"""
When I run `wp search-replace 'https://example.jp/' 'https://example.com/' wp_options --regex-delimiter='/'`
Then STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 2 | PHP |
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.com
"""
# NOTE: The preg_match() error message is a substring of the actual message that matches across supported PHP versions.
# In PHP 8.2, the error message changed from
# "preg_match(): Delimiter must not be alphanumeric or backslash."
# to
# "preg_match(): Delimiter must not be alphanumeric, backslash, or NUL"
When I try `wp search-replace 'HTTPS://EXAMPLE.COM' 'https://example.jp/' wp_options --regex --regex-flags=i --regex-delimiter='1'`
Then STDERR should contain:
"""
Error: The regex '1HTTPS://EXAMPLE.COM1i' fails.
preg_match(): Delimiter must not be alphanumeric
"""
And the return code should be 1
When I try `wp search-replace 'regex error)' '' --regex`
Then STDERR should contain:
"""
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and no flags fails.
"""
And STDERR should contain:
"""
preg_match(): Compilation failed:
"""
And STDERR should contain:
"""
at offset 11
"""
And the return code should be 1
When I try `wp search-replace 'regex error)' '' --regex --regex-flags=u`
Then STDERR should contain:
"""
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and flags 'u' fails.
"""
And STDERR should contain:
"""
preg_match(): Compilation failed:
"""
And STDERR should contain:
"""
at offset 11
"""
And the return code should be 1
When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/`
Then STDERR should contain:
"""
Error: The regex '/regex error)/' fails.
"""
And STDERR should contain:
"""
preg_match(): Compilation failed:
"""
And STDERR should contain:
"""
at offset 11
"""
And the return code should be 1
When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/ --regex-flags=u`
Then STDERR should contain:
"""
Error: The regex '/regex error)/u' fails.
"""
And STDERR should contain:
"""
preg_match(): Compilation failed:
"""
And STDERR should contain:
"""
at offset 11
"""
And the return code should be 1
@require-mysql
Scenario: Formatting as count-only
Given a WP install
And I run `wp option set foo 'ALPHA.example.com'`
# --quite should suppress --format=count
When I run `wp search-replace 'ALPHA.example.com' 'BETA.example.com' --quiet --format=count`
Then STDOUT should be empty
# --format=count should suppress --verbose
When I run `wp search-replace 'BETA.example.com' 'ALPHA.example.com' --format=count --verbose`
Then STDOUT should be:
"""
1
"""
# The normal command
When I run `wp search-replace 'ALPHA.example.com' 'BETA.example.com' --format=count`
Then STDOUT should be:
"""
1
"""
# Lets just make sure that zero works, too.
When I run `wp search-replace 'DELTA.example.com' 'ALPHA.example.com' --format=count`
Then STDOUT should be:
"""
0
"""
@require-mysql
Scenario: Search / replace should cater for field/table names that use reserved words or unusual characters
Given a WP install
And a esc_sql_ident.sql file:
"""
CREATE TABLE `TABLE` (`KEY` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `VALUES` TEXT, `back``tick` TEXT, `single'double"quote` TEXT, PRIMARY KEY (`KEY`) );
INSERT INTO `TABLE` (`VALUES`, `back``tick`, `single'double"quote`) VALUES ('v"vvvv_v1', 'v"vvvv_v1', 'v"vvvv_v1' );
INSERT INTO `TABLE` (`VALUES`, `back``tick`, `single'double"quote`) VALUES ('v"vvvv_v2', 'v"vvvv_v2', 'v"vvvv_v2' );
"""
When I run `wp db query "SOURCE esc_sql_ident.sql;"`
Then STDERR should be empty
When I run `wp search-replace 'v"vvvv_v' 'w"wwww_w' TABLE --format=count --all-tables`
Then STDOUT should be:
"""
6
"""
And STDERR should be empty
# Regex uses wpdb::update() which can't handle backticks in field names so avoid `back``tick` column.
When I run `wp search-replace 'w"wwww_w' 'v"vvvv_v' TABLE --regex --include-columns='VALUES,single'\''double"quote' --format=count --all-tables`
Then STDOUT should be:
"""
4
"""
And STDERR should be empty
@require-mysql @suppress_report__only_changes
Scenario: Suppress report or only report changes
Given a WP install
When I run `wp option set foo baz`
And I run `wp option get foo`
Then STDOUT should be:
"""
baz
"""
When I run `wp post create --post_title=baz --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp post meta add {POST_ID} foo baz`
Then STDOUT should not be empty
When I run `wp search-replace baz baz1`
Then STDOUT should contain:
"""
Success: Made 3 replacements.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_commentmeta | meta_key | 0 | SQL |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | SQL |
| wp_posts | post_title | 1 | SQL |
| wp_users | display_name | 0 | SQL |
And STDERR should be empty
When I run `wp search-replace baz1 baz2 --report`
Then STDOUT should contain:
"""
Success: Made 3 replacements.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_commentmeta | meta_key | 0 | SQL |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | SQL |
| wp_posts | post_title | 1 | SQL |
| wp_users | display_name | 0 | SQL |
And STDERR should be empty
When I run `wp search-replace baz2 baz3 --no-report`
Then STDOUT should contain:
"""
Success: Made 3 replacements.
"""
And STDOUT should not contain:
"""
Table Column Replacements Type
"""
And STDOUT should not contain:
"""
wp_commentmeta meta_key 0 SQL
"""
And STDOUT should not contain:
"""
wp_options option_value 1 PHP
"""
And STDERR should be empty
When I run `wp search-replace baz3 baz4 --no-report-changed-only`
Then STDOUT should contain:
"""
Success: Made 3 replacements.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_commentmeta | meta_key | 0 | SQL |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | SQL |
| wp_posts | post_title | 1 | SQL |
| wp_users | display_name | 0 | SQL |
And STDERR should be empty
When I run `wp search-replace baz4 baz5 --report-changed-only`
Then STDOUT should contain:
"""
Success: Made 3 replacements.
"""
And STDOUT should end with a table containing rows:
| Table | Column | Replacements | Type |
| wp_options | option_value | 1 | PHP |
| wp_postmeta | meta_value | 1 | SQL |
| wp_posts | post_title | 1 | SQL |
And STDOUT should not contain:
"""
wp_commentmeta meta_key 0 SQL
"""
And STDOUT should not contain:
"""
wp_users display_name 0 SQL
"""
And STDERR should be empty
When I run `wp search-replace nobaz1 baz6 --report-changed-only`
Then STDOUT should contain:
"""
Success: Made 0 replacements.
"""
And STDOUT should not contain:
"""
Table Column Replacements Type
"""
And STDERR should be empty
@require-mysql @no_table__no_primary_key
Scenario: Deal with non-existent table and table with no primary keys
Given a WP install
When I try `wp search-replace foo bar no_such_table --all-tables`
Then STDOUT should be empty
And STDERR should be:
"""
Error: Couldn't find any tables matching: no_such_table
"""
And the return code should be 1
When I run `wp db query "CREATE TABLE no_key ( awesome_stuff TEXT );"`
And I run `wp search-replace foo bar no_key --all-tables`
Then STDOUT should contain:
"""
Success: Made 0 replacements.
"""
And STDOUT should end with a table containing rows:
| Table | Column | Replacements | Type |
| no_key | | skipped | |
And STDERR should be empty
When I run `wp search-replace foo bar no_key --report-changed-only --all-tables`
Then STDOUT should contain:
"""
Success: Made 0 replacements.
"""
And STDOUT should not contain:
"""
| Table | Column | Replacements | Type |
| no_key | | skipped | |
"""
And STDERR should be empty
When I try `wp search-replace foo bar no_key --no-report --all-tables`
Then STDOUT should contain:
"""
Success: Made 0 replacements.
"""
And STDOUT should not contain:
"""
Table Column Replacements Type
"""
And STDERR should be:
"""
Warning: No primary keys for table 'no_key'.
"""
And the return code should be 0
@require-mysql
Scenario: Search / replace is case sensitive
Given a WP install
When I run `wp post create --post_title='Case Sensitive' --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp search-replace sensitive insensitive`
Then STDOUT should contain:
"""
Success: Made 0 replacements.
"""
And STDERR should be empty
When I run `wp search-replace sensitive insensitive --dry-run`
Then STDOUT should contain:
"""
Success: 0 replacements to be made.
"""
And STDERR should be empty
When I run `wp search-replace Sensitive insensitive --dry-run`
Then STDOUT should contain:
"""
Success: 1 replacement to be made.
"""
And STDERR should be empty
When I run `wp search-replace Sensitive insensitive`
Then STDOUT should contain:
"""
Success: Made 1 replacement.
"""
And STDERR should be empty
@require-mysql
Scenario: Logging with simple replace
Given a WP install
When I run `wp post create --post_title='Title_baz__baz_' --post_content='Content_baz_12345678901234567890_baz_12345678901234567890' --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp search-replace '_baz_' '_' wp_posts --dry-run --log --before_context=10 --after_context=10`
Then STDOUT should contain:
"""
Success: 2 replacements to be made.
"""
And STDOUT should end with a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 1 | SQL |
| wp_posts | post_title | 1 | SQL |
And STDOUT should contain:
"""
wp_posts.post_content:{POST_ID}
< Content_baz_1234567890 [...] 1234567890_baz_1234567890
> Content_1234567890 [...] 1234567890_1234567890
"""
And STDOUT should contain:
"""
wp_posts.post_title:{POST_ID}
< Title_baz__baz_
> Title__
"""
And STDERR should be empty
When I run `wp search-replace '_baz_' '' wp_posts --dry-run --log=replace.log`
Then STDOUT should contain:
"""
Success: 2 replacements to be made.
"""
And STDOUT should not contain:
"""
< Content
"""
And the replace.log file should contain:
"""
wp_posts.post_content:{POST_ID}
< Content_baz_12345678901234567890_baz_12345678901234567890
> Content1234567890123456789012345678901234567890
"""
And the replace.log file should contain:
"""
wp_posts.post_title:{POST_ID}
< Title_baz__baz_
> Title
"""
And STDERR should be empty
# kana with diacritic and decomposed "a" + umlaut.
When I run `wp search-replace '_baz_' '_バäz_' wp_posts --log=- --before_context=10 --after_context=20`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""
And STDOUT should contain:
"""
wp_posts.post_content:{POST_ID}
< Content_baz_12345678901234567890 [...] 1234567890_baz_12345678901234567890
> Content_バäz_12345678901234567890 [...] 1234567890_バäz_12345678901234567890
"""
And STDERR should be empty
# Testing UTF-8 context
When I run `wp search-replace 'z_' 'zzzz_' wp_posts --log --before_context=2 --after_context=1`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""
And STDOUT should contain:
"""
wp_posts.post_content:{POST_ID}
< バäz_1 [...] バäz_1
> バäzzzz_1 [...] バäzzzz_1
"""
And STDERR should be empty
When I run `wp option set foobar '_bar1_ _bar1_12345678901234567890123456789012345678901234567890_bar1_ _bar1_1234567890123456789012345678901234567890'`
And I run `wp search-replace '_bar1_' '_baz1_' wp_options --log`
Then STDOUT should contain:
"""
< _bar1_ _bar1_1234567890123456789012345678901234567890 [...] 1234567890123456789012345678901234567890_bar1_ _bar1_1234567890123456789012345678901234567890
> _baz1_ _baz1_1234567890123456789012345678901234567890 [...] 1234567890123456789012345678901234567890_baz1_ _baz1_1234567890123456789012345678901234567890
"""
And STDERR should be empty
When I run `wp option get foobar`
Then STDOUT should be:
"""
_baz1_ _baz1_12345678901234567890123456789012345678901234567890_baz1_ _baz1_1234567890123456789012345678901234567890
"""
When I run `wp search-replace '_baz1_' '_bar1_' wp_options --log --before_context=10 --after_context=10`
Then STDOUT should contain:
"""
< _baz1_ _baz1_1234567890 [...] 1234567890_baz1_ _baz1_1234567890
> _bar1_ _bar1_1234567890 [...] 1234567890_bar1_ _bar1_1234567890
"""
And STDERR should be empty
When I run `wp option set foobar2 '12345678901234567890_bar2_1234567890_bar2_ _bar2_ _bar2_'`
And I run `wp search-replace '_bar2_' '_baz2baz2_' wp_options --log --before_context=10 --after_context=10`
Then STDOUT should contain:
"""
< 1234567890_bar2_1234567890 [...] 1234567890_bar2_ _bar2_ _bar2_
> 1234567890_baz2baz2_1234567890 [...] 1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
"""
And STDERR should be empty
When I run `wp option get foobar2`
Then STDOUT should be:
"""
12345678901234567890_baz2baz2_1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
"""
When I run `wp search-replace '_baz2baz2_' '_barz2_' wp_options --log --before_context=10 --after_context=4`
Then STDOUT should contain:
"""
< 1234567890_baz2baz2_1234 [...] 1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
> 1234567890_barz2_1234 [...] 1234567890_barz2_ _barz2_ _barz2_
"""
And STDERR should be empty
When I run `wp option set foobar3 '_bar3 _bar3 _bar3 _bar3'`
And I run `wp search-replace '_bar3' 'baz3' wp_options --log`
Then STDOUT should contain:
"""
< _bar3 _bar3 _bar3 _bar3
> baz3 baz3 baz3 baz3
"""
And STDERR should be empty
When I run `wp option get foobar3`
Then STDOUT should be:
"""
baz3 baz3 baz3 baz3
"""
When I run `wp search-replace 'baz3' 'baz\3' wp_options --dry-run --log`
Then STDOUT should contain:
"""
< baz3 baz3 baz3 baz3
> baz\3 baz\3 baz\3 baz\3
"""
And STDERR should be empty
# See https://github.com/wp-cli/search-replace-command/issues/190
# SQLite: regex backreference replacement produces different results due to
# LIKE case-sensitivity differences. Requires further investigation.
@skip-sqlite
Scenario: Logging with regex replace
Given a WP install
When I run `wp post create --post_title='Title_baz__boz_' --post_content='Content_baz_1234567890_bez_1234567890_biz_1234567890_boz_1234567890_buz_' --porcelain`
Then save STDOUT as {POST_ID}
When I run `wp search-replace '_b[aeiou]z_' '_bz_' wp_posts --regex --dry-run --log --before_context=11 --after_context=11`
Then STDOUT should contain:
"""
Success: 2 replacements to be made.
"""
And STDOUT should end with a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_content | 1 | PHP |
| wp_posts | post_title | 1 | PHP |
And STDOUT should contain:
"""
wp_posts.post_content:{POST_ID}
< Content_baz_1234567890_bez_1234567890_biz_1234567890_boz_1234567890_buz_
> Content_bz_1234567890_bz_1234567890_bz_1234567890_bz_1234567890_bz_
"""
And STDOUT should contain:
"""
wp_posts.post_title:{POST_ID}
< Title_baz__boz_
> Title_bz__bz_
"""
And STDERR should be empty
When I run `wp search-replace '_b([aeiou])z_' '_$1b\\1z_\0' wp_posts --regex --log --before_context=11 --after_context=11`
Then STDOUT should contain:
"""
Success: Made 2 replacements.