Completed
Branch develop (21297d)
by
unknown
02:41
created

test_additional_field_update_errors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 29
loc 29
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Unit tests covering WP_REST_Terms_Controller functionality, used for Tags.
5
 *
6
 * @package WordPress
7
 * @subpackage JSON API
8
 */
9
class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
10
11 View Code Duplication
	public function setUp() {
12
		parent::setUp();
13
		$this->administrator = $this->factory->user->create( array(
14
			'role' => 'administrator',
15
		) );
16
		$this->subscriber = $this->factory->user->create( array(
17
			'role' => 'subscriber',
18
		) );
19
	}
20
21
	public function test_register_routes() {
22
		$routes = $this->server->get_routes();
23
		$this->assertArrayHasKey( '/wp/v2/tags', $routes );
24
		$this->assertArrayHasKey( '/wp/v2/tags/(?P<id>[\d]+)', $routes );
25
	}
26
27 View Code Duplication
	public function test_context_param() {
28
		// Collection
29
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
30
		$response = $this->server->dispatch( $request );
31
		$data = $response->get_data();
32
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
33
		$this->assertEqualSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
34
		// Single
35
		$tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
36
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags/' . $tag1 );
37
		$response = $this->server->dispatch( $request );
38
		$data = $response->get_data();
39
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
40
		$this->assertEqualSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
41
	}
42
43 View Code Duplication
	public function test_registered_query_params() {
44
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
45
		$response = $this->server->dispatch( $request );
46
		$data = $response->get_data();
47
		$keys = array_keys( $data['endpoints'][0]['args'] );
48
		sort( $keys );
49
		$this->assertEquals( array(
50
			'context',
51
			'exclude',
52
			'hide_empty',
53
			'include',
54
			'offset',
55
			'order',
56
			'orderby',
57
			'page',
58
			'per_page',
59
			'post',
60
			'search',
61
			'slug',
62
			), $keys );
63
	}
64
65
	public function test_get_items() {
66
		$this->factory->tag->create();
67
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
68
		$response = $this->server->dispatch( $request );
69
		$this->check_get_taxonomy_terms_response( $response );
70
	}
71
72 View Code Duplication
	public function test_get_items_invalid_permission_for_context() {
73
		wp_set_current_user( 0 );
74
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
75
		$request->set_param( 'context', 'edit' );
76
		$response = $this->server->dispatch( $request );
77
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
78
	}
79
80 View Code Duplication
	public function test_get_items_hide_empty_arg() {
81
		$post_id = $this->factory->post->create();
82
		$tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
83
		$tag2 = $this->factory->tag->create( array( 'name' => 'The Be Sharps' ) );
84
		wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' );
85
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
86
		$request->set_param( 'hide_empty', true );
87
		$response = $this->server->dispatch( $request );
88
		$data = $response->get_data();
89
		$this->assertEquals( 2, count( $data ) );
90
		$this->assertEquals( 'Season 5', $data[0]['name'] );
91
		$this->assertEquals( 'The Be Sharps', $data[1]['name'] );
92
	}
93
94 View Code Duplication
	public function test_get_items_include_query() {
95
		$id1 = $this->factory->tag->create();
96
		$id2 = $this->factory->tag->create();
0 ignored issues
show
Unused Code introduced by
$id2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
		$id3 = $this->factory->tag->create();
98
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
99
		// Orderby=>asc
100
		$request->set_param( 'include', array( $id3, $id1 ) );
101
		$response = $this->server->dispatch( $request );
102
		$data = $response->get_data();
103
		$this->assertEquals( 2, count( $data ) );
104
		$this->assertEquals( $id1, $data[0]['id'] );
105
		// Orderby=>include
106
		$request->set_param( 'orderby', 'include' );
107
		$response = $this->server->dispatch( $request );
108
		$data = $response->get_data();
109
		$this->assertEquals( 2, count( $data ) );
110
		$this->assertEquals( $id3, $data[0]['id'] );
111
	}
