Skip to content

Commit 0071c86

Browse files
committed
phpcbf + features split
1 parent ea90c38 commit 0071c86

4 files changed

Lines changed: 249 additions & 247 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
Feature: Test search-replace --callback option
2+
3+
@require-mysql
4+
Scenario: Search replace with callback function
5+
Given a WP install
6+
And a callback-function.php file:
7+
"""
8+
<?php
9+
function test_callback( $data, $replacement ) {
10+
return str_replace( 'foo', strtoupper( $replacement ), $data );
11+
}
12+
"""
13+
And I run `wp post create --post_title='foo bar' --post_content='foo content' --porcelain`
14+
And save STDOUT as {POST_ID}
15+
16+
When I run `wp search-replace 'foo' 'baz' wp_posts --callback='test_callback' --precise --require=callback-function.php`
17+
Then STDOUT should contain:
18+
"""
19+
Success: Made 2 replacements.
20+
"""
21+
And STDOUT should be a table containing rows:
22+
| Table | Column | Replacements | Type |
23+
| wp_posts | post_title | 1 | PHP |
24+
| wp_posts | post_content | 1 | PHP |
25+
26+
When I run `wp post get {POST_ID} --field=title`
27+
Then STDOUT should be:
28+
"""
29+
BAZ bar
30+
"""
31+
32+
When I run `wp post get {POST_ID} --field=content`
33+
Then STDOUT should be:
34+
"""
35+
BAZ content
36+
"""
37+
38+
@require-mysql
39+
Scenario: Search replace with callback function and regex
40+
Given a WP install
41+
And a callback-regex.php file:
42+
"""
43+
<?php
44+
function regex_callback( $data, $replacement, $search_regex ) {
45+
// Replace matched digits with their square
46+
return preg_replace_callback( $search_regex, function( $matches ) {
47+
$num = (int)$matches[1];
48+
return $num * $num;
49+
}, $data );
50+
}
51+
"""
52+
And I run `wp post create --post_title='Number 5 test' --porcelain`
53+
And save STDOUT as {POST_ID}
54+
55+
When I run `wp search-replace 'Number ([0-9]+)' 'ignored' --regex --callback='regex_callback' --precise --require=callback-regex.php`
56+
Then STDOUT should contain:
57+
"""
58+
Success: Made 1 replacement.
59+
"""
60+
61+
When I run `wp post get {POST_ID} --field=title`
62+
Then STDOUT should be:
63+
"""
64+
25 test
65+
"""
66+
67+
@require-mysql
68+
Scenario: Search replace with callback function that doesn't exist
69+
Given a WP install
70+
71+
When I try `wp search-replace 'foo' 'bar' --callback='nonexistent_function' --precise`
72+
Then STDERR should be:
73+
"""
74+
Error: The callback function does not exist. Skipping operation.
75+
"""
76+
And the return code should be 1
77+
78+
@require-mysql
79+
Scenario: Search replace with callback requires precise mode
80+
Given a WP install
81+
And a callback-function.php file:
82+
"""
83+
<?php
84+
function test_callback( $data, $replacement ) {
85+
return str_replace( 'foo', strtoupper( $replacement ), $data );
86+
}
87+
"""
88+
89+
When I try `wp search-replace 'foo' 'bar' --callback='test_callback' --no-precise --require=callback-function.php`
90+
Then STDERR should be:
91+
"""
92+
Error: PHP is required to execute a callback function. --no-precise cannot be set.
93+
"""
94+
And the return code should be 1
95+
96+
@require-mysql
97+
Scenario: Callback function with regex search parameter
98+
Given a WP install
99+
And a regex-aware-callback.php file:
100+
"""
101+
<?php
102+
function regex_aware_callback( $data, $replacement, $search_regex ) {
103+
// If regex is provided, use preg_replace, otherwise use str_replace
104+
if ( !empty( $search_regex ) ) {
105+
return preg_replace( $search_regex, $replacement . '_regex', $data );
106+
}
107+
return str_replace( 'foo', $replacement, $data );
108+
}
109+
"""
110+
And I run `wp post create --post_title='foo title' --post_content='foo content' --porcelain`
111+
And save STDOUT as {POST_ID}
112+
113+
When I run `wp search-replace 'foo' 'bar' wp_posts --callback='regex_aware_callback' --precise --require=regex-aware-callback.php`
114+
Then STDOUT should contain:
115+
"""
116+
Success: Made 2 replacements.
117+
"""
118+
119+
When I run `wp post get {POST_ID} --field=title`
120+
Then STDOUT should be:
121+
"""
122+
bar title
123+
"""
124+
125+
When I run `wp post get {POST_ID} --field=content`
126+
Then STDOUT should be:
127+
"""
128+
bar content
129+
"""
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
Feature: Test search-replace --revisions option
2+
3+
@require-mysql
4+
Scenario: Search replace without revisions (--no-revisions)
5+
Given a WP install
6+
7+
When I run `wp post create --post_title='Published foo' --post_name='1' --post_status='publish' --porcelain`
8+
Then save STDOUT as {PUBLISHED_ID}
9+
10+
When I run `wp post create --post_title='Draft foo' --post_status='draft' --porcelain`
11+
Then save STDOUT as {DRAFT_ID}
12+
13+
When I run `wp post meta add {PUBLISHED_ID} test_key 'published_foo_meta'`
14+
Then STDOUT should not be empty
15+
16+
When I run `wp post meta add {DRAFT_ID} test_key 'draft_foo_meta'`
17+
Then STDOUT should not be empty
18+
19+
When I run `wp search-replace 'foo' 'bar' --no-revisions`
20+
Then STDOUT should contain:
21+
"""
22+
Success: Made 2 replacements.
23+
"""
24+
25+
# Verify published post was changed
26+
When I run `wp post get {PUBLISHED_ID} --field=title`
27+
Then STDOUT should be:
28+
"""
29+
Published bar
30+
"""
31+
32+
# Verify draft post was NOT changed
33+
When I run `wp post get {DRAFT_ID} --field=title`
34+
Then STDOUT should be:
35+
"""
36+
Draft foo
37+
"""
38+
39+
# Verify published post meta was changed
40+
When I run `wp post meta get {PUBLISHED_ID} test_key`
41+
Then STDOUT should be:
42+
"""
43+
published_bar_meta
44+
"""
45+
46+
# Verify draft post meta was NOT changed
47+
When I run `wp post meta get {DRAFT_ID} test_key`
48+
Then STDOUT should be:
49+
"""
50+
draft_foo_meta
51+
"""
52+
53+
@require-mysql
54+
Scenario: Search replace with default revisions behavior
55+
Given a WP install
56+
57+
When I run `wp post create --post_title='Published fooooo' --post_name=1 --post_status='publish' --porcelain`
58+
Then save STDOUT as {PUBLISHED_ID}
59+
60+
When I run `wp post create --post_title='Draft fooooo' --post_name=2 --post_status='draft' --porcelain`
61+
Then save STDOUT as {DRAFT_ID}
62+
63+
# With default behavior (--revisions=true), both should be changed
64+
When I run `wp search-replace 'fooooo' 'baz'`
65+
Then STDOUT should contain:
66+
"""
67+
Success: Made 2 replacements.
68+
"""
69+
70+
When I run `wp post get {PUBLISHED_ID} --field=title`
71+
Then STDOUT should be:
72+
"""
73+
Published baz
74+
"""
75+
76+
When I run `wp post get {DRAFT_ID} --field=title`
77+
Then STDOUT should be:
78+
"""
79+
Draft baz
80+
"""
81+
82+
@require-mysql
83+
Scenario: Combining no-revisions with regex
84+
Given a WP install
85+
And I run `wp post create --post_title='Test foo123' --post_name='pubslug' --post_status='publish' --porcelain`
86+
And save STDOUT as {PUB_ID}
87+
And I run `wp post create --post_title='Test foo456' --post_name='draftslug' --post_status='draft' --porcelain`
88+
And save STDOUT as {DRAFT_ID}
89+
90+
When I run `wp search-replace 'foo[0-9]+' 'bar999' --regex --no-revisions`
91+
Then STDOUT should contain:
92+
"""
93+
Success: Made 1 replacement.
94+
"""
95+
96+
When I run `wp post get {PUB_ID} --field=title`
97+
Then STDOUT should be:
98+
"""
99+
Test bar999
100+
"""
101+
102+
When I run `wp post get {DRAFT_ID} --field=title`
103+
Then STDOUT should be:
104+
"""
105+
Test foo456
106+
"""

0 commit comments

Comments
 (0)