-
Notifications
You must be signed in to change notification settings - Fork 92
Add wp post revision restore, diff, and prune commands #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
16
commits into
main
Choose a base branch
from
copilot/add-post-revision-commands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+682
−1
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f919b12
Initial plan
Copilot 8f2b422
Add Post_Revision_Command with restore and diff subcommands
Copilot 98d8590
Address code review feedback: validate fields properly and remove unu…
Copilot 598c559
Fix error message display and simplify return value check
Copilot af620a2
Handle null return value from wp_restore_post_revision and improve er…
Copilot 9b54f2f
Update src/Post_Revision_Command.php
swissspidy 3e511a1
Update src/Post_Revision_Command.php
swissspidy acc2907
Update src/Post_Revision_Command.php
swissspidy c1c8cf9
Complete test scenario for diff between two revisions
Copilot 4b174c4
Replace wp_text_diff HTML output with CLI-friendly colored diff
Copilot 087ee6b
Fix test
swissspidy f812fc5
Update tests
swissspidy b6babf6
Add wp post revision prune command
Copilot 52cb85a
Merge branch 'main' into copilot/add-post-revision-commands
swissspidy b18631e
Lint fixes
swissspidy 8503d88
Fix tests
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| Feature: Manage WordPress post revisions | ||
|
|
||
| Background: | ||
| Given a WP install | ||
|
|
||
| # Creating a published post doesn't create an initial revision, | ||
| # so we update it twice here and restore the middle version. | ||
| # See https://github.com/wp-cli/entity-command/issues/564. | ||
| Scenario: Restore a post revision | ||
| When I run `wp post create --post_title='Original Post' --post_content='Original content' --porcelain` | ||
| Then STDOUT should be a number | ||
| And save STDOUT as {POST_ID} | ||
|
|
||
| When I run `wp post update {POST_ID} --post_content='Updated content'` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Updated post {POST_ID}. | ||
| """ | ||
|
|
||
| When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=ids` | ||
| Then STDOUT should not be empty | ||
| And save STDOUT as {REVISION_ID} | ||
|
|
||
| When I run `wp post update {POST_ID} --post_content='Another one'` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Updated post {POST_ID}. | ||
| """ | ||
|
|
||
| When I run `wp post get {POST_ID} --field=post_content` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Another one | ||
| """ | ||
|
|
||
| When I run `wp post revision restore {REVISION_ID}` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Restored revision | ||
| """ | ||
|
|
||
| When I run `wp post get {POST_ID} --field=post_content` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Updated content | ||
| """ | ||
|
|
||
| Scenario: Restore invalid revision should fail | ||
| When I try `wp post revision restore 99999` | ||
| Then STDERR should contain: | ||
| """ | ||
| Error: Invalid revision ID | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Show diff between two revisions | ||
| When I run `wp post create --post_title='Test Post' --post_content='First version' --porcelain` | ||
| Then STDOUT should be a number | ||
| And save STDOUT as {POST_ID} | ||
|
|
||
| When I run `wp post update {POST_ID} --post_content='Second version'` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Updated post {POST_ID}. | ||
| """ | ||
|
|
||
| When I run `wp post update {POST_ID} --post_title='New Title' --post_content='Third version'` | ||
| 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: | ||
| """ | ||
| - Second version | ||
| + Third version | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| --- Test Post | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| +++ New Title | ||
| """ | ||
|
|
||
| Scenario: Show diff between revision and current post | ||
| When I run `wp post create --post_title='Diff Test' --post_content='Original text' --porcelain` | ||
| Then STDOUT should be a number | ||
| And save STDOUT as {POST_ID} | ||
|
|
||
| When I run `wp post update {POST_ID} --post_content='Modified text'` | ||
| 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_ID} | ||
|
|
||
| When I run `wp post revision diff {REVISION_ID}` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: No difference found. | ||
| """ | ||
|
|
||
| Scenario: Diff with invalid revision should fail | ||
| When I try `wp post revision diff 99999` | ||
| Then STDERR should contain: | ||
| """ | ||
| Error: Invalid 'from' ID | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Diff between two invalid revisions should fail | ||
| When I try `wp post revision diff 99998 99999` | ||
| Then STDERR should contain: | ||
| """ | ||
| Error: Invalid 'from' ID | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Diff with specific field | ||
| When I run `wp post create --post_title='Field Test' --post_content='Some content' --porcelain` | ||
| Then STDOUT should be a number | ||
| And save STDOUT as {POST_ID} | ||
|
|
||
| When I run `wp post update {POST_ID} --post_title='Modified Field Test'` | ||
| 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_ID} | ||
|
|
||
| When I run `wp post revision diff {REVISION_ID} --field=post_title` | ||
| Then the return code should be 0 | ||
|
|
||
| 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 | ||
| And save STDOUT as {POST_ID} | ||
|
|
||
| When I run `wp post update {POST_ID} --post_content='Version 2'` | ||
| And I run `wp post update {POST_ID} --post_content='Version 3'` | ||
| And I run `wp post update {POST_ID} --post_content='Version 4'` | ||
| And I run `wp post update {POST_ID} --post_content='Version 5'` | ||
|
|
||
| # The initial post does not create a revision. | ||
| # See https://core.trac.wordpress.org/ticket/30854 | ||
| And I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count` | ||
| Then STDOUT should be: | ||
| """ | ||
| 4 | ||
| """ | ||
|
|
||
| When I run `wp post revision prune {POST_ID} --latest=2 --yes` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Deleted 2 revisions for post {POST_ID}. | ||
| """ | ||
|
|
||
| When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count` | ||
| Then STDOUT should be: | ||
| """ | ||
| 2 | ||
| """ | ||
|
|
||
| Scenario: Prune revisions keeping earliest N | ||
| When I run `wp post create --post_title='Prune Earliest Test' --post_content='Version 1' --porcelain` | ||
| Then STDOUT should be a number | ||
| And save STDOUT as {POST_ID} | ||
|
|
||
| When I run `wp post update {POST_ID} --post_content='Version 2'` | ||
| And I run `wp post update {POST_ID} --post_content='Version 3'` | ||
| And I run `wp post update {POST_ID} --post_content='Version 4'` | ||
|
|
||
| # The initial post does not create a revision. | ||
| # See https://core.trac.wordpress.org/ticket/30854 | ||
| And I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count` | ||
| Then STDOUT should be: | ||
| """ | ||
| 3 | ||
| """ | ||
|
|
||
| When I run `wp post revision prune {POST_ID} --earliest=2 --yes` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Deleted 1 revision for post {POST_ID}. | ||
| """ | ||
|
|
||
| When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count` | ||
| Then STDOUT should be: | ||
| """ | ||
| 2 | ||
| """ | ||
|
|
||
| Scenario: Prune revisions for all posts | ||
| When I run `wp post create --post_title='Post 1' --post_content='Content 1' --porcelain` | ||
| Then save STDOUT as {POST_ID_1} | ||
|
|
||
| When I run `wp post update {POST_ID_1} --post_content='Update 1'` | ||
| And I run `wp post update {POST_ID_1} --post_content='Update 2'` | ||
| And I run `wp post update {POST_ID_1} --post_content='Update 3'` | ||
|
|
||
| And I run `wp post create --post_title='Post 2' --post_content='Content 2' --porcelain` | ||
| Then save STDOUT as {POST_ID_2} | ||
|
|
||
| When I run `wp post update {POST_ID_2} --post_content='Update 1'` | ||
| And I run `wp post update {POST_ID_2} --post_content='Update 2'` | ||
|
|
||
| And I run `wp post revision prune --latest=1 --yes` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Deleted | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| revisions across | ||
| """ | ||
|
|
||
| Scenario: Prune with no flags should fail | ||
| When I run `wp post create --post_title='Test' --post_content='Content' --porcelain` | ||
| Then save STDOUT as {POST_ID} | ||
|
|
||
| When I try `wp post revision prune {POST_ID}` | ||
| Then STDERR should contain: | ||
| """ | ||
| Error: Please specify either --latest or --earliest flag. | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Prune with both flags should fail | ||
| When I run `wp post create --post_title='Test' --post_content='Content' --porcelain` | ||
| Then save STDOUT as {POST_ID} | ||
|
|
||
| When I try `wp post revision prune {POST_ID} --latest=5 --earliest=5` | ||
| Then STDERR should contain: | ||
| """ | ||
| Error: Cannot specify both --latest and --earliest flags. | ||
| """ | ||
| And the return code should be 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test scenario is incomplete. It creates revisions and verifies they exist, but it never actually runs the
wp post revision diffcommand to test the diff functionality between two revisions. Add test steps to execute the diff command with two revision IDs and verify the output.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot apply changes based on this feedback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in c1c8cf9. The test scenario now:
wp post revision diffwith two revision IDs