112
113 View Code Duplication
	public function test_get_items_exclude_query() {
114
		$id1 = $this->factory->tag->create();
115
		$id2 = $this->factory->tag->create();
116
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
117
		$response = $this->server->dispatch( $request );
118
		$data = $response->get_data();
119
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
120
		$this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
121
		$request->set_param( 'exclude', array( $id2 ) );
122
		$response = $this->server->dispatch( $request );
123
		$data = $response->get_data();
124
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
125
		$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
126
	}
127
128
	public function test_get_items_offset_query() {
129
		$id1 = $this->factory->tag->create();
0 ignored issues
show
Unused Code introduced by
$id1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
		$id2 = $this->factory->tag->create();
0 ignored issues
show
Unused Code introduced by
$id2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
		$id3 = $this->factory->tag->create();
0 ignored issues
show
Unused Code introduced by
$id3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
132
		$id4 = $this->factory->tag->create();
0 ignored issues
show
Unused Code introduced by
$id4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
134
		$request->set_param( 'offset', 1 );
135
		$response = $this->server->dispatch( $request );
136
		$this->assertCount( 3, $response->get_data() );
137
		// 'offset' works with 'per_page'
138
		$request->set_param( 'per_page', 2 );
139
		$response = $this->server->dispatch( $request );
140
		$this->assertCount( 2, $response->get_data() );
141
		// 'offset' takes priority over 'page'
142
		$request->set_param( 'page', 3 );
143
		$response = $this->server->dispatch( $request );
144
		$this->assertCount( 2, $response->get_data() );
145
	}
146
147
148 View Code Duplication
	public function test_get_items_orderby_args() {
149
		$tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
0 ignored issues
show
Unused Code introduced by
$tag1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
150
		$tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
0 ignored issues
show
Unused Code introduced by
$tag2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
		/*
152
		 * Tests:
153
		 * - orderby
154
		 * - order
155
		 * - per_page
156
		 */
157
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
158
		$request->set_param( 'orderby', 'name' );
159
		$request->set_param( 'order', 'desc' );
160
		$request->set_param( 'per_page', 1 );
161
		$response = $this->server->dispatch( $request );
162
		$this->assertEquals( 200, $response->get_status() );
163
		$data = $response->get_data();
164
		$this->assertEquals( 1, count( $data ) );
165
		$this->assertEquals( 'Banana', $data[0]['name'] );
166
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
167
		$request->set_param( 'orderby', 'name' );
168
		$request->set_param( 'order', 'asc' );
169
		$request->set_param( 'per_page', 2 );
170
		$response = $this->server->dispatch( $request );
171
		$this->assertEquals( 200, $response->get_status() );
172
		$data = $response->get_data();
173
		$this->assertEquals( 2, count( $data ) );
174
		$this->assertEquals( 'Apple', $data[0]['name'] );
175
	}
176
177
	public function test_get_items_orderby_id() {
178
		$tag0 = $this->factory->tag->create( array( 'name' => 'Cantaloupe' ) );
0 ignored issues
show
Unused Code introduced by
$tag0 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
179
		$tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
0 ignored issues
show
Unused Code introduced by
$tag1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
180
		$tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
0 ignored issues
show
Unused Code introduced by
$tag2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
181
		// defaults to orderby=name, order=asc
182
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
183
		$response = $this->server->dispatch( $request );
184
		$this->assertEquals( 200, $response->get_status() );
185
		$data = $response->get_data();
186
		$this->assertEquals( 'Apple', $data[0]['name'] );
187
		$this->assertEquals( 'Banana', $data[1]['name'] );
188
		$this->assertEquals( 'Cantaloupe', $data[2]['name'] );
189
		// orderby=id, with default order=asc
190
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
191
		$request->set_param( 'orderby', 'id' );
192
		$response = $this->server->dispatch( $request );
193
		$this->assertEquals( 200, $response->get_status() );
194
		$data = $response->get_data();
195
		$this->assertEquals( 'Cantaloupe', $data[0]['name'] );
196
		$this->assertEquals( 'Apple', $data[1]['name'] );
197
		$this->assertEquals( 'Banana', $data[2]['name'] );
198
		// orderby=id, order=desc
199
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
200
		$request->set_param( 'orderby', 'id' );
201
		$request->set_param( 'order', 'desc' );
202
		$response = $this->server->dispatch( $request );
203
		$data = $response->get_data();
204
		$this->assertEquals( 200, $response->get_status() );
205
		$this->assertEquals( 'Banana', $data[0]['name'] );
206
		$this->assertEquals( 'Apple', $data[1]['name'] );
207
		$this->assertEquals( 'Cantaloupe', $data[2]['name'] );
208
	}
