|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
/** |
|
3
|
|
|
* The SignatureTest class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-connection |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
|
9
|
|
|
|
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides unit tests for the methods in the Jetpack_Signature class. |
|
14
|
|
|
*/ |
|
15
|
|
|
class SignatureTest extends TestCase { |
|
16
|
|
|
/** |
|
17
|
|
|
* Tests the Jetpack_Signature->join_with_equal_sign() method. |
|
18
|
|
|
* |
|
19
|
|
|
* @covers Automattic\Jetpack\Connection\Jetpack_Signature->join_with_equal_sign |
|
20
|
|
|
* @dataProvider join_with_equal_sign_data_provider |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $name Query string key value. |
|
23
|
|
|
* @param string|array $value Associated value for query string key. |
|
24
|
|
|
* @param string|array $expected_output The expected output of $signature->join_with_equal_sign. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function test_join_with_equal_sign( $name, $value, $expected_output ) { |
|
27
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
|
|
28
|
|
|
$this->assertEquals( $expected_output, $signature->join_with_equal_sign( $name, $value ) ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Data provider for test_join_with_equal_sign. |
|
33
|
|
|
* |
|
34
|
|
|
* The test data arrays have the format: |
|
35
|
|
|
* 'name' => The value that the constant will be set to. Null if the constant will not be set. |
|
36
|
|
|
* 'value' => The name of the constant. |
|
37
|
|
|
* 'expected_output' => The expected output of $signature->join_with_equal_sign. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function join_with_equal_sign_data_provider() { |
|
40
|
|
|
return array( |
|
41
|
|
|
'string_value' => |
|
42
|
|
|
array( |
|
43
|
|
|
'name' => 'street', |
|
44
|
|
|
'value' => '1600 Pennsylvania Ave', |
|
45
|
|
|
'expected_output' => 'street=1600 Pennsylvania Ave', |
|
46
|
|
|
), |
|
47
|
|
|
'array_value' => |
|
48
|
|
|
array( |
|
49
|
|
|
'name' => 'first_names', |
|
50
|
|
|
'value' => array( 'Michael', 'Jim', 'Pam' ), |
|
51
|
|
|
'expected_output' => array( 'first_names[0]=Michael', 'first_names[1]=Jim', 'first_names[2]=Pam' ), |
|
52
|
|
|
), |
|
53
|
|
|
'associative_array_value' => |
|
54
|
|
|
array( |
|
55
|
|
|
'name' => 'numbers', |
|
56
|
|
|
'value' => array( |
|
57
|
|
|
'one' => 1, |
|
58
|
|
|
'two' => 2, |
|
59
|
|
|
), |
|
60
|
|
|
'expected_output' => array( 'numbers[one]=1', 'numbers[two]=2' ), |
|
61
|
|
|
), |
|
62
|
|
|
'nested_array_value' => |
|
63
|
|
|
array( |
|
64
|
|
|
'name' => 'numbers', |
|
65
|
|
|
'value' => array( array( 0, 1 ), array( 2, 3 ), array( 4, 5 ) ), |
|
66
|
|
|
'expected_output' => array( |
|
67
|
|
|
'numbers[0][0]=0', |
|
68
|
|
|
'numbers[0][1]=1', |
|
69
|
|
|
'numbers[1][0]=2', |
|
70
|
|
|
'numbers[1][1]=3', |
|
71
|
|
|
'numbers[2][0]=4', |
|
72
|
|
|
'numbers[2][1]=5', |
|
73
|
|
|
), |
|
74
|
|
|
), |
|
75
|
|
|
'nested_associative_array_value' => |
|
76
|
|
|
array( |
|
77
|
|
|
'name' => 'people', |
|
78
|
|
|
'value' => array( |
|
79
|
|
|
array( |
|
80
|
|
|
'last_name' => 'Scott', |
|
81
|
|
|
'first_name' => 'Michael', |
|
82
|
|
|
'city' => 'Boulder', |
|
83
|
|
|
), |
|
84
|
|
|
array( |
|
85
|
|
|
'first_name' => 'Jim', |
|
86
|
|
|
'state' => 'Texas', |
|
87
|
|
|
'last_name' => 'Halpert', |
|
88
|
|
|
), |
|
89
|
|
|
), |
|
90
|
|
|
// Note: Expected output is sorted. |
|
91
|
|
|
'expected_output' => array( |
|
92
|
|
|
'people[0][city]=Boulder', |
|
93
|
|
|
'people[0][first_name]=Michael', |
|
94
|
|
|
'people[0][last_name]=Scott', |
|
95
|
|
|
'people[1][first_name]=Jim', |
|
96
|
|
|
'people[1][last_name]=Halpert', |
|
97
|
|
|
'people[1][state]=Texas', |
|
98
|
|
|
), |
|
99
|
|
|
), |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Tests the Jetpack_Signature->normalized_query_parameters() method. |
|
105
|
|
|
* |
|
106
|
|
|
* @covers Automattic\Jetpack\Connection\Jetpack_Signature->normalized_query_parameters |
|
107
|
|
|
* @dataProvider normalized_query_parameters_data_provider |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $query_string Query string key value. |
|
110
|
|
|
* @param string|array $expected_output The expected output of $signature->normalized_query_parameters. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function test_normalized_query_parameters( $query_string, $expected_output ) { |
|
113
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
|
|
114
|
|
|
$this->assertEquals( $expected_output, $signature->normalized_query_parameters( $query_string ) ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Data provider for test_join_with_equal_sign. |
|
119
|
|
|
* |
|
120
|
|
|
* The test data arrays have the format: |
|
121
|
|
|
* 'name' => The value that the constant will be set to. Null if the constant will not be set. |
|
122
|
|
|
* 'value' => The name of the constant. |
|
123
|
|
|
* 'expected_output' => The expected output of $signature->normalized_query_parameters(). |
|
124
|
|
|
*/ |
|
125
|
|
|
public function normalized_query_parameters_data_provider() { |
|
126
|
|
|
return array( |
|
127
|
|
|
'signature_omitted' => |
|
128
|
|
|
array( |
|
129
|
|
|
'query_string' => 'size=10&signature=super-secret', |
|
130
|
|
|
'expected_output' => array( |
|
131
|
|
|
'size=10', |
|
132
|
|
|
), |
|
133
|
|
|
), |
|
134
|
|
|
'query_key_sort' => |
|
135
|
|
|
array( |
|
136
|
|
|
'query_string' => 'size=10&highlight_fields%5B0%5D=title&highlight_fields%5B1%5D=content&aggregations%5Btaxonomy_1%5D%5Bterms%5D%5Bfield%5D=taxonomy.xposts.slug_slash_name&aggregations%5Btaxonomy_1%5D%5Bterms%5D%5Bsize%5D=5&fields%5B0%5D=date&fields%5B1%5D=permalink.url.raw&query=journey', |
|
137
|
|
|
'expected_output' => array( |
|
138
|
|
|
'query=journey', |
|
139
|
|
|
// Note that size has been sorted below query. |
|
140
|
|
|
'size=10', |
|
141
|
|
|
array( |
|
142
|
|
|
'aggregations[taxonomy_1][terms][field]=taxonomy.xposts.slug_slash_name', |
|
143
|
|
|
'aggregations[taxonomy_1][terms][size]=5', |
|
144
|
|
|
), |
|
145
|
|
|
array( |
|
146
|
|
|
'fields[0]=date', |
|
147
|
|
|
'fields[1]=permalink.url.raw', |
|
148
|
|
|
), |
|
149
|
|
|
// Note that highlight_fields has been sorted below aggregations and fields. |
|
150
|
|
|
array( |
|
151
|
|
|
'highlight_fields[0]=title', |
|
152
|
|
|
'highlight_fields[1]=content', |
|
153
|
|
|
), |
|
154
|
|
|
), |
|
155
|
|
|
), |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Tests the get_current_request_port method |
|
161
|
|
|
* |
|
162
|
|
|
* @param mixed $http_x_forwarded_port value of $_SERVER[ 'HTTP_X_FORWARDED_PORT' ]. |
|
163
|
|
|
* @param mixed $server_port value of $_SERVER[ 'SERVER_PORT' ]. Null will unset the value. |
|
164
|
|
|
* @param string $expeceted The expected output. Null will unset the value. |
|
165
|
|
|
* @param boolean $ssl Whether to consider current request using SSL or not. |
|
166
|
|
|
* |
|
167
|
|
|
* @dataProvider get_request_port_data_provider |
|
168
|
|
|
*/ |
|
169
|
|
|
public function test_get_request_port( $http_x_forwarded_port, $server_port, $expeceted, $ssl = false ) { |
|
170
|
|
|
|
|
171
|
|
|
$original_server = $_SERVER; |
|
172
|
|
|
|
|
173
|
|
|
$_SERVER['HTTP_X_FORWARDED_PORT'] = $http_x_forwarded_port; |
|
174
|
|
|
$_SERVER['SERVER_PORT'] = $server_port; |
|
175
|
|
|
|
|
176
|
|
|
if ( $ssl ) { |
|
177
|
|
|
$_SERVER['HTTPS'] = 'on'; // is_ssl will return true. |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if ( is_null( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ) { |
|
181
|
|
|
unset( $_SERVER['HTTP_X_FORWARDED_PORT'] ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
if ( is_null( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ) { |
|
185
|
|
|
unset( $_SERVER['HTTP_X_FORWARDED_PORT'] ); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
|
|
189
|
|
|
$port = $signature->get_current_request_port(); |
|
190
|
|
|
|
|
191
|
|
|
$_SERVER = $original_server; |
|
192
|
|
|
|
|
193
|
|
|
$this->assertSame( $expeceted, $port ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Data provider for test_get_request_port |
|
198
|
|
|
* |
|
199
|
|
|
* @return array |
|
200
|
|
|
*/ |
|
201
|
|
|
public function get_request_port_data_provider() { |
|
202
|
|
|
return array( |
|
203
|
|
|
array( |
|
204
|
|
|
'', |
|
205
|
|
|
80, |
|
206
|
|
|
'', |
|
207
|
|
|
), |
|
208
|
|
|
array( |
|
209
|
|
|
'', |
|
210
|
|
|
'80', |
|
211
|
|
|
'', |
|
212
|
|
|
), |
|
213
|
|
|
array( |
|
214
|
|
|
'', |
|
215
|
|
|
null, |
|
216
|
|
|
'', |
|
217
|
|
|
), |
|
218
|
|
|
array( |
|
219
|
|
|
null, |
|
220
|
|
|
null, |
|
221
|
|
|
'', |
|
222
|
|
|
), |
|
223
|
|
|
array( |
|
224
|
|
|
'', |
|
225
|
|
|
81, |
|
226
|
|
|
'81', |
|
227
|
|
|
), |
|
228
|
|
|
array( |
|
229
|
|
|
'', |
|
230
|
|
|
'81', |
|
231
|
|
|
'81', |
|
232
|
|
|
), |
|
233
|
|
|
array( |
|
234
|
|
|
82, |
|
235
|
|
|
'81', |
|
236
|
|
|
'82', |
|
237
|
|
|
), |
|
238
|
|
|
array( |
|
239
|
|
|
'82', |
|
240
|
|
|
'81', |
|
241
|
|
|
'82', |
|
242
|
|
|
), |
|
243
|
|
|
array( |
|
244
|
|
|
82, |
|
245
|
|
|
'', |
|
246
|
|
|
'82', |
|
247
|
|
|
), |
|
248
|
|
|
|
|
249
|
|
|
// SSL. |
|
250
|
|
|
array( |
|
251
|
|
|
'', |
|
252
|
|
|
443, |
|
253
|
|
|
'', |
|
254
|
|
|
true, |
|
255
|
|
|
), |
|
256
|
|
|
array( |
|
257
|
|
|
'', |
|
258
|
|
|
'443', |
|
259
|
|
|
'', |
|
260
|
|
|
true, |
|
261
|
|
|
), |
|
262
|
|
|
array( |
|
263
|
|
|
null, |
|
264
|
|
|
'443', |
|
265
|
|
|
'', |
|
266
|
|
|
true, |
|
267
|
|
|
), |
|
268
|
|
|
array( |
|
269
|
|
|
null, |
|
270
|
|
|
null, |
|
271
|
|
|
'', |
|
272
|
|
|
true, |
|
273
|
|
|
), |
|
274
|
|
|
array( |
|
275
|
|
|
'', |
|
276
|
|
|
444, |
|
277
|
|
|
'444', |
|
278
|
|
|
true, |
|
279
|
|
|
), |
|
280
|
|
|
array( |
|
281
|
|
|
'', |
|
282
|
|
|
'444', |
|
283
|
|
|
'444', |
|
284
|
|
|
true, |
|
285
|
|
|
), |
|
286
|
|
|
array( |
|
287
|
|
|
445, |
|
288
|
|
|
'444', |
|
289
|
|
|
'445', |
|
290
|
|
|
true, |
|
291
|
|
|
), |
|
292
|
|
|
array( |
|
293
|
|
|
'445', |
|
294
|
|
|
'444', |
|
295
|
|
|
'445', |
|
296
|
|
|
true, |
|
297
|
|
|
), |
|
298
|
|
|
array( |
|
299
|
|
|
445, |
|
300
|
|
|
'', |
|
301
|
|
|
'445', |
|
302
|
|
|
true, |
|
303
|
|
|
), |
|
304
|
|
|
); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
} |
|
308
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: