forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize.php
More file actions
303 lines (272 loc) · 8.52 KB
/
initialize.php
File metadata and controls
303 lines (272 loc) · 8.52 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* Tests for the WP_Plugin_Dependencies::initialize() method.
*
* @package WordPress
*/
require_once __DIR__ . '/base.php';
/**
* @group admin
* @group plugins
*
* @covers WP_Plugin_Dependencies::initialize
*/
class Tests_Admin_WPPluginDependencies_Initialize extends WP_PluginDependencies_UnitTestCase {
/**
* Tests that initialization runs only once.
*
* @ticket 60457
*
* @dataProvider data_static_properties_set_during_initialization
*
* @param string $property_name The name of the property to check.
*/
public function test_should_only_initialize_once( $property_name ) {
$this->assertFalse(
$this->get_property_value( 'initialized' ),
'Plugin Dependencies has already been initialized.'
);
self::$instance->initialize();
$this->assertTrue(
$this->get_property_value( 'initialized' ),
'"initialized" was not set to true during initialization.'
);
$default_value = self::$static_properties[ $property_name ];
$this->assertNotSame(
$default_value,
$this->get_property_value( $property_name ),
"\"{$property_name}\" was not set during initialization."
);
// Reset it to its default.
$this->set_property_value( $property_name, self::$static_properties[ $property_name ] );
self::$instance->initialize();
$this->assertSame(
$default_value,
$this->get_property_value( $property_name ),
"\"{$property_name}\" was set during the second initialization attempt."
);
}
/**
* Data provider.
*
* @return array[]
*/
public function data_static_properties_set_during_initialization() {
/*
* This does not include 'dependency_api_data' as it is only set
* on certain pages. This is tested later.
*/
return self::text_array_to_dataprovider(
array(
'plugins',
'dependencies',
'dependency_slugs',
'dependent_slugs',
)
);
}
/**
* Tests that `$dependency_api_data` is set on certain screens.
*
* @ticket 22316
*
* @covers WP_Plugin_Dependencies::get_dependency_api_data
* @covers WP_Plugin_Dependencies::get_plugins
*
* @dataProvider data_screens
*
* @global string $pagenow The filename of the current screen.
*
* @param string $screen The screen file.
*/
public function test_should_set_dependency_api_data_on_certain_screens( $screen ) {
global $pagenow;
// Backup $pagenow.
$old_pagenow = $pagenow;
// Ensure is_admin() and screen checks pass.
$pagenow = $screen;
set_current_screen( $screen );
self::$instance::initialize();
// Restore $pagenow.
$pagenow = $old_pagenow;
$dependency_api_data = $this->get_property_value( 'dependency_api_data' );
$this->assertIsArray( $dependency_api_data, '$dependency_api_data is not an array.' );
$this->assertEmpty( $dependency_api_data, '$dependency_api_data is not empty.' );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_screens() {
return array(
'plugins.php' => array(
'screen' => 'plugins.php',
),
'plugin-install.php' => array(
'screen' => 'plugin-install.php',
),
);
}
/**
* Tests that `$dependency_api_data` is not set by default.
*
* @ticket 22316
*
* @covers WP_Plugin_Dependencies::get_dependency_api_data
*/
public function test_should_not_set_dependency_api_data() {
self::$instance::initialize();
$dependency_api_data = $this->get_property_value( 'dependency_api_data' );
$this->assertNull( $dependency_api_data, '$dependency_api_data was set.' );
}
/**
* Tests that dependency slugs are loaded and sanitized.
*
* @ticket 22316
*
* @covers WP_Plugin_Dependencies::read_dependencies_from_plugin_headers
* @covers WP_Plugin_Dependencies::sanitize_dependency_slugs
*
* @dataProvider data_should_sanitize_slugs
*
* @param string $requires_plugins The unsanitized dependency slug(s).
* @param array $expected Optional. The sanitized dependency slug(s). Default empty array.
*/
public function test_initialize_should_load_and_sanitize_dependency_slugs_from_plugin_headers( $requires_plugins, $expected = array() ) {
$this->set_property_value( 'plugins', array( 'dependent/dependent.php' => array( 'RequiresPlugins' => $requires_plugins ) ) );
self::$instance->initialize();
$this->assertSame( $expected, $this->get_property_value( 'dependency_slugs' ) );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_should_sanitize_slugs() {
return array(
// Valid slugs.
'one dependency' => array(
'requires_plugins' => 'hello-dolly',
'expected' => array( 'hello-dolly' ),
),
'two dependencies in alphabetical order' => array(
'requires_plugins' => 'hello-dolly, woocommerce',
'expected' => array(
'hello-dolly',
'woocommerce',
),
),
'two dependencies in reverse alphabetical order' => array(
'requires_plugins' => 'woocommerce, hello-dolly',
'expected' => array(
'hello-dolly',
'woocommerce',
),
),
'two dependencies with a space' => array(
'requires_plugins' => 'hello-dolly , woocommerce',
'expected' => array(
'hello-dolly',
'woocommerce',
),
),
'a repeated dependency' => array(
'requires_plugins' => 'hello-dolly, woocommerce, hello-dolly',
'expected' => array(
'hello-dolly',
'woocommerce',
),
),
'a dependency with multiple dashes' => array(
'requires_plugins' => 'this-is-a-valid-slug',
'expected' => array( 'this-is-a-valid-slug' ),
),
'a dependency starting with numbers' => array(
'requires_plugins' => '123slug',
'expected' => array( '123slug' ),
),
'a dependency with a trailing comma' => array(
'requires_plugins' => 'hello-dolly,',
'expected' => array( 'hello-dolly' ),
),
'a dependency with a leading comma' => array(
'requires_plugins' => ',hello-dolly',
'expected' => array( 'hello-dolly' ),
),
'a dependency with leading and trailing commas' => array(
'requires_plugins' => ',hello-dolly,',
'expected' => array( 'hello-dolly' ),
),
'a dependency with a trailing comma and a space' => array(
'requires_plugins' => 'hello-dolly, ',
'expected' => array( 'hello-dolly' ),
),
// Invalid or empty slugs.
'no dependencies' => array(
'requires_plugins' => '',
),
'a dependency with an underscore' => array(
'requires_plugins' => 'hello_dolly',
),
'a dependency with a space' => array(
'requires_plugins' => 'hello dolly',
),
'a dependency in quotes' => array(
'requires_plugins' => '"hello-dolly"',
),
'two dependencies in quotes' => array(
'requires_plugins' => '"hello-dolly, woocommerce"',
),
'a dependency with trailing dash' => array(
'requires_plugins' => 'ending-dash-',
),
'a dependency with leading dash' => array(
'requires_plugins' => '-slug',
),
'a dependency with double dashes' => array(
'requires_plugins' => 'abc--123',
),
'cyrillic dependencies' => array(
'requires_plugins' => 'я-делюсь',
),
'arabic dependencies' => array(
'requires_plugins' => 'لينوكس-ويكى',
),
'chinese dependencies' => array(
'requires_plugins' => '唐诗宋词chinese-poem,社交登录,腾讯微博一键登录,豆瓣秀-for-wordpress',
),
'symbol dependencies' => array(
'requires_plugins' => '★-wpsymbols-★',
),
);
}
/**
* Tests that dependent files are loaded and slugified.
*
* @ticket 22316
*
* @covers WP_Plugin_Dependencies::read_dependencies_from_plugin_headers
* @covers WP_Plugin_Dependencies::convert_to_slug
*/
public function test_should_slugify_dependent_files() {
$plugins = get_plugins();
$expected_slugs = array();
foreach ( $plugins as $plugin_file => &$headers ) {
// Create the expected slugs.
if ( 'hello.php' === $plugin_file ) {
$slug = 'hello-dolly';
} else {
$slug = str_replace( '.php', '', explode( '/', $plugin_file )[0] );
}
$expected_slugs[ $plugin_file ] = $slug;
// While here, ensure the plugins are all dependents.
$headers['RequiresPlugins'] = 'dependency';
}
unset( $headers );
// Set the plugins property with the plugin data modified to make them dependents.
$this->set_property_value( 'plugins', $plugins );
self::$instance->initialize();
$this->assertSame( $expected_slugs, $this->get_property_value( 'dependent_slugs' ) );
}
}