Skip to content

Commit

Permalink
Navigation: Load the raw property on the navigation fallback (#52758)
Browse files Browse the repository at this point in the history
* Navigation: Load the raw property on the navigation fallback

* Update lib/compat/wordpress-6.3/navigation-fallback.php

Co-authored-by: Dave Smith <[email protected]>

* Update lib/compat/wordpress-6.3/navigation-fallback.php

Co-authored-by: Dave Smith <[email protected]>

* Add a test for these properties

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* Update phpunit/class-gutenberg-rest-navigation-fallback-controller-test.php

Co-authored-by: Dave Smith <[email protected]>

* add more comments

* add necessary method

* Fix php coding standards error

---------

Co-authored-by: Dave Smith <[email protected]>
Co-authored-by: Jerry Jones <[email protected]>
  • Loading branch information
3 people committed Jul 21, 2023
1 parent 51774ef commit 87ef1df
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/compat/wordpress-6.3/navigation-fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ function gutenberg_add_fields_to_navigation_fallback_embeded_links( $schema ) {
$schema['properties']['content']['context'] = array_merge( $schema['properties']['content']['context'], array( 'embed' ) );

// Expose sub properties of content field.
// These aren't exposed by the posts controller by default, see:
// https://github.com/WordPress/wordpress-develop/blob/5c3c6258e468c67ba00bbd13db29994f1a57a52a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L2425.
$schema['properties']['content']['properties']['raw']['context'] = array_merge( $schema['properties']['content']['properties']['raw']['context'], array( 'embed' ) );
$schema['properties']['content']['properties']['rendered']['context'] = array_merge( $schema['properties']['content']['properties']['rendered']['context'], array( 'embed' ) );
$schema['properties']['content']['properties']['block_version']['context'] = array_merge( $schema['properties']['content']['properties']['block_version']['context'], array( 'embed' ) );

// Expose sub properties of title field.
// These aren't exposed by the posts controller by default, see:
// https://github.com/WordPress/wordpress-develop/blob/5c3c6258e468c67ba00bbd13db29994f1a57a52a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L2401.
$schema['properties']['title']['properties']['raw']['context'] = array_merge( $schema['properties']['title']['properties']['raw']['context'], array( 'embed' ) );

return $schema;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,46 @@ public function test_prepare_item() {
// Covered by the core test.
}

/**
* Tests that the correct filters are applied to the context parameter.
*
* By default, the REST response for the Posts Controller will not return all fields
* when the context is set to 'embed'. Assert that correct additional fields are added
* to the embedded Navigation Post, when the navigation fallback endpoint
* is called with the `_embed` param.
*
* @covers wp_add_fields_to_navigation_fallback_embeded_links
*/
public function test_embedded_navigation_post_contains_required_fields() {
// First we'll use the navigation fallback to get a link to the navigation endpoint.
$request = new WP_REST_Request( 'GET', '/wp-block-editor/v1/navigation-fallback' );
$response = rest_get_server()->dispatch( $request );
$links = $response->get_links();

// Extract the navigation endpoint URL from the response.
$embedded_navigation_href = $links['self'][0]['href'];
preg_match( '/\?rest_route=(.*)/', $embedded_navigation_href, $matches );
$navigation_endpoint = $matches[1];

// Fetch the "linked" navigation post from the endpoint, with the context parameter set to 'embed' to simulate fetching embedded links.
$request = new WP_REST_Request( 'GET', $navigation_endpoint );
$request->set_param( 'context', 'embed' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

// Verify that the additional status field is present.
$this->assertArrayHasKey( 'status', $data, 'Response title should contain a "status" field.' );

// Verify that the additional content fields are present.
$this->assertArrayHasKey( 'content', $data, 'Response should contain a "content" field.' );
$this->assertArrayHasKey( 'raw', $data['content'], 'Response content should contain a "raw" field.' );
$this->assertArrayHasKey( 'rendered', $data['content'], 'Response content should contain a "rendered" field.' );
$this->assertArrayHasKey( 'block_version', $data['content'], 'Response should contain a "block_version" field.' );

// Verify that the additional title.raw field is present.
$this->assertArrayHasKey( 'raw', $data['title'], 'Response title should contain a "raw" key.' );
}

/**
* @doesNotPerformAssertions
*/
Expand Down

1 comment on commit 87ef1df

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 87ef1df.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5621055317
📝 Reported issues:

Please sign in to comment.