Completed
Push — update/phpunit-php-8 ( e34c58...05aa04 )
by
unknown
137:27 queued 129:12
created

Test_Status::test_is_offline_mode_localhost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Tests for Automattic\Jetpack\Status methods
4
 *
5
 * @package automattic/jetpack-status
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Brain\Monkey;
11
use Brain\Monkey\Filters;
12
use Brain\Monkey\Functions;
13
use PHPUnit\Framework\TestCase;
14
15
/**
16
 * Status test suite.
17
 */
18
class Test_Status extends TestCase {
19
	/**
20
	 * Default site URL.
21
	 *
22
	 * @var string
23
	 */
24
	private $site_url = 'https://yourjetpack.blog';
25
26
	/**
27
	 * Status instance.
28
	 *
29
	 * @var Automattic\Jetpack\Status
30
	 */
31
	private $status_obj;
32
33
	/**
34
	 * Mocked constants.
35
	 *
36
	 * @var array
37
	 */
38
	private $mocked_constants = array();
39
40
	/**
41
	 * Setup before running any of the tests.
42
	 *
43
	 * @beforeClass
44
	 */
45
	public static function set_up_before_class() {
46
		if ( ! defined( 'HOUR_IN_SECONDS' ) ) {
47
			define( 'HOUR_IN_SECONDS', 60 * 60 );
48
		}
49
	}
50
51
	/**
52
	 * Test setup.
53
	 *
54
	 * @before
55
	 */
56
	public function set_up() {
57
		Monkey\setUp();
58
59
		// Set defaults for Core functionality.
60
		Functions\when( 'site_url' )->justReturn( $this->site_url );
61
		Functions\when( 'wp_get_environment_type' )->justReturn( 'production' );
62
		Functions\when( 'wp_parse_url' )->alias( 'parse_url' );
63
		Functions\expect( 'defined' )->andReturnUsing(
64
			function ( $const ) {
65
				return array_key_exists( $const, $this->mocked_constants ) ? true : defined( $const );
66
			}
67
		);
68
		Functions\expect( 'constant' )->andReturnUsing(
69
			function ( $const ) {
70
				return array_key_exists( $const, $this->mocked_constants ) ? $this->mocked_constants[ $const ] : constant( $const );
71
			}
72
		);
73
74
		$this->status_obj = new Status();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Automattic\Jetpack\Status() of type object<Automattic\Jetpack\Status> is incompatible with the declared type object<Automattic\Jetpac...omattic\Jetpack\Status> of property $status_obj.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
	}
76
77
	/**
78
	 * Test teardown.
79
	 *
80
	 * @after
81
	 */
82
	public function tear_down() {
83
		Monkey\tearDown();
84
	}
85
86
	/**
87
	 * Test is_offline_mode when not using any filter
88
	 *
89
	 * @covers Automattic\Jetpack\Status::is_offline_mode
90
	 */
91
	public function test_is_offline_mode_default() {
92
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false );
93
94
		$this->assertFalse( $this->status_obj->is_offline_mode() );
95
	}
96
97
	/**
98
	 * Test is_offline_mode when using the jetpack_offline_mode filter
99
	 *
100
	 * @covers Automattic\Jetpack\Status::is_offline_mode
101
	 */
102
	public function test_is_offline_mode_filter_true() {
103
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( true );
104
105
		$this->assertTrue( $this->status_obj->is_offline_mode() );
106
	}
107
108
	/**
109
	 * Test when using a bool value for the jetpack_offline_mode filter.
110
	 *
111
	 * @covers Automattic\Jetpack\Status::is_offline_mode
112
	 */
113
	public function test_is_offline_mode_filter_bool() {
114
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( 0 );
115
116
		$this->assertFalse( $this->status_obj->is_offline_mode() );
117
	}
118
119
	/**
120
	 * Test when site url is localhost (dev mode on)
121
	 *
122
	 * @covers Automattic\Jetpack\Status::is_offline_mode
123
	 */
124
	public function test_is_offline_mode_localhost() {
125
		Functions\when( 'site_url' )->justReturn( 'localhost' );
126
127
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( true )->andReturn( true );
128
129
		$this->assertTrue( $this->status_obj->is_offline_mode() );
130
	}
