Completed
Push — fix/idc-same-heal ( 5f98ee...a3787f )
by
unknown
08:15
created

test_should_autoloader_reset_unknown_plugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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