This repository was archived by the owner on Sep 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 647
Expand file tree
/
Copy pathtest-rest-settings-controller.php
More file actions
240 lines (194 loc) · 7.56 KB
/
test-rest-settings-controller.php
File metadata and controls
240 lines (194 loc) · 7.56 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Unit tests covering WP_Test_REST_Settings_Controller functionality.
*
* @package WordPress
* @subpackage JSON API
*/
class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase {
public function setUp() {
global $wp_version;
if ( version_compare( $wp_version, '4.7-alpha', '<' ) ) {
return $this->markTestSkipped( 'WordPress version not supported.' );
}
parent::setUp();
$this->administrator = $this->factory->user->create( array(
'role' => 'administrator',
) );
$this->endpoint = new WP_REST_Settings_Controller();
}
public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( '/wp/v2/settings', $routes );
}
public function test_get_items() {
}
public function test_context_param() {
}
public function test_get_item_is_not_public() {
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 403, $response->get_status() );
}
public function test_get_item() {
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( array(
'title',
'description',
'url',
'email',
'timezone',
'date_format',
'time_format',
'start_of_week',
'language',
'use_smilies',
'default_category',
'default_post_format',
'posts_per_page',
), array_keys( $data ) );
}
public function test_get_item_value_is_cast_to_type() {
wp_set_current_user( $this->administrator );
update_option( 'posts_per_page', 'invalid_number' ); // this is cast to (int) 1
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, $data['posts_per_page'] );
}
public function test_get_item_with_custom_setting() {
wp_set_current_user( $this->administrator );
register_setting( 'somegroup', 'mycustomsetting', array(
'show_in_rest' => array(
'name' => 'mycustomsettinginrest',
'schema' => array(
'enum' => array( 'validvalue1', 'validvalue2' ),
'default' => 'validvalue1',
),
),
'type' => 'string',
) );
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertArrayHasKey( 'mycustomsettinginrest', $data );
$this->assertEquals( 'validvalue1', $data['mycustomsettinginrest'] );
update_option( 'mycustomsetting', 'validvalue2' );
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'validvalue2', $data['mycustomsettinginrest'] );
unregister_setting( 'somegroup', 'mycustomsetting' );
}
public function get_setting_custom_callback( $result, $name, $args ) {
switch ( $name ) {
case 'mycustomsetting1':
return 'filtered1';
}
return $result;
}
public function test_get_item_with_filter() {
wp_set_current_user( $this->administrator );
add_filter( 'rest_pre_get_setting', array( $this, 'get_setting_custom_callback' ), 10, 3 );
register_setting( 'somegroup', 'mycustomsetting1', array(
'show_in_rest' => array(
'name' => 'mycustomsettinginrest1',
),
'type' => 'string',
) );
register_setting( 'somegroup', 'mycustomsetting2', array(
'show_in_rest' => array(
'name' => 'mycustomsettinginrest2',
),
'type' => 'string',
) );
update_option( 'mycustomsetting1', 'unfiltered1' );
update_option( 'mycustomsetting2', 'unfiltered2' );
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertArrayHasKey( 'mycustomsettinginrest1', $data );
$this->assertEquals( 'unfiltered1', $data['mycustomsettinginrest1'] );
$this->assertArrayHasKey( 'mycustomsettinginrest2', $data );
$this->assertEquals( 'unfiltered2', $data['mycustomsettinginrest2'] );
unregister_setting( 'somegroup', 'mycustomsetting' );
remove_all_filters( 'rest_pre_get_setting' );
}
public function test_create_item() {
}
public function test_update_item() {
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$request->set_param( 'title', 'The new title!' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'The new title!', $data['title'] );
$this->assertEquals( get_option( 'blogname' ), $data['title'] );
}
public function update_setting_custom_callback( $result, $name, $value, $args ) {
if ( 'title' === $name && 'The new title!' === $value ) {
// Do not allow changing the title in this case
return true;
}
return false;
}
public function test_update_item_with_filter() {
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$request->set_param( 'title', 'The old title!' );
$request->set_param( 'description', 'The old description!' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'The old title!', $data['title'] );
$this->assertEquals( 'The old description!', $data['description'] );
$this->assertEquals( get_option( 'blogname' ), $data['title'] );
$this->assertEquals( get_option( 'blogdescription' ), $data['description'] );
add_filter( 'rest_pre_update_setting', array( $this, 'update_setting_custom_callback' ), 10, 4 );
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$request->set_param( 'title', 'The new title!' );
$request->set_param( 'description', 'The new description!' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'The old title!', $data['title'] );
$this->assertEquals( 'The new description!', $data['description'] );
$this->assertEquals( get_option( 'blogname' ), $data['title'] );
$this->assertEquals( get_option( 'blogdescription' ), $data['description'] );
remove_all_filters( 'rest_pre_update_setting' );
}
public function test_update_item_with_invalid_type() {
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$request->set_param( 'title', array( 'rendered' => 'This should fail.' ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Setting an item to "null" will essentially restore it to it's default value.
*/
public function test_update_item_with_null() {
update_option( 'posts_per_page', 9 );
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$request->set_param( 'posts_per_page', null );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, $data['posts_per_page'] );
}
public function test_delete_item() {
}
public function test_prepare_item() {
}
public function test_get_item_schema() {
}
}