131
132
	/**
133
	 * Test when wp_get_environment_type is local.
134
	 *
135
	 * @covers Automattic\Jetpack\Status::is_local_site
136
	 */
137
	public function test_is_local_wp_get_environment_type_local() {
138
		Functions\when( 'wp_get_environment_type' )->justReturn( 'local' );
139
140
		Filters\expectApplied( 'jetpack_is_local_site' )->once()->with( true )->andReturn( true );
141
142
		$this->assertTrue( $this->status_obj->is_local_site() );
143
	}
144
145
	/**
146
	 * Test when wp_get_environment_type is local.
147
	 *
148
	 * @covers Automattic\Jetpack\Status::is_staging_site
149
	 */
150 View Code Duplication
	public function test_is_staging_wp_get_environment_type_local() {
151
		Functions\when( 'wp_get_environment_type' )->justReturn( 'local' );
152
153
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( false )->andReturn( false );
154
155
		$this->assertFalse( $this->status_obj->is_staging_site() );
156
	}
157
158
	/**
159
	 * Test when wp_get_environment_type is staging.
160
	 *
161
	 * @covers Automattic\Jetpack\Status::is_staging_site
162
	 */
163 View Code Duplication
	public function test_is_staging_wp_get_environment_type_staging() {
164
		Functions\when( 'wp_get_environment_type' )->justReturn( 'staging' );
165
166
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( true )->andReturn( true );
167
168
		$this->assertTrue( $this->status_obj->is_staging_site() );
169
	}
170
171
	/**
172
	 * Test when wp_get_environment_type is production.
173
	 *
174
	 * @covers Automattic\Jetpack\Status::is_staging_site
175
	 */
176 View Code Duplication
	public function test_is_staging_wp_get_environment_type_production() {
177
		Functions\when( 'wp_get_environment_type' )->justReturn( 'production' );
178
179
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( false )->andReturn( false );
180
181
		$this->assertFalse( $this->status_obj->is_staging_site() );
182
	}
183
184
	/**
185
	 * Test when wp_get_environment_type is a random value.
186
	 *
187
	 * @covers Automattic\Jetpack\Status::is_staging_site
188
	 */
189 View Code Duplication
	public function test_is_staging_wp_get_environment_type_random() {
190
		Functions\when( 'wp_get_environment_type' )->justReturn( 'random_string' );
191
192
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( true )->andReturn( true );
193
194
		$this->assertTrue( $this->status_obj->is_staging_site() ); // We assume a site is a staging site for any non-local or non-production value.
195
	}
196
197
	/**
198
	 * Test when using the constant to set dev mode
199
	 *
200
	 * @covers Automattic\Jetpack\Status::is_offline_mode
201
	 *
202
	 * @runInSeparateProcess
203
	 */
204
	public function test_is_offline_mode_constant() {
205
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( true )->andReturn( true );
206
		$this->mocked_constants['\\JETPACK_DEV_DEBUG'] = true;
207
208
		$this->assertTrue( $this->status_obj->is_offline_mode() );
209
	}
210
211
	/**
212
	 * Test for is_multi_network with a single site
213
	 *
214
	 * @covers Automattic\Jetpack\Status::is_multi_network
215
	 */
216
	public function test_is_multi_network_not_multisite() {
217
		Functions\when( 'is_multisite' )->justReturn( false );
218
219
		$this->assertFalse( $this->status_obj->is_multi_network() );
220
	}
221
222
	/**
223
	 * Test is_multi_network with a multisite install
224
	 *
225
	 * @covers Automattic\Jetpack\Status::is_multi_network
226
	 */
227
	public function test_is_multi_network_when_single_network() {
228
		$this->mock_wpdb_get_var( 1 );
229
		Functions\when( 'is_multisite' )->justReturn( true );
230
231
		$this->assertFalse( $this->status_obj->is_multi_network() );
232
233
		$this->clean_mock_wpdb_get_var();
234
	}
235
236
	/**
237
	 * Test is_multi_network when multiple networks
238
	 *
239
	 * @covers Automattic\Jetpack\Status::is_multi_network
240
	 */
