Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions features/search-replace-callback.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
Feature: Test search-replace --callback option

@require-mysql
Scenario: Search replace with callback function
Given a WP install
And a callback-function.php file:
"""
<?php
function test_callback( $data, $replacement ) {
return str_replace( 'foo', strtoupper( $replacement ), $data );
}
"""
And I run `wp post create --post_title='foo bar' --post_content='foo content' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'foo' 'baz' wp_posts --callback='test_callback' --precise --require=callback-function.php`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_title | 1 | PHP |
| wp_posts | post_content | 1 | PHP |

When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
BAZ bar
"""

When I run `wp post get {POST_ID} --field=content`
Then STDOUT should be:
"""
BAZ content
"""

@require-mysql
Scenario: Search replace with callback function and regex
Given a WP install
And a callback-regex.php file:
"""
<?php
function regex_callback( $data, $replacement, $search_regex ) {
// Replace matched digits with their square
return preg_replace_callback( $search_regex, function( $matches ) {
$num = (int)$matches[1];
return $num * $num;
}, $data );
}
"""
And I run `wp post create --post_title='Number 5 test' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'Number ([0-9]+)' 'ignored' --regex --callback='regex_callback' --precise --require=callback-regex.php`
Then STDOUT should contain:
"""
Success: Made 1 replacement.
"""

When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
25 test
"""

@require-mysql
Scenario: Search replace with callback function that doesn't exist
Given a WP install

When I try `wp search-replace 'foo' 'bar' --callback='nonexistent_function' --precise`
Then STDERR should be:
"""
Error: The callback function does not exist. Skipping operation.
"""
And the return code should be 1

@require-mysql
Scenario: Search replace with callback requires precise mode
Given a WP install
And a callback-function.php file:
"""
<?php
function test_callback( $data, $replacement ) {
return str_replace( 'foo', strtoupper( $replacement ), $data );
}
"""

When I try `wp search-replace 'foo' 'bar' --callback='test_callback' --no-precise --require=callback-function.php`
Then STDERR should be:
"""
Error: PHP is required to execute a callback function. --no-precise cannot be set.
"""
And the return code should be 1

@require-mysql
Scenario: Callback function with regex search parameter
Given a WP install
And a regex-aware-callback.php file:
"""
<?php
function regex_aware_callback( $data, $replacement, $search_regex ) {
// If regex is provided, use preg_replace, otherwise use str_replace
if ( !empty( $search_regex ) ) {
return preg_replace( $search_regex, $replacement . '_regex', $data );
}
return str_replace( 'foo', $replacement, $data );
}
"""
And I run `wp post create --post_title='foo title' --post_content='foo content' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'foo' 'bar' wp_posts --callback='regex_aware_callback' --precise --require=regex-aware-callback.php`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""

When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
bar title
"""

When I run `wp post get {POST_ID} --field=content`
Then STDOUT should be:
"""
bar content
"""
106 changes: 106 additions & 0 deletions features/search-replace-revisions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
Feature: Test search-replace --revisions option

@require-mysql
Scenario: Search replace without revisions (--no-revisions)
Given a WP install

When I run `wp post create --post_title='Published foo' --post_name='1' --post_status='publish' --porcelain`
Then save STDOUT as {PUBLISHED_ID}

When I run `wp post create --post_title='Draft foo' --post_status='draft' --porcelain`
Then save STDOUT as {DRAFT_ID}

When I run `wp post meta add {PUBLISHED_ID} test_key 'published_foo_meta'`
Then STDOUT should not be empty

When I run `wp post meta add {DRAFT_ID} test_key 'draft_foo_meta'`
Then STDOUT should not be empty

When I run `wp search-replace 'foo' 'bar' --no-revisions`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""

# Verify published post was changed
When I run `wp post get {PUBLISHED_ID} --field=title`
Then STDOUT should be:
"""
Published bar
"""

# Verify draft post was NOT changed
When I run `wp post get {DRAFT_ID} --field=title`
Then STDOUT should be:
"""
Draft foo
"""

# Verify published post meta was changed
When I run `wp post meta get {PUBLISHED_ID} test_key`
Then STDOUT should be:
"""
published_bar_meta
"""

# Verify draft post meta was NOT changed
When I run `wp post meta get {DRAFT_ID} test_key`
Then STDOUT should be:
"""
draft_foo_meta
"""

@require-mysql
Scenario: Search replace with default revisions behavior
Given a WP install

When I run `wp post create --post_title='Published fooooo' --post_name=1 --post_status='publish' --porcelain`
Then save STDOUT as {PUBLISHED_ID}

When I run `wp post create --post_title='Draft fooooo' --post_name=2 --post_status='draft' --porcelain`
Then save STDOUT as {DRAFT_ID}

# With default behavior (--revisions=true), both should be changed
When I run `wp search-replace 'fooooo' 'baz'`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""

When I run `wp post get {PUBLISHED_ID} --field=title`
Then STDOUT should be:
"""
Published baz
"""

When I run `wp post get {DRAFT_ID} --field=title`
Then STDOUT should be:
"""
Draft baz
"""

@require-mysql
Scenario: Combining no-revisions with regex
Given a WP install
And I run `wp post create --post_title='Test foo123' --post_name='pubslug' --post_status='publish' --porcelain`
And save STDOUT as {PUB_ID}
And I run `wp post create --post_title='Test foo456' --post_name='draftslug' --post_status='draft' --porcelain`
And save STDOUT as {DRAFT_ID}

When I run `wp search-replace 'foo[0-9]+' 'bar999' --regex --no-revisions`
Then STDOUT should contain:
"""
Success: Made 1 replacement.
"""

When I run `wp post get {PUB_ID} --field=title`
Then STDOUT should be:
"""
Test bar999
"""

When I run `wp post get {DRAFT_ID} --field=title`
Then STDOUT should be:
"""
Test foo456
"""
Loading