Completed
Push — fix/sync-checksum-comments ( 2626c3...1807a8 )
by
unknown
204:49 queued 195:31
created

PluginsHandlerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 295
Duplicated Lines 7.46 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 22
loc 295
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A set_up() 0 9 1
A tear_down() 0 3 1
A test_gets_active_plugins() 0 27 1
A test_gets_active_plugins_when_multisite() 0 36 1
A test_gets_active_plugins_records_unknown_plugins() 0 25 1
A test_gets_active_plugins_ignores_unknown_plugins_when_desired() 0 20 1
A test_gets_active_plugins_includes_deactivating() 0 25 1
A test_gets_active_plugins_excludes_deactivating() 0 29 1
A test_gets_cached_plugins() 12 12 1
A test_gets_cached_plugins_handles_invalid_data() 0 10 1
A test_updates_cache_writes_plugins() 10 10 1
A test_detects_when_plugins_change() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php // phpcs:ignore WordPress.Files.FileName
2
3
/**
4
 * Plugins handler test suite.
5
 *
6
 * @package automattic/jetpack-autoloader
7
 */
8
9
// We live in the namespace of the test autoloader to avoid many use statements.
10
namespace Automattic\Jetpack\Autoloader\jpCurrent;
11
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * Test suite class for the autoloader's plugin handler.
16
 *
17
 * @runTestsInSeparateProcesses Ensure that each test has no previously autoloaded files.
18
 * @preserveGlobalState disabled
19
 */
