Completed
Push — add/dynamic-autoloader-tests ( abcf7a )
by
unknown
414:34 queued 404:58
created

PluginLocatorTest::set_up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Plugin guesser test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
// We live in the namespace of the test autoloader to avoid many use statements.
9
namespace Automattic\Jetpack\Autoloader\jpCurrent;
10
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * Test suite class for the Autoloader part that handles active plugin guessing.
15
 *
16
 * @runClassInSeparateProcess
17
 * @preserveGlobalState disabled
18
 */
19
class PluginLocatorTest extends TestCase {
20
21
	/**
22
	 * A mock of the path processor we're using.
23
	 *
24
	 * @var Path_Processor|\PHPUnit\Framework\MockObject\MockObject
25
	 */
26
	private $path_processor;
27
28
	/**
29
	 * The plugin locator that we're testing.
30
	 *
31
	 * @var Plugin_Locator
32
	 */
33
	private $locator;
34
35
	/**
36
	 * Setup runs before each test.
37
	 *
38
	 * @before
39
	 */
40
	public function set_up() {
41
		$this->path_processor = $this->getMockBuilder( Path_Processor::class )->getMock();
42
		$this->locator        = new Plugin_Locator( $this->path_processor );
43
	}
44
45
	/**
46
	 * Teardown runs after each test.
47
	 *
48
	 * @after
49
	 */
50
	public function tear_down() {
51
		// Make sure all of the test data we made use of is cleaned up after each test.
52
		cleanup_test_wordpress_data();
53
	}
54
55
	/**
56
	 * Tests that the locator is able to find the path to the currently executing plugin.
57
	 */
58 View Code Duplication
	public function test_finds_current_plugin() {
59
		$this->path_processor->expects( $this->once() )
60
			->method( 'find_directory_with_autoloader' )
61
			->with( TEST_PLUGIN_DIR, array() )
62
			->willReturn( TEST_PLUGIN_DIR );
63
64
		$path = $this->locator->find_current_plugin();
65
66
		$this->assertEquals( TEST_PLUGIN_DIR, $path );
67
	}
68
69
	/**
70
	 * Tests that the locator throws an exception when the currently executing plugin is not an autoloaded plugin.
71
	 */
72 View Code Duplication
	public function test_finds_current_plugin_throws_exception_when_not_autoloaded() {
73
		$this->path_processor->expects( $this->once() )
74
			->method( 'find_directory_with_autoloader' )
75
			->with( TEST_PLUGIN_DIR, array() )
76
			->willReturn( false );
77
78
		$this->expectExceptionMessage( 'Failed to locate plugin' );
79
80
		$this->locator->find_current_plugin();
81
	}
82
83
	/**
84
	 * Tests that guessing using option doesn't break when looking for plugins that don't exist.
85
	 */
86
	public function test_using_option_does_nothing_without_valid_plugin() {
87
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
88
		$this->assertTrue( is_array( $plugin_paths ) );
89
		$this->assertEmpty( $plugin_paths );
90
91
		add_test_option(
92
			'test_plugin_paths',
93
			array( 'test/test.php' )
94
		);
95
		$this->path_processor->expects( $this->exactly( 2 ) )
96
			->method( 'find_directory_with_autoloader' )
97
			->withConsecutive(
98
				array( 0 ),
99
				array( 'test/test.php' )
100
			)
101
			->willReturn( false );
102
103
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
104
		$this->assertTrue( is_array( $plugin_paths ) );
105
		$this->assertEmpty( $plugin_paths );
106
	}
107
108
	/**
109
	 * Tests that it can guess plugins that are stored in an option.
110
	 */
111 View Code Duplication
	public function test_using_option_finds_in_option() {
112
		add_test_option(
113
			'test_plugin_paths',
114
			array( 'dummy_current/dummy_current.php' )
115
		);
116
117
		$this->path_processor->expects( $this->exactly( 2 ) )
118
			->method( 'find_directory_with_autoloader' )
119
			->withConsecutive(
120
				array( 0 ),
121
				array( 'dummy_current/dummy_current.php' )
122
			)
123
			->willReturnOnConsecutiveCalls( false, WP_PLUGIN_DIR . '/dummy_current' );
124
125
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
126
127
		$this->assertTrue( is_array( $plugin_paths ) );
128
		$this->assertCount( 1, $plugin_paths );
129
		$this->assertContains( WP_PLUGIN_DIR . '/dummy_current', $plugin_paths );
130
	}
131
132
	/**
133
	 * Tests that it can find plugins that are stored in a site option.
134
	 */
135 View Code Duplication
	public function test_using_option_finds_in_site_option() {
136
		add_test_site_option(
137
			'test_plugin_paths',
138
			array( 'dummy_current/dummy_current.php' )
139
		);
140
141
		$this->path_processor->expects( $this->exactly( 2 ) )
142
			->method( 'find_directory_with_autoloader' )
143
			->withConsecutive(
144
				array( 0 ),
145
				array( 'dummy_current/dummy_current.php' )
146
			)
147
			->willReturnOnConsecutiveCalls( false, WP_PLUGIN_DIR . '/dummy_current' );
148
149
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths', true );
150
151
		$this->assertTrue( is_array( $plugin_paths ) );
152
		$this->assertCount( 1, $plugin_paths );
153
		$this->assertContains( WP_PLUGIN_DIR . '/dummy_current', $plugin_paths );
154
	}
155
156
	/**
157
	 * Tests that it can guess plugins that are stored in an option's key.
158
	 */
159 View Code Duplication
	public function test_using_option_finds_plugin_in_key() {
160
		add_test_option(
161
			'test_plugin_paths',
162
			array( 'dummy_current/dummy_current.php' => 123456 )
163
		);
164
165
		$this->path_processor->expects( $this->exactly( 2 ) )
166
			->method( 'find_directory_with_autoloader' )
167
			->withConsecutive(
168
				array( 'dummy_current/dummy_current.php' ),
169
				array( 123456 )
170
			)
171
			->willReturnOnConsecutiveCalls( WP_PLUGIN_DIR . '/dummy_current', false );
172
173
		$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' );
174
175
		$this->assertTrue( is_array( $plugin_paths ) );
176
		$this->assertCount( 1, $plugin_paths );
177
		$this->assertContains( WP_PLUGIN_DIR . '/dummy_current', $plugin_paths );
178
	}
179
180
	/**
181
	 * Tests that plugins in request parameters are not discovered if a nonce is not set.
182
	 */
183
	public function test_using_request_action_returns_nothing_without_nonce() {
184
		$_REQUEST['action'] = 'activate';
185
		$_REQUEST['plugin'] = 'dummy_current/dummy_current.php';
186
187
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) );
188
189
		$this->assertTrue( is_array( $plugin_paths ) );
