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
|
|
|
* Test setup. |
20
|
|
|
*/ |
21
|
|
|
public function setUp() { |
22
|
|
|
$this->status = new Status(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test teardown. |
27
|
|
|
*/ |
28
|
|
|
public function tearDown() { |
29
|
|
|
Mock::disableAll(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function test_is_development_mode_default() { |
36
|
|
|
$this->mock_function( 'site_url', $this->site_url ); |
37
|
|
|
$filters_mock = $this->mock_filters( array( |
38
|
|
|
array( 'jetpack_development_mode', false, false ), |
39
|
|
|
array( 'jetpack_development_mode', true, true ), |
40
|
|
|
) ); |
41
|
|
|
|
42
|
|
|
$this->assertFalse( $this->status->is_development_mode() ); |
43
|
|
|
|
44
|
|
|
$filters_mock->disable(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function test_is_development_mode_filter_true() { |
51
|
|
|
$this->mock_function( 'site_url', $this->site_url ); |
52
|
|
|
$filters_mock = $this->mock_filters( array( |
53
|
|
|
array( 'jetpack_development_mode', false, true ), |
54
|
|
|
) ); |
55
|
|
|
|
56
|
|
|
$this->assertTrue( $this->status->is_development_mode() ); |
57
|
|
|
|
58
|
|
|
$filters_mock->disable(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function test_is_development_mode_filter_bool() { |
65
|
|
|
$this->mock_function( 'site_url', $this->site_url ); |
66
|
|
|
$filters_mock = $this->mock_filters( array( |
67
|
|
|
array( 'jetpack_development_mode', false, 0 ), |
68
|
|
|
) ); |
69
|
|
|
|
70
|
|
|
$this->assertFalse( $this->status->is_development_mode() ); |
71
|
|
|
|
72
|
|
|
$filters_mock->disable(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function test_is_development_mode_localhost() { |
79
|
|
|
$this->mock_function( 'site_url', 'localhost' ); |
80
|
|
|
|
81
|
|
|
$filters_mock = $this->mock_filters( array( |
82
|
|
|
array( 'jetpack_development_mode', false, false ), |
83
|
|
|
array( 'jetpack_development_mode', true, true ), |
84
|
|
|
) ); |
85
|
|
|
|
86
|
|
|
$this->assertTrue( $this->status->is_development_mode() ); |
87
|
|
|
|
88
|
|
|
$filters_mock->disable(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @covers Automattic\Jetpack\Status::is_development_mode |
93
|
|
|
* |
94
|
|
|
* @runInSeparateProcess |
95
|
|
|
*/ |
96
|
|
|
public function test_is_development_mode_constant() { |
97
|
|
|
$this->mock_function( 'site_url', $this->site_url ); |
98
|
|
|
$filters_mock = $this->mock_filters( array( |
99
|
|
|
array( 'jetpack_development_mode', false, false ), |
100
|
|
|
array( 'jetpack_development_mode', true, true ), |
101
|
|
|
) ); |
102
|
|
|
$constants_mocks = $this->mock_constants( array( |
103
|
|
|
array( '\\JETPACK_DEV_DEBUG', true ), |
104
|
|
|
) ); |
105
|
|
|
|
106
|
|
|
$this->assertTrue( $this->status->is_development_mode() ); |
107
|
|
|
|
108
|
|
|
array_map( function( $mock ) { |
109
|
|
|
$mock->disable(); |
110
|
|
|
}, $constants_mocks ); |
111
|
|
|
$filters_mock->disable(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
116
|
|
|
*/ |
117
|
|
|
public function test_is_multi_network_not_multisite() { |
118
|
|
|
$this->mock_function( 'is_multisite', false ); |
119
|
|
|
|
120
|
|
|
$this->assertFalse( $this->status->is_multi_network() ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
125
|
|
|
*/ |
126
|
|
|
public function test_is_multi_network_when_single_network() { |
127
|
|
|
$this->mock_wpdb_get_var( 1 ); |
128
|
|
|
$this->mock_function( 'is_multisite', true ); |
129
|
|
|
|
130
|
|
|
$this->assertFalse( $this->status->is_multi_network() ); |
131
|
|
|
|
132
|
|
|
$this->clean_mock_wpdb_get_var(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @covers Automattic\Jetpack\Status::is_multi_network |
137
|
|
|
*/ |
138
|
|
|
public function test_is_multi_network_when_multiple_networks() { |
139
|
|
|
$this->mock_wpdb_get_var( 2 ); |
140
|
|
|
$this->mock_function( 'is_multisite', true ); |
141
|
|
|
|
142
|
|
|
$this->assertTrue( $this->status->is_multi_network() ); |
143
|
|
|
|
144
|
|
|
$this->clean_mock_wpdb_get_var(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Mock a global function with particular arguments and make it return a certain value. |
149
|
|
|
* |
150
|
|
|
* @param string $function_name Name of the function. |
151
|
|
|
* @param array $args Array of argument sets, last value of each set is used as a return value. |
152
|
|
|
* @return phpmock\Mock The mock object. |
153
|
|
|
*/ |
154
|
|
|
protected function mock_function_with_args( $function_name, $args = array() ) { |
155
|
|
|
$builder = new MockBuilder(); |
156
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
157
|
|
|
->setName( $function_name ) |
158
|
|
|
->setFunction( |
159
|
|
View Code Duplication |
function() use ( &$args ) { |
160
|
|
|
$current_args = func_get_args(); |
161
|
|
|
|
162
|
|
|
foreach ( $args as $arg ) { |
163
|
|
|
if ( array_slice( $arg, 0, -1 ) === $current_args ) { |
164
|
|
|
return array_pop( $arg ); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$mock = $builder->build(); |
171
|
|
|
$mock->enable(); |
172
|
|
|
|
173
|
|
|
return $mock; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Mock a set of filters. |
178
|
|
|
* |
179
|
|
|
* @param array $args Array of filters with their arguments. |
|
|
|
|
180
|
|
|
* @return phpmock\Mock The mock object. |
181
|
|
|
*/ |
182
|
|
|
protected function mock_filters( $filters = array() ) { |
183
|
|
|
return $this->mock_function_with_args( 'apply_filters', $filters ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Mock a set of constants. |
188
|
|
|
* |
189
|
|
|
* @param array $args Array of sets with constants and their respective values. |
|
|
|
|
190
|
|
|
* @return phpmock\Mock The mock object. |
191
|
|
|
*/ |
192
|
|
|
protected function mock_constants( $constants = array() ) { |
193
|
|
|
$prepare_constant = function( $constant ) { |
194
|
|
|
return array( $constant[0], true ); |
195
|
|
|
}; |
196
|
|
|
|
197
|
|
|
return [ |
198
|
|
|
$this->mock_function_with_args( 'defined', array_map( $prepare_constant, $constants ) ), |
199
|
|
|
$this->mock_function_with_args( 'constant', $constants ) |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Mock a global function and make it return a certain value. |
205
|
|
|
* |
206
|
|
|
* @param string $function_name Name of the function. |
207
|
|
|
* @param mixed $return_value Return value of the function. |
208
|
|
|
* @return phpmock\Mock The mock object. |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
protected function mock_function( $function_name, $return_value = null ) { |
211
|
|
|
$builder = new MockBuilder(); |
212
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
213
|
|
|
->setName( $function_name ) |
214
|
|
|
->setFunction( function() use ( &$return_value ) { |
215
|
|
|
return $return_value; |
216
|
|
|
} ); |
217
|
|
|
return $builder->build()->enable(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Mock $wpdb->get_var() and make it return a certain value. |
222
|
|
|
* |
223
|
|
|
* @param mixed $return_value Return value of the function. |
224
|
|
|
* @return PHPUnit\Framework\MockObject\MockObject The mock object. |
225
|
|
|
*/ |
226
|
|
|
protected function mock_wpdb_get_var( $return_value = null ) { |
227
|
|
|
global $wpdb; |
228
|
|
|
$wpdb = $this->getMockBuilder( 'Mock_wpdb' ) |
229
|
|
|
->setMockClassName( 'wpdb' ) |
230
|
|
|
->setMethods( array( 'get_var' ) ) |
231
|
|
|
->getMock(); |
232
|
|
|
$wpdb->method( 'get_var' ) |
233
|
|
|
->willReturn( $return_value ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Clean up the existing $wpdb->get_var() mock. |
238
|
|
|
*/ |
239
|
|
|
protected function clean_mock_wpdb_get_var() { |
240
|
|
|
global $wpdb; |
241
|
|
|
unset( $wpdb ); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
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.