20
class PluginsHandlerTest extends TestCase {
21
22
	/**
23
	 * A dependency mock for the handler.
24
	 *
25
	 * @var Plugin_Locator|\PHPUnit\Framework\MockObject\MockObject
26
	 */
27
	private $plugin_locator;
28
29
	/**
30
	 * A dependency mock for the handler.
31
	 *
32
	 * @var Path_Processor|\PHPUnit\Framework\MockObject\MockObject
33
	 */
34
	private $path_processor;
35
36
	/**
37
	 * The class under test.
38
	 *
39
	 * @var Plugins_Handler
40
	 */
41
	private $plugins_handler;
42
43
	/**
44
	 * Setup runs before each test.
45
	 *
46
	 * @before
47
	 */
48
	public function set_up() {
49
		$this->plugin_locator  = $this->getMockBuilder( Plugin_Locator::class )
50
			->disableOriginalConstructor()
51
			->getMock();
52
		$this->path_processor  = $this->getMockBuilder( Path_Processor::class )
53
			->disableOriginalConstructor()
54
			->getMock();
55
		$this->plugins_handler = new Plugins_Handler( $this->plugin_locator, $this->path_processor );
56
	}
57
58
	/**
59
	 * Teardown runs after each test.
60
	 *
61
	 * @after
62
	 */
63
	public function tear_down() {
64
		cleanup_test_wordpress_data();
65
	}
66
67
	/**
68
	 * Tests that all active plugins are found.
69
	 */
70
	public function test_gets_active_plugins() {
71
		global $jetpack_autoloader_activating_plugins_paths;
72
		$jetpack_autoloader_activating_plugins_paths = array( WP_PLUGIN_DIR . '/plugin_activating' );
73
		$this->plugin_locator->expects( $this->once() )
74
			->method( 'find_using_option' )
75
			->with( 'active_plugins', false )
76
			->willReturn( array( WP_PLUGIN_DIR . '/dummy_current' ) );
77
		$this->plugin_locator->expects( $this->once() )
78
			->method( 'find_using_request_action' )
79
			->with( array( 'activate', 'activate-selected', 'deactivate', 'deactivate-selected' ) )
80
			->willReturn( array( WP_PLUGIN_DIR . '/dummy_dev' ) );
81
		$this->plugin_locator->expects( $this->once() )
82
			->method( 'find_current_plugin' )
83
			->willReturn( WP_PLUGIN_DIR . '/dummy_newer' );
84
85
		$plugin_paths = $this->plugins_handler->get_active_plugins( true, true );
86
87
		$this->assertEquals(
88
			array(
89
				WP_PLUGIN_DIR . '/plugin_activating',
90
				WP_PLUGIN_DIR . '/dummy_current',
91
				WP_PLUGIN_DIR . '/dummy_dev',
92
				WP_PLUGIN_DIR . '/dummy_newer',
93
			),
94
			$plugin_paths
95
		);
96
	}
97
98
	/**
99
	 * Tests that all active plugins are found when the site is multisite.
100
	 */
101
	public function test_gets_active_plugins_when_multisite() {
102
		set_test_is_multisite( true );
103
104
		global $jetpack_autoloader_activating_plugins_paths;
105
		$jetpack_autoloader_activating_plugins_paths = array( WP_PLUGIN_DIR . '/plugin_activating' );
106
		$this->plugin_locator->expects( $this->exactly( 2 ) )
107
			->method( 'find_using_option' )
108
			->withConsecutive(
109
				array( 'active_plugins', false ),
110
				array( 'active_sitewide_plugins', true )
111
			)
112
			->willReturnOnConsecutiveCalls(
113
				array( WP_PLUGIN_DIR . '/dummy_current' ),
114
				array( WP_PLUGIN_DIR . '/dummy_newer' )
115
			);
116
		$this->plugin_locator->expects( $this->once() )
117
			->method( 'find_using_request_action' )
118
			->with( array( 'activate', 'activate-selected', 'deactivate', 'deactivate-selected' ) )
119
			->willReturn( array( WP_PLUGIN_DIR . '/dummy_dev' ) );
120
		$this->plugin_locator->expects( $this->once() )
121
			->method( 'find_current_plugin' )
122
			->willReturn( WP_PLUGIN_DIR . '' );
123
124
		$plugin_paths = $this->plugins_handler->get_active_plugins( true, true );
125
126
		$this->assertEquals(
127
			array(
128
				WP_PLUGIN_DIR . '/plugin_activating',
129
				WP_PLUGIN_DIR . '/dummy_current',
130
				WP_PLUGIN_DIR . '/dummy_newer',
131
				WP_PLUGIN_DIR . '/dummy_dev',
132
				WP_PLUGIN_DIR . '',
133
			),
134
			$plugin_paths
135
		);
136
	}
137
138
	/**
139
	 * Tests that the current plugin is recorded as unknown when it isn't found as an active plugin.
140
	 */
141
	public function test_gets_active_plugins_records_unknown_plugins() {
142
		$this->plugin_locator->expects( $this->once() )
143
			->method( 'find_using_option' )
144
			->with( 'active_plugins', false )
145
			->willReturn( array() );
146
		$this->plugin_locator->expects( $this->once() )
147
			->method( 'find_using_request_action' )
148
			->with( array( 'activate', 'activate-selected', 'deactivate', 'deactivate-selected' ) )
149
			->willReturn( array() );
150
		$this->plugin_locator->expects( $this->once() )
151
			->method( 'find_current_plugin' )
152
			->willReturn( WP_PLUGIN_DIR . '/dummy_newer' );
153
154
		$plugin_paths = $this->plugins_handler->get_active_plugins( true, true );
155
156
		$this->assertEquals(
157
			array(
158
				WP_PLUGIN_DIR . '/dummy_newer',
159
			),
160
			$plugin_paths
161
		);
162
163
		global $jetpack_autoloader_activating_plugins_paths;
164
		$this->assertContains( WP_PLUGIN_DIR . '/dummy_newer', $jetpack_autoloader_activating_plugins_paths );
165
	}
166
167
	/**
168
	 * Tests that the current plugin is ignored when it isn't an active plugin but the
169
	 * autoloader was asked to ignore them.
170
	 */
171
	public function test_gets_active_plugins_ignores_unknown_plugins_when_desired() {
172
		$this->plugin_locator->expects( $this->once() )
173
			->method( 'find_using_option' )
174
			->with( 'active_plugins', false )
175
			->willReturn( array() );
176
		$this->plugin_locator->expects( $this->once() )
177
			->method( 'find_using_request_action' )
178
			->with( array( 'activate', 'activate-selected', 'deactivate', 'deactivate-selected' ) )
179
			->willReturn( array() );
180
		$this->plugin_locator->expects( $this->once() )
181
			->method( 'find_current_plugin' )
182
			->willReturn( WP_PLUGIN_DIR . '/dummy_newer' );
183
184
		$plugin_paths = $this->plugins_handler->get_active_plugins( true, false );
185
186
		$this->assertEmpty( $plugin_paths );
187
188
		global $jetpack_autoloader_activating_plugins_paths;
189
		$this->assertEmpty( $jetpack_autoloader_activating_plugins_paths );
190
	}
191
192
	/**
193
	 * Tests that the active plugin list includes those are deactivating
194
	 */
195
	public function test_gets_active_plugins_includes_deactivating() {
196
		global $jetpack_autoloader_activating_plugins_paths;
197
		$jetpack_autoloader_activating_plugins_paths = array();
198
		$this->plugin_locator->expects( $this->once() )
199
			->method( 'find_using_option' )
200
			->with( 'active_plugins', false )
201
			->willReturn( array() );
202
		$this->plugin_locator->expects( $this->once() )
203
			->method( 'find_using_request_action' )
204
			->with( array( 'activate', 'activate-selected', 'deactivate', 'deactivate-selected' ) )
205
			->willReturn( array( WP_PLUGIN_DIR . '/dummy_newer' ) );
206
		$this->plugin_locator->expects( $this->once() )
207
			->method( 'find_current_plugin' )
208
			->willReturn( WP_PLUGIN_DIR . '/dummy_current' );
209
210
		$plugin_paths = $this->plugins_handler->get_active_plugins( true, true );
211
212
		$this->assertEquals(
213
			array(
214
				WP_PLUGIN_DIR . '/dummy_newer',
215
				WP_PLUGIN_DIR . '/dummy_current',
216
			),
217
			$plugin_paths
218
		);
219
	}
220
221
	/**
222
	 * Tests that the active plugin list excludes those that are deactivating.
223
	 */
224
	public function test_gets_active_plugins_excludes_deactivating() {
225
		global $jetpack_autoloader_activating_plugins_paths;
226
		$jetpack_autoloader_activating_plugins_paths = array();
227
		$this->plugin_locator->expects( $this->once() )
228
			->method( 'find_using_option' )
229
			->with( 'active_plugins', false )
230
			->willReturn( array( WP_PLUGIN_DIR . '/dummy_newer' ) );
231
		$this->plugin_locator->expects( $this->exactly( 2 ) )
232
			->method( 'find_using_request_action' )
233
			->withConsecutive(
234
				array( array( 'activate', 'activate-selected', 'deactivate', 'deactivate-selected' ) ),
235
				array( array( 'deactivate', 'deactivate-selected' ) )
236
			)
237
			->willReturnOnConsecutiveCalls(
238
				array( WP_PLUGIN_DIR . '/dummy_dev' ),
239
				array(
240
					WP_PLUGIN_DIR . '/dummy_current',
241
					WP_PLUGIN_DIR . '/dummy_newer',
242
					WP_PLUGIN_DIR . '/dummy_dev',
243
				)
244
			);
245
		$this->plugin_locator->expects( $this->once() )
246
			->method( 'find_current_plugin' )
247
			->willReturn( WP_PLUGIN_DIR . '/dummy_current' );
248
249
		$plugin_paths = $this->plugins_handler->get_active_plugins( false, true );
250
251
		$this->assertEmpty( $plugin_paths );
252
	}
253
254
	/**
255
	 * Tests that the plugins in the cache are loaded.
256
	 */
257 View Code Duplication
	public function test_gets_cached_plugins() {
258
		set_transient( Plugins_Handler::TRANSIENT_KEY, array( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' ) );
259
260
		$this->path_processor->expects( $this->once() )
261
			->method( 'untokenize_path_constants' )
262
			->with( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' )
263
			->willReturn( WP_PLUGIN_DIR . '/dummy_newer' );
264
265
		$plugin_paths = $this->plugins_handler->get_cached_plugins();
266
267
		$this->assertEquals( array( WP_PLUGIN_DIR . '/dummy_newer' ), $plugin_paths );
268
	}
269
270
	/**
271
	 * Tests that an empty array is returned when the cache contains invalid data.
272
	 */
273
	public function test_gets_cached_plugins_handles_invalid_data() {
274
		set_transient( Plugins_Handler::TRANSIENT_KEY, 'invalid' );
275
276
		$this->path_processor->expects( $this->never() )->method( 'untokenize_path_constants' );
277
278
		$plugin_paths = $this->plugins_handler->get_cached_plugins();
279
280
		$this->assertTrue( is_array( $plugin_paths ) );
281
		$this->assertEmpty( $plugin_paths );
282
	}
283
284
	/**
285
	 * Tests that the plugins are updated when they have changed.
286
	 */
287 View Code Duplication
	public function test_updates_cache_writes_plugins() {
288
		$this->path_processor->expects( $this->once() )
289
			->method( 'tokenize_path_constants' )
290
			->with( WP_PLUGIN_DIR . '/dummy_newer' )
291
			->willReturn( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' );
292
293
		$this->plugins_handler->cache_plugins( array( WP_PLUGIN_DIR . '/dummy_newer' ) );
294
295
		$this->assertEquals( array( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' ), get_transient( Plugins_Handler::TRANSIENT_KEY ) );
296
	}
297
298
	/**
299
	 * Tests that the handler indicate whether or not the plugins have changed from the global cached list.
300
	 */
301
	public function test_detects_when_plugins_change() {
302
		global $jetpack_autoloader_cached_plugin_paths;
303
304
		$plugins = array();
305
		$this->assertTrue( $this->plugins_handler->have_plugins_changed( $plugins ) );
306
		$this->assertSame( $plugins, $jetpack_autoloader_cached_plugin_paths );
307
		$this->assertFalse( $this->plugins_handler->have_plugins_changed( $plugins ) );
308
309
		$plugins = array( WP_PLUGIN_DIR . '/dummy_newer' );
310
		$this->assertTrue( $this->plugins_handler->have_plugins_changed( $plugins ) );
311
		$this->assertSame( $plugins, $jetpack_autoloader_cached_plugin_paths );
312
		$this->assertFalse( $this->plugins_handler->have_plugins_changed( $plugins ) );
313
	}
314
}
315