Completed
Push — update/audio-player ( d9f9b9...6dc0dd )
by
unknown
173:42 queued 163:37
created

Test_Plugin_Locator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 259
Duplicated Lines 37.84 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A test_finds_current_plugin() 0 11 1
A test_finds_current_plugin_throws_exception_when_not_autoloaded() 0 11 1
A test_using_option_does_nothing_without_valid_plugin() 0 21 1
A test_using_option_finds_in_option() 20 20 1
A test_using_option_finds_in_site_option() 20 20 1
A test_using_option_finds_plugin_in_key() 20 20 1
A set_up() 0 4 1
A tear_down() 0 4 1
A test_using_request_action_returns_nothing_without_nonce() 0 25 1
A test_using_request_action_returns_nothing_without_action() 0 17 1
A test_using_request_action_works_for_single() 19 19 1
A test_using_request_action_works_for_multiple() 19 19 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
 * Plugin guesser test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * Test suite class for the Autoloader part that handles active plugin guessing.
12
 *
13
 * @runClassInSeparateProcess
14
 * @preserveGlobalState disabled
15
 */
16
class Test_Plugin_Locator extends TestCase {
17
18
	/**
19
	 * A mock of the path processor we're using.
20
	 *
21
	 * @var Path_Processor|\PHPUnit\Framework\MockObject\MockObject
22
	 */
23
	private $path_processor;
24
25
	/**
26
	 * The plugin locator that we're testing.
27
	 *
28
	 * @var Plugin_Locator
29
	 */
30
	private $locator;
31
32
	/**
33
	 * Setup runs before each test.
34
	 *
35
	 * @before
36
	 */
37
	public function set_up() {
38
		$this->path_processor = $this->getMockBuilder( Path_Processor::class )->getMock();
39
		$this->locator        = new Plugin_Locator( $this->path_processor );
40
	}
41
42
	/**
43
	 * Teardown runs after each test.
44
	 *
45
	 * @after
46
	 */
47
	public function tear_down() {
48
		// Make sure all of the test data we made use of is cleaned up after each test.
49
		cleanup_test_wordpress_data();
50
	}
51
52
	/**
53
	 * Tests that the locator is able to find the path to the currently executing plugin.
54
	 */
55
	public function test_finds_current_plugin() {
56
		$this->path_processor->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
			->method( 'find_directory_with_autoloader' )
58
			// Since we're not in a real plugin, just make sure it escapes 3 levels from the `src` folder.
59
			->with( dirname( TEST_REAL_PACKAGE_PATH ), array() )
60
			->willReturn( dirname( TEST_PACKAGE_PATH ) );
61
62
		$path = $this->locator->find_current_plugin();
63
64
		$this->assertEquals( dirname( TEST_PACKAGE_PATH ), $path );
65
	}
66
67
	/**
68
	 * Tests that the locator throws an exception when the currently executing plugin is not an autoloaded plugin.
69
	 */
70
	public function test_finds_current_plugin_throws_exception_when_not_autoloaded() {
71
		$this->path_processor->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
			->method( 'find_directory_with_autoloader' )
73
			// Since we're not in a real plugin, just make sure it escapes 3 levels from the `src` folder.
74
			->with( dirname( TEST_REAL_PACKAGE_PATH ), array() )
75
			->willReturn( false );
76
77
		$this->expectExceptionMessage( 'Failed to locate plugin' );
78
79
		$this->locator->find_current_plugin();
80
	}
81
82
	/**
83
	 * Tests that guessing using option doesn't break when looking for plugins that don't exist.
84
	 */
85
	public function test_using_option_does_nothing_without_valid_plugin() {
86
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
87
		$this->assertTrue( is_array( $plugin_paths ) );
88
		$this->assertEmpty( $plugin_paths );
89
90
		add_test_option(
91
			'test_plugin_paths',
92
			array( 'test/test.php' )
93
		);
94
		$this->path_processor->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
			->method( 'find_directory_with_autoloader' )
96
			->withConsecutive(
97
				array( 0 ),
98
				array( 'test/test.php' )
99
			)
100
			->willReturn( false );
101
102
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
103
		$this->assertTrue( is_array( $plugin_paths ) );
104
		$this->assertEmpty( $plugin_paths );
105
	}
106
107
	/**
108
	 * Tests that it can guess plugins that are stored in an option.
109
	 */
110 View Code Duplication
	public function test_using_option_finds_in_option() {
111
		add_test_option(
112
			'test_plugin_paths',
113
			array( 'dummy_current/dummy_current.php' )
114
		);
115
116
		$this->path_processor->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
			->method( 'find_directory_with_autoloader' )
118
			->withConsecutive(
119
				array( 0 ),
120
				array( 'dummy_current/dummy_current.php' )
121
			)
122
			->willReturnOnConsecutiveCalls( false, TEST_DATA_PATH . '/plugins/dummy_current' );
123
124
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
125
126
		$this->assertTrue( is_array( $plugin_paths ) );
127
		$this->assertCount( 1, $plugin_paths );
128
		$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_current', $plugin_paths );
129
	}
130
131
	/**
132
	 * Tests that it can find plugins that are stored in a site option.
133
	 */
134 View Code Duplication
	public function test_using_option_finds_in_site_option() {
135
		add_test_site_option(
136
			'test_plugin_paths',
137
			array( 'dummy_current/dummy_current.php' )
138
		);
139
140
		$this->path_processor->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
			->method( 'find_directory_with_autoloader' )
142
			->withConsecutive(
143
				array( 0 ),
144
				array( 'dummy_current/dummy_current.php' )
145
			)
146
			->willReturnOnConsecutiveCalls( false, TEST_DATA_PATH . '/plugins/dummy_current' );
147
148
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths', true );
149
150
		$this->assertTrue( is_array( $plugin_paths ) );
151
		$this->assertCount( 1, $plugin_paths );
152
		$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_current', $plugin_paths );
153
	}
154
155
	/**
156
	 * Tests that it can guess plugins that are stored in an option's key.
157
	 */
158 View Code Duplication
	public function test_using_option_finds_plugin_in_key() {
159
		add_test_option(
160
			'test_plugin_paths',
161
			array( 'dummy_current/dummy_current.php' => 123456 )
162
		);
163
164
		$this->path_processor->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
			->method( 'find_directory_with_autoloader' )
166
			->withConsecutive(
167
				array( 'dummy_current/dummy_current.php' ),
168
				array( 123456 )
169
			)
170
			->willReturnOnConsecutiveCalls( TEST_DATA_PATH . '/plugins/dummy_current', false );
171
172
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
173
174
		$this->assertTrue( is_array( $plugin_paths ) );
175
		$this->assertCount( 1, $plugin_paths );
176
		$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_current', $plugin_paths );
177
	}
178
179
	/**
180
	 * Tests that plugins in request parameters are not discovered if a nonce is not set.
181
	 */
182
	public function test_using_request_action_returns_nothing_without_nonce() {
183
		$_REQUEST['action'] = 'activate';
184
		$_REQUEST['plugin'] = 'dummy_current/dummy_current.php';
185
186
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) );
187
188
		$this->assertTrue( is_array( $plugin_paths ) );
