Completed
Push — add/jetpack_connection_new_met... ( 351fcb...e4c59f )
by
unknown
09:55
created

get_access_token_data_provider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 155

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 155
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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_has_connected_owner() {
107
		$this->assertFalse( $this->manager->get_connection_owner() );
108
		$this->assertFalse( $this->manager->has_connected_owner() );
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->has_connected_owner() );
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->has_connected_owner() );
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
		// No owner and non-logged in user context.
223
		$this->assertFalse( $this->manager->is_connection_owner() );
224
		\Jetpack_Options::update_option( 'master_user', $master_user_id );
225
226
		$this->assertFalse( $this->manager->is_connection_owner() );
227
228
		wp_set_current_user( $master_user_id );
229
		$this->assertTrue( $this->manager->is_connection_owner() );
230
231
		wp_set_current_user( $other_user_id );
232
		$this->assertFalse( $this->manager->is_connection_owner() );
233
234
	}
235
236
	/**
237
	 * Test get_access_token method
238
	 *
239
	 * @dataProvider get_access_token_data_provider
240
	 *
241
	 * @param bool|string $create_blog_token The blog token to be created.
242
	 * @param bool|array  $create_user_tokens The user tokens to be created.
243
	 * @param bool|int    $master_user The ID of the master user to be defined.
244
	 * @param bool|int    $user_id_query The user ID that will be used to fetch the token.
245
	 * @param bool|string $token_key_query The token_key that will be used to fetch the token.
246
	 * @param bool|string $expected_error_code If an error is expected, the error code.
247
	 * @param bool|object $expected_token If success is expected, the expected token object.
248
	 * @return void
249
	 */
250
	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 ) {
251
252
		// Set up.
253
		if ( $create_blog_token ) {
254
			\Jetpack_Options::update_option( 'blog_token', $create_blog_token );
255
			\Jetpack_Options::update_option( 'id', 1234 );
256
		}
257
		if ( $create_user_tokens ) {
258
			\Jetpack_Options::update_option( 'user_tokens', $create_user_tokens );
259
			foreach ( array_keys( $create_user_tokens ) as $uid ) {
260
				wp_insert_user(
261
					array(
262
						'user_login' => 'sample_user' . $uid,
263
						'user_pass'  => 'asdqwe',
264
					)
265
				);
266
			}
267
			if ( $master_user ) {
268
				\Jetpack_Options::update_option( 'master_user', $master_user );
269
			}
270
		}
271
272
		if ( 'CONNECTION_OWNER' === $user_id_query ) {
273
			$manager       = $this->manager; // php 5.6 safe.
274
			$user_id_query = $manager::CONNECTION_OWNER;
275
		}
276
277
		$token = $this->manager->get_access_token( $user_id_query, $token_key_query, false );
278
279
		if ( $expected_error_code ) {
280
			$this->assertInstanceOf( 'WP_Error', $token );
281
			$this->assertSame( $expected_error_code, $token->get_error_code() );
282
		} else {
283
			$this->assertEquals( $expected_token, $token );
284
		}
285
	}
286
287
	/**
288
	 * Data provider for test_get_access_token
289
	 *
290
	 * @return array
291
	 */
292
	public function get_access_token_data_provider() {
293
		return array(
294
			'no tokens'                        => array(
295
				false, // blog token.
296
				false, // user tokens.
297
				false, // master_user.
298
				false, // user_id_query.
299
				false, // token_key_query.
300
				'no_possible_tokens', // expected error code.
301
				false, // expected token.
302
			),
303
			'no tokens'                        => array(
304
				false, // blog token.
305
				false, // user tokens.
306
				false, // master_user.
307
				22, // user_id_query.
308
				false, // token_key_query.
309
				'no_user_tokens', // expected error code.
310
				false, // expected token.
311
			),
312
			'no tokens for the user'           => array(
313
				false, // blog token.
314
				array(
315
					11 => 'asd.zxc.11',
316
				), // user tokens.
317
				false, // master_user.
318
				22, // user_id_query.
319
				false, // token_key_query.
320
				'no_token_for_user', // expected error code.
321
				false, // expected token.
322
			),
323
			'malformed user token'             => array(
324
				false, // blog token.
325
				array(
326
					11 => 'asdzxc.11',
327
				), // user tokens.
328
				false, // master_user.
329
				11, // user_id_query.
330
				false, // token_key_query.
331
				'token_malformed', // expected error code.
332
				false, // expected token.
333
			),
334
			'user mismatch'                    => array(
335
				false, // blog token.
336
				array(
337
					11 => 'asd.zxc.22',
338
				), // user tokens.
339
				false, // master_user.
340
				11, // user_id_query.
341
				false, // token_key_query.
342
				'user_id_mismatch', // expected error code.
343
				false, // expected token.
344
			),
345
			'Connection owner not defined'     => array(
346
				false, // blog token.
347
				array(
348
					11 => 'asd.zxc.11',
349
				), // user tokens.
350
				false, // master_user.
351
				'CONNECTION_OWNER', // user_id_query.
352
				false, // token_key_query.
353
				'empty_master_user_option', // expected error code.
354
				false, // expected token.
355
			),
356
			'Connection owner'                 => array(
357
				false, // blog token.
358
				array(
359
					11 => 'asd.zxc.11',
360
				), // user tokens.
361
				11, // master_user.
362
				'CONNECTION_OWNER', // user_id_query.
363
				false, // token_key_query.
364
				false, // expected error code.
365
				(object) array(
366
					'secret'           => 'asd.zxc',
367
					'external_user_id' => 11,
368
				), // expected token.
369
			),
370
			'Find blog token'                  => array(
371
				'asdasd.qweqwe', // blog token.
372
				false, // user tokens.
373
				false, // master_user.
374
				false, // user_id_query.
375
				false, // token_key_query.
376
				false, // expected error code.
377
				(object) array(
378
					'secret'           => 'asdasd.qweqwe',
379
					'external_user_id' => 0,
380
				), // expected token.
381
			),
382
			'Find user token'                  => array(
383
				false, // blog token.
384
				array(
385
					11 => 'qwe.asd.11',
386
					12 => 'asd.zxc.12',
387
				), // user tokens.
388
				false, // master_user.
389
				11, // user_id_query.
390
				false, // token_key_query.
391
				false, // expected error code.
392
				(object) array(
393
					'secret'           => 'qwe.asd',
394
					'external_user_id' => 11,
395
				), // expected token.
396
			),
397
			'Find user token with secret'      => array(
398
				false, // blog token.
399
				array(
400
					11 => 'qwe.asd.11',
401
					12 => 'asd.zxc.12',
402
				), // user tokens.
403
				false, // master_user.
404
				12, // user_id_query.
405
				'asd', // token_key_query.
406
				false, // expected error code.
407
				(object) array(
408
					'secret'           => 'asd.zxc',
409
					'external_user_id' => 12,
410
				), // expected token.
411
			),
412
			'Find blog token with secret'      => array(
413
				'asdasd.qweqwe', // blog token.
414
				false, // user tokens.
415
				false, // master_user.
416
				false, // user_id_query.
417
				'asdasd', // token_key_query.
418
				false, // expected error code.
419
				(object) array(
420
					'secret'           => 'asdasd.qweqwe',
421
					'external_user_id' => 0,
422
				), // expected token.
423
			),
424
			'Dont find user token with secret' => array(
425
				false, // blog token.
426
				array(
427
					11 => 'qwe.asd.11',
428
					12 => 'asd.zxc.12',
429
				), // user tokens.
430
				false, // master_user.
431
				12, // user_id_query.
432
				'qqq', // token_key_query.
433
				'no_valid_user_token', // expected error code.
434
				false, // expected token.
435
			),
436
			'Dont find blog token with secret' => array(
437
				'asdasd.qweqwe', // blog token.
438
				false, // user tokens.
439
				false, // master_user.
440
				false, // user_id_query.
441
				'kaasdas', // token_key_query.
442
				'no_valid_blog_token', // expected error code.
443
				false, // expected token.
444
			),
445
		);
446
	}
447
448
	/**
449
	 * Make sure we don´t change how we return errors
450
	 */
451
	public function test_get_access_token_suppress_errors() {
452
		$this->assertFalse( $this->manager->get_access_token( 123 ) );
453
		$this->assertInstanceOf( 'WP_Error', $this->manager->get_access_token( 123, '', false ) );
454
	}
455
456
}
457