1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
2
|
|
|
/** |
3
|
|
|
* Connection Manager functionality testing. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Connection Manager functionality testing. |
12
|
|
|
*/ |
13
|
|
|
class ManagerIntegrationTest extends \WorDBless\BaseTestCase { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Initialize the object before running the test method. |
17
|
|
|
*/ |
18
|
|
|
public function setUp() { |
19
|
|
|
parent::setUp(); |
20
|
|
|
$this->manager = new Manager(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Test the `is_connected' method. |
25
|
|
|
* |
26
|
|
|
* @covers Automattic\Jetpack\Connection\Manager::is_connected |
27
|
|
|
* @dataProvider is_connected_data_provider |
28
|
|
|
* |
29
|
|
|
* @param object|boolean $blog_token The blog token. False if the blog token does not exist. |
30
|
|
|
* @param int|boolean $blog_id The blog id. False if the blog id does not exist. |
31
|
|
|
* @param boolean $expected_output The expected output. |
32
|
|
|
*/ |
33
|
|
|
public function test_is_connected( $blog_token, $blog_id, $expected_output ) { |
34
|
|
|
if ( $blog_token ) { |
35
|
|
|
\Jetpack_Options::update_option( 'blog_token', 'asdasd.123123' ); |
36
|
|
|
} else { |
37
|
|
|
\Jetpack_Options::delete_option( 'blog_token' ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ( $blog_id ) { |
41
|
|
|
\Jetpack_Options::update_option( 'id', $blog_id ); |
42
|
|
|
} else { |
43
|
|
|
\Jetpack_Options::delete_option( 'id' ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->assertEquals( $expected_output, $this->manager->is_connected() ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Data provider for test_is_connected. |
51
|
|
|
* |
52
|
|
|
* Structure of the test data arrays: |
53
|
|
|
* [0] => 'blog_token' object|boolean The blog token or false if the blog token does not exist. |
54
|
|
|
* [1] => 'blog_id' int|boolean The blog id or false if the blog id does not exist. |
55
|
|
|
* [2] => 'expected_output' boolean The expected output of the call to is_connected. |
56
|
|
|
*/ |
57
|
|
|
public function is_connected_data_provider() { |
58
|
|
|
|
59
|
|
|
return array( |
60
|
|
|
'blog token, blog id' => array( true, 1234, true ), |
61
|
|
|
'blog token, no blog id' => array( true, false, false ), |
62
|
|
|
'no blog token, blog id' => array( false, 1234, false ), |
63
|
|
|
'no blog token, no blog id' => array( false, false, false ), |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Test get_connected_users |
69
|
|
|
*/ |
70
|
|
|
public function test_get_connected_users() { |
71
|
|
|
$id_admin = wp_insert_user( |
72
|
|
|
array( |
73
|
|
|
'user_login' => 'admin', |
74
|
|
|
'user_pass' => 'pass', |
75
|
|
|
'role' => 'administrator', |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$id_author = wp_insert_user( |
80
|
|
|
array( |
81
|
|
|
'user_login' => 'author', |
82
|
|
|
'user_pass' => 'pass', |
83
|
|
|
'role' => 'author', |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
\Jetpack_Options::update_option( |
88
|
|
|
'user_tokens', |
89
|
|
|
array( |
90
|
|
|
$id_admin => 'asd123', |
91
|
|
|
$id_author => 'asd123', |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$all_users = $this->manager->get_connected_users(); |
96
|
|
|
$admins = $this->manager->get_connected_users( 'manage_options' ); |
97
|
|
|
|
98
|
|
|
$this->assertCount( 2, $all_users ); |
99
|
|
|
$this->assertCount( 1, $admins ); |
100
|
|
|
$this->assertSame( $id_admin, $admins[0]->ID ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test get_connection_owner and is_owner |
105
|
|
|
*/ |
106
|
|
|
public function test_get_connection_owner_and_is_owner() { |
107
|
|
|
$this->assertFalse( $this->manager->get_connection_owner() ); |
108
|
|
|
$this->assertFalse( $this->manager->is_owned() ); |
109
|
|
|
|
110
|
|
|
$id_admin = wp_insert_user( |
111
|
|
|
array( |
112
|
|
|
'user_login' => 'admin', |
113
|
|
|
'user_pass' => 'pass', |
114
|
|
|
'role' => 'administrator', |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$id_author = wp_insert_user( |
119
|
|
|
array( |
120
|
|
|
'user_login' => 'author', |
121
|
|
|
'user_pass' => 'pass', |
122
|
|
|
'role' => 'author', |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
\Jetpack_Options::update_option( 'master_user', $id_admin ); |
127
|
|
|
|
128
|
|
|
// Before tokens are created, no owner is found. |
129
|
|
|
$this->assertFalse( $this->manager->get_connection_owner() ); |
130
|
|
|
$this->assertFalse( $this->manager->is_owned() ); |
131
|
|
|
|
132
|
|
|
\Jetpack_Options::update_option( |
133
|
|
|
'user_tokens', |
134
|
|
|
array( |
135
|
|
|
$id_admin => 'asd.123.' . $id_admin, |
136
|
|
|
$id_author => 'asd.123.' . $id_author, |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$owner = $this->manager->get_connection_owner(); |
141
|
|
|
|
142
|
|
|
$this->assertInstanceOf( 'WP_User', $owner ); |
143
|
|
|
$this->assertSame( $id_admin, $owner->ID ); |
144
|
|
|
$this->assertTrue( $this->manager->is_owned() ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Test has_connected_user and has_connected_admin |
149
|
|
|
*/ |
150
|
|
|
public function test_has_connected_user_and_has_connected_admin() { |
151
|
|
|
$this->assertFalse( $this->manager->has_connected_user() ); |
152
|
|
|
$this->assertFalse( $this->manager->has_connected_admin() ); |
153
|
|
|
|
154
|
|
|
// Create the user. |
155
|
|
|
$id_author = wp_insert_user( |
156
|
|
|
array( |
157
|
|
|
'user_login' => 'author', |
158
|
|
|
'user_pass' => 'pass', |
159
|
|
|
'role' => 'author', |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$this->assertFalse( $this->manager->has_connected_user() ); |
164
|
|
|
$this->assertFalse( $this->manager->has_connected_admin() ); |
165
|
|
|
|
166
|
|
|
// Connect the user. |
167
|
|
|
\Jetpack_Options::update_option( |
168
|
|
|
'user_tokens', |
169
|
|
|
array( |
170
|
|
|
$id_author => 'asd.123.' . $id_author, |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$this->assertTrue( $this->manager->has_connected_user() ); |
175
|
|
|
$this->assertFalse( $this->manager->has_connected_admin() ); |
176
|
|
|
|
177
|
|
|
$id_admin = wp_insert_user( |
178
|
|
|
array( |
179
|
|
|
'user_login' => 'admin', |
180
|
|
|
'user_pass' => 'pass', |
181
|
|
|
'role' => 'administrator', |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
\Jetpack_Options::update_option( |
186
|
|
|
'user_tokens', |
187
|
|
|
array( |
188
|
|
|
$id_admin => 'asd.123.' . $id_admin, |
189
|
|
|
$id_author => 'asd.123.' . $id_author, |
190
|
|
|
) |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$this->assertTrue( $this->manager->has_connected_user() ); |
194
|
|
|
$this->assertTrue( $this->manager->has_connected_admin() ); |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Test is_connection_owner |
200
|
|
|
*/ |
201
|
|
|
public function test_is_connection_owner() { |
202
|
|
|
$master_user_id = wp_insert_user( |
203
|
|
|
array( |
204
|
|
|
'user_login' => 'sample_user', |
205
|
|
|
'user_pass' => 'asdqwe', |
206
|
|
|
'role' => 'administrator', |
207
|
|
|
) |
208
|
|
|
); |
209
|
|
|
$other_user_id = wp_insert_user( |
210
|
|
|
array( |
211
|
|
|
'user_login' => 'other_user', |
212
|
|
|
'user_pass' => 'asdqwe', |
213
|
|
|
'role' => 'administrator', |
214
|
|
|
) |
215
|
|
|
); |
216
|
|
|
\Jetpack_Options::update_option( |
217
|
|
|
'user_tokens', |
218
|
|
|
array( |
219
|
|
|
$master_user_id => 'asd.qwe.' . $master_user_id, |
220
|
|
|
) |
221
|
|
|
); |
222
|
|
|
\Jetpack_Options::update_option( 'master_user', $master_user_id ); |
223
|
|
|
|
224
|
|
|
$this->assertFalse( $this->manager->is_connection_owner() ); |
225
|
|
|
|
226
|
|
|
wp_set_current_user( $master_user_id ); |
227
|
|
|
$this->assertTrue( $this->manager->is_connection_owner() ); |
228
|
|
|
|
229
|
|
|
wp_set_current_user( $other_user_id ); |
230
|
|
|
$this->assertFalse( $this->manager->is_connection_owner() ); |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Test get_access_token method |
236
|
|
|
* |
237
|
|
|
* @dataProvider get_access_token_data_provider |
238
|
|
|
* |
239
|
|
|
* @param bool|string $create_blog_token The blog token to be created. |
240
|
|
|
* @param bool|array $create_user_tokens The user tokens to be created. |
241
|
|
|
* @param bool|int $master_user The ID of the master user to be defined. |
242
|
|
|
* @param bool|int $user_id_query The user ID that will be used to fetch the token. |
243
|
|
|
* @param bool|string $token_key_query The token_key that will be used to fetch the token. |
244
|
|
|
* @param bool|string $expected_error_code If an error is expected, the error code. |
245
|
|
|
* @param bool|object $expected_token If success is expected, the expected token object. |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
public function test_get_access_token( $create_blog_token, $create_user_tokens, $master_user, $user_id_query, $token_key_query, $expected_error_code, $expected_token ) { |
249
|
|
|
|
250
|
|
|
// Set up. |
251
|
|
|
if ( $create_blog_token ) { |
252
|
|
|
\Jetpack_Options::update_option( 'blog_token', $create_blog_token ); |
253
|
|
|
\Jetpack_Options::update_option( 'id', 1234 ); |
254
|
|
|
} |
255
|
|
|
if ( $create_user_tokens ) { |
256
|
|
|
\Jetpack_Options::update_option( 'user_tokens', $create_user_tokens ); |
257
|
|
|
foreach ( array_keys( $create_user_tokens ) as $uid ) { |
258
|
|
|
wp_insert_user( |
259
|
|
|
array( |
260
|
|
|
'user_login' => 'sample_user' . $uid, |
261
|
|
|
'user_pass' => 'asdqwe', |
262
|
|
|
) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
if ( $master_user ) { |
266
|
|
|
\Jetpack_Options::update_option( 'master_user', $master_user ); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if ( 'CONNECTION_OWNER' === $user_id_query ) { |
271
|
|
|
$user_id_query = $this->manager::CONNECTION_OWNER; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$token = $this->manager->get_access_token( $user_id_query, $token_key_query, false ); |
275
|
|
|
|
276
|
|
|
if ( $expected_error_code ) { |
277
|
|
|
$this->assertInstanceOf( 'WP_Error', $token ); |
278
|
|
|
$this->assertSame( $expected_error_code, $token->get_error_code() ); |
279
|
|
|
} else { |
280
|
|
|
$this->assertEquals( $expected_token, $token ); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Data provider for test_get_access_token |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function get_access_token_data_provider() { |
290
|
|
|
return array( |
291
|
|
|
'no tokens' => array( |
292
|
|
|
false, // blog token. |
293
|
|
|
false, // user tokens. |
294
|
|
|
false, // master_user. |
295
|
|
|
false, // user_id_query. |
296
|
|
|
false, // token_key_query. |
297
|
|
|
'no_possible_tokens', // expected error code. |
298
|
|
|
false, // expected token. |
299
|
|
|
), |
300
|
|
|
'no tokens' => array( |
301
|
|
|
false, // blog token. |
302
|
|
|
false, // user tokens. |
303
|
|
|
false, // master_user. |
304
|
|
|
22, // user_id_query. |
305
|
|
|
false, // token_key_query. |
306
|
|
|
'no_user_tokens', // expected error code. |
307
|
|
|
false, // expected token. |
308
|
|
|
), |
309
|
|
|
'no tokens for the user' => array( |
310
|
|
|
false, // blog token. |
311
|
|
|
array( |
312
|
|
|
11 => 'asd.zxc.11', |
313
|
|
|
), // user tokens. |
314
|
|
|
false, // master_user. |
315
|
|
|
22, // user_id_query. |
316
|
|
|
false, // token_key_query. |
317
|
|
|
'no_token_for_user', // expected error code. |
318
|
|
|
false, // expected token. |
319
|
|
|
), |
320
|
|
|
'malformed user token' => array( |
321
|
|
|
false, // blog token. |
322
|
|
|
array( |
323
|
|
|
11 => 'asdzxc.11', |
324
|
|
|
), // user tokens. |
325
|
|
|
false, // master_user. |
326
|
|
|
11, // user_id_query. |
327
|
|
|
false, // token_key_query. |
328
|
|
|
'token_malformed', // expected error code. |
329
|
|
|
false, // expected token. |
330
|
|
|
), |
331
|
|
|
'user mismatch' => array( |
332
|
|
|
false, // blog token. |
333
|
|
|
array( |
334
|
|
|
11 => 'asd.zxc.22', |
335
|
|
|
), // user tokens. |
336
|
|
|
false, // master_user. |
337
|
|
|
11, // user_id_query. |
338
|
|
|
false, // token_key_query. |
339
|
|
|
'user_id_mismatch', // expected error code. |
340
|
|
|
false, // expected token. |
341
|
|
|
), |
342
|
|
|
'Connection owner not defined' => array( |
343
|
|
|
false, // blog token. |
344
|
|
|
array( |
345
|
|
|
11 => 'asd.zxc.11', |
346
|
|
|
), // user tokens. |
347
|
|
|
false, // master_user. |
348
|
|
|
'CONNECTION_OWNER', // user_id_query. |
349
|
|
|
false, // token_key_query. |
350
|
|
|
'empty_master_user_option', // expected error code. |
351
|
|
|
false, // expected token. |
352
|
|
|
), |
353
|
|
|
'Connection owner' => array( |
354
|
|
|
false, // blog token. |
355
|
|
|
array( |
356
|
|
|
11 => 'asd.zxc.11', |
357
|
|
|
), // user tokens. |
358
|
|
|
11, // master_user. |
359
|
|
|
'CONNECTION_OWNER', // user_id_query. |
360
|
|
|
false, // token_key_query. |
361
|
|
|
false, // expected error code. |
362
|
|
|
(object) array( |
363
|
|
|
'secret' => 'asd.zxc', |
364
|
|
|
'external_user_id' => 11, |
365
|
|
|
), // expected token. |
366
|
|
|
), |
367
|
|
|
'Find blog token' => array( |
368
|
|
|
'asdasd.qweqwe', // blog token. |
369
|
|
|
false, // user tokens. |
370
|
|
|
false, // master_user. |
371
|
|
|
false, // user_id_query. |
372
|
|
|
false, // token_key_query. |
373
|
|
|
false, // expected error code. |
374
|
|
|
(object) array( |
375
|
|
|
'secret' => 'asdasd.qweqwe', |
376
|
|
|
'external_user_id' => 0, |
377
|
|
|
), // expected token. |
378
|
|
|
), |
379
|
|
|
'Find user token' => array( |
380
|
|
|
false, // blog token. |
381
|
|
|
array( |
382
|
|
|
11 => 'qwe.asd.11', |
383
|
|
|
12 => 'asd.zxc.12', |
384
|
|
|
), // user tokens. |
385
|
|
|
false, // master_user. |
386
|
|
|
11, // user_id_query. |
387
|
|
|
false, // token_key_query. |
388
|
|
|
false, // expected error code. |
389
|
|
|
(object) array( |
390
|
|
|
'secret' => 'qwe.asd', |
391
|
|
|
'external_user_id' => 11, |
392
|
|
|
), // expected token. |
393
|
|
|
), |
394
|
|
|
'Find user token with secret' => array( |
395
|
|
|
false, // blog token. |
396
|
|
|
array( |
397
|
|
|
11 => 'qwe.asd.11', |
398
|
|
|
12 => 'asd.zxc.12', |
399
|
|
|
), // user tokens. |
400
|
|
|
false, // master_user. |
401
|
|
|
12, // user_id_query. |
402
|
|
|
'asd', // token_key_query. |
403
|
|
|
false, // expected error code. |
404
|
|
|
(object) array( |
405
|
|
|
'secret' => 'asd.zxc', |
406
|
|
|
'external_user_id' => 12, |
407
|
|
|
), // expected token. |
408
|
|
|
), |
409
|
|
|
'Find blog token with secret' => array( |
410
|
|
|
'asdasd.qweqwe', // blog token. |
411
|
|
|
false, // user tokens. |
412
|
|
|
false, // master_user. |
413
|
|
|
false, // user_id_query. |
414
|
|
|
'asdasd', // token_key_query. |
415
|
|
|
false, // expected error code. |
416
|
|
|
(object) array( |
417
|
|
|
'secret' => 'asdasd.qweqwe', |
418
|
|
|
'external_user_id' => 0, |
419
|
|
|
), // expected token. |
420
|
|
|
), |
421
|
|
|
'Dont find user token with secret' => array( |
422
|
|
|
false, // blog token. |
423
|
|
|
array( |
424
|
|
|
11 => 'qwe.asd.11', |
425
|
|
|
12 => 'asd.zxc.12', |
426
|
|
|
), // user tokens. |
427
|
|
|
false, // master_user. |
428
|
|
|
12, // user_id_query. |
429
|
|
|
'qqq', // token_key_query. |
430
|
|
|
'no_valid_user_token', // expected error code. |
431
|
|
|
false, // expected token. |
432
|
|
|
), |
433
|
|
|
'Dont find blog token with secret' => array( |
434
|
|
|
'asdasd.qweqwe', // blog token. |
435
|
|
|
false, // user tokens. |
436
|
|
|
false, // master_user. |
437
|
|
|
false, // user_id_query. |
438
|
|
|
'kaasdas', // token_key_query. |
439
|
|
|
'no_valid_blog_token', // expected error code. |
440
|
|
|
false, // expected token. |
441
|
|
|
), |
442
|
|
|
); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
} |
446
|
|
|
|