189
		$this->assertEmpty( $plugin_paths );
190
191
		$_REQUEST['action'] = 'deactivate';
192
		$_REQUEST['plugin'] = 'dummy_current/dummy_current.php';
193
194
		$plugin_paths = $this->locator->find_using_request_action( array( 'deactivate' ) );
195
196
		$this->assertTrue( is_array( $plugin_paths ) );
197
		$this->assertEmpty( $plugin_paths );
198
199
		$_REQUEST['action']  = 'activate-selected';
200
		$_REQUEST['checked'] = array( 'dummy_current/dummy_current.php' );
201
202
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate-selected' ) );
203
204
		$this->assertTrue( is_array( $plugin_paths ) );
205
		$this->assertEmpty( $plugin_paths );
206
	}
207
208
	/**
209
	 * Tests that plugins in the request action are not found if the action is not found.
210
	 */
211
	public function test_using_request_action_returns_nothing_without_action() {
212
		$_REQUEST['_wpnonce'] = '123abc';
213
		$_REQUEST['action']   = '';
214
215
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) );
216
217
		$this->assertTrue( is_array( $plugin_paths ) );
218
		$this->assertEmpty( $plugin_paths );
219
220
		$_REQUEST['action'] = 'activate';
221
		$_REQUEST['plugin'] = 'dummy_current\\\\dummy_current.php';
222
223
		$plugin_paths = $this->locator->find_using_request_action( array() );
224
225
		$this->assertTrue( is_array( $plugin_paths ) );
226
		$this->assertEmpty( $plugin_paths );
227
	}
228
229
	/**
230
	 * Tests that plugins in the request action can be found for single actions.
231
	 */
232 View Code Duplication
	public function test_using_request_action_works_for_single() {
233
		$_REQUEST['_wpnonce'] = '123abc';
234
		$_REQUEST['action']   = 'activate';
235
		$_REQUEST['plugin']   = 'dummy_current\\\\dummy_current.php';
236
237
		$this->path_processor->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
238
			->method( 'find_directory_with_autoloader' )
239
			->withConsecutive(
240
				array( 0 ),
241
				array( 'dummy_current\\dummy_current.php' )
242
			)
243
			->willReturnOnConsecutiveCalls( false, TEST_DATA_PATH . '/plugins/dummy_current' );
244
245
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) );
246
247
		$this->assertTrue( is_array( $plugin_paths ) );
248
		$this->assertCount( 1, $plugin_paths );
249
		$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_current', $plugin_paths );
250
	}
251
252
	/**
253
	 * Tests that plugins in the request action can be found for multiple actions.
254
	 */
255 View Code Duplication
	public function test_using_request_action_works_for_multiple() {
256
		$_REQUEST['_wpnonce'] = '123abc';
257
		$_REQUEST['action']   = 'activate-selected';
258
		$_REQUEST['checked']  = array( 'dummy_current\\\\dummy_current.php' );
259
260
		$this->path_processor->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
261
			->method( 'find_directory_with_autoloader' )
262
			->withConsecutive(
263
				array( 0 ),
264
				array( 'dummy_current\\dummy_current.php' )
265
			)
266
			->willReturnOnConsecutiveCalls( false, TEST_DATA_PATH . '/plugins/dummy_current' );
267
268
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate-selected' ) );
269
270
		$this->assertTrue( is_array( $plugin_paths ) );
271
		$this->assertCount( 1, $plugin_paths );
272
		$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_current', $plugin_paths );
273
	}
274
}
275