Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Conversation

@adamsilverstein
Copy link

  • Adds some tests to ensure API accepts null date data and empty slug data for draft posts.
  • Adds a test to ensure API accepts back all values it returns when creating a new post as valid.
  • Adds an is_object test to slug validation in prepare_item_for_database, preventing empty slug objects values from being added to the post

@adamsilverstein
Copy link
Author

Fixes #2696

}
// Post slug.
if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) ) {
if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) && ! empty( $request['slug'] ) && ! is_object( $request['slug'] ) ) {
Copy link
Author

Choose a reason for hiding this comment

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

A blank slug sent to the api winds up here as an empty object which was passing the empty test, I didn’t figure out why. Without this change, the test_update_draft_post_with_returned_draft_post_data test below fails.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm @adamsilverstein did you figure out where this empty object is coming from?

Copy link
Author

Choose a reason for hiding this comment

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

Not yet, I'll dig into it @joehoyle. I would expect a blank string here.

Copy link
Author

Choose a reason for hiding this comment

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

@rmccue @joehoyle I tracked this down to our use of the sanitize_title to sanitize the slugs. sanitize_title takes an optional second parameter as a 'fallback'. in class-wp-rest-controller:L807 in sanitize_params where we use call_user_func, we are passing the value, then also passing the request object and the argument key.

In this case sanitize_title sees an empty string as failing and returns the fallback, the request object, and the post update fails.

Do you know why the request and key are added to the sanitization callback? can those be removed? Going to dig into when they were added.

https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api/class-wp-rest-request.php#L807

Copy link
Author

Choose a reason for hiding this comment

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

ps. I removed the is_object test and now my test_update_draft_post_with_empty_slug test fails

@kadamwhite kadamwhite changed the title Ensure post controller accepts as valid values it returns when creating a post Ensure post controller can accept as valid the same values it returns when creating a post Oct 6, 2016
@adamsilverstein
Copy link
Author

@joehoyle I think we need to change our sanitize callback to fix this issue.

In a3c95b5 I added a new rest_sanitize_sanitization callback for the slug that ignores the passed $request and $key, callingsanitize_titleon the value alone, and properly returning a blank string when a blank string is sanitized. I was mixed about whether this should be named as is orrest_sanitize_title`

plugin.php Outdated
}
}

/**
Copy link
Member

Choose a reason for hiding this comment

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

Hrmmm I feel like we already added this somewhere, @rmccue is that true?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed; it's in the base controller as WP_REST_Controller->sanitize_slug

Copy link
Member

Choose a reason for hiding this comment

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

Which, in fact, is already used above, so not sure why this PR is changing it?

Copy link
Author

Choose a reason for hiding this comment

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

@joehoyle @rmccue I think you were one step ahead of me, I just updated develop and see the new function. updating the PR, all I have to add now is the new tests, everything checks out with this fix in place.

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
@adamsilverstein
Copy link
Author

After updating develop, everything checks out for this issue. I'm leaving the test in place for consideration because they validate that everything works as expected.

@rmccue
Copy link
Member

rmccue commented Oct 19, 2016

@adamsilverstein The tests are failing here; does this need develop merged in again to resolve?

@rmccue rmccue added this to the 2.0 milestone Oct 19, 2016
@rmccue rmccue changed the title Ensure post controller can accept as valid the same values it returns when creating a post Add tests to ensure post controller accepts the same values it returns when creating a post Oct 19, 2016
@adamsilverstein
Copy link
Author

@rmccue not sure its my branch? I can refresh patch if needed, I'm seeing this in Travis failure: PHP Fatal error: Cannot use object of type stdClass as array in /home/travis/build/WP-API/WP-API/lib/fields/class-wp-rest-meta-fields.php on line 104

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants