Completed
Push — renovate/debug-4.x ( e10916...91e0c8 )
by
unknown
86:28 queued 77:02
created

Test_Status::get_site_suffix_examples()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 32
rs 9.408
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 phpmock\Mock;
14
use phpmock\MockBuilder;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * Status test suite.
19
 */
20
class Test_Status extends TestCase {
21
	/**
22
	 * Default site URL.
23
	 *
24
	 * @var string
25
	 */
26
	private $site_url = 'https://yourjetpack.blog';
27
28
	/**
29
	 * Status instance.
30
	 *
31
	 * @var Automattic\Jetpack\Status
32
	 */
33
	private $status_obj;
34
35
	/**
36
	 * Setup before running any of the tests.
37
	 */
38
	public static function setUpBeforeClass() {
39
		if ( ! defined( 'HOUR_IN_SECONDS' ) ) {
40
			define( 'HOUR_IN_SECONDS', 60 * 60 );
41
		}
42
	}
43
44
	/**
45
	 * Test setup.
46
	 */
47
	public function setUp() {
48
		parent::setUp();
49
		Monkey\setUp();
50
51
		// Set defaults for Core functionality.
52
		Functions\when( 'site_url' )->justReturn( $this->site_url );
53
		Functions\when( 'wp_get_environment_type' )->justReturn( 'production' );
54
		Functions\when( 'wp_parse_url' )->alias( 'parse_url' );
55
56
		$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...
57
	}
58
59
	/**
60
	 * Test teardown.
61
	 */
62
	public function tearDown() {
63
		// Call Monkey\tearDown(); here, but the following function takes care of it for now.
64
		Mock::disableAll();
65
		parent::tearDown();
66
	}
67
68
	/**
69
	 * Test is_offline_mode when not using any filter
70
	 *
71
	 * @covers Automattic\Jetpack\Status::is_offline_mode
72
	 */
73
	public function test_is_offline_mode_default() {
74
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false );
75
76
		$this->assertFalse( $this->status_obj->is_offline_mode() );
77
	}
78
79
	/**
80
	 * Test is_offline_mode when using the jetpack_offline_mode filter
81
	 *
82
	 * @covers Automattic\Jetpack\Status::is_offline_mode
83
	 */
84
	public function test_is_offline_mode_filter_true() {
85
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( true );
86
87
		$this->assertTrue( $this->status_obj->is_offline_mode() );
88
	}
89
90
	/**
91
	 * Test when using a bool value for the jetpack_offline_mode filter.
92
	 *
93
	 * @covers Automattic\Jetpack\Status::is_offline_mode
94
	 */
95
	public function test_is_offline_mode_filter_bool() {
96
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( 0 );
97
98
		$this->assertFalse( $this->status_obj->is_offline_mode() );
99
	}
100
101
	/**
102
	 * Test when site url is localhost (dev mode on)
103
	 *
104
	 * @covers Automattic\Jetpack\Status::is_offline_mode
105
	 */
106
	public function test_is_offline_mode_localhost() {
107
		Functions\when( 'site_url' )->justReturn( 'localhost' );
108
109
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false );
110
111
		$this->assertTrue( $this->status_obj->is_offline_mode() );
112
	}
113
114
	/**
115
	 * Test when wp_get_environment_type is local.
116
	 *
117
	 * @covers Automattic\Jetpack\Status::is_local_site
118
	 */
119
	public function test_is_local_wp_get_environment_type_local() {
120
		Functions\when( 'wp_get_environment_type' )->justReturn( 'local' );
121
122
		Filters\expectApplied( 'jetpack_is_local_site' )->once()->with( false )->andReturn( false );
123
124
		$this->assertTrue( $this->status_obj->is_local_site() );
125
	}
126
127
	/**
128
	 * Test when wp_get_environment_type is local.
129
	 *
130
	 * @covers Automattic\Jetpack\Status::is_staging_site
131
	 */
132 View Code Duplication
	public function test_is_staging_wp_get_environment_type_local() {
133
		Functions\when( 'wp_get_environment_type' )->justReturn( 'local' );
134
135
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( false )->andReturn( false );
136
137
		$this->assertFalse( $this->status_obj->is_staging_site() );
138
	}
139
140
	/**
141
	 * Test when wp_get_environment_type is staging.
142
	 *
143
	 * @covers Automattic\Jetpack\Status::is_staging_site
144
	 */