209
210
	public function test_get_items_post_args() {
211
		$post_id = $this->factory->post->create();
212
		$tag1 = $this->factory->tag->create( array( 'name' => 'DC' ) );
213
		$tag2 = $this->factory->tag->create( array( 'name' => 'Marvel' ) );
214
		$this->factory->tag->create( array( 'name' => 'Dark Horse' ) );
215
		wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' );
216
217
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
218
		$request->set_param( 'post', $post_id );
219
		$response = $this->server->dispatch( $request );
220
		$this->assertEquals( 200, $response->get_status() );
221
222
		$data = $response->get_data();
223
		$this->assertEquals( 2, count( $data ) );
224
		$this->assertEquals( 'DC', $data[0]['name'] );
225
	}
226
227
	public function test_get_terms_post_args_paging() {
228
		$post_id = $this->factory->post->create();
229
		$tag_ids = array();
230
231
		for ( $i = 0; $i < 30; $i++ ) {
232
			$tag_ids[] = $this->factory->tag->create( array(
233
				'name'   => "Tag {$i}",
234
			) );
235
		}
236
		wp_set_object_terms( $post_id, $tag_ids, 'post_tag' );
237
238
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
239
		$request->set_param( 'post', $post_id );
240
		$request->set_param( 'page', 1 );
241
		$request->set_param( 'per_page', 15 );
242
		$request->set_param( 'orderby', 'id' );
243
		$response = $this->server->dispatch( $request );
244
		$tags = $response->get_data();
245
246
		$i = 0;
247
		foreach ( $tags as $tag ) {
248
			$this->assertEquals( $tag['name'], "Tag {$i}" );
249
			$i++;
250
		}
251
252
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
253
		$request->set_param( 'post', $post_id );
254
		$request->set_param( 'page', 2 );
255
		$request->set_param( 'per_page', 15 );
256
		$request->set_param( 'orderby', 'id' );
257
		$response = $this->server->dispatch( $request );
258
		$tags = $response->get_data();
259
260
		foreach ( $tags as $tag ) {
261
			$this->assertEquals( $tag['name'], "Tag {$i}" );
262
			$i++;
263
		}
264
	}
265
266 View Code Duplication
	public function test_get_items_post_empty() {
267
		$post_id = $this->factory->post->create();
268
269
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
270
		$request->set_param( 'post', $post_id );
271
		$response = $this->server->dispatch( $request );
272
		$this->assertEquals( 200, $response->get_status() );
273
274
		$data = $response->get_data();
275
		$this->assertCount( 0, $data );
276
	}
277
278 View Code Duplication
	public function test_get_items_custom_tax_post_args() {
279
		register_taxonomy( 'batman', 'post', array( 'show_in_rest' => true ) );
280
		$controller = new WP_REST_Terms_Controller( 'batman' );
281
		$controller->register_routes();
282
		$term1 = $this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'batman' ) );
283
		$term2 = $this->factory->term->create( array( 'name' => 'Mask', 'taxonomy' => 'batman' ) );
284
		$this->factory->term->create( array( 'name' => 'Car', 'taxonomy' => 'batman' ) );
285
		$post_id = $this->factory->post->create();
286
		wp_set_object_terms( $post_id, array( $term1, $term2 ), 'batman' );
287
288
		$request = new WP_REST_Request( 'GET', '/wp/v2/batman' );
289
		$request->set_param( 'post', $post_id );
290
		$response = $this->server->dispatch( $request );
291
		$this->assertEquals( 200, $response->get_status() );
292
293
		$data = $response->get_data();
294
		$this->assertEquals( 2, count( $data ) );
295
		$this->assertEquals( 'Cape', $data[0]['name'] );
296
	}
