Tests_Functions   F
last analyzed

Complexity

Total Complexity 71

Size/Duplication

Total Lines 1108
Duplicated Lines 24.55 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 272
loc 1108
rs 1.0526
c 0
b 0
f 0
wmc 71
lcom 0
cbo 4

49 Methods

Rating   Name   Duplication   Size   Complexity  
A test_wp_parse_args_object() 0 9 1
A test_wp_parse_args_array() 0 7 1
A test_wp_parse_args_defaults() 0 10 1
A test_wp_parse_args_other() 0 8 1
A test_wp_parse_args_boolean_strings() 0 5 1
A test_bool_from_yn() 0 5 1
A test_path_is_absolute() 0 16 2
A test_path_is_not_absolute() 0 16 2
A test_wp_normalize_path() 0 3 1
B test_wp_unique_filename() 0 32 1
B test_is_serialized() 0 25 3
A test_no_new_serializable_types() 0 3 1
A test_is_serialized_string() 0 3 1
A test_add_query_arg_numeric_keys() 0 10 1
B test_wp_get_mime_types() 0 28 1
A test_wp_parse_id_list() 0 3 1
A data_wp_parse_id_list() 0 11 1
A test_wp_parse_slug_list() 0 3 1
A data_wp_parse_slug_list() 0 8 1
A test_device_can_upload() 0 6 1
B test_wp_extract_urls() 0 163 1
A test_wp_json_encode() 0 3 1
A test_wp_json_encode_utf8() 0 3 1
A test_wp_json_encode_array() 0 3 1
A test_wp_json_encode_object() 0 5 1
A test_wp_json_encode_depth() 0 13 2
B test_the_date() 0 34 1
A test_mysql_to_rfc3339() 0 8 1
A datetime_provider() 0 9 1
A test_wp_ext2type() 0 12 3
A test_wp_raise_memory_limit() 0 13 2
A test_wp_generate_uuid4() 0 11 2
A test_wp_get_image_mime() 0 7 3
A test_wp_check_filetype_and_ext() 0 7 2
A _filter_mime_types_svg() 0 4 1
A _filter_mime_types_woff() 0 4 1
B _wp_get_image_mime() 0 31 1
B _wp_check_filetype_and_ext_data() 0 82 2
A data_wp_normalize_path() 18 18 1
B data_is_serialized_string() 31 31 1
B test_add_query_arg() 30 82 4
A test_get_allowed_mime_types() 16 16 1
B test_canonical_charset() 33 33 1
A data_device_can_upload() 49 49 1
A test_wp_json_encode_non_utf8() 20 20 3
A test_wp_json_encode_non_utf8_in_array() 20 20 3
A test_wp_get_ext_types() 15 15 1
A test_wp_check_filetype_and_ext_with_filtered_svg() 20 20 2
A test_wp_check_filetype_and_ext_with_filtered_woff() 20 20 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Tests_Functions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Tests_Functions, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @group functions.php
5
 */
6
class Tests_Functions extends WP_UnitTestCase {
7
	function test_wp_parse_args_object() {
8
		$x = new MockClass;
9
		$x->_baba = 5;
0 ignored issues
show
Bug introduced by
The property _baba does not seem to exist in MockClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
10
		$x->yZ = "baba";
0 ignored issues
show
Bug introduced by
The property yZ does not seem to exist in MockClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
11
		$x->a = array(5, 111, 'x');
0 ignored issues
show
Bug introduced by
The property a does not seem to exist in MockClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
12
		$this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x));
13
		$y = new MockClass;
14
		$this->assertEquals(array(), wp_parse_args($y));
15
	}
16
17
	function test_wp_parse_args_array()  {
18
		// arrays
19
		$a = array();
20
		$this->assertEquals(array(), wp_parse_args($a));
21
		$b = array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x'));
22
		$this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($b));
23
	}
24
25
	function test_wp_parse_args_defaults() {
26
		$x = new MockClass;
27
		$x->_baba = 5;
0 ignored issues
show
Bug introduced by
The property _baba does not seem to exist in MockClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
28
		$x->yZ = "baba";
0 ignored issues
show
Bug introduced by
The property yZ does not seem to exist in MockClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
29
		$x->a = array(5, 111, 'x');
0 ignored issues
show
Bug introduced by
The property a does not seem to exist in MockClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
30
		$d = array('pu' => 'bu');
31
		$this->assertEquals(array('pu' => 'bu', '_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x, $d));
32
		$e = array('_baba' => 6);
33
		$this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x, $e));
34
	}
35
36
	function test_wp_parse_args_other() {
37
		$b = true;
38
		wp_parse_str($b, $s);
39
		$this->assertEquals($s, wp_parse_args($b));
40
		$q = 'x=5&_baba=dudu&';
41
		wp_parse_str($q, $ss);
42
		$this->assertEquals($ss, wp_parse_args($q));
43
	}
44
45
	/**
46
	 * @ticket 30753
47
	 */
48
	function test_wp_parse_args_boolean_strings() {
49
		$args = wp_parse_args( 'foo=false&bar=true' );
50
		$this->assertInternalType( 'string', $args['foo'] );
51
		$this->assertInternalType( 'string', $args['bar'] );
52
	}
53
54
	/**
55
	 * @ticket 35972
56
	 */
57
	function test_bool_from_yn() {
58
		$this->assertTrue( bool_from_yn( 'Y' ) );
59
		$this->assertTrue( bool_from_yn( 'y' ) );
60
		$this->assertFalse( bool_from_yn( 'n' ) );
61
	}
62
63
	function test_path_is_absolute() {
64
		$absolute_paths = array(
65
			'/',
66
			'/foo/',
67
			'/foo',
68
			'/FOO/bar',
69
			'/foo/bar/',
70
			'/foo/../bar/',
71
			'\\WINDOWS',
72
			'C:\\',
73
			'C:\\WINDOWS',
74
			'\\\\sambashare\\foo',
75
			);
76
		foreach ($absolute_paths as $path)
77
			$this->assertTrue( path_is_absolute($path), "path_is_absolute('$path') should return true" );
78
	}
