|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Sync; |
|
4
|
|
|
|
|
5
|
|
|
use Automattic\Jetpack\Connection\Manager; |
|
6
|
|
|
use Automattic\Jetpack\Connection\REST_Connector; |
|
7
|
|
|
use Automattic\Jetpack\Constants; |
|
8
|
|
|
use Automattic\Jetpack\Sync\Main as Sync_Main; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use WorDBless\Options as WorDBless_Options; |
|
11
|
|
|
use WP_REST_Request; |
|
12
|
|
|
use WP_REST_Server; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Unit tests for the REST API endpoints. |
|
16
|
|
|
* |
|
17
|
|
|
* @package automattic/jetpack-sync |
|
18
|
|
|
*/ |
|
19
|
|
|
class Test_REST_Endpoints extends TestCase { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* REST Server object. |
|
23
|
|
|
* |
|
24
|
|
|
* @var WP_REST_Server |
|
25
|
|
|
*/ |
|
26
|
|
|
private $server; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The original hostname to restore after tests are finished. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $api_host_original; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Setting up the test. |
|
37
|
|
|
* |
|
38
|
|
|
* @before |
|
39
|
|
|
*/ |
|
40
|
|
|
public function set_up() { |
|
41
|
|
|
global $wp_rest_server; |
|
42
|
|
|
|
|
43
|
|
|
$wp_rest_server = new WP_REST_Server(); |
|
44
|
|
|
$this->server = $wp_rest_server; |
|
45
|
|
|
|
|
46
|
|
|
Sync_Main::configure(); |
|
47
|
|
|
|
|
48
|
|
|
do_action( 'rest_api_init' ); |
|
49
|
|
|
new REST_Connector( new Manager() ); |
|
50
|
|
|
|
|
51
|
|
|
$this->api_host_original = Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ); |
|
52
|
|
|
Constants::$set_constants['JETPACK__WPCOM_JSON_API_BASE'] = 'https://public-api.wordpress.com'; |
|
53
|
|
|
|
|
54
|
|
|
Constants::$set_constants['JETPACK__API_BASE'] = 'https://jetpack.wordpress.com/jetpack.'; |
|
55
|
|
|
|
|
56
|
|
|
set_transient( 'jetpack_assumed_site_creation_date', '2020-02-28 01:13:27' ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returning the environment into its initial state. |
|
61
|
|
|
* |
|
62
|
|
|
* @after |
|
63
|
|
|
*/ |
|
64
|
|
|
public function tear_down() { |
|
65
|
|
|
|
|
66
|
|
|
Constants::$set_constants['JETPACK__WPCOM_JSON_API_BASE'] = $this->api_host_original; |
|
67
|
|
|
|
|
68
|
|
|
delete_transient( 'jetpack_assumed_site_creation_date' ); |
|
69
|
|
|
|
|
70
|
|
|
WorDBless_Options::init()->clear_options(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Testing the `/jetpack/v4/sync/settings` GET endpoint. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function test_sync_settings() { |
|
77
|
|
|
|
|
78
|
|
|
$settings = Settings::get_settings(); |
|
79
|
|
|
|
|
80
|
|
|
$user = wp_get_current_user(); |
|
81
|
|
|
$user->add_cap( 'manage_options' ); |
|
82
|
|
|
|
|
83
|
|
|
$request = new WP_REST_Request( 'GET', '/jetpack/v4/sync/settings' ); |
|
84
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
85
|
|
|
|
|
86
|
|
|
$response = $this->server->dispatch( $request ); |
|
87
|
|
|
$data = $response->get_data(); |
|
88
|
|
|
|
|
89
|
|
|
$user->remove_cap( 'manage_options' ); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
92
|
|
|
$this->assertEquals( $settings, $data ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Testing the `/jetpack/v4/sync/settings` POST endpoint. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function test_update_sync_settings() { |
|
99
|
|
|
|
|
100
|
|
|
// Update Settings to off state. |
|
101
|
|
|
$settings = Settings::get_settings(); |
|
102
|
|
|
$settings['sync_sender_enabled'] = 0; |
|
103
|
|
|
Settings::update_settings( $settings ); |
|
104
|
|
|
$settings['sync_sender_enabled'] = 1; |
|
105
|
|
|
|
|
106
|
|
|
$user = wp_get_current_user(); |
|
107
|
|
|
$user->add_cap( 'manage_options' ); |
|
108
|
|
|
|
|
109
|
|
|
$request = new WP_REST_Request( 'POST', '/jetpack/v4/sync/settings' ); |
|
110
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
111
|
|
|
$request->set_body( '{ "sync_sender_enabled": 1 }' ); |
|
112
|
|
|
|
|
113
|
|
|
$response = $this->server->dispatch( $request ); |
|
114
|
|
|
$data = $response->get_data(); |
|
115
|
|
|
|
|
116
|
|
|
$user->remove_cap( 'manage_options' ); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
119
|
|
|
$this->assertEquals( $settings, $data ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Testing the `/jetpack/v4/sync/status` endpoint. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function test_sync_status() { |
|
126
|
|
|
|
|
127
|
|
|
$sync_status = Actions::get_sync_status( 'debug_details' ); |
|
128
|
|
|
|
|
129
|
|
|
// Unset next_sync times as they will vary. |
|
130
|
|
|
unset( $sync_status['queue_next_sync'] ); |
|
131
|
|
|
unset( $sync_status['full_queue_next_sync'] ); |
|
132
|
|
|
|
|
133
|
|
|
$user = wp_get_current_user(); |
|
134
|
|
|
$user->add_cap( 'manage_options' ); |
|
135
|
|
|
|
|
136
|
|
|
$request = new WP_REST_Request( 'GET', '/jetpack/v4/sync/status' ); |
|
137
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
138
|
|
|
$request->set_body( '{ "fields": "debug_details" }' ); |
|
139
|
|
|
|
|
140
|
|
|
$response = $this->server->dispatch( $request ); |
|
141
|
|
|
$data = $response->get_data(); |
|
142
|
|
|
$user->remove_cap( 'manage_options' ); |
|
143
|
|
|
|
|
144
|
|
|
// Unset next_sync times as they will vary. |
|
145
|
|
|
unset( $data['queue_next_sync'] ); |
|
146
|
|
|
unset( $data['full_queue_next_sync'] ); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
149
|
|
|
$this->assertEquals( $sync_status, $data ); |
|
150
|
|
|
$this->assertArrayHasKey( 'debug_details', $data ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Testing the `/jetpack/v4/sync/health` endpoint. |
|
155
|
|
|
*/ |
|
156
|
|
|
public function test_sync_health() { |
|
157
|
|
|
|
|
158
|
|
|
Health::update_status( Health::STATUS_UNKNOWN ); |
|
159
|
|
|
|
|
160
|
|
|
$user = wp_get_current_user(); |
|
161
|
|
|
$user->add_cap( 'manage_options' ); |
|
162
|
|
|
|
|
163
|
|
|
$request = new WP_REST_Request( 'POST', '/jetpack/v4/sync/health' ); |
|
164
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
165
|
|
|
$request->set_body( '{ "status": "' . Health::STATUS_IN_SYNC . '" }' ); |
|
166
|
|
|
|
|
167
|
|
|
$response = $this->server->dispatch( $request ); |
|
168
|
|
|
$data = $response->get_data(); |
|
169
|
|
|
|
|
170
|
|
|
$user->remove_cap( 'manage_options' ); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
173
|
|
|
$this->assertEquals( Health::STATUS_IN_SYNC, $data['success'] ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Testing the `/jetpack/v4/sync/now` endpoint. |
|
178
|
|
|
*/ |
|
179
|
|
View Code Duplication |
public function test_sync_now() { |
|
180
|
|
|
|
|
181
|
|
|
// TODO add items to queue to verify response. |
|
182
|
|
|
|
|
183
|
|
|
$user = wp_get_current_user(); |
|
184
|
|
|
$user->add_cap( 'manage_options' ); |
|
185
|
|
|
|
|
186
|
|
|
$request = new WP_REST_Request( 'POST', '/jetpack/v4/sync/now' ); |
|
187
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
188
|
|
|
$request->set_body( '{ "queue": "sync" }' ); |
|
189
|
|
|
|
|
190
|
|
|
$response = $this->server->dispatch( $request ); |
|
191
|
|
|
$user->remove_cap( 'manage_options' ); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Testing the `/jetpack/v4/sync/checkout` endpoint. |
|
198
|
|
|
*/ |
|
199
|
|
View Code Duplication |
public function test_sync_checkout() { |
|
200
|
|
|
|
|
201
|
|
|
// TODO add items to queue to verify response. |
|
202
|
|
|
|
|
203
|
|
|
$user = wp_get_current_user(); |
|
204
|
|
|
$user->add_cap( 'manage_options' ); |
|
205
|
|
|
|
|
206
|
|
|
$request = new WP_REST_Request( 'POST', '/jetpack/v4/sync/now' ); |
|
207
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
208
|
|
|
$request->set_body( '{ "queue": "sync", "number_of_items": 50 }' ); |
|
209
|
|
|
|
|
210
|
|
|
$response = $this->server->dispatch( $request ); |
|
211
|
|
|
$user->remove_cap( 'manage_options' ); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Testing the `/jetpack/v4/sync/unlock` endpoint. |
|
218
|
|
|
*/ |
|
219
|
|
View Code Duplication |
public function test_sync_unlock() { |
|
220
|
|
|
|
|
221
|
|
|
$user = wp_get_current_user(); |
|
222
|
|
|
$user->add_cap( 'manage_options' ); |
|
223
|
|
|
|
|
224
|
|
|
$request = new WP_REST_Request( 'POST', '/jetpack/v4/sync/unlock' ); |
|
225
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
226
|
|
|
$request->set_body( '{ "queue": "sync" }' ); |
|
227
|
|
|
|
|
228
|
|
|
$response = $this->server->dispatch( $request ); |
|
229
|
|
|
$user->remove_cap( 'manage_options' ); |
|
230
|
|
|
|
|
231
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
232
|
|
|
$this->assertFalse( get_option( 'jpsq_sync_checkout' ) ); |
|
233
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Array of Sync Endpoints and method. |
|
238
|
|
|
* |
|
239
|
|
|
* @return int[][] |
|
240
|
|
|
*/ |
|
241
|
|
|
public function endpoint_provider() { |
|
242
|
|
|
return array( |
|
243
|
|
|
array( 'sync/full-sync', 'POST', null ), |
|
244
|
|
|
array( 'sync/settings', 'POST', null ), |
|
245
|
|
|
array( 'sync/settings', 'GET', null ), |
|
246
|
|
|
array( 'sync/status', 'GET', null ), |
|
247
|
|
|
array( 'sync/health', 'POST', '{ "status": "' . Health::STATUS_IN_SYNC . '" }' ), |
|
248
|
|
|
array( 'sync/object', 'GET', null ), |
|
249
|
|
|
array( 'sync/now', 'POST', '{ "queue": "sync" }' ), |
|
250
|
|
|
array( 'sync/checkout', 'POST', null ), |
|
251
|
|
|
array( 'sync/close', 'POST', null ), |
|
252
|
|
|
array( 'sync/unlock', 'POST', '{ "queue": "sync" }' ), |
|
253
|
|
|
array( 'sync/object-id-range', 'GET', '{ "sync_module": "posts", "batch_size": "10" }' ), |
|
254
|
|
|
array( 'sync/data-check', 'GET', null ), |
|
255
|
|
|
array( 'sync/data-histogram', 'POST', null ), |
|
256
|
|
|
); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Verify that Sync Endpoints require user permissions. |
|
261
|
|
|
* |
|
262
|
|
|
* @dataProvider endpoint_provider |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $endpoint Sync endpoint under test. |
|
265
|
|
|
* @param string $method Request Method (get, post, etc). |
|
266
|
|
|
* @param string $data Data to be set to body. |
|
|
|
|
|
|
267
|
|
|
*/ |
|
268
|
|
|
public function test_no_access_response( $endpoint, $method, $data = null ) { |
|
269
|
|
|
|
|
270
|
|
|
$request = new WP_REST_Request( $method, '/jetpack/v4/' . $endpoint ); |
|
271
|
|
|
$request->set_header( 'Content-Type', 'application/json' ); |
|
272
|
|
|
$request->set_body( $data ); |
|
273
|
|
|
|
|
274
|
|
|
$response = $this->server->dispatch( $request ); |
|
275
|
|
|
|
|
276
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
|
277
|
|
|
|
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.