297
298 View Code Duplication
	public function test_get_items_search_args() {
299
		$tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
0 ignored issues
show
Unused Code introduced by
$tag1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
300
		$tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
0 ignored issues
show
Unused Code introduced by
$tag2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
301
		/*
302
		 * Tests:
303
		 * - search
304
		 */
305
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
306
		$request->set_param( 'search', 'App' );
307
		$response = $this->server->dispatch( $request );
308
		$this->assertEquals( 200, $response->get_status() );
309
		$data = $response->get_data();
310
		$this->assertEquals( 1, count( $data ) );
311
		$this->assertEquals( 'Apple', $data[0]['name'] );
312
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
313
		$request->set_param( 'search', 'Garbage' );
314
		$response = $this->server->dispatch( $request );
315
		$this->assertEquals( 200, $response->get_status() );
316
		$data = $response->get_data();
317
		$this->assertEquals( 0, count( $data ) );
318
	}
319
320 View Code Duplication
	public function test_get_items_slug_arg() {
321
		$tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
0 ignored issues
show
Unused Code introduced by
$tag1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
322
		$tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
0 ignored issues
show
Unused Code introduced by
$tag2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
323
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
324
		$request->set_param( 'slug', 'apple' );
325
		$response = $this->server->dispatch( $request );
326
		$this->assertEquals( 200, $response->get_status() );
327
		$data = $response->get_data();
328
		$this->assertEquals( 1, count( $data ) );
329
		$this->assertEquals( 'Apple', $data[0]['name'] );
330
	}
331
332 View Code Duplication
	public function test_get_terms_private_taxonomy() {
333
		register_taxonomy( 'robin', 'post', array( 'public' => false ) );
334
		$term1 = $this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'robin' ) );
0 ignored issues
show
Unused Code introduced by
$term1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
335
		$term2 = $this->factory->term->create( array( 'name' => 'Mask', 'taxonomy' => 'robin' ) );
0 ignored issues
show
Unused Code introduced by
$term2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
336
337
		$request = new WP_REST_Request( 'GET', '/wp/v2/terms/robin' );
338
		$response = $this->server->dispatch( $request );
339
		$this->assertErrorResponse( 'rest_no_route', $response, 404 );
340
	}
341
342 View Code Duplication
	public function test_get_terms_pagination_headers() {
343
		// Start of the index
344
		for ( $i = 0; $i < 50; $i++ ) {
345
			$this->factory->tag->create( array(
346
				'name'   => "Tag {$i}",
347
				) );
348
		}
349
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
350
		$response = $this->server->dispatch( $request );
351
		$headers = $response->get_headers();
352
		$this->assertEquals( 50, $headers['X-WP-Total'] );
353
		$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
354
		$next_link = add_query_arg( array(
355
			'page'    => 2,
356
			), rest_url( 'wp/v2/tags' ) );
357
		$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
358
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
359
		// 3rd page
360
		$this->factory->tag->create( array(
361
				'name'   => 'Tag 51',
362
				) );
363
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
364
		$request->set_param( 'page', 3 );
365
		$response = $this->server->dispatch( $request );
366
		$headers = $response->get_headers();
367
		$this->assertEquals( 51, $headers['X-WP-Total'] );
368
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
369
		$prev_link = add_query_arg( array(
370
			'page'    => 2,
371
			), rest_url( 'wp/v2/tags' ) );
372
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
373
		$next_link = add_query_arg( array(
374
			'page'    => 4,
375
			), rest_url( 'wp/v2/tags' ) );
376
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
377
		// Last page
378
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
379
		$request->set_param( 'page', 6 );
380
		$response = $this->server->dispatch( $request );
381
		$headers = $response->get_headers();
382
		$this->assertEquals( 51, $headers['X-WP-Total'] );
383
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
384
		$prev_link = add_query_arg( array(
385
			'page'    => 5,
386
			), rest_url( 'wp/v2/tags' ) );
387
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
388
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
389
		// Out of bounds
390
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
391
		$request->set_param( 'page', 8 );
392
		$response = $this->server->dispatch( $request );
393
		$headers = $response->get_headers();
394
		$this->assertEquals( 51, $headers['X-WP-Total'] );
395
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
396
		$prev_link = add_query_arg( array(
397
			'page'    => 6,
398
			), rest_url( 'wp/v2/tags' ) );
399
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
400
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
401
	}