79
80
	function test_path_is_not_absolute() {
81
		$relative_paths = array(
82
			'',
83
			'.',
84
			'..',
85
			'../foo',
86
			'../',
87
			'../foo.bar',
88
			'foo/bar',
89
			'foo',
90
			'FOO',
91
			'..\\WINDOWS',
92
			);
93
		foreach ($relative_paths as $path)
94
			$this->assertFalse( path_is_absolute($path), "path_is_absolute('$path') should return false" );
95
	}
96
97
	/**
98
	 * @ticket 33265
99
	 * @ticket 35996
100
	 *
101
	 * @dataProvider data_wp_normalize_path
102
	 */
103
	function test_wp_normalize_path( $path, $expected ) {
104
		$this->assertEquals( $expected, wp_normalize_path( $path ) );
105
	}
106 View Code Duplication
	function data_wp_normalize_path() {
107
		return array(
108
			// Windows paths
109
			array( 'C:\\www\\path\\', 'C:/www/path/' ),
110
			array( 'C:\\www\\\\path\\', 'C:/www/path/' ),
111
			array( 'c:/www/path', 'C:/www/path' ),
112
			array( 'c:\\www\\path\\', 'C:/www/path/' ), // uppercase drive letter
113
			array( 'c:\\\\www\\path\\', 'C:/www/path/' ),
114
			array( '\\\\Domain\\DFSRoots\\share\\path\\', '//Domain/DFSRoots/share/path/' ),
115
			array( '\\\\Server\\share\\path', '//Server/share/path' ),
116
			array( '\\\\Server\\share', '//Server/share' ),
117
118
			// Linux paths
119
			array( '/www/path/', '/www/path/' ),
120
			array( '/www/path/////', '/www/path/' ),
121
			array( '/www/path', '/www/path' ),
122
		);
123
	}
124
125
	function test_wp_unique_filename() {
126
127
		$testdir = DIR_TESTDATA . '/images/';
128
129
		// sanity check
130
		$this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Sanitiy check failed' );
131
132
		// check number is appended for file already exists
133
		$this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' );
134
		$this->assertEquals( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'Number not appended correctly' );
135
		$this->assertFileNotExists( $testdir . 'test-image-1.png' );
136
137
		// check special chars
138
		$this->assertEquals( 'testtést-imagé.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' );
139
140
		// check special chars with potential conflicting name
141
		$this->assertEquals( 'tést-imagé.png', wp_unique_filename( $testdir, 'tést-imagé.png' ), 'Filename with special chars failed' );
142
143
		// check with single quotes in name (somehow)
144
		$this->assertEquals( "abcdefgh.png", wp_unique_filename( $testdir, "abcdefg'h.png" ), 'File with quote failed' );
145
146
		// check with single quotes in name (somehow)
147
		$this->assertEquals( "abcdefgh.png", wp_unique_filename( $testdir, 'abcdefg"h.png' ), 'File with quote failed' );
148
149
		// test crazy name (useful for regression tests)
150
		$this->assertEquals( '12af34567890@..^_qwerty-fghjkl-zx.png', wp_unique_filename( $testdir, '12%af34567890#~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png' ), 'Failed crazy file name' );
151
152
		// test slashes in names
153
		$this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\fg.png' ), 'Slash not removed' );
154
		$this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\fg.png' ), 'Double slashed not removed' );
155
		$this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\\fg.png' ), 'Tripple slashed not removed' );
156
	}
157
158
	function test_is_serialized() {
159
		$cases = array(
160
			serialize(null),
161
			serialize(true),
162
			serialize(false),
163
			serialize(-25),
164
			serialize(25),
165
			serialize(1.1),
166
			serialize('this string will be serialized'),
167
			serialize("a\nb"),
168
			serialize(array()),
169
			serialize(array(1,1,2,3,5,8,13)),
170
			serialize( (object)array('test' => true, '3', 4) )
171
		);
172
		foreach ( $cases as $case )
173
			$this->assertTrue( is_serialized($case), "Serialized data: $case" );
174
175
		$not_serialized = array(
176
			'a string',
177
			'garbage:a:0:garbage;',
178
			's:4:test;'
179
		);
180
		foreach ( $not_serialized as $case )
181
			$this->assertFalse( is_serialized($case), "Test data: $case" );
182
	}
183
184
	/**
185
	 * @ticket 17375
186
	 */
187
	function test_no_new_serializable_types() {
188
		$this->assertFalse( is_serialized( 'C:16:"Serialized_Class":6:{a:0:{}}' ) );
189
	}
190
191
	/**
192
	 * @dataProvider data_is_serialized_string
193
	 */
194
	public function test_is_serialized_string( $value, $result ) {
195
		$this->assertSame( is_serialized_string( $value ), $result );
196
	}
197
198 View Code Duplication
	public function data_is_serialized_string() {
199
		return array(
200
			// Not a string.
201
			array( 0, false ),
202
203
			// Too short when trimmed.
204
			array( 's:3   ', false ),
205
206
			// Too short.
207
			array( 's:3', false ),
208
209
			// No colon in second position.
210
			array( 's!3:"foo";', false ),
211
212
			// No trailing semicolon.
213
			array( 's:3:"foo"', false ),
214
215
			// Wrong type.
216
			array( 'a:3:"foo";', false ),
217
218
			// No closing quote.
219
			array( 'a:3:"foo;', false ),
220
221
			// Wrong number of characters is close enough for is_serialized_string().
222
			array( 's:12:"foo";', true ),
223
224
			// Okay.
225
			array( 's:3:"foo";', true ),
226
227
		);
228
	}
229
230
	/**
231
	 * @group add_query_arg
232
	 */
