-
Notifications
You must be signed in to change notification settings - Fork 4k
/
class-gutenberg-rest-template-controller-test.php
360 lines (328 loc) · 10.5 KB
/
class-gutenberg-rest-template-controller-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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
class Gutenberg_REST_Templates_Controller_Test extends WP_Test_REST_Controller_Testcase {
/**
* @var int
*/
protected static $admin_id;
/**
* Create fake data before our tests run.
*
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
public static function wpSetupBeforeClass( $factory ) {
switch_theme( 'tt1-blocks' );
gutenberg_register_template_post_type();
gutenberg_register_template_part_post_type();
gutenberg_register_wp_theme_taxonomy();
gutenberg_register_wp_template_part_area_taxonomy();
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
}
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/templates', $routes );
$this->assertArrayHasKey( '/wp/v2/templates/(?P<id>[\/\w-]+)', $routes );
$this->assertArrayHasKey( '/wp/v2/template-parts', $routes );
$this->assertArrayHasKey( '/wp/v2/template-parts/(?P<id>[\/\w-]+)', $routes );
}
public function test_context_param() {
// TODO: Implement test_context_param() method.
$this->markTestIncomplete();
}
public function test_get_items() {
function find_and_normalize_template_by_id( $templates, $id ) {
foreach ( $templates as $template ) {
if ( $template['id'] === $id ) {
unset( $template['content'] );
unset( $template['_links'] );
return $template;
}
}
return null;
}
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(
array(
'id' => 'tt1-blocks//index',
'theme' => 'tt1-blocks',
'slug' => 'index',
'title' => array(
'raw' => 'Index',
'rendered' => 'Index',
),
'description' => 'The default template used when no other template is available. This is a required template in WordPress.',
'status' => 'publish',
'source' => 'theme',
'type' => 'wp_template',
'wp_id' => null,
'has_theme_file' => true,
'is_custom' => false,
),
find_and_normalize_template_by_id( $data, 'tt1-blocks//index' )
);
// Test template parts.
$request = new WP_REST_Request( 'GET', '/wp/v2/template-parts' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(
array(
'id' => 'tt1-blocks//header',
'theme' => 'tt1-blocks',
'slug' => 'header',
'title' => array(
'raw' => 'header',
'rendered' => 'header',
),
'description' => '',
'status' => 'publish',
'source' => 'theme',
'type' => 'wp_template_part',
'wp_id' => null,
'area' => WP_TEMPLATE_PART_AREA_HEADER,
'has_theme_file' => true,
),
find_and_normalize_template_by_id( $data, 'tt1-blocks//header' )
);
}
public function test_get_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/tt1-blocks//index' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['content'] );
unset( $data['_links'] );
$this->assertEquals(
array(
'id' => 'tt1-blocks//index',
'theme' => 'tt1-blocks',
'slug' => 'index',
'title' => array(
'raw' => 'Index',
'rendered' => 'Index',
),
'description' => 'The default template used when no other template is available. This is a required template in WordPress.',
'status' => 'publish',
'source' => 'theme',
'type' => 'wp_template',
'wp_id' => null,
'has_theme_file' => true,
'is_custom' => false,
),
$data
);
// Test template parts.
$request = new WP_REST_Request( 'GET', '/wp/v2/template-parts/tt1-blocks//header' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['content'] );
unset( $data['_links'] );
$this->assertEquals(
array(
'id' => 'tt1-blocks//header',
'theme' => 'tt1-blocks',
'slug' => 'header',
'title' => array(
'raw' => 'header',
'rendered' => 'header',
),
'description' => '',
'status' => 'publish',
'source' => 'theme',
'type' => 'wp_template_part',
'wp_id' => null,
'area' => WP_TEMPLATE_PART_AREA_HEADER,
'has_theme_file' => true,
),
$data
);
}
/**
* Ticket 54507
*
* @dataProvider get_template_endpoint_urls
*/
public function test_get_item_works_with_a_single_slash( $endpoint_url ) {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', $endpoint_url );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['content'] );
unset( $data['_links'] );
$this->assertEquals(
array(
'id' => 'tt1-blocks//index',
'theme' => 'tt1-blocks',
'slug' => 'index',
'title' => array(
'raw' => 'Index',
'rendered' => 'Index',
),
'description' => 'The default template used when no other template is available. This is a required template in WordPress.',
'status' => 'publish',
'source' => 'theme',
'type' => 'wp_template',
'wp_id' => null,
'has_theme_file' => true,
'is_custom' => false,
),
$data
);
}
/**
*
*/
public function get_template_endpoint_urls() {
return array(
array( '/wp/v2/templates/tt1-blocks/index' ),
array( '/wp/v2/templates/tt1-blocks//index' ),
);
}
/**
* Ticket 54507
*
* @dataProvider get_template_ids_to_sanitize
*/
public function test_sanitize_template_id( $input_id, $sanitized_id ) {
$endpoint = new Gutenberg_REST_Templates_Controller( 'wp_template' );
$this->assertEquals(
$sanitized_id,
$endpoint->_sanitize_template_id( $input_id )
);
}
/**
*
*/
public function get_template_ids_to_sanitize() {
return array(
array( 'tt1-blocks/index', 'tt1-blocks//index' ),
array( 'tt1-blocks//index', 'tt1-blocks//index' ),
array( 'theme-experiments/tt1-blocks/index', 'theme-experiments/tt1-blocks//index' ),
array( 'theme-experiments/tt1-blocks//index', 'theme-experiments/tt1-blocks//index' ),
);
}
public function test_create_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
$request->set_body_params(
array(
'slug' => 'my_custom_template',
'title' => 'My Template',
'description' => 'Just a description',
'content' => 'Content',
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['_links'] );
unset( $data['wp_id'] );
$this->assertEquals(
array(
'id' => 'tt1-blocks//my_custom_template',
'theme' => 'tt1-blocks',
'slug' => 'my_custom_template',
'title' => array(
'raw' => 'My Template',
'rendered' => 'My Template',
),
'description' => 'Just a description',
'status' => 'publish',
'source' => 'custom',
'type' => 'wp_template',
'content' => array(
'raw' => 'Content',
),
'has_theme_file' => false,
'is_custom' => true,
),
$data
);
// Test template parts.
$request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' );
$request->set_body_params(
array(
'slug' => 'my_custom_template_part',
'title' => 'My Template Part',
'description' => 'Just a description of a template part',
'content' => 'Content',
'area' => 'header',
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['_links'] );
unset( $data['wp_id'] );
$this->assertEquals(
array(
'id' => 'tt1-blocks//my_custom_template_part',
'theme' => 'tt1-blocks',
'slug' => 'my_custom_template_part',
'title' => array(
'raw' => 'My Template Part',
'rendered' => 'My Template Part',
),
'description' => 'Just a description of a template part',
'status' => 'publish',
'source' => 'custom',
'type' => 'wp_template_part',
'content' => array(
'raw' => 'Content',
),
'area' => 'header',
'has_theme_file' => false,
),
$data
);
}
public function test_update_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'PUT', '/wp/v2/templates/tt1-blocks//index' );
$request->set_body_params(
array(
'title' => 'My new Index Title',
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'My new Index Title', $data['title']['raw'] );
$this->assertEquals( 'custom', $data['source'] );
// Test template parts.
$request = new WP_REST_Request( 'PUT', '/wp/v2/template-parts/tt1-blocks//header' );
$request->set_body_params(
array(
'area' => 'something unsupported',
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( WP_TEMPLATE_PART_AREA_UNCATEGORIZED, $data['area'] );
$this->assertEquals( 'custom', $data['source'] );
}
public function test_delete_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/justrandom//template' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_template_not_found', $response, 404 );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/tt1-blocks//single' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_template', $response, 400 );
}
public function test_prepare_item() {
// TODO: Implement test_prepare_item() method.
$this->markTestIncomplete();
}
public function test_get_item_schema() {
// TODO: Implement test_get_item_schema() method.
$this->markTestIncomplete();
}
}