190
		$this->assertEmpty( $plugin_paths );
191
192
		$_REQUEST['action'] = 'deactivate';
193
		$_REQUEST['plugin'] = 'dummy_current/dummy_current.php';
194
195
		$plugin_paths = $this->locator->find_using_request_action( array( 'deactivate' ) );
196
197
		$this->assertTrue( is_array( $plugin_paths ) );
198
		$this->assertEmpty( $plugin_paths );
199
200
		$_REQUEST['action']  = 'activate-selected';
201
		$_REQUEST['checked'] = array( 'dummy_current/dummy_current.php' );
202
203
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate-selected' ) );
204
205
		$this->assertTrue( is_array( $plugin_paths ) );
206
		$this->assertEmpty( $plugin_paths );
207
	}
208
209
	/**
210
	 * Tests that plugins in the request action are not found if the action is not found.
211
	 */
212
	public function test_using_request_action_returns_nothing_without_action() {
213
		$_REQUEST['_wpnonce'] = '123abc';
214
		$_REQUEST['action']   = '';
215
216
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) );
217
218
		$this->assertTrue( is_array( $plugin_paths ) );
219
		$this->assertEmpty( $plugin_paths );
220
221
		$_REQUEST['action'] = 'activate';
222
		$_REQUEST['plugin'] = 'dummy_current\\\\dummy_current.php';
223
224
		$plugin_paths = $this->locator->find_using_request_action( array() );
225
226
		$this->assertTrue( is_array( $plugin_paths ) );
227
		$this->assertEmpty( $plugin_paths );
228
	}
229
230
	/**
231
	 * Tests that plugins in the request action can be found for single actions.
232
	 */
233 View Code Duplication
	public function test_using_request_action_works_for_single() {
234
		$_REQUEST['_wpnonce'] = '123abc';
235
		$_REQUEST['action']   = 'activate';
236
		$_REQUEST['plugin']   = 'dummy_current\\\\dummy_current.php';
237
238
		$this->path_processor->expects( $this->exactly( 2 ) )
239
			->method( 'find_directory_with_autoloader' )
240
			->withConsecutive(
241
				array( 0 ),
242
				array( 'dummy_current\\dummy_current.php' )
243
			)
244
			->willReturnOnConsecutiveCalls( false, WP_PLUGIN_DIR . '/dummy_current' );
245
246
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) );
247
248
		$this->assertTrue( is_array( $plugin_paths ) );
249
		$this->assertCount( 1, $plugin_paths );
250
		$this->assertContains( WP_PLUGIN_DIR . '/dummy_current', $plugin_paths );
251
	}
252
253
	/**
254
	 * Tests that plugins in the request action can be found for multiple actions.
255
	 */
256 View Code Duplication
	public function test_using_request_action_works_for_multiple() {
257
		$_REQUEST['_wpnonce'] = '123abc';
258
		$_REQUEST['action']   = 'activate-selected';
259
		$_REQUEST['checked']  = array( 'dummy_current\\\\dummy_current.php' );
260
261
		$this->path_processor->expects( $this->exactly( 2 ) )
262
			->method( 'find_directory_with_autoloader' )
263
			->withConsecutive(
264
				array( 0 ),
265
				array( 'dummy_current\\dummy_current.php' )
266
			)
267
			->willReturnOnConsecutiveCalls( false, WP_PLUGIN_DIR . '/dummy_current' );
268
269
		$plugin_paths = $this->locator->find_using_request_action( array( 'activate-selected' ) );
270
271
		$this->assertTrue( is_array( $plugin_paths ) );
272
		$this->assertCount( 1, $plugin_paths );
273
		$this->assertContains( WP_PLUGIN_DIR . '/dummy_current', $plugin_paths );
274
	}
275
}
276