Completed
Push — fix/12470 ( 0cef1a )
by
unknown
06:42
created

Jetpack_Cxn_Tests::test__identity_crisis()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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