233
	function test_add_query_arg() {
234
		$old_req_uri = $_SERVER['REQUEST_URI'];
235
236
		$urls = array(
237
			'/',
238
			'/2012/07/30/',
239
			'edit.php',
240
			admin_url( 'edit.php' ),
241
			admin_url( 'edit.php', 'https' ),
242
		);
243
244
		$frag_urls = array(
245
			'/#frag',
246
			'/2012/07/30/#frag',
247
			'edit.php#frag',
248
			admin_url( 'edit.php#frag' ),
249
			admin_url( 'edit.php#frag', 'https' ),
250
		);
251
252 View Code Duplication
		foreach ( $urls as $url ) {
253
			$_SERVER['REQUEST_URI'] = 'nothing';
254
255
			$this->assertEquals( "$url?foo=1", add_query_arg( 'foo', '1', $url ) );
256
			$this->assertEquals( "$url?foo=1", add_query_arg( array( 'foo' => '1' ), $url ) );
257
			$this->assertEquals( "$url?foo=2", add_query_arg( array( 'foo' => '1', 'foo' => '2' ), $url ) );
258
			$this->assertEquals( "$url?foo=1&bar=2", add_query_arg( array( 'foo' => '1', 'bar' => '2' ), $url ) );
259
260
			$_SERVER['REQUEST_URI'] = $url;
261
262
			$this->assertEquals( "$url?foo=1", add_query_arg( 'foo', '1' ) );
263
			$this->assertEquals( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) );
264
			$this->assertEquals( "$url?foo=2", add_query_arg( array( 'foo' => '1', 'foo' => '2' ) ) );
265
			$this->assertEquals( "$url?foo=1&bar=2", add_query_arg( array( 'foo' => '1', 'bar' => '2' ) ) );
266
		}
267
268
		foreach ( $frag_urls as $frag_url ) {
269
			$_SERVER['REQUEST_URI'] = 'nothing';
270
			$url = str_replace( '#frag', '', $frag_url );
271
272
			$this->assertEquals( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) );
273
			$this->assertEquals( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) );
274
			$this->assertEquals( "$url?foo=2#frag", add_query_arg( array( 'foo' => '1', 'foo' => '2' ), $frag_url ) );
275
			$this->assertEquals( "$url?foo=1&bar=2#frag", add_query_arg( array( 'foo' => '1', 'bar' => '2' ), $frag_url ) );
276
277
			$_SERVER['REQUEST_URI'] = $frag_url;
278
279
			$this->assertEquals( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) );
280
			$this->assertEquals( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) );
281
			$this->assertEquals( "$url?foo=2#frag", add_query_arg( array( 'foo' => '1', 'foo' => '2' ) ) );
282
			$this->assertEquals( "$url?foo=1&bar=2#frag", add_query_arg( array( 'foo' => '1', 'bar' => '2' ) ) );
283
		}
284
285
		$qs_urls = array(
286
			'baz=1', // #WP4903
287
			'/?baz',
288
			'/2012/07/30/?baz',
289
			'edit.php?baz',
290
			admin_url( 'edit.php?baz' ),
291
			admin_url( 'edit.php?baz', 'https' ),
292
			admin_url( 'edit.php?baz&za=1' ),
293
			admin_url( 'edit.php?baz=1&za=1' ),
294
			admin_url( 'edit.php?baz=0&za=0' ),
295
		);
296
297 View Code Duplication
		foreach ( $qs_urls as $url ) {
298
			$_SERVER['REQUEST_URI'] = 'nothing';
299
300
			$this->assertEquals( "$url&foo=1", add_query_arg( 'foo', '1', $url ) );
301
			$this->assertEquals( "$url&foo=1", add_query_arg( array( 'foo' => '1' ), $url ) );
302
			$this->assertEquals( "$url&foo=2", add_query_arg( array( 'foo' => '1', 'foo' => '2' ), $url ) );
303
			$this->assertEquals( "$url&foo=1&bar=2", add_query_arg( array( 'foo' => '1', 'bar' => '2' ), $url ) );
304
305
			$_SERVER['REQUEST_URI'] = $url;
306
307
			$this->assertEquals( "$url&foo=1", add_query_arg( 'foo', '1' ) );
308
			$this->assertEquals( "$url&foo=1", add_query_arg( array( 'foo' => '1' ) ) );
309
			$this->assertEquals( "$url&foo=2", add_query_arg( array( 'foo' => '1', 'foo' => '2' ) ) );
310
			$this->assertEquals( "$url&foo=1&bar=2", add_query_arg( array( 'foo' => '1', 'bar' => '2' ) ) );
311
		}
312
313
		$_SERVER['REQUEST_URI'] = $old_req_uri;
314
	}
315
316
	/**
317
	 * @ticket 31306
318
	 */
319
	function test_add_query_arg_numeric_keys() {
320
		$url = add_query_arg( array( 'foo' => 'bar' ), '1=1' );
321
		$this->assertEquals('1=1&foo=bar', $url);
322
323
		$url = add_query_arg( array( 'foo' => 'bar', '1' => '2' ), '1=1' );
324
		$this->assertEquals('1=2&foo=bar', $url);
325
326
		$url = add_query_arg( array( '1' => '2' ), 'foo=bar' );
327
		$this->assertEquals('foo=bar&1=2', $url);
328
	}
329
330
	/**
331
	 * @ticket 21594
332
	 */
333 View Code Duplication
	function test_get_allowed_mime_types() {
334
		$mimes = get_allowed_mime_types();
335
336
		$this->assertInternalType( 'array', $mimes );
337
		$this->assertNotEmpty( $mimes );
338
339
		add_filter( 'upload_mimes', '__return_empty_array' );
340
		$mimes = get_allowed_mime_types();
341
		$this->assertInternalType( 'array', $mimes );
342
		$this->assertEmpty( $mimes );
343
344
		remove_filter( 'upload_mimes', '__return_empty_array' );
345
		$mimes = get_allowed_mime_types();
346
		$this->assertInternalType( 'array', $mimes );
347
		$this->assertNotEmpty( $mimes );
348
	}
