-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Rest_API.php
More file actions
272 lines (208 loc) · 10 KB
/
Test_Rest_API.php
File metadata and controls
272 lines (208 loc) · 10 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
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
<?php
/**
* Tests for REST API endpoints.
*
* @package MultiAuthorPosts
*/
namespace MultiAuthorPosts\Tests;
use MultiAuthorPosts\Co_Authors;
use MultiAuthorPosts\Invite;
use WP_UnitTestCase;
use WP_REST_Request;
/**
* @covers \MultiAuthorPosts\Rest_API
*/
class Test_Rest_API extends WP_UnitTestCase {
private int $post_id;
private int $author_id;
private int $editor_id;
private int $subscriber_id;
public function set_up(): void {
parent::set_up();
$this->author_id = self::factory()->user->create( array( 'role' => 'author' ) );
$this->editor_id = self::factory()->user->create( array( 'role' => 'editor' ) );
$this->subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
$this->post_id = self::factory()->post->create( array( 'post_author' => $this->author_id ) );
}
// -------------------------------------------------------------------------
// GET co-authors
// -------------------------------------------------------------------------
public function test_get_co_authors_as_post_author(): void {
Co_Authors::add_co_author( $this->post_id, $this->subscriber_id );
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertSame( $this->subscriber_id, $data[0]['id'] );
}
public function test_get_co_authors_as_co_author(): void {
Co_Authors::add_co_author( $this->post_id, $this->subscriber_id );
wp_set_current_user( $this->subscriber_id );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
}
public function test_get_co_authors_forbidden_for_unrelated_user(): void {
$other = self::factory()->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $other );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 403, $response->get_status() );
}
// -------------------------------------------------------------------------
// POST co-authors (direct add)
// -------------------------------------------------------------------------
public function test_add_co_author_directly_as_post_author(): void {
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'POST', '/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors' );
$request->set_param( 'user_id', $this->editor_id );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertTrue( Co_Authors::is_co_author( $this->post_id, $this->editor_id ) );
}
public function test_add_co_author_directly_rejects_non_editor_user(): void {
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'POST', '/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors' );
$request->set_param( 'user_id', $this->subscriber_id );
$response = rest_get_server()->dispatch( $request );
// Subscriber has no edit_posts cap → 400
$this->assertSame( 400, $response->get_status() );
}
public function test_add_co_author_directly_forbidden_for_co_author(): void {
Co_Authors::add_co_author( $this->post_id, $this->subscriber_id );
wp_set_current_user( $this->subscriber_id );
$request = new WP_REST_Request( 'POST', '/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors' );
$request->set_param( 'user_id', $this->editor_id );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 403, $response->get_status() );
}
// -------------------------------------------------------------------------
// DELETE co-authors
// -------------------------------------------------------------------------
public function test_remove_co_author_as_post_author(): void {
Co_Authors::add_co_author( $this->post_id, $this->subscriber_id );
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request(
'DELETE',
'/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors/' . $this->subscriber_id
);
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertFalse( Co_Authors::is_co_author( $this->post_id, $this->subscriber_id ) );
}
public function test_remove_non_co_author_returns_404(): void {
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request(
'DELETE',
'/multi-author-posts/v1/posts/' . $this->post_id . '/co-authors/' . $this->subscriber_id
);
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 404, $response->get_status() );
}
// -------------------------------------------------------------------------
// Invite
// -------------------------------------------------------------------------
public function test_get_invite_returns_null_when_none_created(): void {
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/invite' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertNull( $response->get_data()['invite_url'] );
}
public function test_create_invite_returns_url(): void {
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'POST', '/multi-author-posts/v1/posts/' . $this->post_id . '/invite' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$this->assertStringContainsString( 'map_invite=', $data['invite_url'] );
}
public function test_revoke_invite(): void {
Invite::create_invite_url( $this->post_id );
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'DELETE', '/multi-author-posts/v1/posts/' . $this->post_id . '/invite' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertNull( Invite::get_invite_url( $this->post_id ) );
}
public function test_invite_management_forbidden_for_co_author(): void {
Co_Authors::add_co_author( $this->post_id, $this->subscriber_id );
wp_set_current_user( $this->subscriber_id );
$request = new WP_REST_Request( 'POST', '/multi-author-posts/v1/posts/' . $this->post_id . '/invite' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 403, $response->get_status() );
}
// -------------------------------------------------------------------------
// Suggested authors
// -------------------------------------------------------------------------
public function test_suggested_authors_returns_empty_for_short_search(): void {
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/suggested-authors' );
$request->set_param( 'search', 'a' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertSame( array(), $response->get_data() );
}
public function test_suggested_authors_returns_empty_for_empty_search(): void {
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/suggested-authors' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertSame( array(), $response->get_data() );
}
public function test_suggested_authors_does_not_search_by_email_for_authors(): void {
$target = self::factory()->user->create(
array(
'role' => 'author',
'user_email' => 'unique-test-email@example.com',
'display_name' => 'Test Target',
)
);
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/suggested-authors' );
$request->set_param( 'search', 'unique-test-email' );
$response = rest_get_server()->dispatch( $request );
$ids = array_column( $response->get_data(), 'id' );
$this->assertNotContains( $target, $ids );
}
public function test_suggested_authors_searches_by_email_for_admins(): void {
$admin = self::factory()->user->create( array( 'role' => 'administrator' ) );
$target = self::factory()->user->create(
array(
'role' => 'author',
'user_email' => 'admin-findable@example.com',
'display_name' => 'Hidden Name',
)
);
wp_set_current_user( $admin );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/suggested-authors' );
$request->set_param( 'search', 'admin-findable' );
$response = rest_get_server()->dispatch( $request );
$ids = array_column( $response->get_data(), 'id' );
$this->assertContains( $target, $ids );
}
public function test_suggested_authors_returns_results_for_valid_search(): void {
$target = self::factory()->user->create(
array(
'role' => 'author',
'display_name' => 'Searchable Author',
)
);
wp_set_current_user( $this->author_id );
$request = new WP_REST_Request( 'GET', '/multi-author-posts/v1/posts/' . $this->post_id . '/suggested-authors' );
$request->set_param( 'search', 'Searchable' );
$response = rest_get_server()->dispatch( $request );
$ids = array_column( $response->get_data(), 'id' );
$this->assertContains( $target, $ids );
}
public function test_editor_can_manage_co_authors_via_edit_others_posts(): void {
// An editor (who has edit_others_posts) can manage co-authors even
// though they are not the post author.
wp_set_current_user( $this->editor_id );
$request = new WP_REST_Request( 'POST', '/multi-author-posts/v1/posts/' . $this->post_id . '/invite' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
}
}