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() ) |
|
|
|
|
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() ) |
|
|
|
|
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 ) ) |
|
|
|
|
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 ) ) |
|
|
|
|
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 ) ) |
|
|
|
|
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 ) ) |
|
|
|
|
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 activating this request are not discovered if a nonce is not set. |
181
|
|
|
*/ |
182
|
|
|
public function test_activating_this_request_does_nothing_without_nonce() { |
183
|
|
|
$_REQUEST['action'] = 'activate'; |
184
|
|
|
$_REQUEST['plugin'] = 'dummy_current/dummy_current.php'; |
185
|
|
|
|
186
|
|
|
$plugin_paths = $this->locator->find_activating_this_request(); |
187
|
|
|
|
188
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
189
|
|
|
$this->assertEmpty( $plugin_paths ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Tests that plugins activating this request are not discovered if there aren't any. |
194
|
|
|
*/ |
195
|
|
|
public function test_activating_this_request_does_nothing_without_parameters() { |
196
|
|
|
$plugin_paths = $this->locator->find_activating_this_request(); |
197
|
|
|
|
198
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
199
|
|
|
$this->assertEmpty( $plugin_paths ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Tests that single plugins activating this request are found. |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
public function test_activating_this_request_works_for_single() { |
206
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
207
|
|
|
$_REQUEST['action'] = 'activate'; |
208
|
|
|
$_REQUEST['plugin'] = 'dummy_current\\\\dummy_current.php'; |
209
|
|
|
|
210
|
|
|
$this->path_processor->expects( $this->exactly( 2 ) ) |
|
|
|
|
211
|
|
|
->method( 'find_directory_with_autoloader' ) |
212
|
|
|
->withConsecutive( |
213
|
|
|
array( 0 ), |
214
|
|
|
array( 'dummy_current\\dummy_current.php' ) |
215
|
|
|
) |
216
|
|
|
->willReturnOnConsecutiveCalls( false, TEST_DATA_PATH . '/plugins/dummy_current' ); |
217
|
|
|
|
218
|
|
|
$plugin_paths = $this->locator->find_activating_this_request(); |
219
|
|
|
|
220
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
221
|
|
|
$this->assertCount( 1, $plugin_paths ); |
222
|
|
|
$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_current', $plugin_paths ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Tests that multiple plugins activating this request are found. |
227
|
|
|
*/ |
228
|
|
View Code Duplication |
public function test_activating_this_request_works_for_multiple() { |
229
|
|
|
$_REQUEST['_wpnonce'] = '123abc'; |
230
|
|
|
$_REQUEST['action'] = 'activate-selected'; |
231
|
|
|
$_REQUEST['checked'] = array( 'dummy_current\\\\dummy_current.php' ); |
232
|
|
|
|
233
|
|
|
$this->path_processor->expects( $this->exactly( 2 ) ) |
|
|
|
|
234
|
|
|
->method( 'find_directory_with_autoloader' ) |
235
|
|
|
->withConsecutive( |
236
|
|
|
array( 0 ), |
237
|
|
|
array( 'dummy_current\\dummy_current.php' ) |
238
|
|
|
) |
239
|
|
|
->willReturnOnConsecutiveCalls( false, TEST_DATA_PATH . '/plugins/dummy_current' ); |
240
|
|
|
|
241
|
|
|
$plugin_paths = $this->locator->find_activating_this_request(); |
242
|
|
|
|
243
|
|
|
$this->assertTrue( is_array( $plugin_paths ) ); |
244
|
|
|
$this->assertCount( 1, $plugin_paths ); |
245
|
|
|
$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_current', $plugin_paths ); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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.