|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Jetpack Connection Testing |
|
4
|
|
|
* |
|
5
|
|
|
* Framework for various "unit tests" against the Jetpack connection. |
|
6
|
|
|
* |
|
7
|
|
|
* Individual tests should be added to the class-jetpack-cxn-tests.php file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Brandon Kraft |
|
10
|
|
|
* @package Jetpack |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* "Unit Tests" for the Jetpack connection. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Jetpack_Cxn_Test_Base { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Tests to run on the Jetpack connection. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array $tests |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $tests = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Results of the Jetpack connection tests. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array $results |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $results = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Status of the testing suite. |
|
34
|
|
|
* |
|
35
|
|
|
* Used internally to determine if a test should be skipped since the tests are already failing. Assume passing. |
|
36
|
|
|
* |
|
37
|
|
|
* @var bool $pass |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $pass = true; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Jetpack_Cxn_Test constructor. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct() { |
|
45
|
|
|
$this->tests = array(); |
|
46
|
|
|
$this->results = array(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Adds a new test to the Jetpack Connection Testing suite. |
|
51
|
|
|
* |
|
52
|
|
|
* @param callable $callable Test to add to queue. |
|
53
|
|
|
* @param array $groups Testing groups to add test to. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool True if successfully added. False for a failure. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function add_test( $callable, $groups = array( 'default' ) ) { |
|
58
|
|
|
if ( is_callable( $callable ) ) { |
|
59
|
|
|
$this->tests[] = array( |
|
60
|
|
|
'test' => $callable, |
|
61
|
|
|
'group' => $groups, |
|
62
|
|
|
); |
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Runs the Jetpack connection suite. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function run_tests() { |
|
73
|
|
|
foreach ( $this->tests as $test ) { |
|
74
|
|
|
$result = call_user_func( $test['test'] ); |
|
75
|
|
|
$result['group'] = $test['group']; |
|
76
|
|
|
$this->results[] = $result; |
|
77
|
|
|
if ( false === $result['pass'] ) { |
|
78
|
|
|
$this->pass = false; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the full results array. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $group Testing group whose results we want. Defaults to "default" group. Use "all" for all tests. |
|
87
|
|
|
* @return array Array of test results. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function raw_results( $group = 'default' ) { |
|
90
|
|
|
if ( ! $this->results ) { |
|
|
|
|
|
|
91
|
|
|
$this->run_tests(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$results = $this->results; |
|
95
|
|
|
|
|
96
|
|
|
if ( 'all' === $group ) { |
|
97
|
|
|
return $results; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
foreach ( $results as $test => $result ) { |
|
101
|
|
|
if ( ! in_array( $group, $result['group'], true ) ) { |
|
102
|
|
|
unset( $results[ $test ] ); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $results; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the status of the connection suite. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $group Testing group to check status of. Optional, default all tests. |
|
113
|
|
|
* |
|
114
|
|
|
* @return true|array True if all tests pass. Array of failed tests. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function pass( $group = 'default' ) { |
|
117
|
|
|
$results = $this->raw_results( $group ); |
|
118
|
|
|
|
|
119
|
|
|
foreach ( $results as $result ) { |
|
120
|
|
|
// 'pass' could be true, false, or 'skipped'. We only want false. |
|
121
|
|
|
if ( isset( $result['pass'] ) && false === $result['pass'] ) { |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return true; |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Return array of failed test messages. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $group Testing group whose failures we want. Defaults to "default". Use "all" for all tests. |
|
134
|
|
|
* |
|
135
|
|
|
* @return false|array False if no failed tests. Otherwise, array of failed tests. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function list_fails( $group = 'default' ) { |
|
138
|
|
|
$results = $this->raw_results( $group ); |
|
139
|
|
|
|
|
140
|
|
|
foreach ( $results as $test => $result ) { |
|
141
|
|
|
// We do not want tests that passed or ones that are misconfigured (no pass status or no failure message). |
|
142
|
|
|
if ( ! isset( $result['pass'] ) || false !== $result['pass'] || ! isset( $result['message'] ) ) { |
|
143
|
|
|
unset( $results[ $test ] ); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $results; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Helper function to return consistent responses for a passing test. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $name Test name. |
|
154
|
|
|
* |
|
155
|
|
|
* @return array Test results. |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function passing_test( $name = 'Unnamed' ) { |
|
158
|
|
|
return array( |
|
159
|
|
|
'name' => $name, |
|
160
|
|
|
'pass' => true, |
|
161
|
|
|
'message' => __( 'Test Passed!', 'jetpack' ), |
|
162
|
|
|
'resolution' => false, |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Helper function to return consistent responses for a skipped test. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $name Test name. |
|
170
|
|
|
* @param string $message Reason for skipping the test. Optional. |
|
|
|
|
|
|
171
|
|
|
* |
|
172
|
|
|
* @return array Test results. |
|
173
|
|
|
*/ |
|
174
|
|
|
public static function skipped_test( $name = 'Unnamed', $message = false ) { |
|
175
|
|
|
return array( |
|
176
|
|
|
'name' => $name, |
|
177
|
|
|
'pass' => 'skipped', |
|
178
|
|
|
'message' => $message, |
|
179
|
|
|
'resolution' => false, |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Helper function to return consistent responses for a failing test. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $name Test name. |
|
187
|
|
|
* @param string $message Message detailing the failure. |
|
188
|
|
|
* @param string $resolution Steps to resolve. |
|
|
|
|
|
|
189
|
|
|
* |
|
190
|
|
|
* @return array Test results. |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function failing_test( $name, $message, $resolution = false ) { |
|
193
|
|
|
// Provide standard resolutions steps, but allow pass-through of non-standard ones. |
|
194
|
|
|
switch ( $resolution ) { |
|
195
|
|
|
case 'cycle_connection': |
|
196
|
|
|
$resolution = __( 'Please disconnect and reconnect Jetpack.', 'jetpack' ); // @todo: Link. |
|
197
|
|
|
break; |
|
198
|
|
|
case 'outbound_requests': |
|
199
|
|
|
$resolution = __( 'Please ask your hosting provider to confirm your server can make outbound requests to jetpack.com.', 'jetpack' ); |
|
200
|
|
|
break; |
|
201
|
|
|
case 'support': |
|
202
|
|
|
$resolution = __( 'Please contact support.', 'jetpack' ); // @todo: Link to support. |
|
203
|
|
|
break; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return array( |
|
207
|
|
|
'name' => $name, |
|
208
|
|
|
'pass' => false, |
|
209
|
|
|
'message' => $message, |
|
210
|
|
|
'resolution' => $resolution, |
|
211
|
|
|
); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Provide WP_CLI friendly testing results. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $group Testing group whose results we are outputting. Default "default". Use "all" for all tests. |
|
218
|
|
|
*/ |
|
219
|
|
|
public function output_results_for_cli( $group = 'default' ) { |
|
220
|
|
|
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
221
|
|
|
if ( Jetpack::is_development_mode() ) { |
|
222
|
|
|
WP_CLI::line( __( 'Jetpack is in Development Mode:', 'jetpack' ) ); |
|
223
|
|
|
WP_CLI::line( Jetpack::development_mode_trigger_text() ); |
|
224
|
|
|
} |
|
225
|
|
|
WP_CLI::line( __( 'TEST RESULTS:', 'jetpack' ) ); |
|
226
|
|
|
foreach ( $this->raw_results( $group ) as $test ) { |
|
227
|
|
|
if ( true === $test['pass'] ) { |
|
228
|
|
|
WP_CLI::log( WP_CLI::colorize( '%gPassed:%n ' . $test['name'] ) ); |
|
229
|
|
|
} elseif ( 'skipped' === $test['pass'] ) { |
|
230
|
|
|
WP_CLI::log( WP_CLI::colorize( '%ySkipped:%n ' . $test['name'] ) ); |
|
231
|
|
|
if ( $test['message'] ) { |
|
232
|
|
|
WP_CLI::log( ' ' . $test['message'] ); // Number of spaces to "tab indent" the reason. |
|
233
|
|
|
} |
|
234
|
|
|
} else { // Failed. |
|
235
|
|
|
WP_CLI::log( WP_CLI::colorize( '%rFailed:%n ' . $test['name'] ) ); |
|
236
|
|
|
WP_CLI::log( ' ' . $test['message'] ); // Number of spaces to "tab indent" the reason. |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Provide single WP Error instance of all failures. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $group Testing group whose failures we want converted. Default "default". Use "all" for all tests. |
|
246
|
|
|
* |
|
247
|
|
|
* @return WP_Error|false WP_Error with all failed tests or false if there were no failures. |
|
248
|
|
|
*/ |
|
249
|
|
|
public function output_fails_as_wp_error( $group = 'default' ) { |
|
250
|
|
|
if ( $this->pass( $group ) ) { |
|
251
|
|
|
return false; |
|
252
|
|
|
} |
|
253
|
|
|
$fails = $this->list_fails( $group ); |
|
254
|
|
|
$error = false; |
|
255
|
|
|
|
|
256
|
|
|
foreach ( $fails as $result ) { |
|
|
|
|
|
|
257
|
|
|
$code = 'failed_' . $result['name']; |
|
258
|
|
|
$message = $result['message']; |
|
259
|
|
|
$data = array( |
|
260
|
|
|
'resolution' => $result['resolution'], |
|
261
|
|
|
); |
|
262
|
|
|
if ( ! $error ) { |
|
263
|
|
|
$error = new WP_Error( $code, $message, $data ); |
|
264
|
|
|
} else { |
|
265
|
|
|
$error->add( $code, $message, $data ); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return $error; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Encrypt data for sending to WordPress.com. |
|
274
|
|
|
* |
|
275
|
|
|
* @todo When PHP minimum is 5.3+, add cipher detection to use an agreed better cipher than RC4. RC4 should be the last resort. |
|
276
|
|
|
* |
|
277
|
|
|
* @param string $data Data to encrypt with the WP.com Public Key. |
|
278
|
|
|
* |
|
279
|
|
|
* @return false|array False if functionality not available. Array of encrypted data, encryption key. |
|
280
|
|
|
*/ |
|
281
|
|
|
public function encrypt_string_for_wpcom( $data ) { |
|
282
|
|
|
$return = false; |
|
283
|
|
|
if ( ! function_exists( 'openssl_get_publickey' ) || ! function_exists( 'openssl_seal' ) ) { |
|
284
|
|
|
return $return; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
$public_key = openssl_get_publickey( JETPACK__DEBUGGER_PUBLIC_KEY ); |
|
288
|
|
|
|
|
289
|
|
|
if ( $public_key && openssl_seal( $data, $encrypted_data, $env_key, array( $public_key ) ) ) { |
|
290
|
|
|
// We are returning base64-encoded values to ensure they're characters we can use in JSON responses without issue. |
|
291
|
|
|
$return = array( |
|
292
|
|
|
'data' => base64_encode( $encrypted_data ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
293
|
|
|
'key' => base64_encode( $env_key[0] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
294
|
|
|
'cipher' => 'RC4', // When Jetpack's minimum WP version is at PHP 5.3+, we will add in detecting and using a stronger one. |
|
295
|
|
|
); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
openssl_free_key( $public_key ); |
|
299
|
|
|
|
|
300
|
|
|
return $return; |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.