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