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