-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
class-gutenberg-rest-server-test.php
88 lines (72 loc) · 2.56 KB
/
class-gutenberg-rest-server-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
class Gutenberg_REST_Server_Test extends WP_Test_REST_TestCase {
protected static $admin_id;
protected static $post_id;
public static function wpSetupBeforeClass( $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
self::$post_id = $factory->post->create();
}
public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
wp_delete_post( self::$post_id );
}
public function set_up() {
parent::set_up();
add_filter(
'wp_rest_server_class',
function () {
return 'Gutenberg_REST_Server';
}
);
}
public function test_populates_target_hints_for_administrator() {
wp_set_current_user( self::$admin_id );
$response = rest_do_request( '/wp/v2/posts' );
$post = $response->get_data()[0];
$link = $post['_links']['self'][0];
$this->assertArrayHasKey( 'targetHints', $link );
$this->assertArrayHasKey( 'allow', $link['targetHints'] );
$this->assertSame( array( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ), $link['targetHints']['allow'] );
}
public function test_populates_target_hints_for_logged_out_user() {
$response = rest_do_request( '/wp/v2/posts' );
$post = $response->get_data()[0];
$link = $post['_links']['self'][0];
$this->assertArrayHasKey( 'targetHints', $link );
$this->assertArrayHasKey( 'allow', $link['targetHints'] );
$this->assertSame( array( 'GET' ), $link['targetHints']['allow'] );
}
public function test_does_not_error_on_invalid_urls() {
$response = new WP_REST_Response();
$response->add_link( 'self', 'this is not a real URL' );
$links = rest_get_server()::get_response_links( $response );
$this->assertArrayNotHasKey( 'targetHints', $links['self'][0] );
}
public function test_does_not_error_on_bad_rest_api_routes() {
$response = new WP_REST_Response();
$response->add_link( 'self', rest_url( '/this/is/not/a/real/route' ) );
$links = rest_get_server()::get_response_links( $response );
$this->assertArrayNotHasKey( 'targetHints', $links['self'][0] );
}
public function test_prefers_developer_defined_target_hints() {
$response = new WP_REST_Response();
$response->add_link(
'self',
'/wp/v2/posts/' . self::$post_id,
array(
'targetHints' => array(
'allow' => array( 'GET', 'PUT' ),
),
)
);
$links = rest_get_server()::get_response_links( $response );
$link = $links['self'][0];
$this->assertArrayHasKey( 'targetHints', $link );
$this->assertArrayHasKey( 'allow', $link['targetHints'] );
$this->assertSame( array( 'GET', 'PUT' ), $link['targetHints']['allow'] );
}
}