349
350
	/**
351
	 * @ticket 21594
352
	 */
353
	function test_wp_get_mime_types() {
354
		$mimes = wp_get_mime_types();
355
356
		$this->assertInternalType( 'array', $mimes );
357
		$this->assertNotEmpty( $mimes );
358
359
		add_filter( 'mime_types', '__return_empty_array' );
360
		$mimes = wp_get_mime_types();
361
		$this->assertInternalType( 'array', $mimes );
362
		$this->assertEmpty( $mimes );
363
364
		remove_filter( 'mime_types', '__return_empty_array' );
365
		$mimes = wp_get_mime_types();
366
		$this->assertInternalType( 'array', $mimes );
367
		$this->assertNotEmpty( $mimes );
368
369
		// upload_mimes shouldn't affect wp_get_mime_types()
370
		add_filter( 'upload_mimes', '__return_empty_array' );
371
		$mimes = wp_get_mime_types();
372
		$this->assertInternalType( 'array', $mimes );
373
		$this->assertNotEmpty( $mimes );
374
375
		remove_filter( 'upload_mimes', '__return_empty_array' );
376
		$mimes2 = wp_get_mime_types();
377
		$this->assertInternalType( 'array', $mimes2 );
378
		$this->assertNotEmpty( $mimes2 );
379
		$this->assertEquals( $mimes2, $mimes );
380
	}
381
382
	/**
383
	 * @ticket 23688
384
	 */
385 View Code Duplication
	function test_canonical_charset() {
386
		$orig_blog_charset = get_option( 'blog_charset' );
387
388
		update_option( 'blog_charset', 'utf8' );
389
		$this->assertEquals( 'UTF-8', get_option( 'blog_charset') );
390
391
		update_option( 'blog_charset', 'utf-8' );
392
		$this->assertEquals( 'UTF-8', get_option( 'blog_charset') );
393
394
		update_option( 'blog_charset', 'UTF8' );
395
		$this->assertEquals( 'UTF-8', get_option( 'blog_charset') );
396
397
		update_option( 'blog_charset', 'UTF-8' );
398
		$this->assertEquals( 'UTF-8', get_option( 'blog_charset') );
399
400
		update_option( 'blog_charset', 'ISO-8859-1' );
401
		$this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') );
402
403
		update_option( 'blog_charset', 'ISO8859-1' );
404
		$this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') );
405
406
		update_option( 'blog_charset', 'iso8859-1' );
407
		$this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') );
408
409
		update_option( 'blog_charset', 'iso-8859-1' );
410
		$this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') );
411
412
		// Arbitrary strings are passed through.
413
		update_option( 'blog_charset', 'foobarbaz' );
414
		$this->assertEquals( 'foobarbaz', get_option( 'blog_charset') );
415
416
		update_option( 'blog_charset', $orig_blog_charset );
417
	}
418
419
	/**
420
	 * @dataProvider data_wp_parse_id_list
421
	 */
422
	function test_wp_parse_id_list( $expected, $actual ) {
423
		$this->assertSame( $expected, array_values( wp_parse_id_list( $actual ) ) );
424
	}
425
426
	function data_wp_parse_id_list() {
427
		return array(
428
			array( array( 1, 2, 3, 4 ), '1,2,3,4' ),
429
			array( array( 1, 2, 3, 4 ), '1, 2,,3,4' ),
430
			array( array( 1, 2, 3, 4 ), '1,2,2,3,4' ),
431
			array( array( 1, 2, 3, 4 ), array( '1', '2', '3', '4', '3' ) ),
432
			array( array( 1, 2, 3, 4 ), array( 1, '2', 3, '4' ) ),
433
			array( array( 1, 2, 3, 4 ), '-1,2,-3,4' ),
434
			array( array( 1, 2, 3, 4 ), array( -1, 2, '-3', '4' ) ),
435
		);
436
	}
437
438
	/**
439
	 * @dataProvider data_wp_parse_slug_list
440
	 */
441
	function test_wp_parse_slug_list( $expected, $actual ) {
442
		$this->assertSame( $expected, array_values( wp_parse_slug_list( $actual ) ) );
443
	}
444
445
	function data_wp_parse_slug_list() {
446
		return array(
447
			array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ),
448
			array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple, banana,,carrot,dog' ),
449
			array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple banana carrot dog' ),
450
			array( array( 'apple', 'banana-carrot', 'd-o-g' ), array( 'apple ', 'banana carrot', 'd o g' ) ),
451
		);
452
	}
453
454
	/**
455
	 * @dataProvider data_device_can_upload
456
	 */
457
	function test_device_can_upload( $user_agent, $expected ) {
458
		$_SERVER['HTTP_USER_AGENT'] = $user_agent;
459
		$actual = _device_can_upload();
460
		unset( $_SERVER['HTTP_USER_AGENT'] );
461
		$this->assertEquals( $expected, $actual );
462
	}
463
464 View Code Duplication
	function data_device_can_upload() {
465
		return array(
466
			// iPhone iOS 5.0.1, Safari 5.1
467
			array(
468
				'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A406)',
469
				false,
470
			),
471
			// iPad iOS 3.2, Safari 4.0.4
472
			array(
473
				'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10',
474
				false,
475
			),
476
			// iPod iOS 4.3.3, Safari 5.0.2
477
			array(
478
				'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
479
				false,
480
			),
481
			// iPhone iOS 6.0.0, Safari 6.0
482
			array(
483
				'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
484
				true,
485
			),
486
			// iPad iOS 6.0.0, Safari 6.0
487
			array(
488
				'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
489
				true,
490
			),
491
			// Android 2.2, Android Webkit Browser
492
			array(
493
				'Mozilla/5.0 (Android 2.2; Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4',
494
				true,
495
			),
496
			// BlackBerry 9900, BlackBerry browser
497
			array(
498
				'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+',
499
				true,
500
			),
501
			// Windows Phone 8.0, Internet Explorer 10.0;
502
			array(
503
				'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)',
504
				true,
505
			),
506
			// Ubuntu desktop, Firefox 41.0
507
			array(
508
				'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0',
509
				true,
510
			),
511
		);
512
	}