145 View Code Duplication
	public function test_is_staging_wp_get_environment_type_staging() {
146
		Functions\when( 'wp_get_environment_type' )->justReturn( 'staging' );
147
148
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( false )->andReturn( false );
149
150
		$this->assertTrue( $this->status_obj->is_staging_site() );
151
	}
152
153
	/**
154
	 * Test when wp_get_environment_type is production.
155
	 *
156
	 * @covers Automattic\Jetpack\Status::is_staging_site
157
	 */
158 View Code Duplication
	public function test_is_staging_wp_get_environment_type_production() {
159
		Functions\when( 'wp_get_environment_type' )->justReturn( 'production' );
160
161
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( false )->andReturn( false );
162
163
		$this->assertFalse( $this->status_obj->is_staging_site() );
164
	}
165
166
	/**
167
	 * Test when wp_get_environment_type is a random value.
168
	 *
169
	 * @covers Automattic\Jetpack\Status::is_staging_site
170
	 */
171 View Code Duplication
	public function test_is_staging_wp_get_environment_type_random() {
172
		Functions\when( 'wp_get_environment_type' )->justReturn( 'random_string' );
173
174
		Filters\expectApplied( 'jetpack_is_staging_site' )->once()->with( false )->andReturn( false );
175
176
		$this->assertTrue( $this->status_obj->is_staging_site() ); // We assume a site is a staging site for any non-local or non-production value.
177
	}
178
179
	/**
180
	 * Test when using the constant to set dev mode
181
	 *
182
	 * @covers Automattic\Jetpack\Status::is_offline_mode
183
	 *
184
	 * @runInSeparateProcess
185
	 */
186
	public function test_is_offline_mode_constant() {
187
		Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false );
188
189
		$constants_mocks = $this->mock_constants(
190
			array(
191
				array( '\\JETPACK_DEV_DEBUG', true ),
192
			)
193
		);
194
195
		$this->assertTrue( $this->status_obj->is_offline_mode() );
196
197
		array_map(
198
			function ( $mock ) {
199
				$mock->disable();
200
			},
201
			$constants_mocks
202
		);
203
	}
204
205
	/**
206
	 * Test for is_multi_network with a single site
207
	 *
208
	 * @covers Automattic\Jetpack\Status::is_multi_network
209
	 */
210
	public function test_is_multi_network_not_multisite() {
211
		Functions\when( 'is_multisite' )->justReturn( false );
212
213
		$this->assertFalse( $this->status_obj->is_multi_network() );
214
	}
215
216
	/**
217
	 * Test is_multi_network with a multisite install
218
	 *
219
	 * @covers Automattic\Jetpack\Status::is_multi_network
220
	 */
221
	public function test_is_multi_network_when_single_network() {
222
		$this->mock_wpdb_get_var( 1 );
223
		Functions\when( 'is_multisite' )->justReturn( true );
224
225
		$this->assertFalse( $this->status_obj->is_multi_network() );
226
227
		$this->clean_mock_wpdb_get_var();
228
	}
229
230
	/**
231
	 * Test is_multi_network when multiple networks
232
	 *
233
	 * @covers Automattic\Jetpack\Status::is_multi_network
234
	 */
235 View Code Duplication
	public function test_is_multi_network_when_multiple_networks() {
236
		$this->mock_wpdb_get_var( 2 );
237
		Functions\when( 'is_multisite' )->justReturn( true );
238
239
		$this->assertTrue( $this->status_obj->is_multi_network() );
240
241
		$this->clean_mock_wpdb_get_var();
242
	}
243
244
	/**
245
	 * Test cached is_single_user_site
246
	 *
247
	 * @covers Automattic\Jetpack\Status::is_single_user_site
248
	 */
249
	public function test_is_single_user_site_with_transient() {
250
		$this->mock_wpdb_get_var( 3 );
251
		Functions\when( 'get_transient' )->justReturn( 1 );
252
253
		$this->assertTrue( $this->status_obj->is_single_user_site() );
254
255
		$this->clean_mock_wpdb_get_var();
256
	}
257
258
	/**
259
	 * Test is_single_user_site
260
	 *
261
	 * @covers Automattic\Jetpack\Status::is_single_user_site
262
	 */