241 View Code Duplication
	public function test_is_multi_network_when_multiple_networks() {
242
		$this->mock_wpdb_get_var( 2 );
243
		Functions\when( 'is_multisite' )->justReturn( true );
244
245
		$this->assertTrue( $this->status_obj->is_multi_network() );
246
247
		$this->clean_mock_wpdb_get_var();
248
	}
249
250
	/**
251
	 * Test cached is_single_user_site
252
	 *
253
	 * @covers Automattic\Jetpack\Status::is_single_user_site
254
	 */
255
	public function test_is_single_user_site_with_transient() {
256
		$this->mock_wpdb_get_var( 3 );
257
		Functions\when( 'get_transient' )->justReturn( 1 );
258
259
		$this->assertTrue( $this->status_obj->is_single_user_site() );
260
261
		$this->clean_mock_wpdb_get_var();
262
	}
263
264
	/**
265
	 * Test is_single_user_site
266
	 *
267
	 * @covers Automattic\Jetpack\Status::is_single_user_site
268
	 */
269 View Code Duplication
	public function test_is_single_user_site_with_one_user() {
270
		$this->mock_wpdb_get_var( 1 );
271
		Functions\when( 'get_transient' )->justReturn( false );
272
		Functions\when( 'set_transient' )->justReturn( true );
273
274
		$this->assertTrue( $this->status_obj->is_single_user_site() );
275
276
		$this->clean_mock_wpdb_get_var();
277
	}
278
279
	/**
280
	 * Test is_single_user_site with multiple users
281
	 *
282
	 * @covers Automattic\Jetpack\Status::is_single_user_site
283
	 */
284 View Code Duplication
	public function test_is_single_user_site_with_multiple_users() {
285
		$this->mock_wpdb_get_var( 3 );
286
		Functions\when( 'get_transient' )->justReturn( false );
287
		Functions\when( 'set_transient' )->justReturn( true );
288
289
		$this->assertFalse( $this->status_obj->is_single_user_site() );
290
291
		$this->clean_mock_wpdb_get_var();
292
	}
293
294
	/**
295
	 * Mock $wpdb->get_var() and make it return a certain value.
296
	 *
297
	 * @param mixed $return_value  Return value of the function.
298
	 *
299
	 * PHPUnit\Framework\MockObject\MockObject The mock object.
300
	 */
301
	protected function mock_wpdb_get_var( $return_value = null ) {
302
		global $wpdb;
303
304
		$wpdb = $this->getMockBuilder( 'Mock_wpdb' ) // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
305
					->setMockClassName( 'wpdb' )
306
					->setMethods( array( 'get_var' ) )
307
					->getMock();
308
		$wpdb->method( 'get_var' )
309
			->willReturn( $return_value );
310
311
		$wpdb->prefix   = 'wp_';
312
		$wpdb->site     = 'wp_site';
313
		$wpdb->usermeta = 'wp_usermeta';
314
	}
315
316
	/**
317
	 * Clean up the existing $wpdb->get_var() mock.
318
	 */
319
	protected function clean_mock_wpdb_get_var() {
320
		global $wpdb;
321
		unset( $wpdb );
322
	}
323
324
	/**
325
	 * Tests known staging sites.
326
	 *
327
	 * @dataProvider get_is_staging_site_known_hosting_providers_data
328
	 * @covers Automattic\Jetpack\Status::is_staging_site
329
	 *
330
	 * @param string $site_url Site URL.
331
	 * @param bool   $expected Expected return.
332
	 */