513
514
	/**
515
	 * @ticket 9064
516
	 */
517
	function test_wp_extract_urls() {
518
		$original_urls = array(
519
			'http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html',
520
			'http://this.com',
521
			'http://127.0.0.1',
522
			'http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437',
523
			'http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html',
524
			'http://wordpress-core.com:8080/',
525
			'http://www.website.com:5000',
526
			'http://wordpress-core/?346236346326&2134362574863.437',
527
			'http://افغانستا.icom.museum',
528
			'http://الجزائر.icom.museum',
529
			'http://österreich.icom.museum',
530
			'http://বাংলাদেশ.icom.museum',
531
			'http://беларусь.icom.museum',
532
			'http://belgië.icom.museum',
533
			'http://българия.icom.museum',
534
			'http://تشادر.icom.museum',
535
			'http://中国.icom.museum',
536
			#'http://القمر.icom.museum', // Comoros	http://القمر.icom.museum
537
			#'http://κυπρος.icom.museum', Cyprus 	http://κυπρος.icom.museum
538
			'http://českárepublika.icom.museum',
539
			#'http://مصر.icom.museum', // Egypt	http://مصر.icom.museum
540
			'http://ελλάδα.icom.museum',
541
			'http://magyarország.icom.museum',
542
			'http://ísland.icom.museum',
543
			'http://भारत.icom.museum',
544
			'http://ايران.icom.museum',
545
			'http://éire.icom.museum',
546
			'http://איקו״ם.ישראל.museum',
547
			'http://日本.icom.museum',
548
			'http://الأردن.icom.museum',
549
			'http://қазақстан.icom.museum',
550
			'http://한국.icom.museum',
551
			'http://кыргызстан.icom.museum',
552
			'http://ລາວ.icom.museum',
553
			'http://لبنان.icom.museum',
554
			'http://македонија.icom.museum',
555
			#'http://موريتانيا.icom.museum', // Mauritania	http://موريتانيا.icom.museum
556
			'http://méxico.icom.museum',
557
			'http://монголулс.icom.museum',
558
			#'http://المغرب.icom.museum', // Morocco	http://المغرب.icom.museum
559
			'http://नेपाल.icom.museum',
560
			#'http://عمان.icom.museum', // Oman	http://عمان.icom.museum
561
			'http://قطر.icom.museum',
562
			'http://românia.icom.museum',
563
			'http://россия.иком.museum',
564
			'http://србијаицрнагора.иком.museum',
565
			'http://இலங்கை.icom.museum',
566
			'http://españa.icom.museum',
567
			'http://ไทย.icom.museum',
568
			'http://تونس.icom.museum',
569
			'http://türkiye.icom.museum',
570
			'http://украина.icom.museum',
571
			'http://việtnam.icom.museum',
572
			'ftp://127.0.0.1/',
573
			'http://www.woo.com/video?v=exvUH2qKLTU',
574
			'http://taco.com?burrito=enchilada#guac'
575
		);
576
577
		$blob ="
578
			http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html
579
580
			http://this.com
581
582
			http://127.0.0.1
583
584
			http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&amp;2134362574863.437
585
586
			http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html
587
588
			http://wordpress-core.com:8080/
589
590
			http://www.website.com:5000
591
592
			http://wordpress-core/?346236346326&amp;2134362574863.437
593
594
			http://افغانستا.icom.museum
595
			http://الجزائر.icom.museum
596
			http://österreich.icom.museum
597
			http://বাংলাদেশ.icom.museum
598
			http://беларусь.icom.museum
599
			http://belgië.icom.museum
600
			http://българия.icom.museum
601
			http://تشادر.icom.museum
602
			http://中国.icom.museum
603
			http://českárepublika.icom.museum
604
			http://ελλάδα.icom.museum
605
			http://magyarország.icom.museum
606
			http://ísland.icom.museum
607
			http://भारत.icom.museum
608
			http://ايران.icom.museum
609
			http://éire.icom.museum
610
			http://איקו״ם.ישראל.museum
611
			http://日本.icom.museum
612
			http://الأردن.icom.museum
613
			http://қазақстан.icom.museum
614
			http://한국.icom.museum
615
			http://кыргызстан.icom.museum
616
			http://ລາວ.icom.museum
617
			http://لبنان.icom.museum
618
			http://македонија.icom.museum
619
			http://méxico.icom.museum
620
			http://монголулс.icom.museum
621
			http://नेपाल.icom.museum
622
			http://قطر.icom.museum
623
			http://românia.icom.museum
624
			http://россия.иком.museum
625
			http://србијаицрнагора.иком.museum
626
			http://இலங்கை.icom.museum
627
			http://españa.icom.museum
628
			http://ไทย.icom.museum
629
			http://تونس.icom.museum
630
			http://türkiye.icom.museum
631
			http://украина.icom.museum
632
			http://việtnam.icom.museum
633
			ftp://127.0.0.1/
634
			http://www.woo.com/video?v=exvUH2qKLTU
635
636
			http://taco.com?burrito=enchilada#guac
637
		";
638
639
		$urls = wp_extract_urls( $blob );
640
		$this->assertNotEmpty( $urls );
641
		$this->assertInternalType( 'array', $urls );
642
		$this->assertCount( count( $original_urls ), $urls );
643
		$this->assertEquals( $original_urls, $urls );
644
645
		$exploded = array_values( array_filter( array_map( 'trim', explode( "\n", $blob ) ) ) );
646
		// wp_extract_urls calls html_entity_decode
647
		$decoded = array_map( 'html_entity_decode', $exploded );
648
649
		$this->assertEquals( $decoded, $urls );
650
		$this->assertEquals( $original_urls, $decoded );
651
652
		$blob ="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
653
			incididunt ut labore http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html et dolore magna aliqua.
654
			Ut http://this.com enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco http://127.0.0.1
655
			laboris nisi ut aliquip ex http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&amp;2134362574863.437 ea
656
			commodo consequat. http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html Duis aute irure dolor in reprehenderit in voluptate
657
			velit esse http://wordpress-core.com:8080/ cillum dolore eu fugiat nulla <A href=\"http://www.website.com:5000\">http://www.website.com:5000</B> pariatur. Excepteur sint occaecat cupidatat non proident,
658
			sunt in culpa qui officia deserunt mollit http://wordpress-core/?346236346326&amp;2134362574863.437 anim id est laborum.";
659
660
		$urls = wp_extract_urls( $blob );
661
		$this->assertNotEmpty( $urls );
662
		$this->assertInternalType( 'array', $urls );
663
		$this->assertCount( 8, $urls );
664
		$this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls );
