Skip to content

Commit 7400871

Browse files
authored
Merge pull request #563 from wp-cli/copilot/add-post-revision-commands
Add wp post revision restore, diff, and prune commands
2 parents 93cf0dd + 5efdc41 commit 7400871

File tree

5 files changed

+698
-1
lines changed

5 files changed

+698
-1
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
114114
"post meta patch",
115115
"post meta pluck",
116116
"post meta update",
117+
"post revision",
118+
"post revision diff",
119+
"post revision prune",
120+
"post revision restore",
117121
"post term",
118122
"post term add",
119123
"post term list",

entity-command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
)
4747
);
4848
WP_CLI::add_command( 'post meta', 'Post_Meta_Command' );
49+
WP_CLI::add_command( 'post revision', 'Post_Revision_Command' );
4950
WP_CLI::add_command( 'post term', 'Post_Term_Command' );
5051
WP_CLI::add_command( 'post-type', 'Post_Type_Command' );
5152
WP_CLI::add_command( 'site', 'Site_Command' );

features/post-revision.feature

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
Feature: Manage WordPress post revisions
2+
3+
Background:
4+
Given a WP install
5+
6+
# Creating a published post doesn't create an initial revision,
7+
# so we update it twice here and restore the middle version.
8+
# See https://github.com/wp-cli/entity-command/issues/564.
9+
Scenario: Restore a post revision
10+
When I run `wp post create --post_title='Original Post' --post_content='Original content' --porcelain`
11+
Then STDOUT should be a number
12+
And save STDOUT as {POST_ID}
13+
14+
When I run `wp post update {POST_ID} --post_content='Updated content'`
15+
Then STDOUT should contain:
16+
"""
17+
Success: Updated post {POST_ID}.
18+
"""
19+
20+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=ids`
21+
Then STDOUT should not be empty
22+
And save STDOUT as {REVISION_ID}
23+
24+
When I run `wp post update {POST_ID} --post_content='Another one'`
25+
Then STDOUT should contain:
26+
"""
27+
Success: Updated post {POST_ID}.
28+
"""
29+
30+
When I run `wp post get {POST_ID} --field=post_content`
31+
Then STDOUT should contain:
32+
"""
33+
Another one
34+
"""
35+
36+
When I run `wp post revision restore {REVISION_ID}`
37+
Then STDOUT should contain:
38+
"""
39+
Success: Restored revision
40+
"""
41+
42+
When I run `wp post get {POST_ID} --field=post_content`
43+
Then STDOUT should contain:
44+
"""
45+
Updated content
46+
"""
47+
48+
Scenario: Restore invalid revision should fail
49+
When I try `wp post revision restore 99999`
50+
Then STDERR should contain:
51+
"""
52+
Error: Invalid revision ID
53+
"""
54+
And the return code should be 1
55+
56+
Scenario: Show diff between two revisions
57+
When I run `wp post create --post_title='Test Post' --post_content='First version' --porcelain`
58+
Then STDOUT should be a number
59+
And save STDOUT as {POST_ID}
60+
61+
When I run `wp post update {POST_ID} --post_content='Second version'`
62+
Then STDOUT should contain:
63+
"""
64+
Success: Updated post {POST_ID}.
65+
"""
66+
67+
When I run `wp post update {POST_ID} --post_title='New Title' --post_content='Third version'`
68+
Then STDOUT should contain:
69+
"""
70+
Success: Updated post {POST_ID}.
71+
"""
72+
73+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --fields=ID --format=ids --orderby=ID --order=ASC`
74+
Then STDOUT should not be empty
75+
And save STDOUT as {REVISION_IDS}
76+
77+
When I run `echo "{REVISION_IDS}" | awk '{print $1}'`
78+
Then save STDOUT as {REVISION_ID_1}
79+
80+
When I run `echo "{REVISION_IDS}" | awk '{print $2}'`
81+
Then save STDOUT as {REVISION_ID_2}
82+
83+
When I run `wp post revision diff {REVISION_ID_1} {REVISION_ID_2}`
84+
Then STDOUT should contain:
85+
"""
86+
- Second version
87+
+ Third version
88+
"""
89+
And STDOUT should contain:
90+
"""
91+
--- Test Post
92+
"""
93+
And STDOUT should contain:
94+
"""
95+
+++ New Title
96+
"""
97+
98+
Scenario: Show diff between revision and current post
99+
When I run `wp post create --post_title='Diff Test' --post_content='Original text' --porcelain`
100+
Then STDOUT should be a number
101+
And save STDOUT as {POST_ID}
102+
103+
When I run `wp post update {POST_ID} --post_content='Modified text'`
104+
Then STDOUT should contain:
105+
"""
106+
Success: Updated post {POST_ID}.
107+
"""
108+
109+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --fields=ID --format=ids --orderby=ID --order=ASC`
110+
Then STDOUT should not be empty
111+
And save STDOUT as {REVISION_ID}
112+
113+
When I run `wp post revision diff {REVISION_ID}`
114+
Then STDOUT should contain:
115+
"""
116+
Success: No difference found.
117+
"""
118+
119+
Scenario: Diff with invalid revision should fail
120+
When I try `wp post revision diff 99999`
121+
Then STDERR should contain:
122+
"""
123+
Error: Invalid 'from' ID
124+
"""
125+
And the return code should be 1
126+
127+
Scenario: Diff between two invalid revisions should fail
128+
When I try `wp post revision diff 99998 99999`
129+
Then STDERR should contain:
130+
"""
131+
Error: Invalid 'from' ID
132+
"""
133+
And the return code should be 1
134+
135+
Scenario: Diff with specific field
136+
When I run `wp post create --post_title='Field Test' --post_content='Some content' --porcelain`
137+
Then STDOUT should be a number
138+
And save STDOUT as {POST_ID}
139+
140+
When I run `wp post update {POST_ID} --post_title='Modified Field Test'`
141+
Then STDOUT should contain:
142+
"""
143+
Success: Updated post {POST_ID}.
144+
"""
145+
146+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --fields=ID --format=ids --orderby=ID --order=ASC`
147+
Then STDOUT should not be empty
148+
And save STDOUT as {REVISION_ID}
149+
150+
When I run `wp post revision diff {REVISION_ID} --field=post_title`
151+
Then the return code should be 0
152+
153+
Scenario: Prune revisions keeping latest N
154+
When I run `wp post create --post_title='Prune Test' --post_content='Version 1' --porcelain`
155+
Then STDOUT should be a number
156+
And save STDOUT as {POST_ID}
157+
158+
When I run `wp post update {POST_ID} --post_content='Version 2'`
159+
And I run `wp post update {POST_ID} --post_content='Version 3'`
160+
And I run `wp post update {POST_ID} --post_content='Version 4'`
161+
And I run `wp post update {POST_ID} --post_content='Version 5'`
162+
163+
# The initial post does not create a revision.
164+
# See https://core.trac.wordpress.org/ticket/30854
165+
And I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
166+
Then STDOUT should be:
167+
"""
168+
4
169+
"""
170+
171+
When I run `wp post revision prune {POST_ID} --latest=2 --yes`
172+
Then STDOUT should contain:
173+
"""
174+
Success: Deleted 2 revisions for post {POST_ID}.
175+
"""
176+
177+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
178+
Then STDOUT should be:
179+
"""
180+
2
181+
"""
182+
183+
Scenario: Prune revisions keeping earliest N
184+
When I run `wp post create --post_title='Prune Earliest Test' --post_content='Version 1' --porcelain`
185+
Then STDOUT should be a number
186+
And save STDOUT as {POST_ID}
187+
188+
When I run `wp post update {POST_ID} --post_content='Version 2'`
189+
And I run `wp post update {POST_ID} --post_content='Version 3'`
190+
And I run `wp post update {POST_ID} --post_content='Version 4'`
191+
192+
# The initial post does not create a revision.
193+
# See https://core.trac.wordpress.org/ticket/30854
194+
And I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
195+
Then STDOUT should be:
196+
"""
197+
3
198+
"""
199+
200+
When I run `wp post revision prune {POST_ID} --earliest=2 --yes`
201+
Then STDOUT should contain:
202+
"""
203+
Success: Deleted 1 revision for post {POST_ID}.
204+
"""
205+
206+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
207+
Then STDOUT should be:
208+
"""
209+
2
210+
"""
211+
212+
Scenario: Prune revisions for all posts
213+
When I run `wp post create --post_title='Post 1' --post_content='Content 1' --porcelain`
214+
Then save STDOUT as {POST_ID_1}
215+
216+
When I run `wp post update {POST_ID_1} --post_content='Update 1'`
217+
And I run `wp post update {POST_ID_1} --post_content='Update 2'`
218+
And I run `wp post update {POST_ID_1} --post_content='Update 3'`
219+
220+
And I run `wp post create --post_title='Post 2' --post_content='Content 2' --porcelain`
221+
Then save STDOUT as {POST_ID_2}
222+
223+
When I run `wp post update {POST_ID_2} --post_content='Update 1'`
224+
And I run `wp post update {POST_ID_2} --post_content='Update 2'`
225+
226+
And I run `wp post revision prune --latest=1 --yes`
227+
Then STDOUT should contain:
228+
"""
229+
Success: Deleted
230+
"""
231+
And STDOUT should contain:
232+
"""
233+
revisions across
234+
"""
235+
236+
Scenario: Prune with no flags should fail
237+
When I run `wp post create --post_title='Test' --post_content='Content' --porcelain`
238+
Then save STDOUT as {POST_ID}
239+
240+
When I try `wp post revision prune {POST_ID}`
241+
Then STDERR should contain:
242+
"""
243+
Error: Please specify either --latest or --earliest flag.
244+
"""
245+
And the return code should be 1
246+
247+
Scenario: Prune with both flags should fail
248+
When I run `wp post create --post_title='Test' --post_content='Content' --porcelain`
249+
Then save STDOUT as {POST_ID}
250+
251+
When I try `wp post revision prune {POST_ID} --latest=5 --earliest=5`
252+
Then STDERR should contain:
253+
"""
254+
Error: Cannot specify both --latest and --earliest flags.
255+
"""
256+
And the return code should be 1

phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<exclude-pattern>*/src/Network_Meta_Command\.php$</exclude-pattern>
6666
<exclude-pattern>*/src/Network_Namespace\.php$</exclude-pattern>
6767
<exclude-pattern>*/src/Option_Command\.php$</exclude-pattern>
68-
<exclude-pattern>*/src/Post(_Block|_Meta|_Term|_Type)?_Command\.php$</exclude-pattern>
68+
<exclude-pattern>*/src/Post(_Block|_Meta|_Revision|_Term|_Type)?_Command\.php$</exclude-pattern>
6969
<exclude-pattern>*/src/Signup_Command\.php$</exclude-pattern>
7070
<exclude-pattern>*/src/Site(_Meta|_Option)?_Command\.php$</exclude-pattern>
7171
<exclude-pattern>*/src/Term(_Meta)?_Command\.php$</exclude-pattern>

0 commit comments

Comments
 (0)