Completed
Push — renovate/commander-5.x ( da542f...98aca7 )
by
unknown
169:19 queued 151:48
created

test_should_autoloader_reset_known_plugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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