665
666
		$blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
667
			incididunt ut labore <a href="http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html">343462^</a> et dolore magna aliqua.
668
			Ut <a href="http://this.com">&amp;3640i6p1yi499</a> enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco <a href="http://127.0.0.1">localhost</a>
669
			laboris nisi ut aliquip ex <a href="http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&amp;2134362574863.437">343462^</a> ea
670
			commodo consequat. <a href="http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html">343462^</a> Duis aute irure dolor in reprehenderit in voluptate
671
			velit esse <a href="http://wordpress-core.com:8080/">-3-4--321-64-4@#!$^$!@^@^</a> cillum dolore eu <A href="http://www.website.com:5000">http://www.website.com:5000</B> fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
672
			sunt in culpa qui officia deserunt mollit <a href="http://wordpress-core/?346236346326&amp;2134362574863.437">)(*&^%$</a> anim id est laborum.';
673
674
		$urls = wp_extract_urls( $blob );
675
		$this->assertNotEmpty( $urls );
676
		$this->assertInternalType( 'array', $urls );
677
		$this->assertCount( 8, $urls );
678
		$this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls );
679
	}
680
681
	/**
682
	 * @ticket 28786
683
	 */
684
	function test_wp_json_encode() {
685
		$this->assertEquals( wp_json_encode( 'a' ), '"a"' );
686
	}
687
688
	/**
689
	 * @ticket 28786
690
	 */
691
	function test_wp_json_encode_utf8() {
692
		$this->assertEquals( wp_json_encode( '这' ), '"\u8fd9"' );
693
	}
694
695
	/**
696
	 * @ticket 28786
697
	 */
698 View Code Duplication
	function test_wp_json_encode_non_utf8() {
699
		if ( ! function_exists( 'mb_detect_order' ) ) {
700
			$this->markTestSkipped( 'mbstring extension not available.' );
701
		}
702
703
		$old_charsets = $charsets = mb_detect_order();
704
		if ( ! in_array( 'EUC-JP', $charsets ) ) {
705
			$charsets[] = 'EUC-JP';
706
			mb_detect_order( $charsets );
707
		}
708
709
		$eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' );
710
		$utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );
711
712
		$this->assertEquals( 'aあb', $utf8 );
713
714
		$this->assertEquals( '"a\u3042b"', wp_json_encode( $eucjp ) );
715
716
		mb_detect_order( $old_charsets );
717
	}
718
719
	/**
720
	 * @ticket 28786
721
	 */
722 View Code Duplication
	function test_wp_json_encode_non_utf8_in_array() {
723
		if ( ! function_exists( 'mb_detect_order' ) ) {
724
			$this->markTestSkipped( 'mbstring extension not available.' );
725
		}
726
727
		$old_charsets = $charsets = mb_detect_order();
728
		if ( ! in_array( 'EUC-JP', $charsets ) ) {
729
			$charsets[] = 'EUC-JP';
730
			mb_detect_order( $charsets );
731
		}
732
733
		$eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' );
734
		$utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );
735
736
		$this->assertEquals( 'aあb', $utf8 );
737
738
		$this->assertEquals( '["c","a\u3042b"]', wp_json_encode( array( 'c', $eucjp ) ) );
739
740
		mb_detect_order( $old_charsets );
741
	}
742
743
	/**
744
	 * @ticket 28786
745
	 */
746
	function test_wp_json_encode_array() {
747
		$this->assertEquals( wp_json_encode( array( 'a' ) ), '["a"]' );
748
	}
749
750
	/**
751
	 * @ticket 28786
752
	 */
753
	function test_wp_json_encode_object() {
754
		$object = new stdClass;
755
		$object->a = 'b';
756
		$this->assertEquals( wp_json_encode( $object ), '{"a":"b"}' );
757
	}
758
759
	/**
760
	 * @ticket 28786
761
	 */
762
	function test_wp_json_encode_depth() {
763
		if ( version_compare( PHP_VERSION, '5.5', '<' ) ) {
764
			$this->markTestSkipped( 'json_encode() supports the $depth parameter in PHP 5.5+' );
765
		};
766
767
		$data = array( array( array( 1, 2, 3 ) ) );
768
		$json = wp_json_encode( $data, 0, 1 );
769
		$this->assertFalse( $json );
770
771
		$data = array( 'あ', array( array( 1, 2, 3 ) ) );
772
		$json = wp_json_encode( $data, 0, 1 );
773
		$this->assertFalse( $json );
774
	}
775
776
	/**
777
	 * @ticket 33750
778
	 */
