Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnynews committed Dec 11, 2023
1 parent 291df06 commit 69c55e3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,11 @@ protected function embed_links( $data, $embed = true ) {
continue;
}

$matched = $this->match_request_to_handler( $request );

if ( ! empty( $matched ) && ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) {
$request['per_page'] = (int) $matched[1]['args']['per_page']['maximum'];
if ( empty( $request['per_page'] ) ) {
$matched = $this->match_request_to_handler( $request );
if ( ! empty( $matched ) && ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) {
$request['per_page'] = (int) $matched[1]['args']['per_page']['maximum'];
}
}

// Embedded resources get passed context=embed.
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te

protected static $supported_formats;
protected static $post_ids = array();
protected static $terms = array();
protected static $total_posts = 30;
protected static $per_page = 50;

Expand All @@ -28,6 +29,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
self::$terms = $factory->term->create_many( 15, array( 'taxonomy' => 'category' ) );
wp_set_object_terms( self::$post_id, self::$terms, 'category' );

self::$superadmin_id = $factory->user->create(
array(
Expand Down Expand Up @@ -223,6 +226,21 @@ public function test_registered_get_item_params() {
$this->assertSame( array( 'context', 'id', 'password' ), $keys );
}

public function test_registered_get_items_embed() {
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'include', array( self::$post_id ) );
$response = rest_get_server()->dispatch( $request );
$response = rest_get_server()->response_to_data( $response, true );
$this->assertArrayHasKey( '_embedded', $response[0], 'The _embedded key must exist' );
$this->assertArrayHasKey( 'wp:term', $response[0]['_embedded'], 'The wp:term key must exist' );
$this->assertCount( 15, $response[0]['_embedded']['wp:term'][0], 'Should should be 15 terms and not the default 10' );
$i = 0;
foreach ( $response[0]['_embedded']['wp:term'][0] as $term ) {
$this->assertSame( self::$terms[ $i ], $term['id'], 'Check term id existing in response' );
++$i;
}
}

/**
* @ticket 43701
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,37 @@ public function test_link_embedding_without_links() {
$this->assertSame( 'data', $result['untouched'] );
}

/**
* Ensure embedding is with links in the data.
*/
public function test_link_embedding_with_links() {
$data = array(
'_links' => array(
'wp:term' => array(
array(
'taxonomy' => 'category',
'embeddable' => true,
'href' => get_rest_url( 0, '/wp/v2/categories' ),
),
array(
'taxonomy' => 'post_tag',
'embeddable' => true,
'href' => get_rest_url( 0, '/wp/v2/tags' ),
),
),
),
);

$mock = new MockAction();
add_filter( 'rest_post_dispatch', array( $mock, 'filter' ), 10, 3 );

rest_get_server()->embed_links( $data, true );
$args = $mock->get_args();
foreach ( $args as $arg ) {
$this->assertSame( 100, $arg[2]['per_page'], 'Posts per page should be 100' );
}
}

/**
* Ensure embed_links handles WP_Error objects returned by dispatch
*
Expand Down

0 comments on commit 69c55e3

Please sign in to comment.