| | 945 | public function test_get_item__embed() { |
| | 946 | |
| | 947 | // Prepare a new post. This will be first item when querying /wp/v2/posts |
| | 948 | wp_set_current_user( self::$editor_id ); |
| | 949 | $post_id = $this->factory->post->create(); |
| | 950 | $category1 = wp_insert_term( 'Embed Test Category', 'category' ); |
| | 951 | $category2 = wp_insert_term( 'Second Embed Test Category', 'category' ); |
| | 952 | wp_set_post_categories( $post_id, array( $category1['term_id'], $category2['term_id'] ) ); |
| | 953 | $this->factory->comment->create_post_comments( $post_id, 1 ); |
| | 954 | |
| | 955 | // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests. |
| | 956 | // ?_embed=1 should return all expected embeds. In this case 'author', 'replies' and 'wp:term'. |
| | 957 | $_GET = array( '_embed' => '1' ); |
| | 958 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| | 959 | $response = $this->server->dispatch( $request ); |
| | 960 | $data = $response->get_data(); |
| | 961 | $data = array_shift( $data ); |
| | 962 | $embeds = $this->server->embed_links( $data ); |
| | 963 | |
| | 964 | $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); |
| | 965 | $this->assertArrayHasKey( 'replies', $embeds['_embedded'] ); |
| | 966 | $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] ); |
| | 967 | |
| | 968 | // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests. |
| | 969 | // ?_embed=true should return all expected embeds. In this case 'author', 'replies' and 'wp:term'. |
| | 970 | $_GET = array( '_embed' => 'true' ); |
| | 971 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| | 972 | $response = $this->server->dispatch( $request ); |
| | 973 | $data = $response->get_data(); |
| | 974 | $data = array_shift( $data ); |
| | 975 | $embeds = $this->server->embed_links( $data ); |
| | 976 | |
| | 977 | $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); |
| | 978 | $this->assertArrayHasKey( 'replies', $embeds['_embedded'] ); |
| | 979 | $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] ); |
| | 980 | |
| | 981 | // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests. |
| | 982 | // ?_embed=author,wp:term should return only relevant embeds. |
| | 983 | $_GET = array( '_embed' => 'author,wp:term' ); |
| | 984 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| | 985 | $response = $this->server->dispatch( $request ); |
| | 986 | $data = $response->get_data(); |
| | 987 | $data = array_shift( $data ); |
| | 988 | $embeds = $this->server->embed_links( $data ); |
| | 989 | |
| | 990 | $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); |
| | 991 | $this->assertArrayNotHasKey( 'replies', $embeds['_embedded'] ); |
| | 992 | $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] ); |
| | 993 | |
| | 994 | // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests. |
| | 995 | // ?_embed[]=author&_embed[]=replies should return only relevant embeds. |
| | 996 | $_GET = array( |
| | 997 | '_embed' => array( |
| | 998 | 'author', |
| | 999 | 'replies', |
| | 1000 | ) |
| | 1001 | ); |
| | 1002 | |
| | 1003 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| | 1004 | $response = $this->server->dispatch( $request ); |
| | 1005 | $data = $response->get_data(); |
| | 1006 | $data = array_shift( $data ); |
| | 1007 | $embeds = $this->server->embed_links( $data ); |
| | 1008 | |
| | 1009 | $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); |
| | 1010 | $this->assertArrayHasKey( 'replies', $embeds['_embedded'] ); |
| | 1011 | $this->assertArrayNotHasKey( 'wp:term', $embeds['_embedded'] ); |
| | 1012 | } |
| | 1013 | |