Skip to content

Commit 95f1414

Browse files
committed
update post 'create/update'
fix code according to review add more test cases mentioned in review
1 parent 0b52cb5 commit 95f1414

File tree

2 files changed

+100
-9
lines changed

2 files changed

+100
-9
lines changed

features/post.feature

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Feature: Manage WordPress posts
2929
When I try the previous command again
3030
Then the return code should be 1
3131

32+
Scenario: Setting post categories
3233
When I run `wp term create category "First Category" --porcelain`
3334
And save STDOUT as {TERM_ID}
3435
And I run `wp term create category "Second Category" --porcelain`
@@ -38,19 +39,109 @@ Feature: Manage WordPress posts
3839
Then STDOUT should be a number
3940
And save STDOUT as {POST_ID}
4041

42+
When I run `wp post term list {POST_ID} category --field=term_id`
43+
Then STDOUT should be:
44+
"""
45+
{TERM_ID}
46+
"""
47+
4148
When I run `wp post update {POST_ID} --post_category={SECOND_TERM_ID}`
4249
Then STDOUT should be:
4350
"""
4451
Success: Updated post {POST_ID}.
4552
"""
4653

54+
When I run `wp post term list {POST_ID} category --field=term_id`
55+
Then STDOUT should be:
56+
"""
57+
{SECOND_TERM_ID}
58+
"""
59+
60+
When I run `wp post update {POST_ID} --post_category='Uncategorized,{TERM_ID},Second Category'`
61+
Then STDOUT should be:
62+
"""
63+
Success: Updated post {POST_ID}.
64+
"""
65+
66+
When I run `wp post term list {POST_ID} category --field=term_id`
67+
And save STDOUT as {MULTI_CATEGORIES_STDOUT}
68+
Then STDOUT should contain:
69+
"""
70+
{TERM_ID}
71+
"""
72+
And STDOUT should contain:
73+
"""
74+
{SECOND_TERM_ID}
75+
"""
76+
And STDOUT should contain:
77+
"""
78+
1
79+
"""
80+
81+
# Blank categories with non-blank ignored.
82+
When I run `wp post update {POST_ID} --post_category='Uncategorized, ,{TERM_ID},Second Category,'`
83+
Then STDOUT should be:
84+
"""
85+
Success: Updated post {POST_ID}.
86+
"""
87+
88+
When I run `wp post term list {POST_ID} category --field=term_id`
89+
Then STDOUT should be:
90+
"""
91+
{MULTI_CATEGORIES_STDOUT}
92+
"""
93+
94+
# Zero category same as default Uncategorized (1) category.
95+
When I try `wp post update {POST_ID} --post_category=0`
96+
Then STDOUT should be:
97+
"""
98+
Success: Updated post {POST_ID}.
99+
"""
100+
101+
When I run `wp post term list {POST_ID} category --field=term_id`
102+
Then STDOUT should be:
103+
"""
104+
1
105+
"""
106+
107+
# Blank category/categories same as default Uncategorized (1) category.
108+
When I try `wp post update {POST_ID} --post_category=,`
109+
Then STDOUT should be:
110+
"""
111+
Success: Updated post {POST_ID}.
112+
"""
113+
114+
When I run `wp post term list {POST_ID} category --field=term_id`
115+
Then STDOUT should be:
116+
"""
117+
1
118+
"""
119+
120+
# Null category same as no categories.
121+
When I try `wp post update {POST_ID} --post_category=' '`
122+
Then STDOUT should be:
123+
"""
124+
Success: Updated post {POST_ID}.
125+
"""
126+
127+
When I run `wp post term list {POST_ID} category --field=term_id`
128+
Then STDOUT should be empty
129+
130+
# Non-existent category.
47131
When I try `wp post update {POST_ID} --post_category=test`
48132
Then STDERR should be:
49133
"""
50134
Error: No such post category 'test'.
51135
"""
52136

53-
When I try `wp post create --post_title="Test category two" --post_category={SECOND_TERM_ID},Test --porcelain`
137+
When I try `wp post create --post_title="Non-existent Category" --post_category={SECOND_TERM_ID},Test --porcelain`
138+
Then STDERR should be:
139+
"""
140+
Error: No such post category 'Test'.
141+
"""
142+
143+
# Error on first non-existent category found.
144+
When I try `wp post create --post_title="More than one non-existent Category" --post_category={SECOND_TERM_ID},Test,Bad --porcelain`
54145
Then STDERR should be:
55146
"""
56147
Error: No such post category 'Test'.

src/Post_Command.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -750,29 +750,29 @@ private function read_from_file_or_stdin( $arg ) {
750750
}
751751

752752
/**
753-
* Get category id if given name/slug
753+
* Resolves post_category arg into an array of category ids.
754754
*
755755
* @param string $arg Supplied argument.
756756
* @return array
757757
*/
758758
private function get_category_ids( $arg ) {
759-
$categoires = explode( ',', $arg );
760759

761-
foreach ( $categoires as $post_category ) {
762-
if ( trim( $post_category ) !== '' ) {
760+
$categories = explode( ',', $arg );
761+
$category_ids = array();
762+
foreach ( $categories as $post_category ) {
763+
if ( trim( $post_category ) ) {
763764
if ( is_numeric( $post_category ) && (int) $post_category ) {
764765
$category_id = category_exists( (int) $post_category );
765766
} else {
766767
$category_id = category_exists( $post_category );
767768
}
768769
if ( ! $category_id ) {
769-
$category_ids[] = $post_category;
770770
WP_CLI::error( "No such post category '$post_category'." );
771-
} else {
772-
$category_ids[] = $category_id;
773771
}
772+
$category_ids[] = $category_id;
774773
}
775774
}
776-
return $category_ids;
775+
// If no category ids found, return exploded array for compat with previous WP-CLI versions.
776+
return $category_ids ? $category_ids : $categories;
777777
}
778778
}

0 commit comments

Comments
 (0)