Completed
Push — add/jetpack_connection_new_met... ( ad75e8...f4cb2d )
by
unknown
159:29 queued 151:05
created

ManagerIntegrationTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
105
	 */
106
	public function test_get_connection_owner() {
107
		$this->assertFalse( $this->manager->get_connection_owner() );
108
109
		$id_admin = wp_insert_user(
110
			array(
111
				'user_login' => 'admin',
112
				'user_pass'  => 'pass',
113
				'role'       => 'administrator',
114
			)
115
		);
116
117
		$id_author = wp_insert_user(
118
			array(
119
				'user_login' => 'author',
120
				'user_pass'  => 'pass',
121
				'role'       => 'author',
122
			)
123
		);
124
125
		\Jetpack_Options::update_option( 'master_user', $id_admin );
126
127
		// Before tokens are created, no owner is found.
128
		$this->assertFalse( $this->manager->get_connection_owner() );
129
130
		\Jetpack_Options::update_option(
131
			'user_tokens',
132
			array(
133
				$id_admin  => 'asd.123.' . $id_admin,
134
				$id_author => 'asd.123.' . $id_author,
135
			)
136
		);
137
138
		$owner = $this->manager->get_connection_owner();
139
140
		$this->assertInstanceOf( 'WP_User', $owner );
141
		$this->assertSame( $id_admin, $owner->ID );
142
	}
143
144
}
145