| 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 |  |  | 			$user_id_query = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 		$token = ( new Tokens() )->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( ( new Tokens() )->get_access_token( 123 ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  | 		$this->assertInstanceOf( 'WP_Error', ( new Tokens() )->get_access_token( 123, '', false ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  | 	 * Test the `is_userless' method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  | 	 * @covers Automattic\Jetpack\Connection\Manager::is_userless | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  | 	 * @dataProvider data_provider_for_test_is_userless | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  | 	 * @param boolean $is_connected              If the blog is connected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  | 	 * @param boolean $has_connected_user        If the blog has a connected user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  | 	 * @param boolean $master_user_option_is_set If the master_user option is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  | 	 * @param boolean $expected                  The expected output. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 466 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 467 |  |  | 	public function test_is_userless( $is_connected, $has_connected_user, $master_user_option_is_set, $expected ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 468 |  |  | 		$id_admin = wp_insert_user( | 
            
                                                                        
                            
            
                                    
            
            
                | 469 |  |  | 			array( | 
            
                                                                        
                            
            
                                    
            
            
                | 470 |  |  | 				'user_login' => 'admin', | 
            
                                                                        
                            
            
                                    
            
            
                | 471 |  |  | 				'user_pass'  => 'pass', | 
            
                                                                        
                            
            
                                    
            
            
                | 472 |  |  | 				'role'       => 'administrator', | 
            
                                                                        
                            
            
                                    
            
            
                | 473 |  |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 474 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 475 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 476 |  |  | 		if ( $is_connected ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 477 |  |  | 			\Jetpack_Options::update_option( 'id', 1234 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 478 |  |  | 			\Jetpack_Options::update_option( 'blog_token', 'asdasd.123123' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 479 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 480 |  |  | 			\Jetpack_Options::delete_option( 'blog_token' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 481 |  |  | 			\Jetpack_Options::delete_option( 'id' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 482 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 483 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 484 |  |  | 		if ( $has_connected_user ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 485 |  |  | 			\Jetpack_Options::update_option( | 
            
                                                                        
                            
            
                                    
            
            
                | 486 |  |  | 				'user_tokens', | 
            
                                                                        
                            
            
                                    
            
            
                | 487 |  |  | 				array( | 
            
                                                                        
                            
            
                                    
            
            
                | 488 |  |  | 					$id_admin => 'asd123', | 
            
                                                                        
                            
            
                                    
            
            
                | 489 |  |  | 				) | 
            
                                                                        
                            
            
                                    
            
            
                | 490 |  |  | 			); | 
            
                                                                        
                            
            
                                    
            
            
                | 491 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 492 |  |  | 			\Jetpack_Options::delete_option( 'user_tokens' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 493 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 494 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 495 |  |  | 		if ( $master_user_option_is_set ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 496 |  |  | 			\Jetpack_Options::update_option( 'master_user', $id_admin ); | 
            
                                                                        
                            
            
                                    
            
            
                | 497 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 498 |  |  | 			\Jetpack_Options::delete_option( 'master_user' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 499 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 500 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 501 |  |  | 		$this->assertEquals( $expected, $this->manager->is_userless() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 502 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  | 	 * Data provider for test_is_userless. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  | 	 * Structure of the test data arrays: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  | 	 *     [0] => 'is_connected'              boolean If the blog is connected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  | 	 *     [1] => 'has_connected_user'        boolean If the blog has a connected user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  | 	 *     [2] => 'master_user_option_is_set' boolean If the master_user option is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  | 	 *     [3] => 'expected'                  boolean The expected output of the call to is_userless. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  | 	public function data_provider_for_test_is_userless() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  | 			'connected, has connected_user, master_user option is set'         => array( true, true, true, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  | 			'not connected, has connected_user, master_user option is set'     => array( false, true, true, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  | 			'connected, no connected_user, master_user option is set'          => array( true, false, true, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  | 			'not connected, no connected_user, master_user option is set'      => array( false, false, true, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  | 			'not connected, has connected_user, master_user option is not set' => array( false, true, false, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  | 			'not connected, no connected_user, master_user option is not set'  => array( false, false, false, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  | 			'connected, has connected_user, master_user option is not set'     => array( true, true, false, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  | 			'connected, no connected_user, master_user option is not set'      => array( true, false, false, true ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 527 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 528 |  |  |  | 
            
                        
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.