402
403 View Code Duplication
	public function test_get_items_invalid_context() {
404
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
405
		$request->set_param( 'context', 'banana' );
406
		$response = $this->server->dispatch( $request );
407
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
408
	}
409
410 View Code Duplication
	public function test_get_item() {
411
		$id = $this->factory->tag->create();
412
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
413
		$response = $this->server->dispatch( $request );
414
		$this->check_get_taxonomy_term_response( $response, $id );
415
	}
416
417
	public function test_get_term_invalid_term() {
418
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
419
		$response = $this->server->dispatch( $request );
420
		$this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
421
	}
422
423
	public function test_get_item_invalid_permission_for_context() {
424
		$id = $this->factory->tag->create();
425
		wp_set_current_user( 0 );
426
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
427
		$request->set_param( 'context', 'edit' );
428
		$response = $this->server->dispatch( $request );
429
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
430
	}
431
432 View Code Duplication
	public function test_get_term_private_taxonomy() {
433
		register_taxonomy( 'robin', 'post', array( 'public' => false ) );
434
		$term1 = $this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'robin' ) );
435
436
		$request = new WP_REST_Request( 'GET', '/wp/v2/terms/robin/' . $term1 );
437
		$response = $this->server->dispatch( $request );
438
		$this->assertErrorResponse( 'rest_no_route', $response, 404 );
439
	}
440
441
	public function test_get_item_incorrect_taxonomy() {
442
		register_taxonomy( 'robin', 'post' );
443
		$term1 = $this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'robin' ) );
444
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term1 );
445
		$response = $this->server->dispatch( $request );
446
		$this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
447
	}
448
449 View Code Duplication
	public function test_create_item() {
450
		wp_set_current_user( $this->administrator );
451
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
452
		$request->set_param( 'name', 'My Awesome Term' );
453
		$request->set_param( 'description', 'This term is so awesome.' );
454
		$request->set_param( 'slug', 'so-awesome' );
455
		$response = $this->server->dispatch( $request );
456
		$this->assertEquals( 201, $response->get_status() );
457
		$headers = $response->get_headers();
458
		$data = $response->get_data();
459
		$this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
460
		$this->assertEquals( 'My Awesome Term', $data['name'] );
461
		$this->assertEquals( 'This term is so awesome.', $data['description'] );
462
		$this->assertEquals( 'so-awesome', $data['slug'] );
463
	}
464
465 View Code Duplication
	public function test_create_item_incorrect_permissions() {
466
		wp_set_current_user( $this->subscriber );
467
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
468
		$request->set_param( 'name', 'Incorrect permissions' );
469
		$response = $this->server->dispatch( $request );
470
		$this->assertErrorResponse( 'rest_cannot_create', $response, 403 );
471
	}
472
473 View Code Duplication
	public function test_create_item_missing_arguments() {
474
		wp_set_current_user( $this->administrator );
475
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
476
		$response = $this->server->dispatch( $request );
477
		$this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 );
478
	}
479
480 View Code Duplication
	public function test_create_item_parent_non_hierarchical_taxonomy() {
481
		wp_set_current_user( $this->administrator );
482
483
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
484
		$request->set_param( 'name', 'My Awesome Term' );
485
		$request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
486
		$response = $this->server->dispatch( $request );
487
		$this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 );
488
	}
489
490 View Code Duplication
	public function test_update_item() {
491
		wp_set_current_user( $this->administrator );
492
		$orig_args = array(
493
			'name'        => 'Original Name',
494
			'description' => 'Original Description',
495
			'slug'        => 'original-slug',
496
			);
497
		$term = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' );
498
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
499
		$request->set_param( 'name', 'New Name' );
500
		$request->set_param( 'description', 'New Description' );
501
		$request->set_param( 'slug', 'new-slug' );
502
		$response = $this->server->dispatch( $request );
503
		$this->assertEquals( 200, $response->get_status() );
504
		$data = $response->get_data();
505
		$this->assertEquals( 'New Name', $data['name'] );
506
		$this->assertEquals( 'New Description', $data['description'] );
507
		$this->assertEquals( 'new-slug', $data['slug'] );
508
	}
