|
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 it does not give errors or warnings when the option contains unexpected data. |
|
182
|
|
|
*/ |
|
183
|
|
|
public function test_using_option_handles_invalid_data() { |
|
184
|
|
|
add_test_option( |
|
185
|
|
|
'test_plugin_paths', |
|
186
|
|
|
'invalid' |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
$this->path_processor->expects( $this->never() )->method( 'find_directory_with_autoloader' ); |
|
190
|
|
|
|
|
191
|
|
|
$plugin_paths = $this->locator->find_using_option( 'test_plugin_paths' ); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
194
|
|
|
$this->assertEmpty( $plugin_paths ); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Tests that plugins in request parameters are not discovered if a nonce is not set. |
|
199
|
|
|
*/ |
|
200
|
|
|
public function test_using_request_action_returns_nothing_without_nonce() { |
|
201
|
|
|
$_REQUEST['action'] = 'activate'; |
|
202
|
|
|
$_REQUEST['plugin'] = 'dummy_current/dummy_current.php'; |
|
203
|
|
|
|
|
204
|
|
|
$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) ); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
207
|
|
|
$this->assertEmpty( $plugin_paths ); |
|
208
|
|
|
|
|
209
|
|
|
$_REQUEST['action'] = 'deactivate'; |
|
210
|
|
|
$_REQUEST['plugin'] = 'dummy_current/dummy_current.php'; |
|
211
|
|
|
|
|
212
|
|
|
$plugin_paths = $this->locator->find_using_request_action( array( 'deactivate' ) ); |
|
213
|
|
|
|
|
214
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
215
|
|
|
$this->assertEmpty( $plugin_paths ); |
|
216
|
|
|
|
|
217
|
|
|
$_REQUEST['action'] = 'activate-selected'; |
|
218
|
|
|
$_REQUEST['checked'] = array( 'dummy_current/dummy_current.php' ); |
|
219
|
|
|
|
|
220
|
|
|
$plugin_paths = $this->locator->find_using_request_action( array( 'activate-selected' ) ); |
|
221
|
|
|
|
|
222
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
223
|
|
|
$this->assertEmpty( $plugin_paths ); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Tests that plugins in the request action are not found if the action is not found. |
|
228
|
|
|
*/ |
|
229
|
|
|
public function test_using_request_action_returns_nothing_without_action() { |
|
230
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
|
231
|
|
|
$_REQUEST['action'] = ''; |
|
232
|
|
|
|
|
233
|
|
|
$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) ); |
|
234
|
|
|
|
|
235
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
236
|
|
|
$this->assertEmpty( $plugin_paths ); |
|
237
|
|
|
|
|
238
|
|
|
$_REQUEST['action'] = 'activate'; |
|
239
|
|
|
$_REQUEST['plugin'] = 'dummy_current\\\\dummy_current.php'; |
|
240
|
|
|
|
|
241
|
|
|
$plugin_paths = $this->locator->find_using_request_action( array() ); |
|
242
|
|
|
|
|
243
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
244
|
|
|
$this->assertEmpty( $plugin_paths ); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Tests that plugins in the request action can be found for single actions. |
|
249
|
|
|
*/ |
|
250
|
|
View Code Duplication |
public function test_using_request_action_works_for_single() { |
|
251
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
|
252
|
|
|
$_REQUEST['action'] = 'activate'; |
|
253
|
|
|
$_REQUEST['plugin'] = 'dummy_current\\\\dummy_current.php'; |
|
254
|
|
|
|
|
255
|
|
|
$this->path_processor->expects( $this->exactly( 2 ) ) |
|
256
|
|
|
->method( 'find_directory_with_autoloader' ) |
|
257
|
|
|
->withConsecutive( |
|
258
|
|
|
array( 0 ), |
|
259
|
|
|
array( 'dummy_current\\dummy_current.php' ) |
|
260
|
|
|
) |
|
261
|
|
|
->willReturnOnConsecutiveCalls( false, WP_PLUGIN_DIR . '/dummy_current' ); |
|
262
|
|
|
|
|
263
|
|
|
$plugin_paths = $this->locator->find_using_request_action( array( 'activate' ) ); |
|
264
|
|
|
|
|
265
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
266
|
|
|
$this->assertCount( 1, $plugin_paths ); |
|
267
|
|
|
$this->assertContains( WP_PLUGIN_DIR . '/dummy_current', $plugin_paths ); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Tests that plugins in the request action can be found for multiple actions. |
|
272
|
|
|
*/ |
|
273
|
|
View Code Duplication |
public function test_using_request_action_works_for_multiple() { |
|
274
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
|
275
|
|
|
$_REQUEST['action'] = 'activate-selected'; |
|
276
|
|
|
$_REQUEST['checked'] = array( 'dummy_current\\\\dummy_current.php' ); |
|
277
|
|
|
|
|
278
|
|
|
$this->path_processor->expects( $this->exactly( 2 ) ) |
|
279
|
|
|
->method( 'find_directory_with_autoloader' ) |
|
280
|
|
|
->withConsecutive( |
|
281
|
|
|
array( 0 ), |
|
282
|
|
|
array( 'dummy_current\\dummy_current.php' ) |
|
283
|
|
|
) |
|
284
|
|
|
->willReturnOnConsecutiveCalls( false, WP_PLUGIN_DIR . '/dummy_current' ); |
|
285
|
|
|
|
|
286
|
|
|
$plugin_paths = $this->locator->find_using_request_action( array( 'activate-selected' ) ); |
|
287
|
|
|
|
|
288
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
|
289
|
|
|
$this->assertCount( 1, $plugin_paths ); |
|
290
|
|
|
$this->assertContains( WP_PLUGIN_DIR . '/dummy_current', $plugin_paths ); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|