diff --git a/features/post-revision.feature b/features/post-revision.feature index 594d6a7ee..443a33113 100644 --- a/features/post-revision.feature +++ b/features/post-revision.feature @@ -150,6 +150,40 @@ Feature: Manage WordPress post revisions When I run `wp post revision diff {REVISION_ID} --field=post_title` Then the return code should be 0 + Scenario: Diff shows percent signs in the compared content as they are + When I run `wp post create --post_title='Percent Test' --post_content='Start' --porcelain` + Then STDOUT should be a number + And save STDOUT as {POST_ID} + + When I run `wp post update {POST_ID} --post_content='Progress: 100%'` + Then STDOUT should contain: + """ + Success: Updated post {POST_ID}. + """ + + When I run `wp post update {POST_ID} --post_content='Progress: 100% of https://example.com/my%20file.pdf'` + Then STDOUT should contain: + """ + Success: Updated post {POST_ID}. + """ + + When I run `wp post list --post_type=revision --post_parent={POST_ID} --fields=ID --format=ids --orderby=ID --order=ASC` + Then STDOUT should not be empty + And save STDOUT as {REVISION_IDS} + + When I run `echo "{REVISION_IDS}" | awk '{print $1}'` + Then save STDOUT as {REVISION_ID_1} + + When I run `echo "{REVISION_IDS}" | awk '{print $2}'` + Then save STDOUT as {REVISION_ID_2} + + When I run `wp post revision diff {REVISION_ID_1} {REVISION_ID_2}` + Then STDOUT should contain: + """ + - Progress: 100% + + Progress: 100% of https://example.com/my%20file.pdf + """ + Scenario: Prune revisions keeping latest N When I run `wp post create --post_title='Prune Test' --post_content='Version 1' --porcelain` Then STDOUT should be a number diff --git a/src/Post_Revision_Command.php b/src/Post_Revision_Command.php index c5a696a76..2504c2c46 100644 --- a/src/Post_Revision_Command.php +++ b/src/Post_Revision_Command.php @@ -204,7 +204,7 @@ public function diff( $args, $assoc_args ) { WP_CLI::colorize( sprintf( '%%y--- %s (%s) - ID %d%%n', - $from_revision->post_title, + $this->escape_color_tokens( $from_revision->post_title ), $from_revision->post_modified, $from_revision->ID ) @@ -214,7 +214,7 @@ public function diff( $args, $assoc_args ) { WP_CLI::colorize( sprintf( '%%y+++ %s (%s) - ID %d%%n', - $to_revision->post_title, + $this->escape_color_tokens( $to_revision->post_title ), $to_revision->post_modified, $to_revision->ID ) @@ -243,25 +243,35 @@ private function render_cli_diff( $diff ) { } elseif ( $edit instanceof \Text_Diff_Op_add ) { // Added lines - show in green foreach ( $edit->final as $line ) { - WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) ); + WP_CLI::line( WP_CLI::colorize( '%g+ ' . $this->escape_color_tokens( $line ) . '%n' ) ); } } elseif ( $edit instanceof \Text_Diff_Op_delete ) { // Deleted lines - show in red foreach ( $edit->orig as $line ) { - WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) ); + WP_CLI::line( WP_CLI::colorize( '%r- ' . $this->escape_color_tokens( $line ) . '%n' ) ); } } elseif ( $edit instanceof \Text_Diff_Op_change ) { // Changed lines - show deletions in red, additions in green foreach ( $edit->orig as $line ) { - WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) ); + WP_CLI::line( WP_CLI::colorize( '%r- ' . $this->escape_color_tokens( $line ) . '%n' ) ); } foreach ( $edit->final as $line ) { - WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) ); + WP_CLI::line( WP_CLI::colorize( '%g+ ' . $this->escape_color_tokens( $line ) . '%n' ) ); } } } } + /** + * Escapes percent signs so that WP_CLI::colorize() does not read them as color tokens. + * + * @param string $text Text to print inside a colorized string. + * @return string Text with every '%' doubled. + */ + private function escape_color_tokens( $text ) { + return str_replace( '%', '%%', $text ); + } + /** * Deletes old post revisions. *