| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use WP_Mock\Tools\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Automattic\Jetpack\Connection\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class ManagerTest extends TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	public function setUp() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		\WP_Mock::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 		$this->mock = $this->getMockBuilder( 'stdClass' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 		                   ->setMethods( [ 'get_option', 'update_option' ] ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		                   ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		$this->generator = $this->getMockBuilder( 'stdClass' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		                        ->setMethods( [ 'generate' ] ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		                        ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		$this->manager = new Manager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		\WP_Mock::onFilter( 'jetpack_connection_option_manager' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		        ->with( false ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		        ->reply( $this->mock ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		\WP_Mock::onFilter( 'jetpack_connection_secret_generator' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		        ->with( 'wp_generate_password' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		        ->reply( array( $this->generator, 'generate' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	public function tearDown() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		\WP_Mock::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	function test_class_implements_interface() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$manager = new Manager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$this->assertInstanceOf( 'Automattic\Jetpack\Connection\Manager_Interface', $manager ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	function test_generate_secrets() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$this->generator->expects( $this->exactly( 2 ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		                ->method( 'generate' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		                ->will( $this->returnValue( 'topsecretstring' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$this->mock->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		           ->method( 'update_option' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		           ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			           $this->equalTo( Manager::SECRETS_OPTION_NAME ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			           $this->equalTo( array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 				           'jetpack_name_1' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 					           'secret_1' => 'topsecretstring', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 					           'secret_2' => 'topsecretstring', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 					           'exp' => time() + 600 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				           ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			           ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		           ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$secrets = $this->manager->generate_secrets( 'name', 1, 600 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$this->assertEquals( 'topsecretstring', $secrets['secret_1'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$this->assertEquals( 'topsecretstring', $secrets['secret_2'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	function test_get_secrets_not_found() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->mock->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		           ->method( 'get_option' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		           ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			           $this->equalTo( Manager::SECRETS_OPTION_NAME ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			           $this->anything() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		           ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			Manager::SECRETS_MISSING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$this->manager->get_secrets( 'name', 1, 600 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @dataProvider secrets_value_provider | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 	function test_get_secrets_expired( $name, $user_id, $expires, $values ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		$this->mock->expects( $this->exactly( 2 ) ) | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		           ->method( 'get_option' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 		           ->with( | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 			           $this->equalTo( Manager::SECRETS_OPTION_NAME ), | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 			           $this->anything() | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		           ) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 		           ->will( | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 			           $this->returnValue( array( | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 				           'jetpack_' . $name . '_' . $user_id => array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 					           $values, | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 					           // Expired secret, should be removed on access. | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 					           array( 'exp' => 0 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 				           ) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 			           ) ) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 		           ); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 		$this->mock->expects( $this->once() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 		           ->method( 'update_option' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 		           ->with( | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 			           $this->equalTo( Manager::SECRETS_OPTION_NAME ), | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 			           $this->equalTo( array() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 		           ); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 		$this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 			Manager::SECRETS_EXPIRED, | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 			$this->manager->get_secrets( $name, $user_id, $expires ) | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @dataProvider secrets_value_provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	function test_get_secrets( $name, $user_id, $expires, $values ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$this->mock->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		           ->method( 'get_option' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		           ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			           $this->equalTo( Manager::SECRETS_OPTION_NAME ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			           $this->anything() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		           ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		           ->will( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			           $this->returnValue( array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 				           'jetpack_' . $name . '_' . $user_id => array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 					           $values, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 					           // Making sure the secret is still active. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 					           array( 'exp' => $values['exp'] + time() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 				           ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			           ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		           ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			$values['secret_1'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$this->manager->get_secrets( $name, $user_id, $expires )['secret_1'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * Provides values for secrets to test. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	function secrets_value_provider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 				'action_name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				123, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 				3600, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 				[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 					'secret_1' => 'secret1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 					'secret_2' => 'secret2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 					'exp'      => 600 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 				] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 				'action_name_2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 				1234, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 				36000, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 				[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 					'secret_1' => 'secret1withsomewords', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 					'secret_2' => 'secret2mithosemthingelse', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 					'exp'      => 36000 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 				] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 			] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  |