263 View Code Duplication
	public function test_is_single_user_site_with_one_user() {
264
		$this->mock_wpdb_get_var( 1 );
265
		Functions\when( 'get_transient' )->justReturn( false );
266
		Functions\when( 'set_transient' )->justReturn( true );
267
268
		$this->assertTrue( $this->status_obj->is_single_user_site() );
269
270
		$this->clean_mock_wpdb_get_var();
271
	}
272
273
	/**
274
	 * Test is_single_user_site with multiple users
275
	 *
276
	 * @covers Automattic\Jetpack\Status::is_single_user_site
277
	 */
278 View Code Duplication
	public function test_is_single_user_site_with_multiple_users() {
279
		$this->mock_wpdb_get_var( 3 );
280
		Functions\when( 'get_transient' )->justReturn( false );
281
		Functions\when( 'set_transient' )->justReturn( true );
282
283
		$this->assertFalse( $this->status_obj->is_single_user_site() );
284
285
		$this->clean_mock_wpdb_get_var();
286
	}
287
288
	/**
289
	 * Mock a global function with particular arguments and make it return a certain value.
290
	 *
291
	 * @param string $function_name Name of the function.
292
	 * @param array  $args          Array of argument sets, last value of each set is used as a return value.
293
	 * @return phpmock\Mock The mock object.
294
	 */
295
	protected function mock_function_with_args( $function_name, $args = array() ) {
296
		$builder = new MockBuilder();
297
		$builder->setNamespace( __NAMESPACE__ )
298
			->setName( $function_name )
299
			->setFunction(
300
				function ( ...$current_args ) use ( &$args ) {
301
					foreach ( $args as $arg ) {
302
						if ( array_slice( $arg, 0, -1 ) === $current_args ) {
303
							return array_pop( $arg );
304
						}
305
					}
306
				}
307
			);
308
309
		$mock = $builder->build();
310
		$mock->enable();
311
312
		return $mock;
313
	}
314
315
	/**
316
	 * Mock a set of constants.
317
	 *
318
	 * @param array $constants Array of sets with constants and their respective values.
319
	 * @return phpmock\Mock The mock object.
320
	 */
321
	protected function mock_constants( $constants = array() ) {
322
		$prepare_constant = function ( $constant ) {
323
			return array( $constant[0], true );
324
		};
325
326
		return array(
327
			$this->mock_function_with_args( 'defined', array_map( $prepare_constant, $constants ) ),
328
			$this->mock_function_with_args( 'constant', $constants ),
329
		);
330
	}
331
332
	/**
333
	 * Mock $wpdb->get_var() and make it return a certain value.
334
	 *
335
	 * @param mixed $return_value  Return value of the function.
336
	 *
337
	 * PHPUnit\Framework\MockObject\MockObject The mock object.
338
	 */
339
	protected function mock_wpdb_get_var( $return_value = null ) {
340
		global $wpdb;
341
342
		$wpdb = $this->getMockBuilder( 'Mock_wpdb' ) // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
343
					->setMockClassName( 'wpdb' )
344
					->setMethods( array( 'get_var' ) )
345
					->getMock();
346
		$wpdb->method( 'get_var' )
347
			->willReturn( $return_value );
348
349
		$wpdb->prefix   = 'wp_';
350
		$wpdb->site     = 'wp_site';
351
		$wpdb->usermeta = 'wp_usermeta';
352
	}
353
354
	/**
355
	 * Clean up the existing $wpdb->get_var() mock.
356
	 */
357
	protected function clean_mock_wpdb_get_var() {
358
		global $wpdb;
359
		unset( $wpdb );
360
	}
361
362
	/**
363
	 * Tests known staging sites.
364
	 *
365
	 * @dataProvider get_is_staging_site_known_hosting_providers_data
366
	 * @covers Automattic\Jetpack\Status::is_staging_site
367
	 *
368
	 * @param string $site_url Site URL.
369
	 * @param bool   $expected Expected return.
370
	 */