779
	function test_the_date() {
780
		ob_start();
781
		the_date();
782
		$actual = ob_get_clean();
783
		$this->assertEquals( '', $actual );
784
785
		$GLOBALS['post']        = self::factory()->post->create_and_get( array(
786
			'post_date' => '2015-09-16 08:00:00'
787
		) );
788
789
		ob_start();
790
		$GLOBALS['currentday']  = '18.09.15';
791
		$GLOBALS['previousday'] = '17.09.15';
792
		the_date();
793
		$this->assertEquals( 'September 16, 2015', ob_get_clean() );
794
795
		ob_start();
796
		$GLOBALS['currentday']  = '18.09.15';
797
		$GLOBALS['previousday'] = '17.09.15';
798
		the_date( 'Y' );
799
		$this->assertEquals( '2015', ob_get_clean() );
800
801
		ob_start();
802
		$GLOBALS['currentday']  = '18.09.15';
803
		$GLOBALS['previousday'] = '17.09.15';
804
		the_date( 'Y', 'before ', ' after' );
805
		$this->assertEquals( 'before 2015 after', ob_get_clean() );
806
807
		ob_start();
808
		$GLOBALS['currentday']  = '18.09.15';
809
		$GLOBALS['previousday'] = '17.09.15';
810
		the_date( 'Y', 'before ', ' after', false );
811
		$this->assertEquals( '', ob_get_clean() );
812
	}
813
814
	/**
815
	 * @ticket 36054
816
	 * @dataProvider datetime_provider
817
	 */
818
	function test_mysql_to_rfc3339( $expected, $actual ) {
819
		$date_return = mysql_to_rfc3339( $actual );
820
821
		$this->assertTrue( is_string( $date_return ), 'The date return must be a string' );
822
		$this->assertNotEmpty( $date_return, 'The date return could not be an empty string' );
823
		$this->assertEquals( $expected, $date_return, 'The date does not match' );
824
		$this->assertEquals( new DateTime( $expected ), new DateTime( $date_return ), 'The date is not the same after the call method' );
825
	}
826
827
	function datetime_provider() {
828
		return array(
829
			array( '2016-03-15T18:54:46', '15-03-2016 18:54:46' ),
830
			array( '2016-03-02T19:13:25', '2016-03-02 19:13:25' ),
831
			array( '2016-03-02T19:13:00', '2016-03-02 19:13' ),
832
			array( '2016-03-02T19:13:00', '16-03-02 19:13' ),
833
			array( '2016-03-02T19:13:00', '16-03-02 19:13' )
834
		);
835
	}
836
837
	/**
838
	 * @ticket 35987
839
	 */
840 View Code Duplication
	public function test_wp_get_ext_types() {
841
		$extensions = wp_get_ext_types();
842
843
		$this->assertInternalType( 'array', $extensions );
844
		$this->assertNotEmpty( $extensions );
845
846
		add_filter( 'ext2type', '__return_empty_array' );
847
		$extensions = wp_get_ext_types();
848
		$this->assertSame( array(), $extensions );
849
850
		remove_filter( 'ext2type', '__return_empty_array' );
851
		$extensions = wp_get_ext_types();
852
		$this->assertInternalType( 'array', $extensions );
853
		$this->assertNotEmpty( $extensions );
854
	}
855
856
	/**
857
	 * @ticket 35987
858
	 */
859
	public function test_wp_ext2type() {
860
		$extensions = wp_get_ext_types();
861
862
		foreach ( $extensions as $type => $extensionList ) {
863
			foreach ( $extensionList as $extension ) {
864
				$this->assertEquals( $type, wp_ext2type( $extension ) );
865
				$this->assertEquals( $type, wp_ext2type( strtoupper( $extension ) ) );
866
			}
867
		}
868
869
		$this->assertNull( wp_ext2type( 'unknown_format' ) );
870
	}
871
872
	/**
873
	 * Tests raising the memory limit.
874
	 *
875
	 * Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the
876
	 * test suite is -1, we can not test the memory limit negotiations.
877
	 *
878
	 * @ticket 32075
879
	 */
880
	function test_wp_raise_memory_limit() {
881
		if ( -1 !== WP_MAX_MEMORY_LIMIT ) {
882
			$this->markTestSkipped( 'WP_MAX_MEMORY_LIMIT should be set to -1' );
883
		}
884
885
		$ini_limit_before = ini_get( 'memory_limit' );
886
		$raised_limit = wp_raise_memory_limit();
887
		$ini_limit_after = ini_get( 'memory_limit' );
888
889
		$this->assertSame( $ini_limit_before, $ini_limit_after );
890
		$this->assertSame( false, $raised_limit );
891
		$this->assertEquals( WP_MAX_MEMORY_LIMIT, $ini_limit_after );
892
	}
893
894
	/**
895
	 * Tests wp_generate_uuid4().
896
	 *
897
	 * @covers ::wp_generate_uuid4
898
	 * @ticket 38164
899
	 */
900
	function test_wp_generate_uuid4() {
901
		$uuids = array();
902
		for ( $i = 0; $i < 20; $i += 1 ) {
903
			$uuid = wp_generate_uuid4();
904
			$this->assertRegExp( '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid );
905
			$uuids[] = $uuid;
906
		}
907
908
		$unique_uuids = array_unique( $uuids );
909
		$this->assertEquals( $uuids, $unique_uuids );
910
	}
911
912
	/**
913
	 * @ticket 40017
914
	 * @dataProvider _wp_get_image_mime
915
	 */
916
	public function test_wp_get_image_mime( $file, $expected ) {
917
		if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) {
918
			$this->markTestSkipped( 'The exif PHP extension is not loaded.' );
919
		}
920
921
		$this->assertEquals( $expected, wp_get_image_mime( $file ) );
922
	}
923
924
	/**
925
	 * @ticket 39550
926
	 * @dataProvider _wp_check_filetype_and_ext_data
927
	 */
