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
|
|
|
define( 'WP_PLUGIN_DIR', '/var/www/wp-content/plugins' ); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Provides unit tests for the methods in the Plugins_Handler class. |
14
|
|
|
*/ |
15
|
|
|
class PluginsHandlerTest extends TestCase { |
16
|
|
|
|
17
|
|
|
const DEFAULT_ACTIVE_PLUGINS = array( |
18
|
|
|
'/var/www/wp-content/plugins/test_1', |
19
|
|
|
'/var/www/wp-content/plugins/test_2', |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
const DEFAULT_MULTISITE_PLUGINS = array( |
23
|
|
|
'/var/www/wp-content/plugins/multi_1', |
24
|
|
|
'/var/www/wp-content/plugins/multi_2', |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This method is called before each test. |
29
|
|
|
* |
30
|
|
|
* @before |
31
|
|
|
*/ |
32
|
|
|
public function set_up() { |
33
|
|
|
$this->plugins_handler = $this->getMockBuilder( 'Plugins_Handler' ) |
34
|
|
|
->setMethods( |
35
|
|
|
array( |
36
|
|
|
'get_current_plugin_path', |
37
|
|
|
'get_multisite_plugins_paths', |
38
|
|
|
'get_active_plugins_paths', |
39
|
|
|
) |
40
|
|
|
) |
41
|
|
|
->getMock(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set up mock Plugins_Handler methods. |
46
|
|
|
* |
47
|
|
|
* @param Array $active_plugins The names of the active plugins. |
48
|
|
|
* @param Boolean $use_multisite Whether the env is multisite. |
49
|
|
|
*/ |
50
|
|
|
private function set_up_mocks( |
51
|
|
|
$active_plugins = self::DEFAULT_ACTIVE_PLUGINS, |
52
|
|
|
$use_multisite = false ) { |
53
|
|
|
|
54
|
|
|
$this->plugins_handler |
55
|
|
|
->method( 'get_active_plugins_paths' ) |
56
|
|
|
->willReturn( (array) $active_plugins ); |
57
|
|
|
|
58
|
|
|
if ( $use_multisite ) { |
59
|
|
|
$this->plugins_handler |
60
|
|
|
->method( 'get_multisite_plugins_paths' ) |
61
|
|
|
->willReturn( self::DEFAULT_MULTISITE_PLUGINS ); |
62
|
|
|
} else { |
63
|
|
|
$this->plugins_handler |
64
|
|
|
->method( 'get_multisite_plugins_paths' ) |
65
|
|
|
->willReturn( array() ); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Tests is_directory_plugin() with a single-file plugin. |
71
|
|
|
* |
72
|
|
|
* @covers Plugins_Handler::is_directory_plugin |
73
|
|
|
*/ |
74
|
|
|
public function test_is_directory_plugin_single_file() { |
75
|
|
|
$this->assertFalse( $this->plugins_handler->is_directory_plugin( 'test.php' ) ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Tests is_directory_plugin() with an empty string. |
80
|
|
|
* |
81
|
|
|
* @covers Plugins_Handler::is_directory_plugin |
82
|
|
|
*/ |
83
|
|
|
public function test_is_directory_plugin_single_file_with_empty_string() { |
84
|
|
|
$this->assertFalse( $this->plugins_handler->is_directory_plugin( '' ) ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tests is_directory_plugin() with a single-file plugin that begins with '/'. |
89
|
|
|
* |
90
|
|
|
* @covers Plugins_Handler::is_directory_plugin |
91
|
|
|
*/ |
92
|
|
|
public function test_is_directory_plugin_single_file_with_slash() { |
93
|
|
|
$this->assertFalse( $this->plugins_handler->is_directory_plugin( '/test.php' ) ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Tests is_directory_plugin() with a plugin with a directory. |
98
|
|
|
* |
99
|
|
|
* @covers Plugins_Handler::is_directory_plugin |
100
|
|
|
*/ |
101
|
|
|
public function test_is_directory_plugin_dir() { |
102
|
|
|
$this->assertTrue( $this->plugins_handler->is_directory_plugin( 'test/test.php' ) ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Tests get_all_active_plugins_paths() with activating plugins (via request and |
107
|
|
|
* non-request methods) and a list of active plugins. |
108
|
|
|
* |
109
|
|
|
* @covers Plugins_Handler::get_all_active_plugins_paths |
110
|
|
|
*/ |
111
|
|
|
public function test_get_all_active_plugins_everything() { |
112
|
|
|
global $jetpack_autoloader_activating_plugins_paths; |
113
|
|
|
|
114
|
|
|
// Activating plugin. |
115
|
|
|
$activating_plugin = '/var/www/wp-content/plugins/activating'; |
116
|
|
|
$jetpack_autoloader_activating_plugins_paths = array( $activating_plugin ); |
117
|
|
|
|
118
|
|
|
// Plugin activating via a request. |
119
|
|
|
$request_plugin_dir = 'request'; |
120
|
|
|
$request_plugin = $request_plugin_dir . '/request.php'; |
121
|
|
|
$_REQUEST['action'] = 'activate'; |
122
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
123
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
124
|
|
|
|
125
|
|
|
// Use default active plugins. |
126
|
|
|
$this->set_up_mocks(); |
127
|
|
|
|
128
|
|
|
$expected_output = array_merge( |
129
|
|
|
array( $activating_plugin ), |
130
|
|
|
array( WP_PLUGIN_DIR . '/' . $request_plugin_dir ), |
131
|
|
|
self::DEFAULT_ACTIVE_PLUGINS |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins_paths(); |
135
|
|
|
|
136
|
|
|
sort( $actual_output ); |
137
|
|
|
sort( $expected_output ); |
138
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Tests get_all_active_plugins_paths() with multiple plugins activating (via request and |
143
|
|
|
* non-request methods) and a list of active plugins. |
144
|
|
|
* |
145
|
|
|
* @covers Plugins_Handler::get_all_active_plugins_paths |
146
|
|
|
*/ |
147
|
|
|
public function test_get_all_active_plugins_multiple_activating() { |
148
|
|
|
global $jetpack_autoloader_activating_plugins_paths; |
149
|
|
|
|
150
|
|
|
// Activating plugins. |
151
|
|
|
$activating_plugins = array( |
152
|
|
|
'/var/www/wp-content/plugins/activating_1', |
153
|
|
|
'/var/www/wp-content/plugins/activating_2', |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$jetpack_autoloader_activating_plugins_paths = $activating_plugins; |
157
|
|
|
|
158
|
|
|
// Plugins activating via a request. |
159
|
|
|
$request_plugin_dirs = array( |
160
|
|
|
'request1', |
161
|
|
|
'request2', |
162
|
|
|
'request3', |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$request_plugins = array(); |
166
|
|
|
foreach ( $request_plugin_dirs as $request_plugin ) { |
167
|
|
|
$request_plugins[] = $request_plugin . '/' . $request_plugin . '.php'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$request_paths = array(); |
171
|
|
|
foreach ( $request_plugin_dirs as $request_plugin ) { |
172
|
|
|
$request_paths[] = WP_PLUGIN_DIR . '/' . $request_plugin; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$_REQUEST['action'] = 'activate-selected'; |
176
|
|
|
$_REQUEST['checked'] = $request_plugins; |
177
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
178
|
|
|
|
179
|
|
|
// Use default active plugins. |
180
|
|
|
$this->set_up_mocks(); |
181
|
|
|
|
182
|
|
|
$expected_output = array_merge( |
183
|
|
|
$activating_plugins, |
184
|
|
|
$request_paths, |
185
|
|
|
self::DEFAULT_ACTIVE_PLUGINS |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins_paths(); |
189
|
|
|
|
190
|
|
|
sort( $actual_output ); |
191
|
|
|
sort( $expected_output ); |
192
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Tests get_all_active_plugins_paths() with no nonce included in the request. Since |
197
|
|
|
* a nonce isn't included, the plugin will not be activated. |
198
|
|
|
* |
199
|
|
|
* @covers Plugins_Handler::get_all_active_plugins_paths |
200
|
|
|
*/ |
201
|
|
|
public function test_get_all_active_plugins_no_nonce() { |
202
|
|
|
global $jetpack_autoloader_activating_plugins_paths; |
203
|
|
|
|
204
|
|
|
// Activating plugin. |
205
|
|
|
$activating_plugin = '/var/www/wp-content/plugins/activating'; |
206
|
|
|
$jetpack_autoloader_activating_plugins_paths = array( $activating_plugin ); |
207
|
|
|
|
208
|
|
|
// Plugin activating via a request without a nonce. |
209
|
|
|
$request_plugin = 'request/request.php'; |
210
|
|
|
$_REQUEST['action'] = 'activate'; |
211
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
212
|
|
|
|
213
|
|
|
// Use default active plugins. |
214
|
|
|
$this->set_up_mocks(); |
215
|
|
|
|
216
|
|
|
// The plugin activating via a request should not be in the output. |
217
|
|
|
$expected_output = array_merge( |
218
|
|
|
array( $activating_plugin ), |
219
|
|
|
self::DEFAULT_ACTIVE_PLUGINS |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins_paths(); |
223
|
|
|
|
224
|
|
|
sort( $actual_output ); |
225
|
|
|
sort( $expected_output ); |
226
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Tests get_all_active_plugins_paths() with no activating plugins. |
231
|
|
|
* |
232
|
|
|
* @covers Plugins_Handler::get_all_active_plugins_paths |
233
|
|
|
*/ |
234
|
|
|
public function test_get_all_active_plugins_no_activating() { |
235
|
|
|
// Plugin deactivating via a request. |
236
|
|
|
$request_plugin = 'request/request.php'; |
237
|
|
|
$_REQUEST['action'] = 'deactivate'; |
238
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
239
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
240
|
|
|
|
241
|
|
|
// Use default active plugins. |
242
|
|
|
$this->set_up_mocks(); |
243
|
|
|
|
244
|
|
|
$expected_output = self::DEFAULT_ACTIVE_PLUGINS; |
245
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins_paths(); |
246
|
|
|
|
247
|
|
|
sort( $actual_output ); |
248
|
|
|
sort( $expected_output ); |
249
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Tests get_all_active_plugins_paths with activating plugins (via request and |
254
|
|
|
* non-request methods) and a list of active plugins. |
255
|
|
|
* |
256
|
|
|
* @covers Plugins_Handler::get_all_active_plugins_paths |
257
|
|
|
*/ |
258
|
|
|
public function test_get_all_active_plugins_multisite() { |
259
|
|
|
global $jetpack_autoloader_activating_plugins_paths; |
260
|
|
|
|
261
|
|
|
// Activating plugin. |
262
|
|
|
$activating_plugin = '/var/www/wp-content/plugins/activating'; |
263
|
|
|
$jetpack_autoloader_activating_plugins_paths = array( $activating_plugin ); |
264
|
|
|
|
265
|
|
|
// Plugin activating via a request. |
266
|
|
|
$request_plugin_dir = 'request'; |
267
|
|
|
$request_plugin = $request_plugin_dir . '/request.php'; |
268
|
|
|
$_REQUEST['action'] = 'activate'; |
269
|
|
|
$_REQUEST['plugin'] = $request_plugin; |
270
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
271
|
|
|
|
272
|
|
|
$this->set_up_mocks( self::DEFAULT_ACTIVE_PLUGINS, true ); |
273
|
|
|
|
274
|
|
|
$expected_output = array_merge( |
275
|
|
|
array( $activating_plugin ), |
276
|
|
|
array( WP_PLUGIN_DIR . '/' . $request_plugin_dir ), |
277
|
|
|
self::DEFAULT_ACTIVE_PLUGINS, |
278
|
|
|
self::DEFAULT_MULTISITE_PLUGINS |
279
|
|
|
); |
280
|
|
|
|
281
|
|
|
$actual_output = $this->plugins_handler->get_all_active_plugins_paths(); |
282
|
|
|
|
283
|
|
|
sort( $actual_output ); |
284
|
|
|
sort( $expected_output ); |
285
|
|
|
$this->assertEquals( $expected_output, $actual_output ); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|