509
510 View Code Duplication
	public function test_update_item_invalid_term() {
511
		wp_set_current_user( $this->administrator );
512
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
513
		$request->set_param( 'name', 'Invalid Term' );
514
		$response = $this->server->dispatch( $request );
515
		$this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
516
	}
517
518 View Code Duplication
	public function test_update_item_incorrect_permissions() {
519
		wp_set_current_user( $this->subscriber );
520
		$term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
521
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
522
		$request->set_param( 'name', 'Incorrect permissions' );
523
		$response = $this->server->dispatch( $request );
524
		$this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
525
	}
526
527 View Code Duplication
	public function test_update_item_parent_non_hierarchical_taxonomy() {
528
		wp_set_current_user( $this->administrator );
529
		$term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
530
531
		$request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_taxonomy_id );
532
		$request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
533
		$response = $this->server->dispatch( $request );
534
		$this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 );
535
	}
536
537 View Code Duplication
	public function test_delete_item() {
538
		wp_set_current_user( $this->administrator );
539
		$term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
540
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
541
		$request->set_param( 'force', true );
542
		$response = $this->server->dispatch( $request );
543
		$this->assertEquals( 200, $response->get_status() );
544
		$data = $response->get_data();
545
		$this->assertEquals( 'Deleted Tag', $data['name'] );
546
	}
547
548 View Code Duplication
	public function test_delete_item_force_false() {
549
		wp_set_current_user( $this->administrator );
550
		$term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
551
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
552
		// force defaults to false
553
		$response = $this->server->dispatch( $request );
554
		$this->assertEquals( 501, $response->get_status() );
555
	}
556
557 View Code Duplication
	public function test_delete_item_invalid_term() {
558
		wp_set_current_user( $this->administrator );
559
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
560
		$response = $this->server->dispatch( $request );
561
		$this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
562
	}
563
564
	public function test_delete_item_incorrect_permissions() {
565
		wp_set_current_user( $this->subscriber );
566
		$term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
567
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
568
		$response = $this->server->dispatch( $request );
569
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
570
	}
571
572
	public function test_prepare_item() {
573
		$term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
574
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term->term_id );
575
		$response = $this->server->dispatch( $request );
576
		$data = $response->get_data();
577
578
		$this->check_taxonomy_term( $term, $data, $response->get_links() );
579
	}
580
581
	public function test_get_item_schema() {
582
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
583
		$response = $this->server->dispatch( $request );
584
		$data = $response->get_data();
585
		$properties = $data['schema']['properties'];
586
		$this->assertEquals( 7, count( $properties ) );
587
		$this->assertArrayHasKey( 'id', $properties );
588
		$this->assertArrayHasKey( 'count', $properties );
589
		$this->assertArrayHasKey( 'description', $properties );
590
		$this->assertArrayHasKey( 'link', $properties );
591
		$this->assertArrayHasKey( 'name', $properties );
592
		$this->assertArrayHasKey( 'slug', $properties );
593
		$this->assertArrayHasKey( 'taxonomy', $properties );
594
		$this->assertEquals( array_keys( get_taxonomies() ), $properties['taxonomy']['enum'] );
595
	}
596
597
	public function test_get_item_schema_non_hierarchical() {
598
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
599
		$response = $this->server->dispatch( $request );
600
		$data = $response->get_data();
601
		$properties = $data['schema']['properties'];
602
		$this->assertArrayHasKey( 'id', $properties );
603
		$this->assertFalse( isset( $properties['parent'] ) );
604
	}
605
606 View Code Duplication
	public function test_get_additional_field_registration() {
607
608
		$schema = array(
609
			'type'        => 'integer',
610
			'description' => 'Some integer of mine',
611
			'enum'        => array( 1, 2, 3, 4 ),
612
			'context'     => array( 'view', 'edit' ),
613
		);
614
615
		register_rest_field( 'tag', 'my_custom_int', array(
616
			'schema'          => $schema,
617
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
618
		) );
619
620
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
621
622
		$response = $this->server->dispatch( $request );
623
		$data = $response->get_data();
624
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
625
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
626
627
		$tag_id = $this->factory->tag->create();
628
		$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id );
629
630
		$response = $this->server->dispatch( $request );