371
	public function test_is_staging_site_for_known_hosting_providers( $site_url, $expected ) {
372
		Functions\when( 'site_url' )->justReturn( $site_url );
373
		$result = $this->status_obj->is_staging_site();
374
		$this->assertSame(
375
			$expected,
376
			$result,
377
			sprintf(
378
				'Expected %1$s to return %2$s for is_staging_site()',
379
				$site_url,
380
				var_export( $expected, 1 ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
381
			)
382
		);
383
	}
384
385
	/**
386
	 * Known hosting providers.
387
	 *
388
	 * Including a couple of general RegEx checks (subdir, ending slash).
389
	 *
390
	 * @return array
391
	 */
392
	public function get_is_staging_site_known_hosting_providers_data() {
393
		return array(
394
			'wpengine'              => array(
395
				'http://bjk.staging.wpengine.com',
396
				true,
397
			),
398
			'kinsta'                => array(
399
				'http://test.staging.kinsta.com',
400
				true,
401
			),
402
			'dreampress'            => array(
403
				'http://ebinnion.stage.site',
404
				true,
405
			),
406
			'newspack'              => array(
407
				'http://test.newspackstaging.com',
408
				true,
409
			),
410
			'wpengine_subdirectory' => array(
411
				'http://bjk.staging.wpengine.com/staging',
412
				true,
413
			),
414
			'wpengine_endslash'     => array(
415
				'http://bjk.staging.wpengine.com/',
416
				true,
417
			),
418
			'not_a_staging_site'    => array(
419
				'http://staging.wpengine.com.example.com/',
420
				false,
421
			),
422
		);
423
	}
424
425
	/**
426
	 * Tests known local development sites.
427
	 *
428
	 * @dataProvider get_is_local_site_known_tld
429
	 *
430
	 * @param string $site_url Site URL.
431
	 * @param bool   $expected_response Expected response.
432
	 */
433
	public function test_is_local_site_for_known_tld( $site_url, $expected_response ) {
434
		Functions\when( 'site_url' )->justReturn( $site_url );
435
		$result = $this->status_obj->is_local_site();
436
		$this->assertEquals(
437
			$expected_response,
438
			$result,
439
			sprintf(
440
				'Expected %1$s to return %2$s for is_local_site()',
441
				$site_url,
442
				$expected_response
443
			)
444
		);
445
	}
446
447
	/**
448
	 * Known hosting providers.
449
	 *
450
	 * @return array
451
	 */
452
	public function get_is_local_site_known_tld() {
453
		return array(
454
			'vvv'            => array(
455
				'http://jetpack.test',
456
				true,
457
			),
458
			'docksal'        => array(
459
				'http://jetpack.docksal',
460
				true,
461
			),
462
			'serverpress'    => array(
463
				'http://jetpack.dev.cc',
464
				true,
465
			),
466
			'lando'          => array(
467
				'http://jetpack.lndo.site',
468
				true,
469
			),
470
			'test_subdomain' => array(
471
				'https://test.jetpack.com',
472
				false,
473
			),
474
			'test_in_domain' => array(
475
				'https://jetpack.test.jetpack.com',
476
				false,
477
			),
478
		);
479
	}
480
481
	/**
482
	 * Tests for site_suffix().
483
	 *
484
	 * @covers Automattic\Jetpack\Status::get_site_suffix
485
	 * @dataProvider get_site_suffix_examples
486
	 *
487
	 * @param string $site     Given site URL.
488
	 * @param string $expected Site suffix.
489
	 */
490
	public function test_jetpack_get_site_suffix( $site, $expected ) {
491
		Functions\when( 'home_url' )->justReturn( $this->site_url );
492
		$suffix = $this->status_obj->get_site_suffix( $site );
493
494
		$this->assertSame( $expected, $suffix );
495
	}
496
497
	/**
498
	 * Examples of sites passed to get_site_suffix
499
	 *
500
	 * @covers Automattic\Jetpack\Status::get_site_suffix
501
	 */
502
	public function get_site_suffix_examples() {
503
		return array(
504
			'no_site_home_url' => array(
505
				'',
506
				'yourjetpack.blog',
507
			),
508
			'tld'              => array(
509
				'https://example.org',
510
				'example.org',
511
			),
512
			'subdomain'        => array(
513
				'https://borussia.dortmund.example.org',
514
				'borussia.dortmund.example.org',
515
			),
516
			'subfolder'        => array(
517
				'https://example.org/borussia-dortmund',
518
				'example.org::borussia-dortmund',
519
			),
520
			'ip'               => array(
521
				'127.0.0.1',
522
				'127.0.0.1',
523
			),
524
			'no_tld'           => array(
525
				'https://localhost',
526
				'localhost',
527
			),
528
			'double_domain'    => array(
529
				'https://example.org/http://example.com',
530
				'example.org::http:::::example.com',
531
			),
532
		);
533
	}
534
}
535