Completed
Push — add/debugger-package ( efda91 )
by
unknown
15:59 queued 06:31
created

Tests::last__wpcom_self_test()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 44

Duplication

Lines 3
Ratio 6.82 %

Importance

Changes 0
Metric Value
cc 8
nc 4
nop 0
dl 3
loc 44
rs 7.9715
c 0
b 0
f 0
1
<?php
2
/**
3
 * Collection of tests to run on the Jetpack connection locally.
4
 *
5
 * @package Automattic/jetpack-debugger
6
 */
7
8
namespace Automattic\Jetpack\Debugger;
9
10
use Automattic\Jetpack\Connection\Client;
11
use Automattic\Jetpack\Connection\Utils as Connection_Utils;
12
use Automattic\Jetpack\Status;
13
14
/**
15
 * Class Tests contains all of the actual tests.
16
 */
17
class Tests extends Base {
18
19
	/**
20
	 * Tests constructor.
21
	 */
22
	public function __construct() {
23
		parent::__construct();
24
25
		$methods = get_class_methods( $this );
26
27
		foreach ( $methods as $method ) {
28
			if ( false === strpos( $method, 'test__' ) ) {
29
				continue;
30
			}
31
			$this->add_test( array( $this, $method ), $method, 'direct' );
32
		}
33
34
		/**
35
		 * Fires after loading default Jetpack Connection tests.
36
		 *
37
		 * @since 7.1.0
38
		 * @since 8.3.0 Passes the Tests instance.
39
		 */
40
		do_action( 'jetpack_connection_tests_loaded', $this );
41
42
		/**
43
		 * Determines if the WP.com testing suite should be included.
44
		 *
45
		 * @since 7.1.0
46
		 * @since 8.1.0 Default false.
47
		 *
48
		 * @param bool $run_test To run the WP.com testing suite. Default false.
49
		 */
50
		if ( apply_filters( 'jetpack_debugger_run_self_test', false ) ) {
51
			/**
52
			 * Intentionally added last as it checks for an existing failure state before attempting.
53
			 * Generally, any failed location condition would result in the WP.com check to fail too, so
54
			 * we will skip it to avoid confusing error messages.
55
			 *
56
			 * Note: This really should be an 'async' test.
57
			 */
58
			$this->add_test( array( $this, 'last__wpcom_self_test' ), 'test__wpcom_self_test', 'direct' );
59
		}
60
	}
61
62
	/**
63
	 * Helper function to look up the expected master user and return the local WP_User.
64
	 *
65
	 * @return WP_User Jetpack's expected master user.
66
	 */
67
	protected function helper_retrieve_local_master_user() {
68
		$master_user = \Jetpack_Options::get_option( 'master_user' );
69
		return new \WP_User( $master_user );
70
	}
71
72
	/**
73
	 * Is Jetpack even connected and supposed to be talking to WP.com?
74
	 */
75
	protected function helper_is_jetpack_connected() {
76
		return ( \Jetpack::is_active() && ! ( new Status() )->is_development_mode() );
77
	}
78
79
	/**
80
	 * Simple helper for an outbound connection.
81
	 *
82
	 * @param string $name Name of test.
83
	 * @param string $url URL to request.
84
	 */
85
	protected function helper_outbound_request( $name, $url ) {
86
		$request = wp_remote_get( $url );
87
		$code    = wp_remote_retrieve_response_code( $request );
88
		$scheme  = wp_parse_url( $url, PHP_URL_SCHEME );
89
90 View Code Duplication
		if ( 200 === intval( $code ) ) {
91
			$result = self::passing_test( $name );
92
		} else {
93
			$result = self::failing_test(
94
				$name,
95
				sprintf(
96
					/* translators: URL Scheme, either http or https. */
97
					__( 'Your server did not successfully connect to the Jetpack server using %s.', 'jetpack' ),
98
					$scheme
99
				),
100
				'outbound_requests'
101
			);
102
		}
103
104
		return $result;
105
106
	}
107
108
	/**
109
	 * Returns 30 for use with a filter.
110
	 *
111
	 * To allow time for WP.com to run upstream testing, this function exists to increase the http_request_timeout value
112
	 * to 30.
113
	 *
114
	 * @return int 30
115
	 */
116
	public static function increase_timeout() {
117
		return 30; // seconds.
118
	}
119
120
	/**
121
	 * Test if Jetpack is connected.
122
	 */
123
	protected function test__check_if_connected() {
124
		$name = __FUNCTION__;
125
		if ( $this->helper_is_jetpack_connected() ) {
126
			$result = self::passing_test(
127
				$name,
128
				__( 'Test passed!', 'jetpack' ),
129
				__( 'Your site is connected to Jetpack', 'jetpack' ),
130
				sprintf(
131
					'<p>%1$s</p>' .
132
					'<p><span class="dashicons pass"><span class="screen-reader-text">%2$s</span></span> %3$s</p>',
133
					__( 'A healthy connection ensures Jetpack essential services are provided to your WordPress site, such as Stats and Site Security.', 'jetpack' ),
134
					/* translators: Screen reader text indicating a test has passed */
135
					__( 'Passed', 'jetpack' ),
136
					__( 'Your site is connected to Jetpack.', 'jetpack' )
137
				)
138
			);
139 View Code Duplication
		} elseif ( ( new Status() )->is_development_mode() ) {
140
			$result = self::skipped_test( $name, __( 'Jetpack is in Development Mode:', 'jetpack' ) . ' ' . Jetpack::development_mode_trigger_text(), __( 'Disable development mode.', 'jetpack' ) );
141
		} else {
142
			$result = self::failing_test(
143
				$name,
144
				__( 'Jetpack is not connected.', 'jetpack' ),
145
				'connect_jetpack',
146
				admin_url( 'admin.php?page=jetpack#/dashboard' ),
147
				'critical',
148
				__( 'Your site is not connected to Jetpack', 'jetpack' ),
149
				__( 'Reconnect your site now', 'jetpack' ),
150
				sprintf(
151
					'<p>%1$s</p>' .
152
					'<p><span class="dashicons fail"><span class="screen-reader-text">%2$s</span></span> %3$s<strong> %4$s</strong></p>',
153
					__( 'A healthy connection ensures Jetpack essential services are provided to your WordPress site, such as Stats and Site Security.', 'jetpack' ),
154
					/* translators: screen reader text indicating a test failed */
155
					__( 'Error', 'jetpack' ),
156
					__( 'Your site is not connected to Jetpack.', 'jetpack' ),
157
					__( 'We recommend reconnecting Jetpack.', 'jetpack' )
158
				)
159
			);
160
		}
161
162
		return $result;
163
	}
164
165
	/**
166
	 * Test that the master user still exists on this site.
167
	 *
168
	 * @return array Test results.
169
	 */
170
	protected function test__master_user_exists_on_site() {
171
		$name = __FUNCTION__;
172
		if ( ! $this->helper_is_jetpack_connected() ) {
173
			return self::skipped_test( $name, __( 'Jetpack is not connected. No master user to check.', 'jetpack' ) ); // Skip test.
174
		}
175
		$local_user = $this->helper_retrieve_local_master_user();
176
177
		if ( $local_user->exists() ) {
178
			$result = self::passing_test( $name );
179
		} else {
180
			$result = self::failing_test( $name, __( 'The user who setup the Jetpack connection no longer exists on this site.', 'jetpack' ), 'cycle_connection' );
181
		}
182
183
		return $result;
184
	}
185
186
	/**
187
	 * Test that the master user has the manage options capability (e.g. is an admin).
188
	 *
189
	 * Generic calls from WP.com execute on Jetpack as the master user. If it isn't an admin, random things will fail.
190
	 *
191
	 * @return array Test results.
192
	 */
193
	protected function test__master_user_can_manage_options() {
194
		$name = __FUNCTION__;
195
		if ( ! $this->helper_is_jetpack_connected() ) {
196
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
197
		}
198
		$master_user = $this->helper_retrieve_local_master_user();
199
200
		if ( user_can( $master_user, 'manage_options' ) ) {
201
			$result = self::passing_test( $name );
202
		} else {
203
			/* translators: a WordPress username */
204
			$result = self::failing_test( $name, sprintf( __( 'The user (%s) who setup the Jetpack connection is not an administrator.', 'jetpack' ), $master_user->user_login ), __( 'Either upgrade the user or disconnect and reconnect Jetpack.', 'jetpack' ) ); // @todo: Link to the right places.
205
		}
206
207
		return $result;
208
	}
209
210
	/**
211
	 * Test that the PHP's XML library is installed.
212
	 *
213
	 * While it should be installed by default, increasingly in PHP 7, some OSes require an additional php-xml package.
214
	 *
215
	 * @return array Test results.
216
	 */
217
	protected function test__xml_parser_available() {
218
		$name = __FUNCTION__;
219 View Code Duplication
		if ( function_exists( 'xml_parser_create' ) ) {
220
			$result = self::passing_test( $name );
221
		} else {
222
			$result = self::failing_test( $name, __( 'PHP XML manipulation libraries are not available.', 'jetpack' ), __( "Please ask your hosting provider to refer to our server requirements at https://jetpack.com/support/server-requirements/ and enable PHP's XML module.", 'jetpack' ) );
223
		}
224
225
		return $result;
226
	}
227
228
	/**
229
	 * Test that the server is able to send an outbound http communication.
230
	 *
231
	 * @return array Test results.
232
	 */
233
	protected function test__outbound_http() {
234
		$name = __FUNCTION__;
235
		$url  = preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/';
236
237
		return self::helper_outbound_request( $name, $url );
238
	}
239
240
	/**
241
	 * Test that the server is able to send an outbound https communication.
242
	 *
243
	 * @return array Test results.
244
	 */
245
	protected function test__outbound_https() {
246
		$name = __FUNCTION__;
247
		$url  = preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/';
248
249
		return self::helper_outbound_request( $name, $url );
250
	}
251
252
	/**
253
	 * Check for an IDC.
254
	 *
255
	 * @return array Test results.
256
	 */
257
	protected function test__identity_crisis() {
258
		$name = __FUNCTION__;
259
		if ( ! $this->helper_is_jetpack_connected() ) {
260
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
261
		}
262
		$identity_crisis = \Jetpack::check_identity_crisis();
263
264
		if ( ! $identity_crisis ) {
265
			$result = self::passing_test( $name );
266
		} else {
267
			$message = sprintf(
268
				/* translators: Two URLs. The first is the locally-recorded value, the second is the value as recorded on WP.com. */
269
				__( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ),
270
				$identity_crisis['home'],
271
				$identity_crisis['wpcom_home']
272
			);
273
			$result = self::failing_test( $name, $message, 'support' );
274
		}
275
		return $result;
276
	}
277
278
	/**
279
	 * Tests connection status against wp.com's test-connection endpoint.
280
	 *
281
	 * @todo: Compare with the wpcom_self_test. We only need one of these.
282
	 *
283
	 * @return array Test results.
284
	 */
285
	protected function test__wpcom_connection_test() {
286
		$name = __FUNCTION__;
287
288
		$status = new Status();
289 View Code Duplication
		if ( ! \Jetpack::is_active() || $status->is_development_mode() || $status->is_staging_site() || ! $this->pass ) {
290
			return self::skipped_test( $name );
291
		}
292
293
		add_filter( 'http_request_timeout', array( 'Automattic\Jetpack\Debugger\Tests', 'increase_timeout' ) );
294
		$response = Client::wpcom_json_api_request_as_blog(
295
			sprintf( '/jetpack-blogs/%d/test-connection', \Jetpack_Options::get_option( 'id' ) ),
296
			Client::WPCOM_JSON_API_VERSION
297
		);
298
		remove_filter( 'http_request_timeout', array( 'Automattic\Jetpack\Debugger\Tests', 'increase_timeout' ) );
299
300 View Code Duplication
		if ( is_wp_error( $response ) ) {
301
			/* translators: %1$s is the error code, %2$s is the error message */
302
			$message = sprintf( __( 'Connection test failed (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() );
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
303
			return self::failing_test( $name, $message );
304
		}
305
306
		$body = wp_remote_retrieve_body( $response );
307
		if ( ! $body ) {
308
			$message = __( 'Connection test failed (empty response body)', 'jetpack' ) . wp_remote_retrieve_response_code( $response );
309
			return self::failing_test( $name, $message );
310
		}
311
312
		if ( 404 === wp_remote_retrieve_response_code( $response ) ) {
313
			return self::skipped_test( $name, __( 'The WordPress.com API returned a 404 error.', 'jetpack' ) );
314
		}
315
316
		$result       = json_decode( $body );
317
		$is_connected = (bool) $result->connected;
318
		$message      = $result->message . ': ' . wp_remote_retrieve_response_code( $response );
319
320
		if ( $is_connected ) {
321
			return self::passing_test( $name );
322
		} else {
323
			return self::failing_test( $name, $message );
324
		}
325
	}
326
327
	/**
328
	 * Tests the port number to ensure it is an expected value.
329
	 *
330
	 * We expect that sites on be on one of:
331
	 * port 80,
332
	 * port 443 (https sites only),
333
	 * the value of JETPACK_SIGNATURE__HTTP_PORT,
334
	 * unless the site is intentionally on a different port (e.g. example.com:8080 is the site's URL).
335
	 *
336
	 * If the value isn't one of those and the site's URL doesn't include a port, then the signature verification will fail.
337
	 *
338
	 * This happens most commonly on sites with reverse proxies, so the edge (e.g. Varnish) is running on 80/443, but nginx
339
	 * or Apache is responding internally on a different port (e.g. 81).
340
	 *
341
	 * @return array Test results
342
	 */
343
	protected function test__server_port_value() {
344
		$name = __FUNCTION__;
345
		if ( ! isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) && ! isset( $_SERVER['SERVER_PORT'] ) ) {
346
			$message = 'The server port values are not defined. This is most common when running PHP via a CLI.';
347
			return self::skipped_test( $name, $message );
348
		}
349
		$site_port   = wp_parse_url( home_url(), PHP_URL_PORT );
350
		$server_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? (int) $_SERVER['HTTP_X_FORWARDED_PORT'] : (int) $_SERVER['SERVER_PORT'];
351
		$http_ports  = array( 80 );
352
		$https_ports = array( 80, 443 );
353
354
		if ( defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ) {
355
			$http_ports[] = JETPACK_SIGNATURE__HTTP_PORT;
356
		}
357
358
		if ( defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ) {
359
			$https_ports[] = JETPACK_SIGNATURE__HTTPS_PORT;
360
		}
361
362
		if ( $site_port ) {
363
			return self::skipped_test( $name ); // Not currently testing for this situation.
364
		}
365
366
		if ( is_ssl() && in_array( $server_port, $https_ports, true ) ) {
367
			return self::passing_test( $name );
368
		} elseif ( in_array( $server_port, $http_ports, true ) ) {
369
			return self::passing_test( $name );
370
		} else {
371
			if ( is_ssl() ) {
372
				$needed_constant = 'JETPACK_SIGNATURE__HTTPS_PORT';
373
			} else {
374
				$needed_constant = 'JETPACK_SIGNATURE__HTTP_PORT';
375
			}
376
			$message    = __( 'The server port value is unexpected.', 'jetpack' );
377
			$resolution = __( 'Try adding the following to your wp-config.php file:', 'jetpack' ) . " define( '$needed_constant', $server_port );";
378
			return self::failing_test( $name, $message, $resolution );
379
		}
380
	}
381
382
	/**
383
	 * Calls to WP.com to run the connection diagnostic testing suite.
384
	 *
385
	 * Intentionally added last as it will be skipped if any local failed conditions exist.
386
	 *
387
	 * @since 7.1.0
388
	 * @since 7.9.0 Timeout waiting for a WP.com response no longer fails the test. Test is marked skipped instead.
389
	 *
390
	 * @return array Test results.
391
	 */
392
	protected function last__wpcom_self_test() {
393
		$name = 'test__wpcom_self_test';
394
395
		$status = new Status();
396 View Code Duplication
		if ( ! \Jetpack::is_active() || $status->is_development_mode() || $status->is_staging_site() || ! $this->pass ) {
397
			return self::skipped_test( $name );
398
		}
399
400
		$self_xml_rpc_url = site_url( 'xmlrpc.php' );
401
402
		$testsite_url = Connection_Utils::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
403
404
		add_filter( 'http_request_timeout', array( 'Automattic\Jetpack\Debugger\Tests', 'increase_timeout' ) );
405
406
		$response = wp_remote_get( $testsite_url . $self_xml_rpc_url );
407
408
		remove_filter( 'http_request_timeout', array( 'Automattic\Jetpack\Debugger\Tests', 'increase_timeout' ) );
409
410
		$error_msg = wp_kses(
411
			sprintf(
412
				/* translators: Placeholder is a link to site's Jetpack debug page. */
413
				__(
414
					'<a target="_blank" rel="noopener noreferrer" href="%s">Visit the Jetpack.com debug page</a> for more information or <a target="_blank" rel="noopener noreferrer" href="https://jetpack.com/contact-support/">contact support</a>.',
415
					'jetpack'
416
				),
417
				esc_url( add_query_arg( 'url', rawurlencode( site_url() ), 'https://jetpack.com/support/debug/' ) )
418
			),
419
			array(
420
				'a' => array(
421
					'href'   => array(),
422
					'target' => array(),
423
					'rel'    => array(),
424
				),
425
			)
426
		);
427
428
		if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
429
			return self::passing_test( $name );
430
		} elseif ( is_wp_error( $response ) && false !== strpos( $response->get_error_message(), 'cURL error 28' ) ) { // Timeout.
431
			return self::skipped_test( $name, __( 'The test timed out which may sometimes indicate a failure or may be a false failure.', 'jetpack' ) );
432
		} else {
433
			return self::failing_test( $name, __( 'Jetpack.com detected an error on the WPcom Self Test.', 'jetpack' ), $error_msg );
434
		}
435
	}
436
}
437