1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Connection; |
4
|
|
|
|
5
|
|
|
use Automattic\Jetpack\Connection\Plugin as Connection_Plugin; |
6
|
|
|
use Automattic\Jetpack\Connection\Plugin_Storage as Connection_Plugin_Storage; |
7
|
|
|
use Automattic\Jetpack\Constants; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Requests_Utility_CaseInsensitiveDictionary; |
10
|
|
|
use WorDBless\Options as WorDBless_Options; |
11
|
|
|
use WP_REST_Request; |
12
|
|
|
use WP_REST_Server; |
13
|
|
|
use WP_User; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Unit tests for the REST API endpoints. |
17
|
|
|
* |
18
|
|
|
* @package automattic/jetpack-connection |
19
|
|
|
* @see \Automattic\Jetpack\Connection\REST_Connector |
20
|
|
|
*/ |
21
|
|
|
class Test_REST_Endpoints extends TestCase { |
22
|
|
|
|
23
|
|
|
const BLOG_TOKEN = 'new.blogtoken'; |
24
|
|
|
const BLOG_ID = 42; |
25
|
|
|
const USER_ID = 111; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* REST Server object. |
29
|
|
|
* |
30
|
|
|
* @var WP_REST_Server |
31
|
|
|
*/ |
32
|
|
|
private $server; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The original hostname to restore after tests are finished. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $api_host_original; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Setting up the test. |
43
|
|
|
* |
44
|
|
|
* @before |
45
|
|
|
*/ |
46
|
|
|
public function set_up() { |
47
|
|
|
global $wp_rest_server; |
48
|
|
|
|
49
|
|
|
$wp_rest_server = new WP_REST_Server(); |
50
|
|
|
$this->server = $wp_rest_server; |
51
|
|
|
|
52
|
|
|
do_action( 'rest_api_init' ); |
53
|
|
|
new REST_Connector( new Manager() ); |
54
|
|
|
|
55
|
|
|
add_action( 'jetpack_disabled_raw_options', array( $this, 'bypass_raw_options' ) ); |
56
|
|
|
|
57
|
|
|
$user = wp_get_current_user(); |
58
|
|
|
$user->add_cap( 'jetpack_reconnect' ); |
59
|
|
|
|
60
|
|
|
$this->api_host_original = Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ); |
61
|
|
|
Constants::$set_constants['JETPACK__WPCOM_JSON_API_BASE'] = 'https://public-api.wordpress.com'; |
62
|
|
|
|
63
|
|
|
Constants::$set_constants['JETPACK__API_BASE'] = 'https://jetpack.wordpress.com/jetpack.'; |
64
|
|
|
|
65
|
|
|
set_transient( 'jetpack_assumed_site_creation_date', '2020-02-28 01:13:27' ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returning the environment into its initial state. |
70
|
|
|
* |
71
|
|
|
* @after |
72
|
|
|
*/ |
73
|
|
|
public function tear_down() { |
74
|
|
|
remove_action( 'jetpack_disabled_raw_options', array( $this, 'bypass_raw_options' ) ); |
75
|
|
|
|
76
|
|
|
$user = wp_get_current_user(); |
77
|
|
|
$user->remove_cap( 'jetpack_reconnect' ); |
78
|
|
|
|
79
|
|
|
Constants::$set_constants['JETPACK__WPCOM_JSON_API_BASE'] = $this->api_host_original; |
80
|
|
|
|
81
|
|
|
delete_transient( 'jetpack_assumed_site_creation_date' ); |
82
|
|
|
|
83
|
|
|
WorDBless_Options::init()->clear_options(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Testing the `/jetpack/v4/remote_authorize` endpoint. |
88
|
|
|
*/ |
89
|
|
|
public function test_remote_authorize() { |
90
|
|
|
add_filter( 'jetpack_options', array( $this, 'mock_jetpack_userless_options' ), 10, 2 ); |
91
|
|
|
add_filter( 'pre_http_request', array( $this, 'intercept_auth_token_request' ), 10, 3 ); |
92
|
|
|
|
93
|
|
|
wp_cache_set( |
94
|
|
|
self::USER_ID, |
95
|
|
|
(object) array( |
96
|
|
|
'ID' => self::USER_ID, |
97
|
|
|
'user_email' => '[email protected]', |
98
|
|
|
), |
99
|
|
|
'users' |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$secret_1 = 'Az0g39toGWlYiTJ4NnDuAz0g39toGWlY'; |
103
|
|
|
|
104
|
|
|
$secrets = array( |
105
|
|
|
'jetpack_authorize_' . self::USER_ID => array( |
106
|
|
|
'secret_1' => $secret_1, |
107
|
|
|
'secret_2' => 'zfIFcym2Jlzd8AVgzfIFcym2Jlzd8AVg', |
108
|
|
|
'exp' => time() + 60, |
109
|
|
|
), |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
113
|
|
|
$options_filter = function ( $value ) use ( $secrets ) { |
114
|
|
|
return $secrets; |
115
|
|
|
}; |
116
|
|
|
add_filter( 'pre_option_' . Secrets::LEGACY_SECRETS_OPTION_NAME, $options_filter ); |
117
|
|
|
|
118
|
|
|
$user_caps_filter = function ( $allcaps, $caps, $args, $user ) { |
119
|
|
|
if ( $user instanceof WP_User && self::USER_ID === $user->ID ) { |
|
|
|
|
120
|
|
|
$allcaps['manage_options'] = true; |
121
|
|
|
$allcaps['administrator'] = true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $allcaps; |
125
|
|
|
}; |
126
|
|
|
add_filter( 'user_has_cap', $user_caps_filter, 10, 4 ); |
127
|
|
|
|
128
|
|
|
$this->request = new WP_REST_Request( 'POST', '/jetpack/v4/remote_authorize' ); |
129
|
|
|
$this->request->set_header( 'Content-Type', 'application/json' ); |
130
|
|
|
$this->request->set_body( '{ "state": "' . self::USER_ID . '", "secret": "' . $secret_1 . '", "redirect_uri": "https://example.org", "code": "54321" }' ); |
131
|
|
|
|
132
|
|
|
$response = $this->server->dispatch( $this->request ); |
133
|
|
|
$data = $response->get_data(); |
134
|
|
|
|
135
|
|
|
remove_filter( 'user_has_cap', $user_caps_filter ); |
136
|
|
|
remove_filter( 'pre_option_' . Secrets::LEGACY_SECRETS_OPTION_NAME, $options_filter ); |
137
|
|
|
remove_filter( 'pre_http_request', array( $this, 'intercept_auth_token_request' ) ); |
138
|
|
|
remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_userless_options' ) ); |
139
|
|
|
|
140
|
|
|
wp_cache_delete( self::USER_ID, 'users' ); |
141
|
|
|
|
142
|
|
|
wp_set_current_user( 0 ); |
143
|
|
|
|
144
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
145
|
|
|
$this->assertEquals( 'authorized', $data['result'] ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Testing the `/jetpack/v4/connection` endpoint. |
150
|
|
|
*/ |
151
|
|
|
public function test_connection() { |
152
|
|
|
add_filter( 'jetpack_offline_mode', '__return_true' ); |
153
|
|
|
try { |
154
|
|
|
$this->request = new WP_REST_Request( 'GET', '/jetpack/v4/connection' ); |
155
|
|
|
|
156
|
|
|
$response = $this->server->dispatch( $this->request ); |
157
|
|
|
$data = $response->get_data(); |
158
|
|
|
|
159
|
|
|
$this->assertFalse( $data['isActive'] ); |
160
|
|
|
$this->assertFalse( $data['isRegistered'] ); |
161
|
|
|
$this->assertTrue( $data['offlineMode']['isActive'] ); |
162
|
|
|
} finally { |
163
|
|
|
remove_filter( 'jetpack_offline_mode', '__return_true' ); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Testing the `/jetpack/v4/connection` endpoint jetpack_connection_status filter. |
169
|
|
|
*/ |
170
|
|
|
public function test_connection_jetpack_connection_status_filter() { |
171
|
|
|
add_filter( |
172
|
|
|
'jetpack_connection_status', |
173
|
|
|
function ( $status_data ) { |
174
|
|
|
$this->assertTrue( is_array( $status_data ) ); |
175
|
|
|
return array(); |
176
|
|
|
} |
177
|
|
|
); |
178
|
|
|
try { |
179
|
|
|
$this->request = new WP_REST_Request( 'GET', '/jetpack/v4/connection' ); |
180
|
|
|
|
181
|
|
|
$response = $this->server->dispatch( $this->request ); |
182
|
|
|
$data = $response->get_data(); |
183
|
|
|
|
184
|
|
|
$this->assertSame( array(), $data ); |
185
|
|
|
} finally { |
186
|
|
|
remove_all_filters( 'jetpack_connection_status' ); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Testing the `/jetpack/v4/connection/plugins` endpoint. |
192
|
|
|
*/ |
193
|
|
|
public function test_connection_plugins() { |
194
|
|
|
$user = wp_get_current_user(); |
195
|
|
|
$user->add_cap( 'activate_plugins' ); |
196
|
|
|
|
197
|
|
|
$plugins = array( |
198
|
|
|
array( |
199
|
|
|
'name' => 'Plugin Name 1', |
200
|
|
|
'slug' => 'plugin-slug-1', |
201
|
|
|
), |
202
|
|
|
array( |
203
|
|
|
'name' => 'Plugin Name 2', |
204
|
|
|
'slug' => 'plugin-slug-2', |
205
|
|
|
), |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
array_walk( |
209
|
|
|
$plugins, |
210
|
|
|
function ( $plugin ) { |
211
|
|
|
( new Connection_Plugin( $plugin['slug'] ) )->add( $plugin['name'] ); |
212
|
|
|
} |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
Connection_Plugin_Storage::configure(); |
216
|
|
|
|
217
|
|
|
$this->request = new WP_REST_Request( 'GET', '/jetpack/v4/connection/plugins' ); |
218
|
|
|
|
219
|
|
|
$response = $this->server->dispatch( $this->request ); |
220
|
|
|
|
221
|
|
|
$user->remove_cap( 'activate_plugins' ); |
222
|
|
|
|
223
|
|
|
$this->assertEquals( $plugins, $response->get_data() ); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Testing the `connection/reconnect` endpoint, full reconnect. |
228
|
|
|
*/ |
229
|
|
|
public function test_connection_reconnect_full() { |
230
|
|
|
$this->setup_reconnect_test( null ); |
231
|
|
|
add_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
232
|
|
|
add_filter( 'pre_http_request', array( $this, 'intercept_register_request' ), 10, 3 ); |
233
|
|
|
|
234
|
|
|
$response = $this->server->dispatch( $this->build_reconnect_request() ); |
235
|
|
|
$data = $response->get_data(); |
236
|
|
|
|
237
|
|
|
remove_filter( 'pre_http_request', array( $this, 'intercept_register_request' ), 10 ); |
238
|
|
|
remove_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
239
|
|
|
$this->shutdown_reconnect_test( null ); |
240
|
|
|
|
241
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
242
|
|
|
$this->assertEquals( 'in_progress', $data['status'] ); |
243
|
|
|
$this->assertSame( 0, strpos( $data['authorizeUrl'], 'https://jetpack.wordpress.com/jetpack.authorize/' ) ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
248
|
|
|
*/ |
249
|
|
View Code Duplication |
public function test_connection_reconnect_partial_blog_token_success() { |
250
|
|
|
$this->setup_reconnect_test( 'blog_token' ); |
251
|
|
|
add_filter( 'pre_http_request', array( $this, 'intercept_refresh_blog_token_request' ), 10, 3 ); |
252
|
|
|
|
253
|
|
|
$response = $this->server->dispatch( $this->build_reconnect_request() ); |
254
|
|
|
$data = $response->get_data(); |
255
|
|
|
|
256
|
|
|
remove_filter( 'pre_http_request', array( $this, 'intercept_refresh_blog_token_request' ), 10 ); |
257
|
|
|
$this->shutdown_reconnect_test( 'blog_token' ); |
258
|
|
|
|
259
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
260
|
|
|
$this->assertEquals( 'completed', $data['status'] ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
265
|
|
|
*/ |
266
|
|
View Code Duplication |
public function test_connection_reconnect_partial_blog_token_fail() { |
267
|
|
|
$this->setup_reconnect_test( 'blog_token' ); |
268
|
|
|
add_filter( 'pre_http_request', array( $this, 'intercept_refresh_blog_token_request_fail' ), 10, 3 ); |
269
|
|
|
|
270
|
|
|
$response = $this->server->dispatch( $this->build_reconnect_request() ); |
271
|
|
|
$data = $response->get_data(); |
272
|
|
|
|
273
|
|
|
remove_filter( 'pre_http_request', array( $this, 'intercept_refresh_blog_token_request_fail' ), 10 ); |
274
|
|
|
$this->shutdown_reconnect_test( 'blog_token' ); |
275
|
|
|
|
276
|
|
|
$this->assertEquals( 500, $response->get_status() ); |
277
|
|
|
$this->assertEquals( 'jetpack_secret', $data['code'] ); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
282
|
|
|
*/ |
283
|
|
|
public function test_connection_reconnect_partial_user_token_success() { |
284
|
|
|
$this->setup_reconnect_test( 'user_token' ); |
285
|
|
|
|
286
|
|
|
$response = $this->server->dispatch( $this->build_reconnect_request() ); |
287
|
|
|
$data = $response->get_data(); |
288
|
|
|
|
289
|
|
|
$this->shutdown_reconnect_test( 'user_token' ); |
290
|
|
|
|
291
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
292
|
|
|
$this->assertEquals( 'in_progress', $data['status'] ); |
293
|
|
|
$this->assertSame( 0, strpos( $data['authorizeUrl'], 'https://jetpack.wordpress.com/jetpack.authorize/' ) ); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Testing the `connection/reconnect` endpoint, userless (full reconnect). |
298
|
|
|
*/ |
299
|
|
|
public function test_connection_reconnect_userless() { |
300
|
|
|
add_filter( 'jetpack_options', array( $this, 'mock_jetpack_userless_options' ), 10, 2 ); |
301
|
|
|
add_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
302
|
|
|
add_filter( 'pre_http_request', array( $this, 'intercept_register_request' ), 10, 3 ); |
303
|
|
|
|
304
|
|
|
$response = $this->server->dispatch( $this->build_reconnect_request() ); |
305
|
|
|
$data = $response->get_data(); |
306
|
|
|
|
307
|
|
|
remove_filter( 'pre_http_request', array( $this, 'intercept_register_request' ), 10 ); |
308
|
|
|
remove_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
309
|
|
|
remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_userless_options' ) ); |
310
|
|
|
|
311
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
312
|
|
|
$this->assertEquals( 'completed', $data['status'] ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Testing the `connection/reconnect` endpoint when the token validation request fails. |
317
|
|
|
*/ |
318
|
|
|
public function test_connection_reconnect_when_token_validation_request_fails() { |
319
|
|
|
$this->setup_reconnect_test( 'token_validation_failed' ); |
320
|
|
|
add_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
321
|
|
|
add_filter( 'pre_http_request', array( $this, 'intercept_register_request' ), 10, 3 ); |
322
|
|
|
|
323
|
|
|
$response = $this->server->dispatch( $this->build_reconnect_request() ); |
324
|
|
|
$data = $response->get_data(); |
325
|
|
|
|
326
|
|
|
remove_filter( 'pre_http_request', array( $this, 'intercept_register_request' ), 10 ); |
327
|
|
|
remove_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
328
|
|
|
$this->shutdown_reconnect_test( 'token_validation_failed' ); |
329
|
|
|
|
330
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
331
|
|
|
$this->assertEquals( 'in_progress', $data['status'] ); |
332
|
|
|
$this->assertSame( 0, strpos( $data['authorizeUrl'], 'https://jetpack.wordpress.com/jetpack.authorize/' ) ); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
337
|
|
|
* |
338
|
|
|
* @param array $options List of options already skipping the database request. |
339
|
|
|
* |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
public function bypass_raw_options( array $options ) { |
343
|
|
|
$options[ Secrets::LEGACY_SECRETS_OPTION_NAME ] = true; |
344
|
|
|
|
345
|
|
|
return $options; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
350
|
|
|
* |
351
|
|
|
* @param bool|array $response The existing response. |
352
|
|
|
* @param array $args The request arguments. |
353
|
|
|
* @param string $url The request URL. |
354
|
|
|
* |
355
|
|
|
* @return array |
356
|
|
|
*/ |
357
|
|
|
public function intercept_register_request( $response, $args, $url ) { |
358
|
|
|
if ( false === strpos( $url, 'jetpack.register' ) ) { |
359
|
|
|
return $response; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return array( |
363
|
|
|
'headers' => new Requests_Utility_CaseInsensitiveDictionary( array( 'content-type' => 'application/json' ) ), |
364
|
|
|
'body' => wp_json_encode( |
365
|
|
|
array( |
366
|
|
|
'jetpack_id' => '12345', |
367
|
|
|
'jetpack_secret' => 'sample_secret', |
368
|
|
|
) |
369
|
|
|
), |
370
|
|
|
'response' => array( |
371
|
|
|
'code' => 200, |
372
|
|
|
'message' => 'OK', |
373
|
|
|
), |
374
|
|
|
); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
379
|
|
|
* |
380
|
|
|
* @param bool|array $response The existing response. |
381
|
|
|
* @param array $args The request arguments. |
382
|
|
|
* @param string $url The request URL. |
383
|
|
|
* |
384
|
|
|
* @return array |
385
|
|
|
*/ |
386
|
|
|
public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
387
|
|
|
if ( false === strpos( $url, 'jetpack-token-health' ) ) { |
388
|
|
|
return $response; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $this->build_validate_tokens_response( 'blog_token' ); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
396
|
|
|
* |
397
|
|
|
* @param bool|array $response The existing response. |
398
|
|
|
* @param array $args The request arguments. |
399
|
|
|
* @param string $url The request URL. |
400
|
|
|
* |
401
|
|
|
* @return array |
402
|
|
|
*/ |
403
|
|
|
public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
404
|
|
|
if ( false === strpos( $url, 'jetpack-token-health' ) ) { |
405
|
|
|
return $response; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return $this->build_validate_tokens_response( 'user_token' ); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
413
|
|
|
* |
414
|
|
|
* @param bool|array $response The existing response. |
415
|
|
|
* @param array $args The request arguments. |
416
|
|
|
* @param string $url The request URL. |
417
|
|
|
* |
418
|
|
|
* @return array |
419
|
|
|
*/ |
420
|
|
|
public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
421
|
|
|
if ( false === strpos( $url, 'jetpack-token-health' ) ) { |
422
|
|
|
return $response; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return $this->build_validate_tokens_response( null ); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Intercept the `jetpack-token-health` API request sent to WP.com, and mock failed response. |
430
|
|
|
* |
431
|
|
|
* @param bool|array $response The existing response. |
432
|
|
|
* @param array $args The request arguments. |
433
|
|
|
* @param string $url The request URL. |
434
|
|
|
* |
435
|
|
|
* @return array |
436
|
|
|
*/ |
437
|
|
View Code Duplication |
public function intercept_validate_tokens_request_failed( $response, $args, $url ) { |
438
|
|
|
if ( false === strpos( $url, 'jetpack-token-health' ) ) { |
439
|
|
|
return $response; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return array( |
443
|
|
|
'headers' => new Requests_Utility_CaseInsensitiveDictionary( array( 'content-type' => 'application/json' ) ), |
444
|
|
|
'body' => wp_json_encode( array( 'dummy_error' => true ) ), |
445
|
|
|
'response' => array( |
446
|
|
|
'code' => 500, |
447
|
|
|
'message' => 'failed', |
448
|
|
|
), |
449
|
|
|
); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Build the response for a tokens validation request |
454
|
|
|
* |
455
|
|
|
* @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
456
|
|
|
* |
457
|
|
|
* @return array |
458
|
|
|
*/ |
459
|
|
|
private function build_validate_tokens_response( $invalid_token ) { |
460
|
|
|
$body = array( |
461
|
|
|
'blog_token' => array( |
462
|
|
|
'is_healthy' => true, |
463
|
|
|
), |
464
|
|
|
'user_token' => array( |
465
|
|
|
'is_healthy' => true, |
466
|
|
|
'is_master_user' => true, |
467
|
|
|
), |
468
|
|
|
); |
469
|
|
|
|
470
|
|
|
switch ( $invalid_token ) { |
471
|
|
|
case 'blog_token': |
472
|
|
|
$body['blog_token'] = array( |
473
|
|
|
'is_healthy' => false, |
474
|
|
|
'code' => 'unknown_token', |
475
|
|
|
); |
476
|
|
|
break; |
477
|
|
|
case 'user_token': |
478
|
|
|
$body['user_token'] = array( |
479
|
|
|
'is_healthy' => false, |
480
|
|
|
'code' => 'unknown_token', |
481
|
|
|
); |
482
|
|
|
break; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
return array( |
486
|
|
|
'headers' => new Requests_Utility_CaseInsensitiveDictionary( array( 'content-type' => 'application/json' ) ), |
487
|
|
|
'body' => wp_json_encode( $body ), |
488
|
|
|
'response' => array( |
489
|
|
|
'code' => 200, |
490
|
|
|
'message' => 'OK', |
491
|
|
|
), |
492
|
|
|
); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
497
|
|
|
* |
498
|
|
|
* @param bool|array $response The existing response. |
499
|
|
|
* @param array $args The request arguments. |
500
|
|
|
* @param string $url The request URL. |
501
|
|
|
* |
502
|
|
|
* @return array |
503
|
|
|
*/ |
504
|
|
View Code Duplication |
public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
505
|
|
|
if ( false === strpos( $url, 'jetpack-refresh-blog-token' ) ) { |
506
|
|
|
return $response; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
return array( |
510
|
|
|
'headers' => new Requests_Utility_CaseInsensitiveDictionary( array( 'content-type' => 'application/json' ) ), |
511
|
|
|
'body' => wp_json_encode( array( 'jetpack_secret' => self::BLOG_TOKEN ) ), |
512
|
|
|
'response' => array( |
513
|
|
|
'code' => 200, |
514
|
|
|
'message' => 'OK', |
515
|
|
|
), |
516
|
|
|
); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
521
|
|
|
* |
522
|
|
|
* @param bool|array $response The existing response. |
523
|
|
|
* @param array $args The request arguments. |
524
|
|
|
* @param string $url The request URL. |
525
|
|
|
* |
526
|
|
|
* @return array |
527
|
|
|
*/ |
528
|
|
View Code Duplication |
public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
529
|
|
|
if ( false === strpos( $url, 'jetpack-refresh-blog-token' ) ) { |
530
|
|
|
return $response; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
return array( |
534
|
|
|
'headers' => new Requests_Utility_CaseInsensitiveDictionary( array( 'content-type' => 'application/json' ) ), |
535
|
|
|
'body' => wp_json_encode( array( 'jetpack_secret_missing' => true ) ), // Meaningless body. |
536
|
|
|
'response' => array( |
537
|
|
|
'code' => 200, |
538
|
|
|
'message' => 'OK', |
539
|
|
|
), |
540
|
|
|
); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
545
|
|
|
* |
546
|
|
|
* @param bool|array $response The existing response. |
547
|
|
|
* @param array $args The request arguments. |
548
|
|
|
* @param string $url The request URL. |
549
|
|
|
* |
550
|
|
|
* @return array |
551
|
|
|
*/ |
552
|
|
|
public function intercept_auth_token_request( $response, $args, $url ) { |
553
|
|
|
if ( false === strpos( $url, '/jetpack.token/' ) ) { |
554
|
|
|
return $response; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
return array( |
558
|
|
|
'headers' => new Requests_Utility_CaseInsensitiveDictionary( array( 'content-type' => 'application/json' ) ), |
559
|
|
|
'body' => wp_json_encode( |
560
|
|
|
array( |
561
|
|
|
'access_token' => 'mock.token', |
562
|
|
|
'token_type' => 'X_JETPACK', |
563
|
|
|
'scope' => ( new Manager() )->sign_role( 'administrator' ), |
564
|
|
|
) |
565
|
|
|
), |
566
|
|
|
'response' => array( |
567
|
|
|
'code' => 200, |
568
|
|
|
'message' => 'OK', |
569
|
|
|
), |
570
|
|
|
); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Intercept the `Jetpack_Options` call and mock the values. |
575
|
|
|
* Site level / user-less connection set-up. |
576
|
|
|
* |
577
|
|
|
* @param mixed $value The current option value. |
578
|
|
|
* @param string $name Option name. |
579
|
|
|
* |
580
|
|
|
* @return mixed |
581
|
|
|
*/ |
582
|
|
|
public function mock_jetpack_userless_options( $value, $name ) { |
583
|
|
|
switch ( $name ) { |
584
|
|
|
case 'blog_token': |
585
|
|
|
return self::BLOG_TOKEN; |
586
|
|
|
case 'id': |
587
|
|
|
return self::BLOG_ID; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
return $value; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Intercept the `Jetpack_Options` call and mock the values. |
595
|
|
|
* Full connection set-up. |
596
|
|
|
* |
597
|
|
|
* @param mixed $value The current option value. |
598
|
|
|
* @param string $name Option name. |
599
|
|
|
* |
600
|
|
|
* @return mixed |
601
|
|
|
*/ |
602
|
|
|
public function mock_jetpack_options( $value, $name ) { |
603
|
|
|
switch ( $name ) { |
604
|
|
|
case 'blog_token': |
605
|
|
|
return self::BLOG_TOKEN; |
606
|
|
|
case 'id': |
607
|
|
|
return self::BLOG_ID; |
608
|
|
|
case 'master_user': |
609
|
|
|
return self::USER_ID; |
610
|
|
|
case 'user_tokens': |
611
|
|
|
return array( |
612
|
|
|
self::USER_ID => 'new.usertoken.' . self::USER_ID, |
613
|
|
|
); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
return $value; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Build the `connection/reconnect` request object. |
621
|
|
|
* |
622
|
|
|
* @return WP_REST_Request |
623
|
|
|
*/ |
624
|
|
|
private function build_reconnect_request() { |
625
|
|
|
$this->request = new WP_REST_Request( 'POST', '/jetpack/v4/connection/reconnect' ); |
626
|
|
|
$this->request->set_header( 'Content-Type', 'application/json' ); |
627
|
|
|
|
628
|
|
|
return $this->request; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Setup the environment to test the reconnection process. |
633
|
|
|
* |
634
|
|
|
* @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
635
|
|
|
*/ |
636
|
|
|
private function setup_reconnect_test( $invalid_token ) { |
637
|
|
|
switch ( $invalid_token ) { |
638
|
|
|
case 'blog_token': |
639
|
|
|
add_filter( |
640
|
|
|
'pre_http_request', |
641
|
|
|
array( |
642
|
|
|
$this, |
643
|
|
|
'intercept_validate_tokens_request_invalid_blog_token', |
644
|
|
|
), |
645
|
|
|
10, |
646
|
|
|
3 |
647
|
|
|
); |
648
|
|
|
break; |
649
|
|
|
case 'user_token': |
650
|
|
|
add_filter( |
651
|
|
|
'pre_http_request', |
652
|
|
|
array( |
653
|
|
|
$this, |
654
|
|
|
'intercept_validate_tokens_request_invalid_user_token', |
655
|
|
|
), |
656
|
|
|
10, |
657
|
|
|
3 |
658
|
|
|
); |
659
|
|
|
break; |
660
|
|
|
case 'token_validation_failed': |
661
|
|
|
add_filter( |
662
|
|
|
'pre_http_request', |
663
|
|
|
array( |
664
|
|
|
$this, |
665
|
|
|
'intercept_validate_tokens_request_failed', |
666
|
|
|
), |
667
|
|
|
10, |
668
|
|
|
3 |
669
|
|
|
); |
670
|
|
|
break; |
671
|
|
|
case null: |
|
|
|
|
672
|
|
|
add_filter( |
673
|
|
|
'pre_http_request', |
674
|
|
|
array( |
675
|
|
|
$this, |
676
|
|
|
'intercept_validate_tokens_request_valid_tokens', |
677
|
|
|
), |
678
|
|
|
10, |
679
|
|
|
3 |
680
|
|
|
); |
681
|
|
|
break; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
add_filter( 'jetpack_options', array( $this, 'mock_jetpack_options' ), 10, 2 ); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Restore the environment after the `reconnect` test has been run. |
689
|
|
|
* |
690
|
|
|
* @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
691
|
|
|
*/ |
692
|
|
|
private function shutdown_reconnect_test( $invalid_token ) { |
693
|
|
|
switch ( $invalid_token ) { |
694
|
|
|
case 'blog_token': |
695
|
|
|
remove_filter( |
696
|
|
|
'pre_http_request', |
697
|
|
|
array( |
698
|
|
|
$this, |
699
|
|
|
'intercept_validate_tokens_request_invalid_blog_token', |
700
|
|
|
), |
701
|
|
|
10 |
702
|
|
|
); |
703
|
|
|
break; |
704
|
|
|
case 'user_token': |
705
|
|
|
remove_filter( |
706
|
|
|
'pre_http_request', |
707
|
|
|
array( |
708
|
|
|
$this, |
709
|
|
|
'intercept_validate_tokens_request_invalid_user_token', |
710
|
|
|
), |
711
|
|
|
10 |
712
|
|
|
); |
713
|
|
|
break; |
714
|
|
|
case 'token_validation_failed': |
715
|
|
|
remove_filter( |
716
|
|
|
'pre_http_request', |
717
|
|
|
array( |
718
|
|
|
$this, |
719
|
|
|
'intercept_validate_tokens_request_failed', |
720
|
|
|
), |
721
|
|
|
10 |
722
|
|
|
); |
723
|
|
|
break; |
724
|
|
|
case null: |
|
|
|
|
725
|
|
|
remove_filter( |
726
|
|
|
'pre_http_request', |
727
|
|
|
array( |
728
|
|
|
$this, |
729
|
|
|
'intercept_validate_tokens_request_valid_tokens', |
730
|
|
|
), |
731
|
|
|
10 |
732
|
|
|
); |
733
|
|
|
break; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_options' ), 10 ); |
737
|
|
|
remove_filter( 'pre_http_request', array( $this, 'intercept_validate_tokens_request' ), 10 ); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
} |
741
|
|
|
|