Repository navigation
Expand file tree
/
Copy pathactivatePlugins.php
More file actions
261 lines (216 loc) · 7.73 KB
/
Copy pathactivatePlugins.php
File metadata and controls
261 lines (216 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* Tests for activate_plugins().
*
* @group admin
* @group plugins
*
* @covers ::activate_plugins
*/
class Tests_Admin_Includes_Plugin_ActivatePlugins extends WP_UnitTestCase {
/**
* The slugs of the test plugins.
*
* @var string[]
*/
private $slugs = array();
/**
* The redirect locations passed to wp_redirect().
*
* @var string[]
*/
private $redirects = array();
/**
* Resets the active plugins and prepares unique test plugins.
*/
public function set_up() {
parent::set_up();
update_option( 'active_plugins', array( 'zzz/zzz.php' ) );
update_site_option( 'active_sitewide_plugins', array() );
$suffix = wp_generate_password( 8, false );
$this->slugs = array( "activate-plugins-one-$suffix", "activate-plugins-two-$suffix" );
$this->redirects = array();
add_filter( 'wp_redirect', array( $this, 'record_redirect' ) );
}
/**
* Removes the test plugins and restores global state.
*/
public function tear_down() {
global $wp_plugin_paths;
foreach ( $this->slugs as $slug ) {
$this->remove_plugin( $slug );
if ( is_array( $wp_plugin_paths ) ) {
unset( $wp_plugin_paths[ WP_PLUGIN_DIR . "/$slug/$slug.php" ] );
}
}
wp_cache_delete( 'plugins', 'plugins' );
unset( $_GET['networkwide'] );
parent::tear_down();
}
/**
* Tests that several plugins are activated.
*
* @ticket 66555
*/
public function test_should_activate_several_plugins() {
$this->create_plugins();
$this->assertTrue( activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ) ) );
$this->assertEqualSets(
array( 'zzz/zzz.php', $this->get_file( 0 ), $this->get_file( 1 ) ),
get_option( 'active_plugins' ),
'Both plugins should be active.'
);
}
/**
* Tests that a plugin that is not in an array is activated.
*
* @ticket 66555
*/
public function test_should_activate_a_single_plugin() {
$this->create_plugins();
$this->assertTrue( activate_plugins( $this->get_file( 0 ) ) );
$this->assertContains( $this->get_file( 0 ), get_option( 'active_plugins' ), 'The plugin should be active.' );
$this->assertNotContains( $this->get_file( 1 ), get_option( 'active_plugins' ), 'The other plugin should not be active.' );
}
/**
* Tests that an error is returned when a plugin cannot be activated, and that the other plugins are activated.
*
* @ticket 66555
*/
public function test_should_return_the_errors_of_the_plugins_that_cannot_be_activated() {
$this->create_plugins( array( 1 => " * Requires PHP: 99.0\n" ) );
$result = activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ) );
$this->assertWPError( $result );
$this->assertSame( 'plugins_invalid', $result->get_error_code() );
$this->assertSame( 'One of the plugins is invalid.', $result->get_error_message() );
$errors = $result->get_error_data();
$this->assertSame( array( $this->get_file( 1 ) ), array_keys( $errors ), 'Only the plugin that failed should have an error.' );
$this->assertWPError( $errors[ $this->get_file( 1 ) ] );
$this->assertSame( 'plugin_php_incompatible', $errors[ $this->get_file( 1 ) ]->get_error_code() );
$this->assertContains( $this->get_file( 0 ), get_option( 'active_plugins' ), 'The other plugin should be active.' );
$this->assertNotContains( $this->get_file( 1 ), get_option( 'active_plugins' ), 'The plugin that failed should not be active.' );
}
/**
* Tests that the errors of all the plugins that cannot be activated are returned.
*
* @ticket 66555
*/
public function test_should_return_the_errors_of_all_the_plugins_that_cannot_be_activated() {
$this->create_plugins(
array(
0 => " * Requires PHP: 99.0\n",
1 => " * Requires PHP: 99.0\n",
)
);
$result = activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ) );
$this->assertWPError( $result );
$this->assertSame( array( $this->get_file( 0 ), $this->get_file( 1 ) ), array_keys( $result->get_error_data() ) );
$this->assertSame( array( 'zzz/zzz.php' ), get_option( 'active_plugins' ), 'No plugin should be activated.' );
}
/**
* Tests that the plugin is added to the redirect URL of each plugin.
*
* @ticket 66555
*/
public function test_should_add_each_plugin_to_the_redirect() {
$this->create_plugins();
activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ), 'plugins.php?error=true' );
$this->assertSame(
array(
'plugins.php?error=true&plugin=' . rawurlencode( $this->get_file( 0 ) ) . '&_error_nonce=' . wp_create_nonce( 'plugin-activation-error_' . $this->get_file( 0 ) ),
'plugins.php?error=true&plugin=' . rawurlencode( $this->get_file( 1 ) ) . '&_error_nonce=' . wp_create_nonce( 'plugin-activation-error_' . $this->get_file( 1 ) ),
),
$this->redirects
);
}
/**
* Tests that nothing is added to a redirect that is empty.
*
* @ticket 66555
*/
public function test_should_not_redirect_without_a_redirect_url() {
$this->create_plugins();
activate_plugins( array( $this->get_file( 0 ) ) );
$this->assertSame( array(), $this->redirects );
}
/**
* Tests that the plugins are activated silently.
*
* @ticket 66555
*/
public function test_should_pass_the_silent_argument() {
$this->create_plugins();
$mock = new MockAction();
add_action( 'activated_plugin', array( $mock, 'action' ) );
$this->assertTrue( activate_plugins( array( $this->get_file( 0 ) ), '', false, true ) );
$this->assertSame( array(), $mock->get_events(), 'The activation hooks should not fire.' );
$this->assertTrue( activate_plugins( array( $this->get_file( 1 ) ) ) );
$this->assertCount( 1, $mock->get_events(), 'The activation hook should fire when the plugin is not activated silently.' );
}
/**
* Tests that the plugins are activated for the network.
*
* @ticket 66555
*
* @group ms-required
*/
public function test_should_pass_the_network_wide_argument() {
$this->create_plugins();
$this->assertTrue( activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ), '', true ) );
$this->assertEqualSets(
array( $this->get_file( 0 ), $this->get_file( 1 ) ),
array_keys( get_site_option( 'active_sitewide_plugins' ) ),
'Both plugins should be network active.'
);
$this->assertSame( array( 'zzz/zzz.php' ), get_option( 'active_plugins' ), 'The site plugins should not change.' );
}
/**
* Records redirect locations and stops the redirect.
*
* @param string $location The redirect location.
* @return false False, so that no header is sent.
*/
public function record_redirect( $location ) {
$this->redirects[] = $location;
return false;
}
/**
* Returns the plugin file of a test plugin.
*
* @param int $index The index of the plugin.
* @return string The plugin file.
*/
private function get_file( $index ) {
$slug = $this->slugs[ $index ];
return "$slug/$slug.php";
}
/**
* Creates the test plugins.
*
* @param string[] $headers Optional. Additional plugin headers, by index of the plugin. Default none.
*/
private function create_plugins( array $headers = array() ) {
foreach ( $this->slugs as $index => $slug ) {
$dir = WP_PLUGIN_DIR . '/' . $slug;
$this->remove_plugin( $slug );
$this->assertTrue( mkdir( $dir ), 'The test plugin directory should be created.' );
$this->assertNotFalse(
file_put_contents( "{$dir}/{$slug}.php", "<?php\n/**\n * Plugin Name: Activate Plugins Test Plugin $index\n" . ( $headers[ $index ] ?? '' ) . " */\n" ),
'The test plugin should be written.'
);
}
wp_cache_delete( 'plugins', 'plugins' );
}
/**
* Removes a test plugin, including one left over by an interrupted run.
*
* @param string $slug The slug of the plugin.
*/
private function remove_plugin( $slug ) {
$dir = WP_PLUGIN_DIR . '/' . $slug;
if ( is_dir( $dir ) ) {
$this->rmdir( $dir );
$this->delete_folders( $dir );
}
}
}