1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
/** |
3
|
|
|
* The Plugins_HandlerTest class file. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-autoloader |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides unit tests for the methods in the Plugins_Handler class. |
12
|
|
|
*/ |
13
|
|
|
class PluginsHandlerTest extends TestCase { |
14
|
|
|
|
15
|
|
|
const DEFAULT_ACTIVE_PLUGINS = array( 'test1/test1.php', 'test2/test2.php' ); |
16
|
|
|
const DEFAULT_ACTIVE_PLUGIN_DIRS = array( 'test1', 'test2' ); |
17
|
|
|
const DEFAULT_MULTISITE_PLUGINS = array( |
18
|
|
|
'multi1/multi1.php' => 'details', |
19
|
|
|
'multi2/multi2.php' => 'details', |
20
|
|
|
); |
21
|
|
|
const DEFAULT_MULTISITE_PLUGIN_DIRS = array( 'multi1', 'multi2' ); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This method is called before each test. |
25
|
|
|
*/ |
26
|
|
|
public function setUp() { |
27
|
|
|
$this->plugins_handler = $this->getMockBuilder( 'Plugins_Handler' ) |
28
|
|
|
->setMethods( |
29
|
|
|
array( |
30
|
|
|
'get_current_plugin_dir', |
31
|
|
|
'get_multisite_plugins', |
32
|
|
|
'get_active_plugins', |
33
|
|
|
) |
34
|
|
|
) |
35
|
|
|
->getMock(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set up mock Plugins_Handler methods. |
40
|
|
|
* |
41
|
|
|
* @param Array $active_plugins The names of the active plugins. |
42
|
|
|
* @param Boolean $use_multisite Whether the env is multisite. |
43
|
|
|
*/ |
44
|
|
|
private function set_up_mocks( |
45
|
|
|
$active_plugins = self::DEFAULT_ACTIVE_PLUGINS, |
46
|
|
|
$use_multisite = false ) { |
47
|
|
|
|
48
|
|
|
$this->plugins_handler |
49
|
|
|
->method( 'get_active_plugins' ) |
50
|
|
|
->willReturn( (array) $active_plugins ); |
51
|
|
|
|
52
|
|
|
if ( $use_multisite ) { |
53
|
|
|
$this->plugins_handler |
54
|
|
|
->method( 'get_multisite_plugins' ) |
55
|
|
|
->willReturn( array_keys( self::DEFAULT_MULTISITE_PLUGINS ) ); |
56
|
|
|
} else { |
57
|
|
|
$this->plugins_handler |
58
|
|
|
->method( 'get_multisite_plugins' ) |
59
|
|
|
->willReturn( array() ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests is_directory_plugin() with a single-file plugin. |
65
|
|
|
* |
66
|
|
|
* @covers Plugins_Handler::is_directory_plugin |
67
|
|
|
*/ |
68
|
|
|
public function test_is_directory_plugin_single_file() { |
69
|
|
|
$this->assertFalse( $this->plugins_handler->is_directory_plugin( 'test.php' ) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Tests is_directory_plugin() with a single-file plugin that begins with '/'. |
74
|
|
|
* |
75
|
|
|
* @covers Plugins_Handler::is_directory_plugin |
76
|
|
|
*/ |
77
|
|
|
public function test_is_directory_plugin_single_file_with_slash() { |
78
|
|
|
$this->assertFalse( $this->plugins_handler->is_directory_plugin( '/test.php' ) ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Tests is_directory_plugin() with a plugin with a directory. |
83
|
|
|
* |
84
|
|
|
* @covers Plugins_Handler::is_directory_plugin |
85
|
|
|
*/ |
86
|
|
|
public function test_is_directory_plugin_dir() { |
87
|
|
|
$this->assertTrue( $this->plugins_handler->is_directory_plugin( 'test/test.php' ) ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Tests should_autoloader_reset() with an already active plugin. |
92
|
|
|
* |
93
|
|
|
* @covers Plugins_Handler::should_autoloader_reset |
94
|
|
|
*/ |
95
|
|
|
public function test_should_autoloader_reset_known_plugin() { |
96
|
|
|
global $jetpack_autoloader_activating_plugins; |
97
|
|
|
|
98
|
|
|
// 'test1/test1.php' is in self::DEFAULT_ACTIVE_PLUGINS. |
99
|
|
|
$this->plugins_handler |
100
|
|
|
->method( 'get_current_plugin_dir' ) |
101
|
|
|
->willReturn( 'test1' ); |
102
|
|
|
|
103
|
|
|
$this->set_up_mocks(); |
104
|
|
|
|
105
|
|
|
$this->assertFalse( $this->plugins_handler->should_autoloader_reset() ); |
106
|
|
|
$this->assertEmpty( $jetpack_autoloader_activating_plugins ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Tests should_autoloader_reset() with an activating, unknown plugin. |
111
|
|
|
* |
112
|
|
|
* @covers Plugins_Handler::should_autoloader_reset |
113
|
|
|
*/ |
114
|
|
|
public function test_should_autoloader_reset_unknown_plugin() { |
115
|
|
|
global $jetpack_autoloader_activating_plugins; |
116
|
|
|
|
117
|
|
|
$current_plugin = 'unknown'; |
118
|
|
|
|
119
|
|
|
// 'unknown' is not in self::DEFAULT_ACTIVE_PLUGINS. |
120
|
|
|
$this->plugins_handler |
121
|
|
|
->method( 'get_current_plugin_dir' ) |
122
|
|
|
->willReturn( $current_plugin ); |
123
|
|
|
|
124
|
|
|
$this->set_up_mocks(); |
125
|
|
|
|
126
|
|
|
$this->assertTrue( $this->plugins_handler->should_autoloader_reset() ); |
127
|
|
|
$this->assertCount( 1, $jetpack_autoloader_activating_plugins ); |
128
|
|
|
$this->assertEquals( $current_plugin, $jetpack_autoloader_activating_plugins[0] ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Tests get_all_active_plugins() with activating plugins (via request and |
133
|
|
|
* non-request methods) and a list of active plugins. |
134
|
|
|
* |
135
|
|
|
* @covers Plugins_Handler::get_all_active_plugins |
136
|
|
|
*/ |
137
|
|
|
public function test_get_all_active_plugins_everything() { |
138
|
|
|
global $jetpack_autoloader_activating_plugins; |
139
|
|
|
|
140
|
|
|
// Activating plugin. |
141
|
|
|
$activating_plugin = 'activating'; |
142
|
|
|
$jetpack_autoloader_activating_plugins = array( $activating_plugin ); |
143
|
|
|
|
144
|
|
|
// Plugin activating via a request. |
145
|
|
|
$request_plugin_dir = 'request'; |
146
|
|
|
$request_plugin = $request_plugin_dir . '/request.php'; |
147
|
|
|
$_REQUEST['action'] = 'activate'; |
148
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
149
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
150
|
|
|
|
151
|
|
|
// Use default active plugins. |
152
|
|
|
$this->set_up_mocks(); |
153
|
|
|
|
154
|
|
|
$expected_output = array_merge( |
155
|
|
|
array( $activating_plugin ), |
156
|
|
|
array( $request_plugin_dir ), |
157
|
|
|
self::DEFAULT_ACTIVE_PLUGIN_DIRS |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins(); |
161
|
|
|
|
162
|
|
|
sort( $actual_output ); |
163
|
|
|
sort( $expected_output ); |
164
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Tests get_all_active_plugins() with multiple plugins activating (via request and |
169
|
|
|
* non-request methods) and a list of active plugins. |
170
|
|
|
* |
171
|
|
|
* @covers Plugins_Handler::get_all_active_plugins |
172
|
|
|
*/ |
173
|
|
|
public function test_get_all_active_plugins_multiple_activating() { |
174
|
|
|
global $jetpack_autoloader_activating_plugins; |
175
|
|
|
|
176
|
|
|
// Activating plugins. |
177
|
|
|
$activating_plugins = array( 'activating1', 'activating2' ); |
178
|
|
|
|
179
|
|
|
$jetpack_autoloader_activating_plugins = $activating_plugins; |
180
|
|
|
|
181
|
|
|
// Plugins activating via a request. |
182
|
|
|
$request_plugin_dirs = array( |
183
|
|
|
'request1', |
184
|
|
|
'request2', |
185
|
|
|
'request3', |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$request_plugins = array(); |
189
|
|
|
foreach ( $request_plugin_dirs as $request_plugin ) { |
190
|
|
|
$request_plugins[] = $request_plugin . '/' . $request_plugin . '.php'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$_REQUEST['action'] = 'activate-selected'; |
194
|
|
|
$_REQUEST['checked'] = $request_plugins; |
195
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
196
|
|
|
|
197
|
|
|
// Use default active plugins. |
198
|
|
|
$this->set_up_mocks(); |
199
|
|
|
|
200
|
|
|
$expected_output = array_merge( |
201
|
|
|
$activating_plugins, |
202
|
|
|
$request_plugin_dirs, |
203
|
|
|
self::DEFAULT_ACTIVE_PLUGIN_DIRS |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins(); |
207
|
|
|
|
208
|
|
|
sort( $actual_output ); |
209
|
|
|
sort( $expected_output ); |
210
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Tests get_all_active_plugins() with no nonce included in the request. Since |
215
|
|
|
* a nonce isn't included, the plugin will not be activated. |
216
|
|
|
* |
217
|
|
|
* @covers Plugins_Handler::get_all_active_plugins |
218
|
|
|
*/ |
219
|
|
|
public function test_get_all_active_plugins_no_nonce() { |
220
|
|
|
global $jetpack_autoloader_activating_plugins; |
221
|
|
|
|
222
|
|
|
// Activating plugin. |
223
|
|
|
$activating_plugin = 'activating'; |
224
|
|
|
$jetpack_autoloader_activating_plugins = array( $activating_plugin ); |
225
|
|
|
|
226
|
|
|
// Plugin activating via a request without a nonce. |
227
|
|
|
$request_plugin = 'request/request.php'; |
228
|
|
|
$_REQUEST['action'] = 'activate'; |
229
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
230
|
|
|
|
231
|
|
|
// Use default active plugins. |
232
|
|
|
$this->set_up_mocks(); |
233
|
|
|
|
234
|
|
|
// The plugin activating via a request should not be in the output. |
235
|
|
|
$expected_output = array_merge( |
236
|
|
|
array( $activating_plugin ), |
237
|
|
|
self::DEFAULT_ACTIVE_PLUGIN_DIRS |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins(); |
241
|
|
|
|
242
|
|
|
sort( $actual_output ); |
243
|
|
|
sort( $expected_output ); |
244
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Tests get_all_active_plugins() with no activating plugins. |
249
|
|
|
* |
250
|
|
|
* @covers Plugins_Handler::get_all_active_plugins |
251
|
|
|
*/ |
252
|
|
|
public function test_get_all_active_plugins_no_activating() { |
253
|
|
|
// Plugin deactivating via a request. |
254
|
|
|
$request_plugin = 'request/request.php'; |
255
|
|
|
$_REQUEST['action'] = 'deactivate'; |
256
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
257
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
258
|
|
|
|
259
|
|
|
// Use default active plugins. |
260
|
|
|
$this->set_up_mocks(); |
261
|
|
|
|
262
|
|
|
$expected_output = self::DEFAULT_ACTIVE_PLUGIN_DIRS; |
263
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins(); |
264
|
|
|
|
265
|
|
|
sort( $actual_output ); |
266
|
|
|
sort( $expected_output ); |
267
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Tests get_all_active_plugins() with no activating plugins and a single |
273
|
|
|
* active plugin. |
274
|
|
|
* |
275
|
|
|
* @covers Plugins_Handler::get_all_active_plugins |
276
|
|
|
*/ |
277
|
|
|
public function test_get_all_active_plugins_single_active() { |
278
|
|
|
$active_plugin_dir = 'test'; |
279
|
|
|
$active_plugin = array( $active_plugin_dir . '/test.php' ); |
280
|
|
|
$this->set_up_mocks( $active_plugin ); |
281
|
|
|
|
282
|
|
|
$expected_output = array( $active_plugin_dir ); |
283
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins(); |
284
|
|
|
|
285
|
|
|
sort( $actual_output ); |
286
|
|
|
sort( $expected_output ); |
287
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Tests get_all_active_plugins() with an active single-file plugin and single-file |
292
|
|
|
* plugins skipped. |
293
|
|
|
* |
294
|
|
|
* @covers Plugins_Handler::get_all_active_plugins |
295
|
|
|
*/ |
296
|
|
|
public function test_get_all_active_plugins_single_file_plugin() { |
297
|
|
|
$active_plugin_dir = 'test'; |
298
|
|
|
$active_plugin = array( $active_plugin_dir . '/test.php' ); |
299
|
|
|
$active_plugins = array_merge( $active_plugin, array( 'single_file.php' ) ); |
300
|
|
|
$this->set_up_mocks( $active_plugins ); |
301
|
|
|
|
302
|
|
|
$expected_output = array( $active_plugin_dir ); |
303
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins(); |
304
|
|
|
|
305
|
|
|
sort( $actual_output ); |
306
|
|
|
sort( $expected_output ); |
307
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Tests get_all_active_plugins with activating plugins (via request and |
312
|
|
|
* non-request methods) and a list of active plugins. |
313
|
|
|
* |
314
|
|
|
* @covers Plugins_Handler::get_all_active_plugins |
315
|
|
|
*/ |
316
|
|
|
public function test_get_all_active_plugins_multisite() { |
317
|
|
|
global $jetpack_autoloader_activating_plugins; |
318
|
|
|
|
319
|
|
|
// Activating plugin. |
320
|
|
|
$activating_plugin = 'activating_plugin'; |
321
|
|
|
$jetpack_autoloader_activating_plugins = array( $activating_plugin ); |
322
|
|
|
|
323
|
|
|
// Plugin activating via a request. |
324
|
|
|
$request_plugin_dir = 'request'; |
325
|
|
|
$request_plugin = $request_plugin_dir . '/request.php'; |
326
|
|
|
$_REQUEST['action'] = 'activate'; |
327
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
328
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
329
|
|
|
|
330
|
|
|
$this->set_up_mocks( self::DEFAULT_ACTIVE_PLUGINS, true ); |
331
|
|
|
|
332
|
|
|
$expected_output = array_merge( |
333
|
|
|
array( $activating_plugin ), |
334
|
|
|
array( $request_plugin_dir ), |
335
|
|
|
self::DEFAULT_ACTIVE_PLUGIN_DIRS, |
336
|
|
|
self::DEFAULT_MULTISITE_PLUGIN_DIRS |
337
|
|
|
); |
338
|
|
|
|
339
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins(); |
340
|
|
|
|
341
|
|
|
sort( $actual_output ); |
342
|
|
|
sort( $expected_output ); |
343
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|