Skip to content

update post create/update#129

Merged
gitlost merged 7 commits intowp-cli:masterfrom
thrijith:GH-4605
Jan 23, 2018
Merged

update post create/update#129
gitlost merged 7 commits intowp-cli:masterfrom
thrijith:GH-4605

Conversation

@thrijith
Copy link
Member

to fix wp-cli/wp-cli#4605
update the condition to set post category if name/slug is given in --post_category
work in progress

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 thrijith changed the title WIP: update post create/update update post create/update Jan 14, 2018
@schlessera
Copy link
Member

@thrijith Can you provide the corresponding tests with the above change?

@schlessera schlessera added the command:post-create Related to 'post create' command label Jan 15, 2018
@thrijith
Copy link
Member Author

ok @schlessera

update code and fix tests
@thrijith thrijith changed the title update post create/update WIP: update post create/update Jan 15, 2018
Success: Updated post {POST_ID}.
"""

When I run `wp post update {POST_ID} --post_category=test --porcelain`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}.
      """

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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...)

@schlessera
Copy link
Member

I'm wondering now whether the following behavior wouldn't be preferable:
If a category is provided as a string:

  • if a category term query for that string returns 0 IDs, throw an error that the category is unknown;
  • if a category term query for that string only returns 1 ID, use that ID instead;
  • if a category term query for that string returns more than 1 ID, throw an error that the category is ambiguous and that an ID is needed instead.

@gitlost Thoughts about the above? Goal would be to get around the WP limitation by pre-querying IDs to replace the strings.

@schlessera
Copy link
Member

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...

@gitlost
Copy link
Contributor

gitlost commented Jan 16, 2018

@schlessera yes, agree that would be the preferable behavior.

@gitlost
Copy link
Contributor

gitlost commented Jan 16, 2018

From @thrijith slack:

not sure i got it completely https://github.com/wp-cli/entity-command/pull/129#issuecomment-357878268
if a category term query for that string returns 0 IDs, throw an error that the category is unknown [done ?]
if a category term query for that string only returns 1 ID, use that ID instead [done ?]
if a category term query for that string returns more than 1 ID, throw an error that the category is ambiguous and that an ID is needed instead. [in what conditions will we get more than 1 ID ]

Yes you're right, category_exists() won't return more than 1 ID, only an int (if the category exists) or a falsey value (0 or null), so the code in the loop would be eg:

$post_category = is_numeric( $post_category ) && (int) $post_category ? (int) $post_category : $post_category;
$category_id = category_exists( $post_category );
if ( ! $category_id ) {
	WP_CLI::error( "No such post category '$post_category'." );
}
$category_ids[] = $category_id;

@gitlost
Copy link
Contributor

gitlost commented Jan 17, 2018

Thinking about it this is probably better (as keep original passed-in $post_category to report in error):

if ( is_numeric( $post_category ) && (int) $post_category ) {
	$category_id = category_exists( (int) $post_category );
} else {
	$category_id = category_exists( $post_category );
}

etc.

update code according to review and modify tests
@thrijith thrijith changed the title WIP: update post create/update update post create/update Jan 18, 2018
When I try the previous command again
Then the return code should be 1

When I run `wp term create category "First Category" --porcelain`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'.
      """

* @return array
*/
private function get_category_ids( $arg ) {
$categoires = explode( ',', $arg );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo $categoires -> $categories

private function get_category_ids( $arg ) {
$categoires = explode( ',', $arg );

foreach ( $categoires as $post_category ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to init $category_ids = array(); before loop.

$categoires = explode( ',', $arg );

foreach ( $categoires as $post_category ) {
if ( trim( $post_category ) !== '' ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think leave the comparison out, to just have if ( trim( $post_category ) ) {

$category_id = category_exists( $post_category );
}
if ( ! $category_id ) {
$category_ids[] = $post_category;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This $category_ids[] = $post_category; can be removed as about to error and not return.

if ( ! $category_id ) {
$category_ids[] = $post_category;
WP_CLI::error( "No such post category '$post_category'." );
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As WP_CLI::error() doesn't return, no need to else here.

}
}
}
return $category_ids;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...??

Copy link
Member Author

@thrijith thrijith Jan 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

}

/**
* Get category id if given name/slug
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@gitlost
Copy link
Contributor

gitlost commented Jan 18, 2018

Thanks @thrijith.

@schlessera could you give this a quick check before it's merged?

@gitlost gitlost added this to the 1.2.0 milestone Jan 23, 2018
@gitlost gitlost merged commit 2413aa3 into wp-cli:master Jan 23, 2018
@gitlost
Copy link
Contributor

gitlost commented Jan 23, 2018

Merged this anyway as @schlessera has got enough on his plate. Thanks @thrijith !

@schlessera
Copy link
Member

I quickly looked through it and LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:post-create Related to 'post create' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--post_category="not working"

3 participants