update post create/update#129
Conversation
add condition to set post category if name/slug is given
update condition to set post category if name/slug is given
update condition to set post category if name/slug is given
|
@thrijith Can you provide the corresponding tests with the above change? |
|
ok @schlessera |
update code and fix tests
features/post.feature
Outdated
| Success: Updated post {POST_ID}. | ||
| """ | ||
|
|
||
| When I run `wp post update {POST_ID} --post_category=test --porcelain` |
There was a problem hiding this comment.
If a command produces a warning or an error, it appears on STDERR, and need to use try instead of run:
When I try `wp post update {POST_ID} --post_category=test`
Then STDERR should be:
"""
Warning: Unable to set test as post category
"""
And STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""
There was a problem hiding this comment.
(This is a real gotcha with the behat tests, and we should throw a reminder to use try when it happens as it's so easy to forget to do it...)
fix tests
|
I'm wondering now whether the following behavior wouldn't be preferable:
@gitlost Thoughts about the above? Goal would be to get around the WP limitation by pre-querying IDs to replace the strings. |
|
Oh, btw, @thrijith , good job so far on the pull request. It does exactly what the initial goal was. It's my fault that I didn't think sooner about trying to prefetch the IDs instead... |
|
@schlessera yes, agree that would be the preferable behavior. |
|
From @thrijith slack:
Yes you're right, |
|
Thinking about it this is probably better (as keep original passed-in etc. |
update code according to review and modify tests
| When I try the previous command again | ||
| Then the return code should be 1 | ||
|
|
||
| When I run `wp term create category "First Category" --porcelain` |
There was a problem hiding this comment.
I think it'd be better to move the category stuff to its own scenario. Here's an iteration that also checks some BC behaviour (for details, see get_category_ids() return comment below):
Scenario: Setting post categories
When I run `wp term create category "First Category" --porcelain`
And save STDOUT as {TERM_ID}
And I run `wp term create category "Second Category" --porcelain`
And save STDOUT as {SECOND_TERM_ID}
When I run `wp post create --post_title="Test category" --post_category="First Category" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}
When I run `wp post term list {POST_ID} category --field=term_id`
Then STDOUT should be:
"""
{TERM_ID}
"""
When I run `wp post update {POST_ID} --post_category={SECOND_TERM_ID}`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""
When I run `wp post term list {POST_ID} category --field=term_id`
Then STDOUT should be:
"""
{SECOND_TERM_ID}
"""
When I run `wp post update {POST_ID} --post_category='Uncategorized,{TERM_ID},Second Category'`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""
When I run `wp post term list {POST_ID} category --field=term_id`
And save STDOUT as {MULTI_CATEGORIES_STDOUT}
Then STDOUT should contain:
"""
{TERM_ID}
"""
And STDOUT should contain:
"""
{SECOND_TERM_ID}
"""
And STDOUT should contain:
"""
1
"""
# Blank categories with non-blank ignored.
When I run `wp post update {POST_ID} --post_category='Uncategorized, ,{TERM_ID},Second Category,'`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""
When I run `wp post term list {POST_ID} category --field=term_id`
Then STDOUT should be:
"""
{MULTI_CATEGORIES_STDOUT}
"""
# Zero category same as default Uncategorized (1) category.
When I try `wp post update {POST_ID} --post_category=0`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""
When I run `wp post term list {POST_ID} category --field=term_id`
Then STDOUT should be:
"""
1
"""
# Blank category/categories same as default Uncategorized (1) category.
When I try `wp post update {POST_ID} --post_category=,`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""
When I run `wp post term list {POST_ID} category --field=term_id`
Then STDOUT should be:
"""
1
"""
# Null category same as no categories.
When I try `wp post update {POST_ID} --post_category=' '`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""
When I run `wp post term list {POST_ID} category --field=term_id`
Then STDOUT should be empty
# Non-existent category.
When I try `wp post update {POST_ID} --post_category=test`
Then STDERR should be:
"""
Error: No such post category 'test'.
"""
When I try `wp post create --post_title="Non-existent Category" --post_category={SECOND_TERM_ID},Test --porcelain`
Then STDERR should be:
"""
Error: No such post category 'Test'.
"""
# Error on first non-existent category found.
When I try `wp post create --post_title="More than one non-existent Category" --post_category={SECOND_TERM_ID},Test,Bad --porcelain`
Then STDERR should be:
"""
Error: No such post category 'Test'.
"""
src/Post_Command.php
Outdated
| * @return array | ||
| */ | ||
| private function get_category_ids( $arg ) { | ||
| $categoires = explode( ',', $arg ); |
There was a problem hiding this comment.
Typo $categoires -> $categories
src/Post_Command.php
Outdated
| private function get_category_ids( $arg ) { | ||
| $categoires = explode( ',', $arg ); | ||
|
|
||
| foreach ( $categoires as $post_category ) { |
There was a problem hiding this comment.
Need to init $category_ids = array(); before loop.
src/Post_Command.php
Outdated
| $categoires = explode( ',', $arg ); | ||
|
|
||
| foreach ( $categoires as $post_category ) { | ||
| if ( trim( $post_category ) !== '' ) { |
There was a problem hiding this comment.
Think leave the comparison out, to just have if ( trim( $post_category ) ) {
src/Post_Command.php
Outdated
| $category_id = category_exists( $post_category ); | ||
| } | ||
| if ( ! $category_id ) { | ||
| $category_ids[] = $post_category; |
There was a problem hiding this comment.
This $category_ids[] = $post_category; can be removed as about to error and not return.
src/Post_Command.php
Outdated
| if ( ! $category_id ) { | ||
| $category_ids[] = $post_category; | ||
| WP_CLI::error( "No such post category '$post_category'." ); | ||
| } else { |
There was a problem hiding this comment.
As WP_CLI::error() doesn't return, no need to else here.
src/Post_Command.php
Outdated
| } | ||
| } | ||
| } | ||
| return $category_ids; |
There was a problem hiding this comment.
For BC with previous behaviour, we need to do:
// If no category ids found, return exploded array for compat with previous WP-CLI versions.
return $category_ids ? $category_ids : $categories;
here.
The BC behaviour is weird and non-intuitive, in that --post_category=' ' will set no categories, while --post_category=0 will set the default category (see tests above), but probably we should keep this behaviour to be safe...??
There was a problem hiding this comment.
@gitlost tests look really good.
so if valid categories are passed we will send the resolved ids
if a non-existent one is passed then throw an error ?
There was a problem hiding this comment.
Thanks @thrijith !
so if valid categories are passed we will send the resolved ids
if a non-existent one is passed then throw an error ?
I think so, giving an error is a BC break but makes more sense than just warning...
src/Post_Command.php
Outdated
| } | ||
|
|
||
| /** | ||
| * Get category id if given name/slug |
There was a problem hiding this comment.
Maybe Resolves post_category arg into an array of category ids. or something better?
fix code according to review add more test cases mentioned in review
|
Thanks @thrijith. @schlessera could you give this a quick check before it's merged? |
|
Merged this anyway as @schlessera has got enough on his plate. Thanks @thrijith ! |
|
I quickly looked through it and LGTM! |
to fix wp-cli/wp-cli#4605
update the condition to set post category if name/slug is given in
--post_categorywork in progress