1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
2
|
|
|
/** |
3
|
|
|
* REST Authentication functionality testing. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* REST Authentication functionality testing. |
14
|
|
|
*/ |
15
|
|
|
class REST_Authentication_Test extends TestCase { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Setting up the test. |
19
|
|
|
*/ |
20
|
|
|
public function setUp() { |
21
|
|
|
parent::setUp(); |
22
|
|
|
|
23
|
|
|
$this->rest_authentication = Rest_Authentication::init(); |
24
|
|
|
|
25
|
|
|
$this->manager = $this->getMockBuilder( 'Manager' ) |
26
|
|
|
->setMethods( array( 'verify_xml_rpc_signature', 'reset_saved_auth_state' ) ) |
27
|
|
|
->getMock(); |
28
|
|
|
|
29
|
|
|
$reflection_class = new \ReflectionClass( get_class( $this->rest_authentication ) ); |
30
|
|
|
$manager_property = $reflection_class->getProperty( 'connection_manager' ); |
31
|
|
|
$manager_property->setAccessible( true ); |
32
|
|
|
$manager_property->setValue( $this->rest_authentication, $this->manager ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returning the environment into its initial state. |
37
|
|
|
*/ |
38
|
|
|
public function tearDown() { |
39
|
|
|
parent::tearDown(); |
40
|
|
|
|
41
|
|
|
$_GET = null; |
42
|
|
|
unset( $_SERVER['REQUEST_METHOD'] ); |
43
|
|
|
$this->rest_authentication->reset_saved_auth_state(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Tests wp_rest_authentication_errors with an incoming error. |
48
|
|
|
* |
49
|
|
|
* @covers REST_Authentication::wp_rest_authentication_errors |
50
|
|
|
*/ |
51
|
|
|
public function test_wp_rest_authentication_errors_existing_error() { |
52
|
|
|
$error = new \WP_Error( 'test_error', 'This is a test error' ); |
|
|
|
|
53
|
|
|
$this->assertEquals( $error, $this->rest_authentication->wp_rest_authentication_errors( $error ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Tests wp_rest_authentication with an incoming user id. |
58
|
|
|
* |
59
|
|
|
* @covers REST_Authentication::wp_rest_authentication |
60
|
|
|
*/ |
61
|
|
|
public function test_wp_rest_authentication_existing_user() { |
62
|
|
|
$user_id = 123; |
63
|
|
|
$this->assertEquals( $user_id, $this->rest_authentication->wp_rest_authenticate( $user_id ) ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Tests wp_rest_authentication with an incoming user id. |
68
|
|
|
* |
69
|
|
|
* @param array $test_inputs The array containing the test inputs. |
70
|
|
|
* @param array $expected_outputs The array containg the expected test outputs. |
71
|
|
|
* |
72
|
|
|
* @covers REST_Authentication::wp_rest_authentication |
73
|
|
|
* @dataProvider wp_rest_authenticate_data_provider |
74
|
|
|
*/ |
75
|
|
|
public function test_wp_rest_authenticate( $test_inputs, $expected_outputs ) { |
76
|
|
|
$_GET = $test_inputs['get_params']; |
77
|
|
|
if ( isset( $test_inputs['request_method'] ) ) { |
78
|
|
|
$_SERVER['REQUEST_METHOD'] = $test_inputs['request_method']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->manager->expects( $this->any() ) |
82
|
|
|
->method( 'verify_xml_rpc_signature' ) |
83
|
|
|
->will( $this->returnValue( $test_inputs['verified'] ) ); |
84
|
|
|
|
85
|
|
|
$this->assertEquals( $expected_outputs['authenticate'], $this->rest_authentication->wp_rest_authenticate( '' ) ); |
86
|
|
|
|
87
|
|
|
if ( is_string( $expected_outputs['errors'] ) ) { |
88
|
|
|
$this->assertInstanceOf( $expected_outputs['errors'], $this->rest_authentication->wp_rest_authentication_errors( null ) ); |
89
|
|
|
} else { |
90
|
|
|
$this->assertEquals( $expected_outputs['errors'], $this->rest_authentication->wp_rest_authentication_errors( null ) ); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The data provider for test_wp_rest_authenticate. |
96
|
|
|
* |
97
|
|
|
* @return array An array containg the test inputs and expected outputs. Each test array has the format: |
98
|
|
|
* ['test_inputs'] => [ |
99
|
|
|
* ['get'] => |
100
|
|
|
* ['_for'] => (string) The _for parameter value. Optional. |
101
|
|
|
* ['token'] => (string) The token parameter value. Optional. |
102
|
|
|
* ['signature'] => (string) The signature parameter value. Optional. |
103
|
|
|
* ['request_method'] => (string) The request method. Optional. |
104
|
|
|
* ['verified'] => (false|array) The mocked return value of Manager::verify_xml_rpc_signature. Required. |
105
|
|
|
* ], |
106
|
|
|
* ['test_outputs'] => [ |
107
|
|
|
* ['authenticate'] (int|null) The expected return value of wp_rest_authenticate. Required. |
108
|
|
|
* ['errors'] (null|string|true) The expected return value of wp_rest_authenticate_errors. If the value is |
109
|
|
|
* a string, this is the expected class of the object returned by |
110
|
|
|
* wp_rest_authenticate_errors. Required. |
111
|
|
|
* ] |
112
|
|
|
*/ |
113
|
|
|
public function wp_rest_authenticate_data_provider() { |
114
|
|
|
$token_data = array( |
115
|
|
|
'type' => 'user', |
116
|
|
|
'token_key' => '123abc', |
117
|
|
|
'user_id' => 123, |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return array( |
121
|
|
|
'no for parameter' => array( |
122
|
|
|
'test_inputs' => array( |
123
|
|
|
'get_params' => array( |
124
|
|
|
'token' => 'token', |
125
|
|
|
'signature' => 'signature', |
126
|
|
|
), |
127
|
|
|
'request_method' => 'GET', |
128
|
|
|
'verified' => $token_data, |
129
|
|
|
), |
130
|
|
|
'test_outputs' => array( |
131
|
|
|
'authenticate' => null, |
132
|
|
|
'errors' => null, |
133
|
|
|
), |
134
|
|
|
), |
135
|
|
|
'for parameter is not jetpack' => array( |
136
|
|
|
'test_inputs' => array( |
137
|
|
|
'get_params' => array( |
138
|
|
|
'_for' => 'not_jetpack', |
139
|
|
|
'token' => 'token', |
140
|
|
|
'signature' => 'signature', |
141
|
|
|
), |
142
|
|
|
'request_method' => 'GET', |
143
|
|
|
'verified' => $token_data, |
144
|
|
|
), |
145
|
|
|
'test_outputs' => array( |
146
|
|
|
'authenticate' => null, |
147
|
|
|
'errors' => null, |
148
|
|
|
), |
149
|
|
|
), |
150
|
|
|
'no token or signature parameter' => array( |
151
|
|
|
'test_inputs' => array( |
152
|
|
|
'get_params' => array( |
153
|
|
|
'_for' => 'jetpack', |
154
|
|
|
), |
155
|
|
|
'request_method' => 'GET', |
156
|
|
|
'verified' => $token_data, |
157
|
|
|
), |
158
|
|
|
'test_outputs' => array( |
159
|
|
|
'authenticate' => null, |
160
|
|
|
'errors' => null, |
161
|
|
|
), |
162
|
|
|
), |
163
|
|
|
'no request method' => array( |
164
|
|
|
'test_inputs' => array( |
165
|
|
|
'get_params' => array( |
166
|
|
|
'_for' => 'jetpack', |
167
|
|
|
'token' => 'token', |
168
|
|
|
'signature' => 'signature', |
169
|
|
|
), |
170
|
|
|
'verified' => $token_data, |
171
|
|
|
), |
172
|
|
|
'test_outputs' => array( |
173
|
|
|
'authenticate' => null, |
174
|
|
|
'errors' => 'WP_Error', |
175
|
|
|
), |
176
|
|
|
), |
177
|
|
|
'invalid request method' => array( |
178
|
|
|
'test_inputs' => array( |
179
|
|
|
'get_params' => array( |
180
|
|
|
'_for' => 'jetpack', |
181
|
|
|
'token' => 'token', |
182
|
|
|
'signature' => 'signature', |
183
|
|
|
), |
184
|
|
|
'request_method' => 'DELETE', |
185
|
|
|
'verified' => $token_data, |
186
|
|
|
), |
187
|
|
|
'test_outputs' => array( |
188
|
|
|
'authenticate' => null, |
189
|
|
|
'errors' => 'WP_Error', |
190
|
|
|
), |
191
|
|
|
), |
192
|
|
|
'successful GET request' => array( |
193
|
|
|
'test_inputs' => array( |
194
|
|
|
'get_params' => array( |
195
|
|
|
'_for' => 'jetpack', |
196
|
|
|
'token' => 'token', |
197
|
|
|
'signature' => 'signature', |
198
|
|
|
), |
199
|
|
|
'request_method' => 'GET', |
200
|
|
|
'verified' => $token_data, |
201
|
|
|
), |
202
|
|
|
'test_outputs' => array( |
203
|
|
|
'authenticate' => $token_data['user_id'], |
204
|
|
|
'errors' => true, |
205
|
|
|
), |
206
|
|
|
), |
207
|
|
|
'successful POST request' => array( |
208
|
|
|
'test_inputs' => array( |
209
|
|
|
'get_params' => array( |
210
|
|
|
'_for' => 'jetpack', |
211
|
|
|
'token' => 'token', |
212
|
|
|
'signature' => 'signature', |
213
|
|
|
), |
214
|
|
|
'request_method' => 'POST', |
215
|
|
|
'verified' => $token_data, |
216
|
|
|
), |
217
|
|
|
'test_outputs' => array( |
218
|
|
|
'authenticate' => $token_data['user_id'], |
219
|
|
|
'errors' => true, |
220
|
|
|
), |
221
|
|
|
), |
222
|
|
|
'signature verification failed' => array( |
223
|
|
|
'test_inputs' => array( |
224
|
|
|
'get_params' => array( |
225
|
|
|
'_for' => 'jetpack', |
226
|
|
|
'token' => 'token', |
227
|
|
|
'signature' => 'signature', |
228
|
|
|
), |
229
|
|
|
'request_method' => 'GET', |
230
|
|
|
'verified' => false, |
231
|
|
|
), |
232
|
|
|
'test_outputs' => array( |
233
|
|
|
'authenticate' => null, |
234
|
|
|
'errors' => 'WP_Error', |
235
|
|
|
), |
236
|
|
|
), |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.