333
	public function test_is_staging_site_for_known_hosting_providers( $site_url, $expected ) {
334
		Functions\when( 'site_url' )->justReturn( $site_url );
335
		$result = $this->status_obj->is_staging_site();
336
		$this->assertSame(
337
			$expected,
338
			$result,
339
			sprintf(
340
				'Expected %1$s to return %2$s for is_staging_site()',
341
				$site_url,
342
				var_export( $expected, 1 ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
343
			)
344
		);
345
	}
346
347
	/**
348
	 * Known hosting providers.
349
	 *
350
	 * Including a couple of general RegEx checks (subdir, ending slash).
351
	 *
352
	 * @return array
353
	 */
354
	public function get_is_staging_site_known_hosting_providers_data() {
355
		return array(
356
			'wpengine'              => array(
357
				'http://bjk.staging.wpengine.com',
358
				true,
359
			),
360
			'kinsta'                => array(
361
				'http://test.staging.kinsta.com',
362
				true,
363
			),
364
			'dreampress'            => array(
365
				'http://ebinnion.stage.site',
366
				true,
367
			),
368
			'newspack'              => array(
369
				'http://test.newspackstaging.com',
370
				true,
371
			),
372
			'wpengine_subdirectory' => array(
373
				'http://bjk.staging.wpengine.com/staging',
374
				true,
375
			),
376
			'wpengine_endslash'     => array(
377
				'http://bjk.staging.wpengine.com/',
378
				true,
379
			),
380
			'not_a_staging_site'    => array(
381
				'http://staging.wpengine.com.example.com/',
382
				false,
383
			),
384
		);
385
	}
386
387
	/**
388
	 * Tests known local development sites.
389
	 *
390
	 * @dataProvider get_is_local_site_known_tld
391
	 *
392
	 * @param string $site_url Site URL.
393
	 * @param bool   $expected_response Expected response.
394
	 */
395
	public function test_is_local_site_for_known_tld( $site_url, $expected_response ) {
396
		Functions\when( 'site_url' )->justReturn( $site_url );
397
		$result = $this->status_obj->is_local_site();
398
		$this->assertEquals(
399
			$expected_response,
400
			$result,
401
			sprintf(
402
				'Expected %1$s to return %2$s for is_local_site()',
403
				$site_url,
404
				$expected_response
405
			)
406
		);
407
	}
408
409
	/**
410
	 * Known hosting providers.
411
	 *
412
	 * @return array
413
	 */
414
	public function get_is_local_site_known_tld() {
415
		return array(
416
			'vvv'            => array(
417
				'http://jetpack.test',
418
				true,
419
			),
420
			'docksal'        => array(
421
				'http://jetpack.docksal',
422
				true,
423
			),
424
			'serverpress'    => array(
425
				'http://jetpack.dev.cc',
426
				true,
427
			),
428
			'lando'          => array(
429
				'http://jetpack.lndo.site',
430
				true,
431
			),
432
			'test_subdomain' => array(
433
				'https://test.jetpack.com',
434
				false,
435
			),
436
			'test_in_domain' => array(
437
				'https://jetpack.test.jetpack.com',
438
				false,
439
			),
440
		);
441
	}
442
443
	/**
444
	 * Tests for site_suffix().
445
	 *
446
	 * @covers Automattic\Jetpack\Status::get_site_suffix
447
	 * @dataProvider get_site_suffix_examples
448
	 *
449
	 * @param string $site     Given site URL.
450
	 * @param string $expected Site suffix.
451
	 */
452
	public function test_jetpack_get_site_suffix( $site, $expected ) {
453
		Functions\when( 'home_url' )->justReturn( $this->site_url );
454
		$suffix = $this->status_obj->get_site_suffix( $site );
455
456
		$this->assertSame( $expected, $suffix );
457
	}
458
459
	/**
460
	 * Examples of sites passed to get_site_suffix
461
	 *
462
	 * @covers Automattic\Jetpack\Status::get_site_suffix
463
	 */
464
	public function get_site_suffix_examples() {
465
		return array(
466
			'no_site_home_url' => array(
467
				'',
468
				'yourjetpack.blog',
469
			),
470
			'tld'              => array(
471
				'https://example.org',
472
				'example.org',
473
			),
474
			'subdomain'        => array(
475
				'https://borussia.dortmund.example.org',
476
				'borussia.dortmund.example.org',
477
			),
478
			'subfolder'        => array(
479
				'https://example.org/borussia-dortmund',
480
				'example.org::borussia-dortmund',
481
			),
482
			'ip'               => array(
483
				'127.0.0.1',
484
				'127.0.0.1',
485
			),
486
			'no_tld'           => array(
487
				'https://localhost',
488
				'localhost',
489
			),
490
			'double_domain'    => array(
491
				'https://example.org/http://example.com',
492
				'example.org::http:::::example.com',
493
			),
494
		);
495
	}
496
}
497