1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack; |
4
|
|
|
|
5
|
|
|
use Automattic\Jetpack\Status; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use phpmock\Mock; |
8
|
|
|
use phpmock\MockBuilder; |
9
|
|
|
use Brain\Monkey; |
10
|
|
|
use Brain\Monkey\Functions; |
11
|
|
|
use Brain\Monkey\Filters; |
12
|
|
|
|
13
|
|
|
class Test_Status extends TestCase { |
14
|
|
|
/** |
15
|
|
|
* Default site URL. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $site_url = 'https://yourjetpack.blog'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Setup before running any of the tests. |
23
|
|
|
*/ |
24
|
|
|
public static function setUpBeforeClass() { |
25
|
|
|
if ( ! defined( 'HOUR_IN_SECONDS' ) ) { |
26
|
|
|
define( 'HOUR_IN_SECONDS', 60 * 60 ); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Test setup. |
32
|
|
|
*/ |
33
|
|
|
public function setUp() { |
34
|
|
|
$this->status = new Status(); |
35
|
|
|
Monkey\setUp(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test teardown. |
40
|
|
|
*/ |
41
|
|
|
public function tearDown() { |
42
|
|
|
// Call Monkey\tearDown(); here, but the following function takes care of it for now. |
43
|
|
|
Mock::disableAll(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
48
|
|
|
*/ |
49
|
|
|
public function test_is_development_mode_default() { |
50
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
51
|
|
|
Filters\expectApplied( 'jetpack_development_mode' )->once()->with( false )->andReturn( false ); |
52
|
|
|
|
53
|
|
|
$this->assertFalse( $this->status->is_development_mode() ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
58
|
|
|
*/ |
59
|
|
|
public function test_is_development_mode_filter_true() { |
60
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
61
|
|
|
Filters\expectApplied( 'jetpack_development_mode' )->once()->with( false )->andReturn( true ); |
62
|
|
|
|
63
|
|
|
$this->assertTrue( $this->status->is_development_mode() ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
68
|
|
|
*/ |
69
|
|
|
public function test_is_development_mode_filter_bool() { |
70
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
71
|
|
|
Filters\expectApplied( 'jetpack_development_mode' )->once()->with( false )->andReturn( 0 ); |
72
|
|
|
|
73
|
|
|
$this->assertFalse( $this->status->is_development_mode() ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
78
|
|
|
*/ |
79
|
|
|
public function test_is_development_mode_localhost() { |
80
|
|
|
Functions\when( 'site_url' )->justReturn( 'localhost' ); |
81
|
|
|
|
82
|
|
|
Filters\expectApplied( 'jetpack_development_mode' )->once()->with( false )->andReturn( false ); |
83
|
|
|
|
84
|
|
|
$this->assertTrue( $this->status->is_development_mode() ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
89
|
|
|
* |
90
|
|
|
* @runInSeparateProcess |
91
|
|
|
*/ |
92
|
|
|
public function test_is_development_mode_constant() { |
93
|
|
|
Functions\when( 'site_url' )->justReturn( $this->site_url ); |
94
|
|
|
Filters\expectApplied( 'jetpack_development_mode' )->once()->with( false )->andReturn( false ); |
95
|
|
|
|
96
|
|
|
$constants_mocks = $this->mock_constants( array( |
97
|
|
|
array( '\\JETPACK_DEV_DEBUG', true ), |
98
|
|
|
) ); |
99
|
|
|
|
100
|
|
|
$this->assertTrue( $this->status->is_development_mode() ); |
101
|
|
|
|
102
|
|
|
array_map( function( $mock ) { |
103
|
|
|
$mock->disable(); |
104
|
|
|
}, $constants_mocks ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
109
|
|
|
*/ |
110
|
|
|
public function test_is_multi_network_not_multisite() { |
111
|
|
|
Functions\when( 'is_multisite' )->justReturn( false ); |
112
|
|
|
|
113
|
|
|
$this->assertFalse( $this->status->is_multi_network() ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
118
|
|
|
*/ |
119
|
|
|
public function test_is_multi_network_when_single_network() { |
120
|
|
|
$this->mock_wpdb_get_var( 1 ); |
121
|
|
|
Functions\when( 'is_multisite' )->justReturn( true ); |
122
|
|
|
|
123
|
|
|
$this->assertFalse( $this->status->is_multi_network() ); |
124
|
|
|
|
125
|
|
|
$this->clean_mock_wpdb_get_var(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function test_is_multi_network_when_multiple_networks() { |
132
|
|
|
$this->mock_wpdb_get_var( 2 ); |
133
|
|
|
Functions\when( 'is_multisite' )->justReturn( true ); |
134
|
|
|
|
135
|
|
|
$this->assertTrue( $this->status->is_multi_network() ); |
136
|
|
|
|
137
|
|
|
$this->clean_mock_wpdb_get_var(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @covers Automattic\Jetpack\Status::is_single_user_site |
142
|
|
|
*/ |
143
|
|
|
public function test_is_single_user_site_with_transient() { |
144
|
|
|
$this->mock_wpdb_get_var( 3 ); |
145
|
|
|
Functions\when( 'get_transient' )->justReturn( 1 ); |
146
|
|
|
|
147
|
|
|
$this->assertTrue( $this->status->is_single_user_site() ); |
148
|
|
|
|
149
|
|
|
$this->clean_mock_wpdb_get_var(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @covers Automattic\Jetpack\Status::is_single_user_site |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function test_is_single_user_site_with_one_user() { |
156
|
|
|
$this->mock_wpdb_get_var( 1 ); |
157
|
|
|
Functions\when( 'get_transient' )->justReturn( false ); |
158
|
|
|
Functions\when( 'set_transient' )->justReturn( true ); |
159
|
|
|
|
160
|
|
|
$this->assertTrue( $this->status->is_single_user_site() ); |
161
|
|
|
|
162
|
|
|
$this->clean_mock_wpdb_get_var(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @covers Automattic\Jetpack\Status::is_single_user_site |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function test_is_single_user_site_with_multiple_users() { |
169
|
|
|
$this->mock_wpdb_get_var( 3 ); |
170
|
|
|
Functions\when( 'get_transient' )->justReturn( false ); |
171
|
|
|
Functions\when( 'set_transient' )->justReturn( true ); |
172
|
|
|
|
173
|
|
|
$this->assertFalse( $this->status->is_single_user_site() ); |
174
|
|
|
|
175
|
|
|
$this->clean_mock_wpdb_get_var(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Mock a global function with particular arguments and make it return a certain value. |
181
|
|
|
* |
182
|
|
|
* @param string $function_name Name of the function. |
183
|
|
|
* @param array $args Array of argument sets, last value of each set is used as a return value. |
184
|
|
|
* @return phpmock\Mock The mock object. |
185
|
|
|
*/ |
186
|
|
|
protected function mock_function_with_args( $function_name, $args = array() ) { |
187
|
|
|
$builder = new MockBuilder(); |
188
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
189
|
|
|
->setName( $function_name ) |
190
|
|
|
->setFunction( |
191
|
|
|
function( ...$current_args ) use ( &$args ) { |
192
|
|
|
foreach ( $args as $arg ) { |
193
|
|
|
if ( array_slice( $arg, 0, -1 ) === $current_args ) { |
194
|
|
|
return array_pop( $arg ); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$mock = $builder->build(); |
201
|
|
|
$mock->enable(); |
202
|
|
|
|
203
|
|
|
return $mock; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Mock a set of constants. |
208
|
|
|
* |
209
|
|
|
* @param array $args Array of sets with constants and their respective values. |
|
|
|
|
210
|
|
|
* @return phpmock\Mock The mock object. |
211
|
|
|
*/ |
212
|
|
|
protected function mock_constants( $constants = array() ) { |
213
|
|
|
$prepare_constant = function( $constant ) { |
214
|
|
|
return array( $constant[0], true ); |
215
|
|
|
}; |
216
|
|
|
|
217
|
|
|
return [ |
218
|
|
|
$this->mock_function_with_args( 'defined', array_map( $prepare_constant, $constants ) ), |
219
|
|
|
$this->mock_function_with_args( 'constant', $constants ) |
220
|
|
|
]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Mock $wpdb->get_var() and make it return a certain value. |
225
|
|
|
* |
226
|
|
|
* @param mixed $return_value Return value of the function. |
227
|
|
|
* @return PHPUnit\Framework\MockObject\MockObject The mock object. |
228
|
|
|
*/ |
229
|
|
|
protected function mock_wpdb_get_var( $return_value = null ) { |
230
|
|
|
global $wpdb; |
231
|
|
|
|
232
|
|
|
$wpdb = $this->getMockBuilder( 'Mock_wpdb' ) |
233
|
|
|
->setMockClassName( 'wpdb' ) |
234
|
|
|
->setMethods( array( 'get_var' ) ) |
235
|
|
|
->getMock(); |
236
|
|
|
$wpdb->method( 'get_var' ) |
237
|
|
|
->willReturn( $return_value ); |
238
|
|
|
|
239
|
|
|
$wpdb->prefix = 'wp_'; |
240
|
|
|
$wpdb->site = 'wp_site'; |
241
|
|
|
$wpdb->usermeta = 'wp_usermeta'; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Clean up the existing $wpdb->get_var() mock. |
246
|
|
|
*/ |
247
|
|
|
protected function clean_mock_wpdb_get_var() { |
248
|
|
|
global $wpdb; |
249
|
|
|
unset( $wpdb ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Tests a WP Engine staging site URL. |
254
|
|
|
* |
255
|
|
|
* @author kraftbj |
256
|
|
|
* @covers is_staging_site |
257
|
|
|
* @since 3.9.0 |
258
|
|
|
*/ |
259
|
|
|
public function test_is_staging_site_will_report_staging_for_wpengine_sites_by_url() { |
260
|
|
|
Functions\when( 'site_url' )->justReturn( 'http://bjk.staging.wpengine.com' ); |
261
|
|
|
$this->assertTrue( $this->status->is_staging_site() ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Tests known staging sites. |
266
|
|
|
* |
267
|
|
|
* @dataProvider get_is_staging_site_known_hosting_providers_data |
268
|
|
|
* |
269
|
|
|
* @param string $site_url Site URL. |
270
|
|
|
*/ |
271
|
|
|
public function test_is_staging_site_for_known_hosting_providers( $site_url ) { |
272
|
|
|
Functions\when( 'site_url' )->justReturn( $site_url ); |
273
|
|
|
$result = $this->status->is_staging_site(); |
274
|
|
|
$this->assertTrue( |
275
|
|
|
$result, |
276
|
|
|
sprintf( 'Expected %s to return true for `is_staging_site()', $site_url ) |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Known hosting providers. |
282
|
|
|
* |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
public function get_is_staging_site_known_hosting_providers_data() { |
286
|
|
|
return array( |
287
|
|
|
'wpengine' => array( |
288
|
|
|
'http://bjk.staging.wpengine.com', |
289
|
|
|
), |
290
|
|
|
'kinsta' => array( |
291
|
|
|
'http://test.staging.kinsta.com', |
292
|
|
|
), |
293
|
|
|
'dreampress' => array( |
294
|
|
|
'http://ebinnion.stage.site', |
295
|
|
|
), |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.