Completed
Push — update/deps ( 2890a5 )
by
unknown
08:31 queued 02:06
created

Jetpack_Cxn_Tests::test__check_if_connected()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 3
Ratio 25 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 3
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Collection of tests to run on the Jetpack connection locally.
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Connection\Client;
9
10
/**
11
 * Class Jetpack_Cxn_Tests contains all of the actual tests.
12
 */
13
class Jetpack_Cxn_Tests extends Jetpack_Cxn_Test_Base {
14
15
	/**
16
	 * Jetpack_Cxn_Tests constructor.
17
	 */
18
	public function __construct() {
19
		parent::__construct();
20
21
		$methods = get_class_methods( 'Jetpack_Cxn_Tests' );
22
23
		foreach ( $methods as $method ) {
24
			if ( false === strpos( $method, 'test__' ) ) {
25
				continue;
26
			}
27
			$this->add_test( array( $this, $method ), $method, 'direct' );
28
		}
29
30
		/**
31
		 * Fires after loading default Jetpack Connection tests.
32
		 *
33
		 * @since 7.1.0
34
		 */
35
		do_action( 'jetpack_connection_tests_loaded' );
36
37
		/**
38
		 * Determines if the WP.com testing suite should be included.
39
		 *
40
		 * @since 7.1.0
41
		 *
42
		 * @param bool $run_test To run the WP.com testing suite. Default true.
43
		 */
44
		if ( apply_filters( 'jetpack_debugger_run_self_test', true ) ) {
45
			/**
46
			 * Intentionally added last as it checks for an existing failure state before attempting.
47
			 * Generally, any failed location condition would result in the WP.com check to fail too, so
48
			 * we will skip it to avoid confusing error messages.
49
			 *
50
			 * Note: This really should be an 'async' test.
51
			 */
52
			$this->add_test( array( $this, 'last__wpcom_self_test' ), 'test__wpcom_self_test', 'direct' );
53
		}
54
	}
55
56
	/**
57
	 * Helper function to look up the expected master user and return the local WP_User.
58
	 *
59
	 * @return WP_User Jetpack's expected master user.
60
	 */
61
	protected function helper_retrieve_local_master_user() {
62
		$master_user = Jetpack_Options::get_option( 'master_user' );
63
		return new WP_User( $master_user );
64
	}
65
66
	/**
67
	 * Is Jetpack even connected and supposed to be talking to WP.com?
68
	 */
69
	protected function helper_is_jetpack_connected() {
70
		return ( Jetpack::is_active() && ! Jetpack::is_development_mode() );
71
	}
72
73
	/**
74
	 * Test if Jetpack is connected.
75
	 */
76
	protected function test__check_if_connected() {
77
		$name = __FUNCTION__;
78
		if ( $this->helper_is_jetpack_connected() ) {
79
			$result = self::passing_test( $name );
80 View Code Duplication
		} elseif ( Jetpack::is_development_mode() ) {
81
			$result = self::skipped_test( $name, __( 'Jetpack is in Development Mode:', 'jetpack' ) . ' ' . Jetpack::development_mode_trigger_text(), __( 'Disable development mode.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Cxn_Tests::skipped_test() has too many arguments starting with __('Disable development mode.', 'jetpack').

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.

Loading history...
82
		} else {
83
			$result = self::failing_test( $name, __( 'Jetpack is not connected.', 'jetpack' ), 'cycle_connection' );
84
		}
85
86
		return $result;
87
	}
88
89
	/**
90
	 * Test that the master user still exists on this site.
91
	 *
92
	 * @return array Test results.
93
	 */
94
	protected function test__master_user_exists_on_site() {
95
		$name = __FUNCTION__;
96
		if ( ! $this->helper_is_jetpack_connected() ) {
97
			return self::skipped_test( $name, __( 'Jetpack is not connected. No master user to check.', 'jetpack' ) ); // Skip test.
98
		}
99
		$local_user = $this->helper_retrieve_local_master_user();
100
101
		if ( $local_user->exists() ) {
102
			$result = self::passing_test( $name );
103
		} else {
104
			$result = self::failing_test( $name, __( 'The user who setup the Jetpack connection no longer exists on this site.', 'jetpack' ), 'cycle_connection' );
105
		}
106
107
		return $result;
108
	}
109
110
	/**
111
	 * Test that the master user has the manage options capability (e.g. is an admin).
112
	 *
113
	 * Generic calls from WP.com execute on Jetpack as the master user. If it isn't an admin, random things will fail.
114
	 *
115
	 * @return array Test results.
116
	 */
117
	protected function test__master_user_can_manage_options() {
118
		$name = __FUNCTION__;
119
		if ( ! $this->helper_is_jetpack_connected() ) {
120
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
121
		}
122
		$master_user = $this->helper_retrieve_local_master_user();
123
124
		if ( user_can( $master_user, 'manage_options' ) ) {
125
			$result = self::passing_test( $name );
126
		} else {
127
			/* translators: a WordPress username */
128
			$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.
129
		}
130
131
		return $result;
132
	}
133
134
	/**
135
	 * Test that the PHP's XML library is installed.
136
	 *
137
	 * While it should be installed by default, increasingly in PHP 7, some OSes require an additional php-xml package.
138
	 *
139
	 * @return array Test results.
140
	 */
141
	protected function test__xml_parser_available() {
142
		$name = __FUNCTION__;
143
		if ( function_exists( 'xml_parser_create' ) ) {
144
			$result = self::passing_test( $name );
145
		} else {
146
			$result = self::failing_test( $name, __( 'PHP XML manipluation 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' ) );
147
		}
148
149
		return $result;
150
	}
151
152
	/**
153
	 * Test that the server is able to send an outbound http communication.
154
	 *
155
	 * @return array Test results.
156
	 */
157 View Code Duplication
	protected function test__outbound_http() {
158
		$name    = __FUNCTION__;
159
		$request = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' );
160
		$code    = wp_remote_retrieve_response_code( $request );
161
162
		if ( 200 === intval( $code ) ) {
163
			$result = self::passing_test( $name );
164
		} else {
165
			$result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTP', 'jetpack' ), 'outbound_requests' );
166
		}
167
168
		return $result;
169
	}
170
171
	/**
172
	 * Test that the server is able to send an outbound https communication.
173
	 *
174
	 * @return array Test results.
175
	 */
176 View Code Duplication
	protected function test__outbound_https() {
177
		$name    = __FUNCTION__;
178
		$request = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' );
179
		$code    = wp_remote_retrieve_response_code( $request );
180
181
		if ( 200 === intval( $code ) ) {
182
			$result = self::passing_test( $name );
183
		} else {
184
			$result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTPS', 'jetpack' ), 'outbound_requests' );
185
		}
186
187
		return $result;
188
	}
189
190
	/**
191
	 * Check for an IDC.
192
	 *
193
	 * @return array Test results.
194
	 */
195
	protected function test__identity_crisis() {
196
		$name = __FUNCTION__;
197
		if ( ! $this->helper_is_jetpack_connected() ) {
198
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
199
		}
200
		$identity_crisis = Jetpack::check_identity_crisis();
201
202
		if ( ! $identity_crisis ) {
203
			$result = self::passing_test( $name );
204
		} else {
205
			$message = sprintf(
206
				/* translators: Two URLs. The first is the locally-recorded value, the second is the value as recorded on WP.com. */
207
				__( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ),
208
				$identity_crisis['home'],
209
				$identity_crisis['wpcom_home']
210
			);
211
			$result = self::failing_test( $name, $message, 'support' );
212
		}
213
		return $result;
214
	}
215
216
	/**
217
	 * Tests connection status against wp.com's test-connection endpoint
218
	 *
219
	 * @todo: Compare with the wpcom_self_test. We only need one of these.
220
	 *
221
	 * @return array Test results.
222
	 */
223
	protected function test__wpcom_connection_test() {
224
		$name = __FUNCTION__;
225
226 View Code Duplication
		if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) {
227
			return self::skipped_test( $name );
228
		}
229
230
		$response = Client::wpcom_json_api_request_as_blog(
231
			sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
232
			Client::WPCOM_JSON_API_VERSION
233
		);
234
235 View Code Duplication
		if ( is_wp_error( $response ) ) {
236
			/* translators: %1$s is the error code, %2$s is the error message */
237
			$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...
238
			return self::failing_test( $name, $message );
239
		}
240
241
		$body = wp_remote_retrieve_body( $response );
242
		if ( ! $body ) {
243
			$message = __( 'Connection test failed (empty response body)', 'jetpack' ) . wp_remote_retrieve_response_code( $response );
244
			return self::failing_test( $name, $message );
245
		}
246
247
		if ( 404 === wp_remote_retrieve_response_code( $response ) ) {
248
			return self::skipped_test( $name, __( 'The WordPress.com API returned a 404 error.', 'jetpack' ) );
249
		}
250
251
		$result       = json_decode( $body );
252
		$is_connected = (bool) $result->connected;
253
		$message      = $result->message . ': ' . wp_remote_retrieve_response_code( $response );
254
255
		if ( $is_connected ) {
256
			return self::passing_test( $name );
257
		} else {
258
			return self::failing_test( $name, $message );
259
		}
260
	}
261
262
	/**
263
	 * Tests the port number to ensure it is an expected value.
264
	 *
265
	 * We expect that sites on be on one of:
266
	 * port 80,
267
	 * port 443 (https sites only),
268
	 * the value of JETPACK_SIGNATURE__HTTP_PORT,
269
	 * unless the site is intentionally on a different port (e.g. example.com:8080 is the site's URL).
270
	 *
271
	 * If the value isn't one of those and the site's URL doesn't include a port, then the signature verification will fail.
272
	 *
273
	 * This happens most commonly on sites with reverse proxies, so the edge (e.g. Varnish) is running on 80/443, but nginx
274
	 * or Apache is responding internally on a different port (e.g. 81).
275
	 *
276
	 * @return array Test results
277
	 */
278
	protected function test__server_port_value() {
279
		$name = __FUNCTION__;
280
		if ( ! isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) && ! isset( $_SERVER['SERVER_PORT'] ) ) {
281
			$message = 'The server port values are not defined. This is most common when running PHP via a CLI.';
282
			return self::skipped_test( $name, $message );
283
		}
284
		$site_port   = wp_parse_url( home_url(), PHP_URL_PORT );
285
		$server_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? (int) $_SERVER['HTTP_X_FORWARDED_PORT'] : (int) $_SERVER['SERVER_PORT'];
286
		$http_ports  = array( 80 );
287
		$https_ports = array( 80, 443 );
288
289
		if ( defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ) {
290
			$http_ports[] = JETPACK_SIGNATURE__HTTP_PORT;
291
		}
292
293
		if ( defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ) {
294
			$https_ports[] = JETPACK_SIGNATURE__HTTPS_PORT;
295
		}
296
297
		if ( $site_port ) {
298
			return self::skipped_test( $name ); // Not currently testing for this situation.
299
		}
300
301
		if ( is_ssl() && in_array( $server_port, $https_ports, true ) ) {
302
			return self::passing_test( $name );
303
		} elseif ( in_array( $server_port, $http_ports, true ) ) {
304
			return self::passing_test( $name );
305
		} else {
306
			if ( is_ssl() ) {
307
				$needed_constant = 'JETPACK_SIGNATURE__HTTPS_PORT';
308
			} else {
309
				$needed_constant = 'JETPACK_SIGNATURE__HTTP_PORT';
310
			}
311
			$message    = __( 'The server port value is unexpected.', 'jetpack' );
312
			$resolution = __( 'Try adding the following to your wp-config.php file:', 'jetpack' ) . " define( '$needed_constant', $server_port );";
313
			return self::failing_test( $name, $message, $resolution );
314
		}
315
	}
316
317
	/**
318
	 * Calls to WP.com to run the connection diagnostic testing suite.
319
	 *
320
	 * Intentionally added last as it will be skipped if any local failed conditions exist.
321
	 *
322
	 * @since 7.1.0
323
	 * @since 7.9.0 Timeout waiting for a WP.com response no longer fails the test. Test is marked skipped instead.
324
	 *
325
	 * @return array Test results.
326
	 */
327
	protected function last__wpcom_self_test() {
328
		$name = 'test__wpcom_self_test';
329 View Code Duplication
		if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) {
330
			return self::skipped_test( $name );
331
		}
332
333
		$self_xml_rpc_url = site_url( 'xmlrpc.php' );
334
335
		$testsite_url = Jetpack::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
336
337
		add_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
338
339
		$response = wp_remote_get( $testsite_url . $self_xml_rpc_url );
340
341
		remove_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
342
343
		if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
344
			return self::passing_test( $name );
345
		} elseif ( is_wp_error( $response ) && false !== strpos( $response->get_error_message(), 'cURL error 28' ) ) { // Timeout.
346
			return self::skipped_test( $name, __( 'The test timed out which may sometimes indicate a failure or may be a false failure.', 'jetpack' ) );
347
		} else {
348
			return self::failing_test( $name, __( 'Jetpack.com detected an error on the WPcom Self Test.', 'jetpack' ), __( 'Visit the Jetpack.com debugging page for more information or contact support.', 'jetpack' ) ); // @todo direct links.
349
		}
350
	}
351
}
352