928
	function test_wp_check_filetype_and_ext( $file, $filename, $expected ) {
929
		if ( ! extension_loaded( 'fileinfo' ) ) {
930
			$this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' );
931
		}
932
933
		$this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
934
	}
935
936
	/**
937
	 * @ticket 39550
938
	 * @group ms-excluded
939
	 */
940 View Code Duplication
	function test_wp_check_filetype_and_ext_with_filtered_svg() {
941
		if ( ! extension_loaded( 'fileinfo' ) ) {
942
			$this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' );
943
		}
944
945
		$file = DIR_TESTDATA . '/uploads/video-play.svg';
946
		$filename = 'video-play.svg';
947
948
		$expected = array(
949
			'ext' => 'svg',
950
			'type' => 'image/svg+xml',
951
			'proper_filename' => false,
952
		);
953
954
		add_filter( 'upload_mimes', array( $this, '_filter_mime_types_svg' ) );
955
		$this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
956
957
		// Cleanup.
958
		remove_filter( 'upload_mimes', array( $this, '_test_add_mime_types_svg' ) );
959
	}
960
961
	/**
962
	 * @ticket 39550
963
	 * @group ms-excluded
964
	 */
965 View Code Duplication
	function test_wp_check_filetype_and_ext_with_filtered_woff() {
966
		if ( ! extension_loaded( 'fileinfo' ) ) {
967
			$this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' );
968
		}
969
970
		$file = DIR_TESTDATA . '/uploads/dashicons.woff';
971
		$filename = 'dashicons.woff';
972
973
		$expected = array(
974
			'ext' => 'woff',
975
			'type' => 'application/font-woff',
976
			'proper_filename' => false,
977
		);
978
979
		add_filter( 'upload_mimes', array( $this, '_filter_mime_types_woff' ) );
980
		$this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
981
982
		// Cleanup.
983
		remove_filter( 'upload_mimes', array( $this, '_test_add_mime_types_woff' ) );
984
	}
985
986
	public function _filter_mime_types_svg( $mimes ) {
987
		$mimes['svg'] = 'image/svg+xml';
988
		return $mimes;
989
	}
990
991
	public function _filter_mime_types_woff( $mimes ) {
992
		$mimes['woff'] = 'application/font-woff';
993
		return $mimes;
994
	}
995
996
	/**
997
	 * Data profider for test_wp_get_image_mime();
998
	 */
999
	public function _wp_get_image_mime() {
1000
		$data = array(
1001
			// Standard JPEG.
1002
			array(
1003
				DIR_TESTDATA . '/images/test-image.jpg',
1004
				'image/jpeg',
1005
			),
1006
			// Standard GIF.
1007
			array(
1008
				DIR_TESTDATA . '/images/test-image.gif',
1009
				'image/gif',
1010
			),
1011
			// Standard PNG.
1012
			array(
1013
				DIR_TESTDATA . '/images/test-image.png',
1014
				'image/png',
1015
			),
1016
			// Image with wrong extension.
1017
			array(
1018
				DIR_TESTDATA . '/images/test-image-mime-jpg.png',
1019
				'image/jpeg',
1020
			),
1021
			// Not an image.
1022
			array(
1023
				DIR_TESTDATA . '/uploads/dashicons.woff',
1024
				false,
1025
			),
1026
		);
1027
1028
		return $data;
1029
	}
1030
1031
	public function _wp_check_filetype_and_ext_data() {
1032
		$data = array(
1033
			// Standard image.
1034
			array(
1035
				DIR_TESTDATA . '/images/canola.jpg',
1036
				'canola.jpg',
1037
				array(
1038
					'ext' => 'jpg',
1039
					'type' => 'image/jpeg',
1040
					'proper_filename' => false,
1041
				),
1042
			),
1043
			// Image with wrong extension.
1044
			array(
1045
				DIR_TESTDATA . '/images/test-image-mime-jpg.png',
1046
				'test-image-mime-jpg.png',
1047
				array(
1048
					'ext' => 'jpg',
1049
					'type' => 'image/jpeg',
1050
					'proper_filename' => 'test-image-mime-jpg.jpg',
1051
				),
1052
			),
1053
			// Image without extension.
1054
			array(
1055
				DIR_TESTDATA . '/images/test-image-no-extension',
1056
				'test-image-no-extension',
1057
				array(
1058
					'ext' => false,
1059
					'type' => false,
1060
					'proper_filename' => false,
1061
				),
1062
			),
1063
			// Valid non-image file with an image extension.
1064
			array(
1065
				DIR_TESTDATA . '/formatting/big5.txt',
1066
				'big5.jpg',
1067
				array(
1068
					'ext' => 'jpg',
1069
					'type' => 'image/jpeg',
1070
					'proper_filename' => false,
1071
				),
1072
			),
1073
			// Non-image file not allowed.
1074
			array(
1075
				DIR_TESTDATA . '/export/crazy-cdata.xml',
1076
				'crazy-cdata.xml',
1077
				array(
1078
					'ext' => false,
1079
					'type' => false,
1080
					'proper_filename' => false,
1081
				),
1082
			),
1083
		);
1084
1085
		// Test a few additional file types on single sites.
1086
		if ( ! is_multisite() ) {
1087
			$data = array_merge( $data, array(
1088
				// Standard non-image file.
1089
				array(
1090
					DIR_TESTDATA . '/formatting/big5.txt',
1091
					'big5.txt',
1092
					array(
1093
						'ext' => 'txt',
1094
						'type' => 'text/plain',
1095
						'proper_filename' => false,
1096
					),
1097
				),
1098
				// Non-image file with wrong sub-type.
1099
				array(
1100
					DIR_TESTDATA . '/uploads/pages-to-word.docx',
1101
					'pages-to-word.docx',
1102
					array(
1103
						'ext' => 'docx',
1104
						'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
1105
						'proper_filename' => false,
1106
					),
1107
				),
1108
			) );
1109
		}
1110
1111
		return $data;
1112
	}
1113
}
1114