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 Automattic\Jetpack\Status; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use phpmock\Mock; |
13
|
|
|
use phpmock\MockBuilder; |
14
|
|
|
use Brain\Monkey; |
15
|
|
|
use Brain\Monkey\Functions; |
16
|
|
|
use Brain\Monkey\Filters; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Status test suite. |
20
|
|
|
*/ |
21
|
|
|
class Test_Status extends TestCase { |
22
|
|
|
/** |
23
|
|
|
* Default site URL. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $site_url = 'https://yourjetpack.blog'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Setup before running any of the tests. |
31
|
|
|
*/ |
32
|
|
|
public static function setUpBeforeClass() { |
33
|
|
|
if ( ! defined( 'HOUR_IN_SECONDS' ) ) { |
34
|
|
|
define( 'HOUR_IN_SECONDS', 60 * 60 ); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test setup. |
40
|
|
|
*/ |
41
|
|
|
public function setUp() { |
42
|
|
|
Monkey\setUp(); |
43
|
|
|
$this->status = new Status(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test teardown. |
48
|
|
|
*/ |
49
|
|
|
public function tearDown() { |
50
|
|
|
// Call Monkey\tearDown(); here, but the following function takes care of it for now. |
51
|
|
|
Mock::disableAll(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test is_offline_mode when not using any filter |
56
|
|
|
* |
57
|
|
|
* @covers Automattic\Jetpack\Status::is_offline_mode |
58
|
|
|
*/ |
59
|
|
|
public function test_is_offline_mode_default() { |
60
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
61
|
|
|
Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false ); |
62
|
|
|
|
63
|
|
|
$this->assertFalse( $this->status->is_offline_mode() ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test is_offline_mode when using the jetpack_offline_mode filter |
68
|
|
|
* |
69
|
|
|
* @covers Automattic\Jetpack\Status::is_offline_mode |
70
|
|
|
*/ |
71
|
|
|
public function test_is_offline_mode_filter_true() { |
72
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
73
|
|
|
Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( true ); |
74
|
|
|
|
75
|
|
|
$this->assertTrue( $this->status->is_offline_mode() ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test when using a bool value for the jetpack_offline_mode filter. |
80
|
|
|
* |
81
|
|
|
* @covers Automattic\Jetpack\Status::is_offline_mode |
82
|
|
|
*/ |
83
|
|
|
public function test_is_offline_mode_filter_bool() { |
84
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
85
|
|
|
Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( 0 ); |
86
|
|
|
|
87
|
|
|
$this->assertFalse( $this->status->is_offline_mode() ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test when site url is localhost (dev mode on) |
92
|
|
|
* |
93
|
|
|
* @covers Automattic\Jetpack\Status::is_offline_mode |
94
|
|
|
*/ |
95
|
|
|
public function test_is_offline_mode_localhost() { |
96
|
|
|
Functions\when( 'site_url' )->justReturn( 'localhost' ); |
97
|
|
|
|
98
|
|
|
Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false ); |
99
|
|
|
|
100
|
|
|
$this->assertTrue( $this->status->is_offline_mode() ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test when using the constant to set dev mode |
105
|
|
|
* |
106
|
|
|
* @covers Automattic\Jetpack\Status::is_offline_mode |
107
|
|
|
* |
108
|
|
|
* @runInSeparateProcess |
109
|
|
|
*/ |
110
|
|
|
public function test_is_offline_mode_constant() { |
111
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
112
|
|
|
Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false ); |
113
|
|
|
|
114
|
|
|
$constants_mocks = $this->mock_constants( |
115
|
|
|
array( |
116
|
|
|
array( '\\JETPACK_DEV_DEBUG', true ), |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->assertTrue( $this->status->is_offline_mode() ); |
121
|
|
|
|
122
|
|
|
array_map( |
123
|
|
|
function( $mock ) { |
124
|
|
|
$mock->disable(); |
125
|
|
|
}, |
126
|
|
|
$constants_mocks |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Test for is_multi_network with a single site |
132
|
|
|
* |
133
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
134
|
|
|
*/ |
135
|
|
|
public function test_is_multi_network_not_multisite() { |
136
|
|
|
Functions\when( 'is_multisite' )->justReturn( false ); |
137
|
|
|
|
138
|
|
|
$this->assertFalse( $this->status->is_multi_network() ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Test is_multi_network with a multisite install |
143
|
|
|
* |
144
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
145
|
|
|
*/ |
146
|
|
|
public function test_is_multi_network_when_single_network() { |
147
|
|
|
$this->mock_wpdb_get_var( 1 ); |
148
|
|
|
Functions\when( 'is_multisite' )->justReturn( true ); |
149
|
|
|
|
150
|
|
|
$this->assertFalse( $this->status->is_multi_network() ); |
151
|
|
|
|
152
|
|
|
$this->clean_mock_wpdb_get_var(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Test is_multi_network when multiple networks |
157
|
|
|
* |
158
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function test_is_multi_network_when_multiple_networks() { |
161
|
|
|
$this->mock_wpdb_get_var( 2 ); |
162
|
|
|
Functions\when( 'is_multisite' )->justReturn( true ); |
163
|
|
|
|
164
|
|
|
$this->assertTrue( $this->status->is_multi_network() ); |
165
|
|
|
|
166
|
|
|
$this->clean_mock_wpdb_get_var(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Test cached is_single_user_site |
171
|
|
|
* |
172
|
|
|
* @covers Automattic\Jetpack\Status::is_single_user_site |
173
|
|
|
*/ |
174
|
|
|
public function test_is_single_user_site_with_transient() { |
175
|
|
|
$this->mock_wpdb_get_var( 3 ); |
176
|
|
|
Functions\when( 'get_transient' )->justReturn( 1 ); |
177
|
|
|
|
178
|
|
|
$this->assertTrue( $this->status->is_single_user_site() ); |
179
|
|
|
|
180
|
|
|
$this->clean_mock_wpdb_get_var(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Test is_single_user_site |
185
|
|
|
* |
186
|
|
|
* @covers Automattic\Jetpack\Status::is_single_user_site |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function test_is_single_user_site_with_one_user() { |
189
|
|
|
$this->mock_wpdb_get_var( 1 ); |
190
|
|
|
Functions\when( 'get_transient' )->justReturn( false ); |
191
|
|
|
Functions\when( 'set_transient' )->justReturn( true ); |
192
|
|
|
|
193
|
|
|
$this->assertTrue( $this->status->is_single_user_site() ); |
194
|
|
|
|
195
|
|
|
$this->clean_mock_wpdb_get_var(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Test is_single_user_site with multiple users |
200
|
|
|
* |
201
|
|
|
* @covers Automattic\Jetpack\Status::is_single_user_site |
202
|
|
|
*/ |
203
|
|
View Code Duplication |
public function test_is_single_user_site_with_multiple_users() { |
204
|
|
|
$this->mock_wpdb_get_var( 3 ); |
205
|
|
|
Functions\when( 'get_transient' )->justReturn( false ); |
206
|
|
|
Functions\when( 'set_transient' )->justReturn( true ); |
207
|
|
|
|
208
|
|
|
$this->assertFalse( $this->status->is_single_user_site() ); |
209
|
|
|
|
210
|
|
|
$this->clean_mock_wpdb_get_var(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Mock a global function with particular arguments and make it return a certain value. |
216
|
|
|
* |
217
|
|
|
* @param string $function_name Name of the function. |
218
|
|
|
* @param array $args Array of argument sets, last value of each set is used as a return value. |
219
|
|
|
* @return phpmock\Mock The mock object. |
220
|
|
|
*/ |
221
|
|
|
protected function mock_function_with_args( $function_name, $args = array() ) { |
222
|
|
|
$builder = new MockBuilder(); |
223
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
224
|
|
|
->setName( $function_name ) |
225
|
|
|
->setFunction( |
226
|
|
|
function( ...$current_args ) use ( &$args ) { |
227
|
|
|
foreach ( $args as $arg ) { |
228
|
|
|
if ( array_slice( $arg, 0, -1 ) === $current_args ) { |
229
|
|
|
return array_pop( $arg ); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
$mock = $builder->build(); |
236
|
|
|
$mock->enable(); |
237
|
|
|
|
238
|
|
|
return $mock; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Mock a set of constants. |
243
|
|
|
* |
244
|
|
|
* @param array $constants Array of sets with constants and their respective values. |
245
|
|
|
* @return phpmock\Mock The mock object. |
246
|
|
|
*/ |
247
|
|
|
protected function mock_constants( $constants = array() ) { |
248
|
|
|
$prepare_constant = function( $constant ) { |
249
|
|
|
return array( $constant[0], true ); |
250
|
|
|
}; |
251
|
|
|
|
252
|
|
|
return array( |
253
|
|
|
$this->mock_function_with_args( 'defined', array_map( $prepare_constant, $constants ) ), |
254
|
|
|
$this->mock_function_with_args( 'constant', $constants ), |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Mock $wpdb->get_var() and make it return a certain value. |
260
|
|
|
* |
261
|
|
|
* @param mixed $return_value Return value of the function. |
262
|
|
|
* |
263
|
|
|
* PHPUnit\Framework\MockObject\MockObject The mock object. |
264
|
|
|
*/ |
265
|
|
|
protected function mock_wpdb_get_var( $return_value = null ) { |
266
|
|
|
global $wpdb; |
267
|
|
|
|
268
|
|
|
$wpdb = $this->getMockBuilder( 'Mock_wpdb' ) // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
269
|
|
|
->setMockClassName( 'wpdb' ) |
270
|
|
|
->setMethods( array( 'get_var' ) ) |
271
|
|
|
->getMock(); |
272
|
|
|
$wpdb->method( 'get_var' ) |
273
|
|
|
->willReturn( $return_value ); |
274
|
|
|
|
275
|
|
|
$wpdb->prefix = 'wp_'; |
276
|
|
|
$wpdb->site = 'wp_site'; |
277
|
|
|
$wpdb->usermeta = 'wp_usermeta'; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Clean up the existing $wpdb->get_var() mock. |
282
|
|
|
*/ |
283
|
|
|
protected function clean_mock_wpdb_get_var() { |
284
|
|
|
global $wpdb; |
285
|
|
|
unset( $wpdb ); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Tests a WP Engine staging site URL. |
290
|
|
|
* |
291
|
|
|
* @author kraftbj |
292
|
|
|
* @covers is_staging_site |
293
|
|
|
* @since 3.9.0 |
294
|
|
|
*/ |
295
|
|
|
public function test_is_staging_site_will_report_staging_for_wpengine_sites_by_url() { |
296
|
|
|
Functions\when( 'site_url' )->justReturn( 'http://bjk.staging.wpengine.com' ); |
297
|
|
|
$this->assertTrue( $this->status->is_staging_site() ); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Tests known staging sites. |
302
|
|
|
* |
303
|
|
|
* @dataProvider get_is_staging_site_known_hosting_providers_data |
304
|
|
|
* |
305
|
|
|
* @param string $site_url Site URL. |
306
|
|
|
*/ |
307
|
|
|
public function test_is_staging_site_for_known_hosting_providers( $site_url ) { |
308
|
|
|
Functions\when( 'site_url' )->justReturn( $site_url ); |
309
|
|
|
$result = $this->status->is_staging_site(); |
310
|
|
|
$this->assertTrue( |
311
|
|
|
$result, |
312
|
|
|
sprintf( 'Expected %s to return true for `is_staging_site()', $site_url ) |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Known hosting providers. |
318
|
|
|
* |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
public function get_is_staging_site_known_hosting_providers_data() { |
322
|
|
|
return array( |
323
|
|
|
'wpengine' => array( |
324
|
|
|
'http://bjk.staging.wpengine.com', |
325
|
|
|
), |
326
|
|
|
'kinsta' => array( |
327
|
|
|
'http://test.staging.kinsta.com', |
328
|
|
|
), |
329
|
|
|
'dreampress' => array( |
330
|
|
|
'http://ebinnion.stage.site', |
331
|
|
|
), |
332
|
|
|
'newspack' => array( |
333
|
|
|
'http://test.newspackstaging.com', |
334
|
|
|
), |
335
|
|
|
); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Tests known local development sites. |
340
|
|
|
* |
341
|
|
|
* @dataProvider get_is_local_site_known_tld |
342
|
|
|
* |
343
|
|
|
* @param string $site_url Site URL. |
344
|
|
|
* @param bool $expected_response Expected response. |
345
|
|
|
*/ |
346
|
|
|
public function test_is_local_site_for_known_tld( $site_url, $expected_response ) { |
347
|
|
|
Functions\when( 'site_url' )->justReturn( $site_url ); |
348
|
|
|
$result = $this->status->is_local_site(); |
349
|
|
|
$this->assertEquals( |
350
|
|
|
$expected_response, |
351
|
|
|
$result, |
352
|
|
|
sprintf( |
353
|
|
|
'Expected %1$s to return %2$s for is_local_site()', |
354
|
|
|
$site_url, |
355
|
|
|
$expected_response |
356
|
|
|
) |
357
|
|
|
); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Known hosting providers. |
362
|
|
|
* |
363
|
|
|
* @return array |
364
|
|
|
*/ |
365
|
|
|
public function get_is_local_site_known_tld() { |
366
|
|
|
return array( |
367
|
|
|
'vvv' => array( |
368
|
|
|
'http://jetpack.test', |
369
|
|
|
true, |
370
|
|
|
), |
371
|
|
|
'docksal' => array( |
372
|
|
|
'http://jetpack.docksal', |
373
|
|
|
true, |
374
|
|
|
), |
375
|
|
|
'serverpress' => array( |
376
|
|
|
'http://jetpack.dev.cc', |
377
|
|
|
true, |
378
|
|
|
), |
379
|
|
|
'lando' => array( |
380
|
|
|
'http://jetpack.lndo.site', |
381
|
|
|
true, |
382
|
|
|
), |
383
|
|
|
'test_subdomain' => array( |
384
|
|
|
'https://test.jetpack.com', |
385
|
|
|
false, |
386
|
|
|
), |
387
|
|
|
'test_in_domain' => array( |
388
|
|
|
'https://jetpack.test.jetpack.com', |
389
|
|
|
false, |
390
|
|
|
), |
391
|
|
|
); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|