|
| 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 | + """ |
0 commit comments