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