Completed
Push — update/ad-gutenblock-load ( 0c433b...289acb )
by
unknown
12:28 queued 05:20
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
/**
9
 * Class Jetpack_Cxn_Tests contains all of the actual tests.
10
 */
11
class Jetpack_Cxn_Tests extends Jetpack_Cxn_Test_Base {
12
13
	/**
14
	 * Jetpack_Cxn_Tests constructor.
15
	 */
16
	public function __construct() {
17
		parent::__construct();
18
19
		$methods = get_class_methods( 'Jetpack_Cxn_Tests' );
20
21
		foreach ( $methods as $method ) {
22
			if ( false === strpos( $method, 'test__' ) ) {
23
				continue;
24
			}
25
			$this->add_test( array( $this, $method ) );
26
		}
27
28
		/**
29
		 * Fires after loading default Jetpack Connection tests.
30
		 *
31
		 * @since 7.1.0
32
		 */
33
		do_action( 'jetpack_connection_tests_loaded' );
34
35
		/**
36
		 * Determines if the WP.com testing suite should be included.
37
		 *
38
		 * @since 7.1.0
39
		 *
40
		 * @param bool $run_test To run the WP.com testing suite. Default true.
41
		 */
42
		if ( apply_filters( 'jetpack_debugger_run_self_test', true ) ) {
43
			/**
44
			 * Intentionally added last as it checks for an existing failure state before attempting.
45
			 * Generally, any failed location condition would result in the WP.com check to fail too, so
46
			 * we will skip it to avoid confusing error messages.
47
			 */
48
			$this->add_test( array( $this, 'last__wpcom_self_test' ) );
49
		}
50
	}
51
52
	/**
53
	 * Helper function to look up the expected master user and return the local WP_User.
54
	 *
55
	 * @return WP_User Jetpack's expected master user.
56
	 */
57
	protected function helper_retrieve_local_master_user() {
58
		$master_user = Jetpack_Options::get_option( 'master_user' );
59
		return new WP_User( $master_user );
60
	}
61
62
	/**
63
	 * Is Jetpack even connected and supposed to be talking to WP.com?
64
	 */
65
	protected function helper_is_jetpack_connected() {
66
		return ( Jetpack::is_active() && ! Jetpack::is_development_mode() );
67
	}
68
69
	/**
70
	 * Test if Jetpack is connected.
71
	 */
72
	protected function test__check_if_connected() {
73
		$name = __FUNCTION__;
74
		if ( $this->helper_is_jetpack_connected() ) {
75
			$result = self::passing_test( $name );
76 View Code Duplication
		} elseif ( Jetpack::is_development_mode() ) {
77
			$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...
78
		} else {
79
			$result = self::failing_test( $name, __( 'Jetpack is not connected.', 'jetpack' ), 'cycle_connection' );
80
		}
81
82
		return $result;
83
	}
84
85
	/**
86
	 * Test that the master user still exists on this site.
87
	 *
88
	 * @return array Test results.
89
	 */
90
	protected function test__master_user_exists_on_site() {
91
		$name = __FUNCTION__;
92
		if ( ! $this->helper_is_jetpack_connected() ) {
93
			return self::skipped_test( $name, __( 'Jetpack is not connected. No master user to check.', 'jetpack' ) ); // Skip test.
94
		}
95
		$local_user = $this->helper_retrieve_local_master_user();
96
97
		if ( $local_user->exists() ) {
98
			$result = self::passing_test( $name );
99
		} else {
100
			$result = self::failing_test( $name, __( 'The user who setup the Jetpack connection no longer exists on this site.', 'jetpack' ), 'cycle_connection' );
101
		}
102
103
		return $result;
104
	}
105
106
	/**
107
	 * Test that the master user has the manage options capability (e.g. is an admin).
108
	 *
109
	 * Generic calls from WP.com execute on Jetpack as the master user. If it isn't an admin, random things will fail.
110
	 *
111
	 * @return array Test results.
112
	 */
113
	protected function test__master_user_can_manage_options() {
114
		$name = __FUNCTION__;
115
		if ( ! $this->helper_is_jetpack_connected() ) {
116
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
117
		}
118
		$master_user = $this->helper_retrieve_local_master_user();
119
120
		if ( user_can( $master_user, 'manage_options' ) ) {
121
			$result = self::passing_test( $name );
122
		} else {
123
			/** translators: a WordPress username */
124
			$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.
125
		}
126
127
		return $result;
128
	}
129
130
	/**
131
	 * Test that the PHP's XML library is installed.
132
	 *
133
	 * While it should be installed by default, increasingly in PHP 7, some OSes require an additional php-xml package.
134
	 *
135
	 * @return array Test results.
136
	 */
137
	protected function test__xml_parser_available() {
138
		$name = __FUNCTION__;
139
		if ( function_exists( 'xml_parser_create' ) ) {
140
			$result = self::passing_test( $name );
141
		} else {
142
			$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' ) );
143
		}
144
145
		return $result;
146
	}
147
148
	/**
149
	 * Test that the server is able to send an outbound http communication.
150
	 *
151
	 * @return array Test results.
152
	 */
153 View Code Duplication
	protected function test__outbound_http() {
154
		$name    = __FUNCTION__;
155
		$request = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' );
156
		$code    = wp_remote_retrieve_response_code( $request );
157
158
		if ( 200 === intval( $code ) ) {
159
			$result = self::passing_test( $name );
160
		} else {
161
			$result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTP', 'jetpack' ), 'outbound_requests' );
162
		}
163
164
		return $result;
165
	}
166
167
	/**
168
	 * Test that the server is able to send an outbound https communication.
169
	 *
170
	 * @return array Test results.
171
	 */
172 View Code Duplication
	protected function test__outbound_https() {
173
		$name    = __FUNCTION__;
174
		$request = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' );
175
		$code    = wp_remote_retrieve_response_code( $request );
176
177
		if ( 200 === intval( $code ) ) {
178
			$result = self::passing_test( $name );
179
		} else {
180
			$result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTPS', 'jetpack' ), 'outbound_requests' );
181
		}
182
183
		return $result;
184
	}
185
186
	/**
187
	 * Check for an IDC.
188
	 *
189
	 * @return array Test results.
190
	 */
191
	protected function test__identity_crisis() {
192
		$name = __FUNCTION__;
193
		if ( ! $this->helper_is_jetpack_connected() ) {
194
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
195
		}
196
		$identity_crisis = Jetpack::check_identity_crisis();
197
198
		if ( ! $identity_crisis ) {
199
			$result = self::passing_test( $name );
200
		} else {
201
			$message = sprintf(
202
				/* translators: Two URLs. The first is the locally-recorded value, the second is the value as recorded on WP.com. */
203
				__( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ),
204
				$identity_crisis['home'],
205
				$identity_crisis['wpcom_home']
206
			);
207
			$result = self::failing_test( $name, $message, 'support' );
208
		}
209
		return $result;
210
	}
211
212
	/**
213
	 * Tests connection status against wp.com's test-connection endpoint
214
	 *
215
	 * @todo: Compare with the wpcom_self_test. We only need one of these.
216
	 *
217
	 * @return array Test results.
218
	 */
219
	protected function test__wpcom_connection_test() {
220
		$name = __FUNCTION__;
221
222 View Code Duplication
		if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) {
223
			return self::skipped_test( $name );
224
		}
225
226
		$response = Jetpack_Client::wpcom_json_api_request_as_blog(
227
			sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
228
			Jetpack_Client::WPCOM_JSON_API_VERSION
229
		);
230
231 View Code Duplication
		if ( is_wp_error( $response ) ) {
232
			/* translators: %1$s is the error code, %2$s is the error message */
233
			$message = sprintf( __( 'Connection test failed (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() );
234
			return self::failing_test( $name, $message );
235
		}
236
237
		$body = wp_remote_retrieve_body( $response );
238
		if ( ! $body ) {
239
			$message = __( 'Connection test failed (empty response body)', 'jetpack' ) . wp_remote_retrieve_response_code( $response );
240
			return self::failing_test( $name, $message );
241
		}
242
243
		$result       = json_decode( $body );
244
		$is_connected = (bool) $result->connected;
245
		$message      = $result->message . wp_remote_retrieve_response_code( $response );
246
247
		if ( $is_connected ) {
248
			return self::passing_test( $name );
249
		} else {
250
			return self::failing_test( $name, $message );
251
		}
252
	}
253
254
	/**
255
	 * Tests the port number to ensure it is an expected value.
256
	 *
257
	 * We expect that sites on be on one of:
258
	 * port 80,
259
	 * port 443 (https sites only),
260
	 * the value of JETPACK_SIGNATURE__HTTP_PORT,
261
	 * unless the site is intentionally on a different port (e.g. example.com:8080 is the site's URL).
262
	 *
263
	 * If the value isn't one of those and the site's URL doesn't include a port, then the signature verification will fail.
264
	 *
265
	 * This happens most commonly on sites with reverse proxies, so the edge (e.g. Varnish) is running on 80/443, but nginx
266
	 * or Apache is responding internally on a different port (e.g. 81).
267
	 *
268
	 * @return array Test results
269
	 */
270
	protected function test__server_port_value() {
271
		$name = __FUNCTION__;
272
		if ( ! isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) && ! isset( $_SERVER['SERVER_PORT'] ) ) {
273
			$message = 'The server port values are not defined. This is most common when running PHP via a CLI.';
274
			return self::skipped_test( $name, $message );
275
		}
276
		$site_port   = wp_parse_url( home_url(), PHP_URL_PORT );
277
		$server_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? (int) $_SERVER['HTTP_X_FORWARDED_PORT'] : (int) $_SERVER['SERVER_PORT'];
278
		$http_ports  = array( 80 );
279
		$https_ports = array( 80, 443 );
280
281
		if ( defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ) {
282
			$http_ports[] = JETPACK_SIGNATURE__HTTP_PORT;
283
		}
284
285
		if ( defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ) {
286
			$https_ports[] = JETPACK_SIGNATURE__HTTPS_PORT;
287
		}
288
289
		if ( $site_port ) {
290
			return self::skipped_test( $name ); // Not currently testing for this situation.
291
		}
292
293
		if ( is_ssl() && in_array( $server_port, $https_ports, true ) ) {
294
			return self::passing_test( $name );
295
		} elseif ( in_array( $server_port, $http_ports, true ) ) {
296
			return self::passing_test( $name );
297
		} else {
298
			if ( is_ssl() ) {
299
				$needed_constant = 'JETPACK_SIGNATURE__HTTPS_PORT';
300
			} else {
301
				$needed_constant = 'JETPACK_SIGNATURE__HTTP_PORT';
302
			}
303
			$message    = __( 'The server port value is unexpected.', 'jetpack' );
304
			$resolution = __( 'Try adding the following to your wp-config.php file:', 'jetpack' ) . " define( '$needed_constant', $server_port );";
305
			return self::failing_test( $name, $message, $resolution );
306
		}
307
	}
308
309
	/**
310
	 * Calls to WP.com to run the connection diagnostic testing suite.
311
	 *
312
	 * Intentionally added last as it will be skipped if any local failed conditions exist.
313
	 *
314
	 * @return array Test results.
315
	 */
316
	protected function last__wpcom_self_test() {
317
		$name = 'test__wpcom_self_test';
318 View Code Duplication
		if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) {
319
			return self::skipped_test( $name );
320
		}
321
322
		$self_xml_rpc_url = site_url( 'xmlrpc.php' );
323
324
		$testsite_url = Jetpack::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
325
326
		add_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
327
328
		$response = wp_remote_get( $testsite_url . $self_xml_rpc_url );
329
330
		remove_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
331
332
		if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
333
			return self::passing_test( $name );
334
		} else {
335
			return self::failing_test( $name, __( 'Jetpack.com detected an error.', 'jetpack' ), __( 'Visit the Jetpack.com debugging page for more information or contact support.', 'jetpack' ) ); // @todo direct links.
336
		}
337
	}
338
}
339