Repository navigation
Updates the view config endpoint with changes for WordPress 7.2 #14495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -342,6 +342,7 @@ function _wp_get_entity_view_config_posttype_wp_block( $data ) { | |
| 'styles' => array( | ||
| 'author' => array( | ||
| 'width' => '1%', | ||
| 'align' => 'start', | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was missing from https://core.trac.wordpress.org/ticket/66307 |
||
| ), | ||
| ), | ||
| ), | ||
|
|
||
163 changes: 163 additions & 0 deletions
163
tests/phpunit/tests/rest-api/restCastEmptyObjectsFromSchema.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for the `_rest_cast_empty_objects_from_schema()` function. | ||
| * | ||
| * @group restapi | ||
| * @covers ::_rest_cast_empty_objects_from_schema | ||
| */ | ||
| class Tests_REST_API_RestCastEmptyObjectsFromSchema extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Casts the value and encodes it, since `{}` versus `[]` is only visible | ||
| * once serialized. | ||
| * | ||
| * @param mixed $value The value to normalize. | ||
| * @param array $schema The schema node describing the value. | ||
| * @return string The JSON encoded, normalized value. | ||
| */ | ||
| private function cast_and_encode( $value, $schema ) { | ||
| return wp_json_encode( _rest_cast_empty_objects_from_schema( $value, $schema ) ); | ||
| } | ||
|
|
||
| public function test_empty_object_encodes_as_object() { | ||
| $this->assertSame( '{}', $this->cast_and_encode( array(), array( 'type' => 'object' ) ) ); | ||
| } | ||
|
|
||
| public function test_empty_array_encodes_as_array() { | ||
| $this->assertSame( '[]', $this->cast_and_encode( array(), array( 'type' => 'array' ) ) ); | ||
| } | ||
|
|
||
| public function test_empty_value_with_object_in_type_list_encodes_as_object() { | ||
| $this->assertSame( '{}', $this->cast_and_encode( array(), array( 'type' => array( 'object', 'boolean' ) ) ) ); | ||
| } | ||
|
|
||
| public function test_non_array_values_are_returned_unchanged() { | ||
| $this->assertFalse( _rest_cast_empty_objects_from_schema( false, array( 'type' => array( 'object', 'boolean' ) ) ) ); | ||
| $this->assertSame( 'text', _rest_cast_empty_objects_from_schema( 'text', array( 'type' => 'string' ) ) ); | ||
| } | ||
|
|
||
| public function test_value_without_schema_is_returned_unchanged() { | ||
| $this->assertSame( array(), _rest_cast_empty_objects_from_schema( array(), array() ) ); | ||
| } | ||
|
|
||
| public function test_nested_properties_are_cast() { | ||
| $schema = array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'layout' => array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'styles' => array( 'type' => 'object' ), | ||
| ), | ||
| ), | ||
| 'fields' => array( 'type' => 'array' ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->assertSame( | ||
| '{"layout":{"styles":{}},"fields":[]}', | ||
| $this->cast_and_encode( | ||
| array( | ||
| 'layout' => array( 'styles' => array() ), | ||
| 'fields' => array(), | ||
| ), | ||
| $schema | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public function test_object_emptied_of_known_properties_is_still_cast() { | ||
| $schema = array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'layout' => array( 'type' => 'object' ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->assertSame( '{}', $this->cast_and_encode( array(), $schema ) ); | ||
| } | ||
|
|
||
| public function test_array_items_are_cast() { | ||
| $schema = array( | ||
| 'type' => 'array', | ||
| 'items' => array( 'type' => 'object' ), | ||
| ); | ||
|
|
||
| $this->assertSame( '[{},{}]', $this->cast_and_encode( array( array(), array() ), $schema ) ); | ||
| } | ||
|
|
||
| public function test_additional_properties_are_cast() { | ||
| $schema = array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'known' => array( 'type' => 'array' ), | ||
| ), | ||
| 'additionalProperties' => array( 'type' => 'object' ), | ||
| ); | ||
|
|
||
| $this->assertSame( | ||
| '{"known":[],"title":{}}', | ||
| $this->cast_and_encode( | ||
| array( | ||
| 'known' => array(), | ||
| 'title' => array(), | ||
| ), | ||
| $schema | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public function test_boolean_additional_properties_are_left_alone() { | ||
| $schema = array( | ||
| 'type' => 'object', | ||
| 'additionalProperties' => true, | ||
| ); | ||
|
|
||
| $this->assertSame( '{"extra":[]}', $this->cast_and_encode( array( 'extra' => array() ), $schema ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider data_union_keywords | ||
| * | ||
| * @param string $keyword The union keyword, `oneOf` or `anyOf`. | ||
| */ | ||
| public function test_empty_value_is_cast_when_a_union_branch_allows_an_object( $keyword ) { | ||
| $schema = array( | ||
| $keyword => array( | ||
| array( 'type' => 'string' ), | ||
| array( 'type' => 'object' ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->assertSame( '{}', $this->cast_and_encode( array(), $schema ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider data_union_keywords | ||
| * | ||
| * @param string $keyword The union keyword, `oneOf` or `anyOf`. | ||
| */ | ||
| public function test_empty_value_is_not_cast_when_no_union_branch_allows_an_object( $keyword ) { | ||
| $schema = array( | ||
| $keyword => array( | ||
| array( 'type' => 'string' ), | ||
| array( 'type' => 'array' ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->assertSame( '[]', $this->cast_and_encode( array(), $schema ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Data provider. | ||
| * | ||
| * @return array[] | ||
| */ | ||
| public function data_union_keywords() { | ||
| return array( | ||
| 'oneOf' => array( 'oneOf' ), | ||
| 'anyOf' => array( 'anyOf' ), | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This utility is now used by the view config endpoint and it's going to be used by the fields and actions endpoints as well (landing in the next hours). I thought I'd extract it first.