Repository navigation
Expand file tree
/
Copy pathtest-rest-post-types-controller.php
More file actions
173 lines (144 loc) · 6.67 KB
/
Copy pathtest-rest-post-types-controller.php
File metadata and controls
173 lines (144 loc) · 6.67 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcase {
public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( '/wp/v2/types', $routes );
$this->assertArrayHasKey( '/wp/v2/types/(?P<type>[\w-]+)', $routes );
}
public function test_context_param() {
// Collection
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/types' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/types/post' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
}
public function test_get_items() {
$request = new WP_REST_Request( 'GET', '/wp/v2/types' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$post_types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
$this->assertEquals( count( $post_types ), count( $data ) );
$this->assertEquals( $post_types['post']->name, $data['post']['slug'] );
$this->check_post_type_obj( 'view', $post_types['post'], $data['post'], $data['post']['_links'] );
$this->assertEquals( $post_types['page']->name, $data['page']['slug'] );
$this->check_post_type_obj( 'view', $post_types['page'], $data['page'], $data['page']['_links'] );
$this->assertFalse( isset( $data['revision'] ) );
}
public function test_get_items_invalid_permission_for_context() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/types' );
$request->set_param( 'context', 'edit' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
}
public function test_get_item() {
$request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
$response = $this->server->dispatch( $request );
$this->check_post_type_object_response( 'view', $response );
}
public function test_get_item_invalid_type() {
$request = new WP_REST_Request( 'GET', '/wp/v2/types/invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_type_invalid', $response, 404 );
}
public function test_get_item_edit_context() {
$editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
$request->set_param( 'context', 'edit' );
$response = $this->server->dispatch( $request );
$this->check_post_type_object_response( 'edit', $response );
}
public function test_get_item_invalid_permission_for_context() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
$request->set_param( 'context', 'edit' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
}
public function test_create_item() {
/** Post types can't be created **/
}
public function test_update_item() {
/** Post types can't be updated **/
}
public function test_delete_item() {
/** Post types can't be deleted **/
}
public function test_prepare_item() {
$obj = get_post_type_object( 'post' );
$endpoint = new WP_REST_Post_Types_Controller;
$request = new WP_REST_Request;
$request->set_param( 'context', 'edit' );
$response = $endpoint->prepare_item_for_response( $obj, $request );
$this->check_post_type_obj( 'edit', $obj, $response->get_data(), $response->get_links() );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/types' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 6, count( $properties ) );
$this->assertArrayHasKey( 'capabilities', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'hierarchical', $properties );
$this->assertArrayHasKey( 'labels', $properties );
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'slug', $properties );
}
public function test_get_additional_field_registration() {
$schema = array(
'type' => 'integer',
'description' => 'Some integer of mine',
'enum' => array( 1, 2, 3, 4 ),
'context' => array( 'view', 'edit' ),
);
register_rest_field( 'type', 'my_custom_int', array(
'schema' => $schema,
'get_callback' => array( $this, 'additional_field_get_callback' ),
'update_callback' => array( $this, 'additional_field_update_callback' ),
) );
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/types/schema' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
$request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
$response = $this->server->dispatch( $request );
$this->assertArrayHasKey( 'my_custom_int', $response->data );
global $wp_rest_additional_fields;
$wp_rest_additional_fields = array();
}
public function additional_field_get_callback( $object ) {
return 123;
}
protected function check_post_type_obj( $context, $post_type_obj, $data, $links ) {
$this->assertEquals( $post_type_obj->label, $data['name'] );
$this->assertEquals( $post_type_obj->name, $data['slug'] );
$this->assertEquals( $post_type_obj->description, $data['description'] );
$this->assertEquals( $post_type_obj->hierarchical, $data['hierarchical'] );
$links = test_rest_expand_compact_links( $links );
$this->assertEquals( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );
$this->assertArrayHasKey( 'https://api.w.org/items', $links );
if ( 'edit' === $context ) {
$this->assertEquals( $post_type_obj->cap, $data['capabilities'] );
$this->assertEquals( $post_type_obj->labels, $data['labels'] );
} else {
$this->assertFalse( isset( $data['capabilities'] ) );
$this->assertFalse( isset( $data['labels'] ) );
}
}
protected function check_post_type_object_response( $context, $response ) {
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$obj = get_post_type_object( 'post' );
$this->check_post_type_obj( $context, $obj, $data, $response->get_links() );
}
}