631
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
632
633
		global $wp_rest_additional_fields;
634
		$wp_rest_additional_fields = array();
635
	}
636
637 View Code Duplication
	public function test_additional_field_update_errors() {
638
		$schema = array(
639
			'type'        => 'integer',
640
			'description' => 'Some integer of mine',
641
			'enum'        => array( 1, 2, 3, 4 ),
642
			'context'     => array( 'view', 'edit' ),
643
		);
644
645
		register_rest_field( 'tag', 'my_custom_int', array(
646
			'schema'          => $schema,
647
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
648
			'update_callback' => array( $this, 'additional_field_update_callback' ),
649
		) );
650
651
		wp_set_current_user( $this->administrator );
652
		$tag_id = $this->factory->tag->create();
653
		// Check for error on update.
654
		$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/tags/%d', $tag_id ) );
655
		$request->set_body_params( array(
656
			'my_custom_int' => 'returnError',
657
		) );
658
659
		$response = $this->server->dispatch( $request );
660
661
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
662
663
		global $wp_rest_additional_fields;
664
		$wp_rest_additional_fields = array();
665
	}
666
667
	public function additional_field_get_callback( $object, $request ) {
668
		return 123;
669
	}
670
671
	public function additional_field_update_callback( $value, $tag ) {
672
		if ( 'returnError' === $value ) {
673
			return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
674
		}
675
	}
676
677
	public function tearDown() {
678
		_unregister_taxonomy( 'batman' );
679
		_unregister_taxonomy( 'robin' );
680
		parent::tearDown();
681
	}
682
683 View Code Duplication
	protected function check_get_taxonomy_terms_response( $response ) {
684
		$this->assertEquals( 200, $response->get_status() );
685
		$data = $response->get_data();
686
		$args = array(
687
			'hide_empty' => false,
688
		);
689
		$tags = get_terms( 'post_tag', $args );
690
		$this->assertEquals( count( $tags ), count( $data ) );
691
		$this->assertEquals( $tags[0]->term_id, $data[0]['id'] );
692
		$this->assertEquals( $tags[0]->name, $data[0]['name'] );
693
		$this->assertEquals( $tags[0]->slug, $data[0]['slug'] );
694
		$this->assertEquals( $tags[0]->taxonomy, $data[0]['taxonomy'] );
695
		$this->assertEquals( $tags[0]->description, $data[0]['description'] );
696
		$this->assertEquals( $tags[0]->count, $data[0]['count'] );
697
	}
698
699 View Code Duplication
	protected function check_taxonomy_term( $term, $data, $links ) {
700
		$this->assertEquals( $term->term_id, $data['id'] );
701
		$this->assertEquals( $term->name, $data['name'] );
702
		$this->assertEquals( $term->slug, $data['slug'] );
703
		$this->assertEquals( $term->description, $data['description'] );
704
		$this->assertEquals( get_term_link( $term ),  $data['link'] );
705
		$this->assertEquals( $term->count, $data['count'] );
706
		$taxonomy = get_taxonomy( $term->taxonomy );
707
		if ( $taxonomy->hierarchical ) {
708
			$this->assertEquals( $term->parent, $data['parent'] );
709
		} else {
710
			$this->assertFalse( isset( $data['parent'] ) );
711
		}
712
		$expected_links = array(
713
			'self',
714
			'collection',
715
			'about',
716
			'https://api.w.org/post_type',
717
		);
718
		if ( $taxonomy->hierarchical && $term->parent ) {
719
			$expected_links[] = 'up';
720
		}
721
		$this->assertEqualSets( $expected_links, array_keys( $links ) );
722
		$this->assertContains( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
723
		$this->assertEquals( add_query_arg( 'tags', $term->term_id, rest_url( 'wp/v2/posts' ) ), $links['https://api.w.org/post_type'][0]['href'] );
724
	}
725
726 View Code Duplication
	protected function check_get_taxonomy_term_response( $response, $id ) {
727
728
		$this->assertEquals( 200, $response->get_status() );
729
730
		$data = $response->get_data();
731
		$tag = get_term( $id, 'post_tag' );
732
		$this->check_taxonomy_term( $tag, $data, $response->get_links() );
733
	}
734
}
735