Tests_Functions::test_is_serialized()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 4
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @group functions.php
5
 */
6
class Tests_Functions extends WP_UnitTestCase
7
{
8
    function test_wp_parse_args_object() 
9
    {
10
        $x = new MockClass;
11
        $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...
12
        $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...
13
        $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...
14
        $this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x));
15
        $y = new MockClass;
16
        $this->assertEquals(array(), wp_parse_args($y));
17
    }
18
19
    function test_wp_parse_args_array()  
20
    {
21
        // arrays
22
        $a = array();
23
        $this->assertEquals(array(), wp_parse_args($a));
24
        $b = array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x'));
25
        $this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($b));
26
    }
27
28
    function test_wp_parse_args_defaults() 
29
    {
30
        $x = new MockClass;
31
        $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...
32
        $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...
33
        $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...
34
        $d = array('pu' => 'bu');
35
        $this->assertEquals(array('pu' => 'bu', '_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x, $d));
36
        $e = array('_baba' => 6);
37
        $this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x, $e));
38
    }
39
40
    function test_wp_parse_args_other() 
41
    {
42
        $b = true;
43
        wp_parse_str($b, $s);
0 ignored issues
show
Bug introduced by
The variable $s does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
44
        $this->assertEquals($s, wp_parse_args($b));
45
        $q = 'x=5&_baba=dudu&';
46
        wp_parse_str($q, $ss);
0 ignored issues
show
Bug introduced by
The variable $ss does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
        $this->assertEquals($ss, wp_parse_args($q));
48
    }
49
50
    /**
51
     * @ticket 30753
52
     */
53
    function test_wp_parse_args_boolean_strings() 
54
    {
55
        $args = wp_parse_args('foo=false&bar=true');
56
        $this->assertInternalType('string', $args['foo']);
57
        $this->assertInternalType('string', $args['bar']);
58
    }
59
60
    /**
61
     * @ticket 35972
62
     */
63
    function test_bool_from_yn() 
64
    {
65
        $this->assertTrue(bool_from_yn('Y'));
66
        $this->assertTrue(bool_from_yn('y'));
67
        $this->assertFalse(bool_from_yn('n'));
68
    }
69
70 View Code Duplication
    function test_path_is_absolute() 
71
    {
72
        if (!is_callable('path_is_absolute') ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
73
              $this->markTestSkipped();
74
        }
75
76
        $absolute_paths = array(
77
         '/',
78
         '/foo/',
79
         '/foo',
80
         '/FOO/bar',
81
         '/foo/bar/',
82
         '/foo/../bar/',
83
         '\\WINDOWS',
84
         'C:\\',
85
         'C:\\WINDOWS',
86
         '\\\\sambashare\\foo',
87
         );
88
        foreach ($absolute_paths as $path) {
0 ignored issues
show
introduced by
No space before closing parenthesis is prohibited
Loading history...
89
                 $this->assertTrue(path_is_absolute($path), "path_is_absolute('$path') should return true");
90
        }
91
    }
92
93 View Code Duplication
    function test_path_is_not_absolute() 
94
    {
95
        if (!is_callable('path_is_absolute') ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
96
              $this->markTestSkipped();
97
        }
98
99
        $relative_paths = array(
100
         '',
101
         '.',
102
         '..',
103
         '../foo',
104
         '../',
105
         '../foo.bar',
106
         'foo/bar',
107
         'foo',
108
         'FOO',
109
         '..\\WINDOWS',
110
         );
111
        foreach ($relative_paths as $path) {
0 ignored issues
show
introduced by
No space before closing parenthesis is prohibited
Loading history...
112
                 $this->assertFalse(path_is_absolute($path), "path_is_absolute('$path') should return false");
113
        }
114
    }
115
116
    /**
117
     * @ticket 33265
118
     * @ticket 35996
119
     *
120
     * @dataProvider data_wp_normalize_path
121
     */
122
    function test_wp_normalize_path( $path, $expected ) 
123
    {
124
        $this->assertEquals($expected, wp_normalize_path($path));
125
    }
126 View Code Duplication
    function data_wp_normalize_path() 
127
    {
128
        return array(
129
         // Windows paths
130
         array( 'C:\\www\\path\\', 'C:/www/path/' ),
131
         array( 'C:\\www\\\\path\\', 'C:/www/path/' ),
132
         array( 'c:/www/path', 'C:/www/path' ),
133
         array( 'c:\\www\\path\\', 'C:/www/path/' ), // uppercase drive letter
134
         array( 'c:\\\\www\\path\\', 'C:/www/path/' ),
135
         array( '\\\\Domain\\DFSRoots\\share\\path\\', '//Domain/DFSRoots/share/path/' ),
136
         array( '\\\\Server\\share\\path', '//Server/share/path' ),
137
         array( '\\\\Server\\share', '//Server/share' ),
138
139
         // Linux paths
140
         array( '/www/path/', '/www/path/' ),
141
         array( '/www/path/////', '/www/path/' ),
142
         array( '/www/path', '/www/path' ),
143
        );
144
    }
145
146
    function test_wp_unique_filename() 
147
    {
148
149
        $testdir = DIR_TESTDATA . '/images/';
150
151
        // sanity check
152
        $this->assertEquals('abcdefg.png', wp_unique_filename($testdir, 'abcdefg.png'), 'Sanitiy check failed');
153
154
        // check number is appended for file already exists
155
        $this->assertFileExists($testdir . 'test-image.png', 'Test image does not exist');
156
        $this->assertEquals('test-image-1.png', wp_unique_filename($testdir, 'test-image.png'), 'Number not appended correctly');
157
        $this->assertFileNotExists($testdir . 'test-image-1.png');
158
159
        // check special chars
160
        $this->assertEquals('testtést-imagé.png', wp_unique_filename($testdir, 'testtést-imagé.png'), 'Filename with special chars failed');
161
162
        // check special chars with potential conflicting name
163
        $this->assertEquals('tést-imagé.png', wp_unique_filename($testdir, 'tést-imagé.png'), 'Filename with special chars failed');
164
165
        // check with single quotes in name (somehow)
166
        $this->assertEquals("abcdefgh.png", wp_unique_filename($testdir, "abcdefg'h.png"), 'File with quote failed');
167
168
        // check with single quotes in name (somehow)
169
        $this->assertEquals("abcdefgh.png", wp_unique_filename($testdir, 'abcdefg"h.png'), 'File with quote failed');
170
171
        // test crazy name (useful for regression tests)
172
        $this->assertEquals('12af34567890@..^_qwerty-fghjkl-zx.png', wp_unique_filename($testdir, '12%af34567890#~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png'), 'Failed crazy file name');
173
174
        // test slashes in names
175
        $this->assertEquals('abcdefg.png', wp_unique_filename($testdir, 'abcde\fg.png'), 'Slash not removed');
176
        $this->assertEquals('abcdefg.png', wp_unique_filename($testdir, 'abcde\\fg.png'), 'Double slashed not removed');
177
        $this->assertEquals('abcdefg.png', wp_unique_filename($testdir, 'abcde\\\fg.png'), 'Tripple slashed not removed');
178
    }
179
180
    function test_is_serialized() 
181
    {
182
        $cases = array(
183
         serialize(null),
184
         serialize(true),
185
         serialize(false),
186
         serialize(-25),
187
         serialize(25),
188
         serialize(1.1),
189
         serialize('this string will be serialized'),
190
         serialize("a\nb"),
191
         serialize(array()),
192
         serialize(array(1,1,2,3,5,8,13)),
193
         serialize((object)array('test' => true, '3', 4))
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
introduced by
No space before opening casting parenthesis is prohibited
Loading history...
194
        );
195
        foreach ( $cases as $case ) {
196
                 $this->assertTrue(is_serialized($case), "Serialized data: $case");
197
        }
198
199
        $not_serialized = array(
200
         'a string',
201
         'garbage:a:0:garbage;',
202
         's:4:test;'
203
        );
204
        foreach ( $not_serialized as $case ) {
205
                 $this->assertFalse(is_serialized($case), "Test data: $case");
206
        }
207
    }
208
209
    /**
210
     * @ticket 17375
211
     */
212
    function test_no_new_serializable_types() 
213
    {
214
        $this->assertFalse(is_serialized('C:16:"Serialized_Class":6:{a:0:{}}'));
215
    }
216
217
    /**
218
     * @dataProvider data_is_serialized_string
219
     */
220
    public function test_is_serialized_string( $value, $result ) 
221
    {
222
        $this->assertSame(is_serialized_string($value), $result);
223
    }
224
225 View Code Duplication
    public function data_is_serialized_string() 
226
    {
227
        return array(
228
         // Not a string.
229
         array( 0, false ),
230
231
         // Too short when trimmed.
232
         array( 's:3   ', false ),
233
234
         // Too short.
235
         array( 's:3', false ),
236
237
         // No colon in second position.
238
         array( 's!3:"foo";', false ),
239
240
         // No trailing semicolon.
241
         array( 's:3:"foo"', false ),
242
243
         // Wrong type.
244
         array( 'a:3:"foo";', false ),
245
246
         // No closing quote.
247
         array( 'a:3:"foo;', false ),
248
249
         // Wrong number of characters is close enough for is_serialized_string().
250
         array( 's:12:"foo";', true ),
251
252
         // Okay.
253
         array( 's:3:"foo";', true ),
254
255
        );
256
    }
257
258
    /**
259
     * @group add_query_arg
260
     */
261
    function test_add_query_arg() 
262
    {
263
        $old_req_uri = $_SERVER['REQUEST_URI'];
0 ignored issues
show
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
264
265
        $urls = array(
266
         '/',
267
         '/2012/07/30/',
268
         'edit.php',
269
         admin_url('edit.php'),
270
         admin_url('edit.php', 'https'),
271
        );
272
273
        $frag_urls = array(
274
         '/#frag',
275
         '/2012/07/30/#frag',
276
         'edit.php#frag',
277
         admin_url('edit.php#frag'),
278
         admin_url('edit.php#frag', 'https'),
279
        );
280
281 View Code Duplication
        foreach ( $urls as $url ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
282
               $_SERVER['REQUEST_URI'] = 'nothing';
283
284
               $this->assertEquals("$url?foo=1", add_query_arg('foo', '1', $url));
285
               $this->assertEquals("$url?foo=1", add_query_arg(array( 'foo' => '1' ), $url));
286
               $this->assertEquals("$url?foo=2", add_query_arg(array( 'foo' => '1', 'foo' => '2' ), $url));
287
               $this->assertEquals("$url?foo=1&bar=2", add_query_arg(array( 'foo' => '1', 'bar' => '2' ), $url));
288
289
               $_SERVER['REQUEST_URI'] = $url;
290
291
               $this->assertEquals("$url?foo=1", add_query_arg('foo', '1'));
292
               $this->assertEquals("$url?foo=1", add_query_arg(array( 'foo' => '1' )));
293
               $this->assertEquals("$url?foo=2", add_query_arg(array( 'foo' => '1', 'foo' => '2' )));
294
               $this->assertEquals("$url?foo=1&bar=2", add_query_arg(array( 'foo' => '1', 'bar' => '2' )));
295
        }
296
297
        foreach ( $frag_urls as $frag_url ) {
298
             $_SERVER['REQUEST_URI'] = 'nothing';
299
             $url = str_replace('#frag', '', $frag_url);
300
301
             $this->assertEquals("$url?foo=1#frag", add_query_arg('foo', '1', $frag_url));
302
             $this->assertEquals("$url?foo=1#frag", add_query_arg(array( 'foo' => '1' ), $frag_url));
303
             $this->assertEquals("$url?foo=2#frag", add_query_arg(array( 'foo' => '1', 'foo' => '2' ), $frag_url));
304
             $this->assertEquals("$url?foo=1&bar=2#frag", add_query_arg(array( 'foo' => '1', 'bar' => '2' ), $frag_url));
305
306
             $_SERVER['REQUEST_URI'] = $frag_url;
307
308
             $this->assertEquals("$url?foo=1#frag", add_query_arg('foo', '1'));
309
             $this->assertEquals("$url?foo=1#frag", add_query_arg(array( 'foo' => '1' )));
310
             $this->assertEquals("$url?foo=2#frag", add_query_arg(array( 'foo' => '1', 'foo' => '2' )));
311
             $this->assertEquals("$url?foo=1&bar=2#frag", add_query_arg(array( 'foo' => '1', 'bar' => '2' )));
312
        }
313
314
        $qs_urls = array(
315
         'baz=1', // #WP4903
316
         '/?baz',
317
         '/2012/07/30/?baz',
318
         'edit.php?baz',
319
         admin_url('edit.php?baz'),
320
         admin_url('edit.php?baz', 'https'),
321
         admin_url('edit.php?baz&za=1'),
322
         admin_url('edit.php?baz=1&za=1'),
323
         admin_url('edit.php?baz=0&za=0'),
324
        );
325
326 View Code Duplication
        foreach ( $qs_urls as $url ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
               $_SERVER['REQUEST_URI'] = 'nothing';
328
329
               $this->assertEquals("$url&foo=1", add_query_arg('foo', '1', $url));
330
               $this->assertEquals("$url&foo=1", add_query_arg(array( 'foo' => '1' ), $url));
331
               $this->assertEquals("$url&foo=2", add_query_arg(array( 'foo' => '1', 'foo' => '2' ), $url));
332
               $this->assertEquals("$url&foo=1&bar=2", add_query_arg(array( 'foo' => '1', 'bar' => '2' ), $url));
333
334
               $_SERVER['REQUEST_URI'] = $url;
335
336
               $this->assertEquals("$url&foo=1", add_query_arg('foo', '1'));
337
               $this->assertEquals("$url&foo=1", add_query_arg(array( 'foo' => '1' )));
338
               $this->assertEquals("$url&foo=2", add_query_arg(array( 'foo' => '1', 'foo' => '2' )));
339
               $this->assertEquals("$url&foo=1&bar=2", add_query_arg(array( 'foo' => '1', 'bar' => '2' )));
340
        }
341
342
        $_SERVER['REQUEST_URI'] = $old_req_uri;
343
    }
344
345
    /**
346
     * @ticket 31306
347
     */
348
    function test_add_query_arg_numeric_keys() 
349
    {
350
        $url = add_query_arg(array( 'foo' => 'bar' ), '1=1');
351
        $this->assertEquals('1=1&foo=bar', $url);
352
353
        $url = add_query_arg(array( 'foo' => 'bar', '1' => '2' ), '1=1');
354
        $this->assertEquals('1=2&foo=bar', $url);
355
356
        $url = add_query_arg(array( '1' => '2' ), 'foo=bar');
357
        $this->assertEquals('foo=bar&1=2', $url);
358
    }
359
360
    /**
361
     * @ticket 21594
362
     */
363 View Code Duplication
    function test_get_allowed_mime_types() 
364
    {
365
        $mimes = get_allowed_mime_types();
366
367
        $this->assertInternalType('array', $mimes);
368
        $this->assertNotEmpty($mimes);
369
370
        add_filter('upload_mimes', '__return_empty_array');
371
        $mimes = get_allowed_mime_types();
372
        $this->assertInternalType('array', $mimes);
373
        $this->assertEmpty($mimes);
374
375
        remove_filter('upload_mimes', '__return_empty_array');
376
        $mimes = get_allowed_mime_types();
377
        $this->assertInternalType('array', $mimes);
378
        $this->assertNotEmpty($mimes);
379
    }
380
381
    /**
382
     * @ticket 21594
383
     */
384
    function test_wp_get_mime_types() 
385
    {
386
        $mimes = wp_get_mime_types();
387
388
        $this->assertInternalType('array', $mimes);
389
        $this->assertNotEmpty($mimes);
390
391
        add_filter('mime_types', '__return_empty_array');
392
        $mimes = wp_get_mime_types();
393
        $this->assertInternalType('array', $mimes);
394
        $this->assertEmpty($mimes);
395
396
        remove_filter('mime_types', '__return_empty_array');
397
        $mimes = wp_get_mime_types();
398
        $this->assertInternalType('array', $mimes);
399
        $this->assertNotEmpty($mimes);
400
401
        // upload_mimes shouldn't affect wp_get_mime_types()
402
        add_filter('upload_mimes', '__return_empty_array');
403
        $mimes = wp_get_mime_types();
404
        $this->assertInternalType('array', $mimes);
405
        $this->assertNotEmpty($mimes);
406
407
        remove_filter('upload_mimes', '__return_empty_array');
408
        $mimes2 = wp_get_mime_types();
409
        $this->assertInternalType('array', $mimes2);
410
        $this->assertNotEmpty($mimes2);
411
        $this->assertEquals($mimes2, $mimes);
412
    }
413
414
    /**
415
     * @ticket 23688
416
     */
417 View Code Duplication
    function test_canonical_charset() 
418
    {
419
        $orig_blog_charset = get_option('blog_charset');
420
421
        update_option('blog_charset', 'utf8');
422
        $this->assertEquals('UTF-8', get_option('blog_charset'));
423
424
        update_option('blog_charset', 'utf-8');
425
        $this->assertEquals('UTF-8', get_option('blog_charset'));
426
427
        update_option('blog_charset', 'UTF8');
428
        $this->assertEquals('UTF-8', get_option('blog_charset'));
429
430
        update_option('blog_charset', 'UTF-8');
431
        $this->assertEquals('UTF-8', get_option('blog_charset'));
432
433
        update_option('blog_charset', 'ISO-8859-1');
434
        $this->assertEquals('ISO-8859-1', get_option('blog_charset'));
435
436
        update_option('blog_charset', 'ISO8859-1');
437
        $this->assertEquals('ISO-8859-1', get_option('blog_charset'));
438
439
        update_option('blog_charset', 'iso8859-1');
440
        $this->assertEquals('ISO-8859-1', get_option('blog_charset'));
441
442
        update_option('blog_charset', 'iso-8859-1');
443
        $this->assertEquals('ISO-8859-1', get_option('blog_charset'));
444
445
        // Arbitrary strings are passed through.
446
        update_option('blog_charset', 'foobarbaz');
447
        $this->assertEquals('foobarbaz', get_option('blog_charset'));
448
449
        update_option('blog_charset', $orig_blog_charset);
450
    }
451
452
    /**
453
     * @dataProvider data_wp_parse_id_list
454
     */
455
    function test_wp_parse_id_list( $expected, $actual ) 
456
    {
457
        $this->assertSame($expected, array_values(wp_parse_id_list($actual)));
458
    }
459
460
    function data_wp_parse_id_list() 
461
    {
462
        return array(
463
         array( array( 1, 2, 3, 4 ), '1,2,3,4' ),
464
         array( array( 1, 2, 3, 4 ), '1, 2,,3,4' ),
465
         array( array( 1, 2, 3, 4 ), '1,2,2,3,4' ),
466
         array( array( 1, 2, 3, 4 ), array( '1', '2', '3', '4', '3' ) ),
467
         array( array( 1, 2, 3, 4 ), array( 1, '2', 3, '4' ) ),
468
         array( array( 1, 2, 3, 4 ), '-1,2,-3,4' ),
469
         array( array( 1, 2, 3, 4 ), array( -1, 2, '-3', '4' ) ),
470
        );
471
    }
472
473
    /**
474
     * @dataProvider data_device_can_upload
475
     */
476
    function test_device_can_upload( $user_agent, $expected ) 
477
    {
478
        $_SERVER['HTTP_USER_AGENT'] = $user_agent;
0 ignored issues
show
introduced by
Due to using Batcache, server side based client related logic will not work, use JS instead.
Loading history...
479
        $actual = _device_can_upload();
480
        unset($_SERVER['HTTP_USER_AGENT']);
0 ignored issues
show
introduced by
Due to using Batcache, server side based client related logic will not work, use JS instead.
Loading history...
481
        $this->assertEquals($expected, $actual);
482
    }
483
484 View Code Duplication
    function data_device_can_upload() 
485
    {
486
        return array(
487
         // iPhone iOS 5.0.1, Safari 5.1
488
         array(
489
          'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A406)',
490
          false,
491
         ),
492
         // iPad iOS 3.2, Safari 4.0.4
493
         array(
494
          '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',
495
          false,
496
         ),
497
         // iPod iOS 4.3.3, Safari 5.0.2
498
         array(
499
          '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',
500
          false,
501
         ),
502
         // iPhone iOS 6.0.0, Safari 6.0
503
         array(
504
          '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',
505
          true,
506
         ),
507
         // iPad iOS 6.0.0, Safari 6.0
508
         array(
509
          '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',
510
          true,
511
         ),
512
         // Android 2.2, Android Webkit Browser
513
         array(
514
          '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',
515
          true,
516
         ),
517
         // BlackBerry 9900, BlackBerry browser
518
         array(
519
          'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+',
520
          true,
521
         ),
522
         // Windows Phone 8.0, Internet Explorer 10.0;
523
         array(
524
          'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)',
525
          true,
526
         ),
527
         // Ubuntu desktop, Firefox 41.0
528
         array(
529
          'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0',
530
          true,
531
         ),
532
        );
533
    }
534
535
    /**
536
     * @ticket 9064
537
     */
538
    function test_wp_extract_urls() 
539
    {
540
        $original_urls = array(
541
         'http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html',
542
         'http://this.com',
543
         'http://127.0.0.1',
544
         'http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437',
545
         'http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html',
546
         'http://wordpress-core.com:8080/',
547
         'http://www.website.com:5000',
548
         'http://wordpress-core/?346236346326&2134362574863.437',
549
         'http://افغانستا.icom.museum',
550
         'http://الجزائر.icom.museum',
551
         'http://österreich.icom.museum',
552
         'http://বাংলাদেশ.icom.museum',
553
         'http://беларусь.icom.museum',
554
         'http://belgië.icom.museum',
555
         'http://българия.icom.museum',
556
         'http://تشادر.icom.museum',
557
         'http://中国.icom.museum',
558
         // 'http://القمر.icom.museum', // Comoros	http://القمر.icom.museum
559
         // 'http://κυπρος.icom.museum', Cyprus 	http://κυπρος.icom.museum
560
         'http://českárepublika.icom.museum',
561
         // 'http://مصر.icom.museum', // Egypt	http://مصر.icom.museum
562
         'http://ελλάδα.icom.museum',
563
         'http://magyarország.icom.museum',
564
         'http://ísland.icom.museum',
565
         'http://भारत.icom.museum',
566
         'http://ايران.icom.museum',
567
         'http://éire.icom.museum',
568
         'http://איקו״ם.ישראל.museum',
569
         'http://日本.icom.museum',
570
         'http://الأردن.icom.museum',
571
         'http://қазақстан.icom.museum',
572
         'http://한국.icom.museum',
573
         'http://кыргызстан.icom.museum',
574
         'http://ລາວ.icom.museum',
575
         'http://لبنان.icom.museum',
576
         'http://македонија.icom.museum',
577
         // 'http://موريتانيا.icom.museum', // Mauritania	http://موريتانيا.icom.museum
578
         'http://méxico.icom.museum',
579
         'http://монголулс.icom.museum',
580
         // 'http://المغرب.icom.museum', // Morocco	http://المغرب.icom.museum
581
         'http://नेपाल.icom.museum',
582
         // 'http://عمان.icom.museum', // Oman	http://عمان.icom.museum
583
         'http://قطر.icom.museum',
584
         'http://românia.icom.museum',
585
         'http://россия.иком.museum',
586
         'http://србијаицрнагора.иком.museum',
587
         'http://இலங்கை.icom.museum',
588
         'http://españa.icom.museum',
589
         'http://ไทย.icom.museum',
590
         'http://تونس.icom.museum',
591
         'http://türkiye.icom.museum',
592
         'http://украина.icom.museum',
593
         'http://việtnam.icom.museum',
594
         'ftp://127.0.0.1/',
595
         'http://www.woo.com/video?v=exvUH2qKLTU',
596
         'http://taco.com?burrito=enchilada#guac'
597
        );
598
599
        $blob ="
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
600
			http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html
601
602
			http://this.com
603
604
			http://127.0.0.1
605
606
			http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&amp;2134362574863.437
607
608
			http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html
609
610
			http://wordpress-core.com:8080/
611
612
			http://www.website.com:5000
613
614
			http://wordpress-core/?346236346326&amp;2134362574863.437
615
616
			http://افغانستا.icom.museum
617
			http://الجزائر.icom.museum
618
			http://österreich.icom.museum
619
			http://বাংলাদেশ.icom.museum
620
			http://беларусь.icom.museum
621
			http://belgië.icom.museum
622
			http://българия.icom.museum
623
			http://تشادر.icom.museum
624
			http://中国.icom.museum
625
			http://českárepublika.icom.museum
626
			http://ελλάδα.icom.museum
627
			http://magyarország.icom.museum
628
			http://ísland.icom.museum
629
			http://भारत.icom.museum
630
			http://ايران.icom.museum
631
			http://éire.icom.museum
632
			http://איקו״ם.ישראל.museum
633
			http://日本.icom.museum
634
			http://الأردن.icom.museum
635
			http://қазақстан.icom.museum
636
			http://한국.icom.museum
637
			http://кыргызстан.icom.museum
638
			http://ລາວ.icom.museum
639
			http://لبنان.icom.museum
640
			http://македонија.icom.museum
641
			http://méxico.icom.museum
642
			http://монголулс.icom.museum
643
			http://नेपाल.icom.museum
644
			http://قطر.icom.museum
645
			http://românia.icom.museum
646
			http://россия.иком.museum
647
			http://србијаицрнагора.иком.museum
648
			http://இலங்கை.icom.museum
649
			http://españa.icom.museum
650
			http://ไทย.icom.museum
651
			http://تونس.icom.museum
652
			http://türkiye.icom.museum
653
			http://украина.icom.museum
654
			http://việtnam.icom.museum
655
			ftp://127.0.0.1/
656
			http://www.woo.com/video?v=exvUH2qKLTU
657
658
			http://taco.com?burrito=enchilada#guac
659
		";
660
661
        $urls = wp_extract_urls($blob);
662
        $this->assertNotEmpty($urls);
663
        $this->assertInternalType('array', $urls);
664
        $this->assertCount(count($original_urls), $urls);
665
        $this->assertEquals($original_urls, $urls);
666
667
        $exploded = array_values(array_filter(array_map('trim', explode("\n", $blob))));
668
        // wp_extract_urls calls html_entity_decode
669
        $decoded = array_map('html_entity_decode', $exploded);
670
671
        $this->assertEquals($decoded, $urls);
672
        $this->assertEquals($original_urls, $decoded);
673
674
        $blob ="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
675
			incididunt ut labore http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html et dolore magna aliqua.
676
			Ut http://this.com enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco http://127.0.0.1
677
			laboris nisi ut aliquip ex http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&amp;2134362574863.437 ea
678
			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
679
			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,
680
			sunt in culpa qui officia deserunt mollit http://wordpress-core/?346236346326&amp;2134362574863.437 anim id est laborum.";
681
682
        $urls = wp_extract_urls($blob);
683
        $this->assertNotEmpty($urls);
684
        $this->assertInternalType('array', $urls);
685
        $this->assertCount(8, $urls);
686
        $this->assertEquals(array_slice($original_urls, 0, 8), $urls);
687
688
        $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
689
			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.
690
			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>
691
			laboris nisi ut aliquip ex <a href="http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&amp;2134362574863.437">343462^</a> ea
692
			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
693
			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,
694
			sunt in culpa qui officia deserunt mollit <a href="http://wordpress-core/?346236346326&amp;2134362574863.437">)(*&^%$</a> anim id est laborum.';
695
696
        $urls = wp_extract_urls($blob);
697
        $this->assertNotEmpty($urls);
698
        $this->assertInternalType('array', $urls);
699
        $this->assertCount(8, $urls);
700
        $this->assertEquals(array_slice($original_urls, 0, 8), $urls);
701
    }
702
703
    /**
704
     * @ticket 28786
705
     */
706
    function test_wp_json_encode() 
707
    {
708
        $this->assertEquals(wp_json_encode('a'), '"a"');
709
    }
710
711
    /**
712
     * @ticket 28786
713
     */
714
    function test_wp_json_encode_utf8() 
715
    {
716
        $this->assertEquals(wp_json_encode('这'), '"\u8fd9"');
717
    }
718
719
    /**
720
     * @ticket 28786
721
     */
722 View Code Duplication
    function test_wp_json_encode_non_utf8() 
723
    {
724
        if (! function_exists('mb_detect_order') ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
725
            $this->markTestSkipped('mbstring extension not available.');
726
        }
727
728
        $old_charsets = $charsets = mb_detect_order();
729
        if (! in_array('EUC-JP', $charsets) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
730
            $charsets[] = 'EUC-JP';
731
            mb_detect_order($charsets);
732
        }
733
734
        $eucjp = mb_convert_encoding('aあb', 'EUC-JP', 'UTF-8');
735
        $utf8 = mb_convert_encoding($eucjp, 'UTF-8', 'EUC-JP');
736
737
        $this->assertEquals('aあb', $utf8);
738
739
        $this->assertEquals('"a\u3042b"', wp_json_encode($eucjp));
740
741
        mb_detect_order($old_charsets);
742
    }
743
744
    /**
745
     * @ticket 28786
746
     */
747 View Code Duplication
    function test_wp_json_encode_non_utf8_in_array() 
748
    {
749
        if (! function_exists('mb_detect_order') ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
750
            $this->markTestSkipped('mbstring extension not available.');
751
        }
752
753
        $old_charsets = $charsets = mb_detect_order();
754
        if (! in_array('EUC-JP', $charsets) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
755
            $charsets[] = 'EUC-JP';
756
            mb_detect_order($charsets);
757
        }
758
759
        $eucjp = mb_convert_encoding('aあb', 'EUC-JP', 'UTF-8');
760
        $utf8 = mb_convert_encoding($eucjp, 'UTF-8', 'EUC-JP');
761
762
        $this->assertEquals('aあb', $utf8);
763
764
        $this->assertEquals('["c","a\u3042b"]', wp_json_encode(array( 'c', $eucjp )));
765
766
        mb_detect_order($old_charsets);
767
    }
768
769
    /**
770
     * @ticket 28786
771
     */
772
    function test_wp_json_encode_array() 
773
    {
774
        $this->assertEquals(wp_json_encode(array( 'a' )), '["a"]');
775
    }
776
777
    /**
778
     * @ticket 28786
779
     */
780
    function test_wp_json_encode_object() 
781
    {
782
        $object = new stdClass;
783
        $object->a = 'b';
784
        $this->assertEquals(wp_json_encode($object), '{"a":"b"}');
785
    }
786
787
    /**
788
     * @ticket 28786
789
     */
790
    function test_wp_json_encode_depth() 
791
    {
792
        if (version_compare(PHP_VERSION, '5.5', '<') ) {
793
            $this->markTestSkipped('json_encode() supports the $depth parameter in PHP 5.5+');
794
        };
795
796
        $data = array( array( array( 1, 2, 3 ) ) );
797
        $json = wp_json_encode($data, 0, 1);
798
        $this->assertFalse($json);
799
800
        $data = array( 'あ', array( array( 1, 2, 3 ) ) );
801
        $json = wp_json_encode($data, 0, 1);
802
        $this->assertFalse($json);
803
    }
804
805
    /**
806
     * @ticket 33750
807
     */
808
    function test_the_date() 
809
    {
810
        ob_start();
811
        the_date();
812
        $actual = ob_get_clean();
813
        $this->assertEquals('', $actual);
814
815
        $GLOBALS['post']        = self::factory()->post->create_and_get(
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
816
            array(
817
            'post_date' => '2015-09-16 08:00:00'
818
            ) 
819
        );
820
821
        ob_start();
822
        $GLOBALS['currentday']  = '18.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
823
        $GLOBALS['previousday'] = '17.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
824
        the_date();
825
        $this->assertEquals('September 16, 2015', ob_get_clean());
826
827
        ob_start();
828
        $GLOBALS['currentday']  = '18.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
829
        $GLOBALS['previousday'] = '17.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
830
        the_date('Y');
831
        $this->assertEquals('2015', ob_get_clean());
832
833
        ob_start();
834
        $GLOBALS['currentday']  = '18.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
835
        $GLOBALS['previousday'] = '17.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
836
        the_date('Y', 'before ', ' after');
837
        $this->assertEquals('before 2015 after', ob_get_clean());
838
839
        ob_start();
840
        $GLOBALS['currentday']  = '18.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
841
        $GLOBALS['previousday'] = '17.09.15';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
842
        the_date('Y', 'before ', ' after', false);
843
        $this->assertEquals('', ob_get_clean());
844
    }
845
846
    /**
847
     * @ticket 36054
848
     * @dataProvider datetime_provider
849
     */
850
    function test_mysql_to_rfc3339( $expected, $actual ) 
851
    {
852
        $date_return = mysql_to_rfc3339($actual);
853
854
        $this->assertTrue(is_string($date_return), 'The date return must be a string');
855
        $this->assertNotEmpty($date_return, 'The date return could not be an empty string');
856
        $this->assertEquals($expected, $date_return, 'The date does not match');
857
        $this->assertEquals(new DateTime($expected), new DateTime($date_return), 'The date is not the same after the call method');
858
    }
859
860
    function datetime_provider() 
861
    {
862
        return array(
863
         array( '2016-03-15T18:54:46', '15-03-2016 18:54:46' ),
864
         array( '2016-03-02T19:13:25', '2016-03-02 19:13:25' ),
865
         array( '2016-03-02T19:13:00', '2016-03-02 19:13' ),
866
         array( '2016-03-02T19:13:00', '16-03-02 19:13' ),
867
         array( '2016-03-02T19:13:00', '16-03-02 19:13' )
868
        );
869
    }
870
871
    /**
872
     * @ticket 35987
873
     */
874 View Code Duplication
    public function test_wp_get_ext_types() 
875
    {
876
        $extensions = wp_get_ext_types();
877
878
        $this->assertInternalType('array', $extensions);
879
        $this->assertNotEmpty($extensions);
880
881
        add_filter('ext2type', '__return_empty_array');
882
        $extensions = wp_get_ext_types();
883
        $this->assertSame(array(), $extensions);
884
885
        remove_filter('ext2type', '__return_empty_array');
886
        $extensions = wp_get_ext_types();
887
        $this->assertInternalType('array', $extensions);
888
        $this->assertNotEmpty($extensions);
889
    }
890
891
    /**
892
     * @ticket 35987
893
     */
894
    public function test_wp_ext2type() 
895
    {
896
        $extensions = wp_get_ext_types();
897
898
        foreach ( $extensions as $type => $extensionList ) {
899
            foreach ( $extensionList as $extension ) {
900
                $this->assertEquals($type, wp_ext2type($extension));
901
                $this->assertEquals($type, wp_ext2type(strtoupper($extension)));
902
            }
903
        }
904
905
        $this->assertNull(wp_ext2type('unknown_format'));
906
    }
907
908
    /**
909
     * Tests raising the memory limit.
910
     *
911
     * Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the
912
     * test suite is -1, we can not test the memory limit negotiations.
913
     *
914
     * @ticket 32075
915
     */
916
    function test_wp_raise_memory_limit() 
917
    {
918
        if (-1 !== WP_MAX_MEMORY_LIMIT ) {
919
            $this->markTestSkipped('WP_MAX_MEMORY_LIMIT should be set to -1');
920
        }
921
922
        $ini_limit_before = ini_get('memory_limit');
923
        $raised_limit = wp_raise_memory_limit();
924
        $ini_limit_after = ini_get('memory_limit');
925
926
        $this->assertSame($ini_limit_before, $ini_limit_after);
927
        $this->assertSame(false, $raised_limit);
928
        $this->assertEquals(WP_MAX_MEMORY_LIMIT, $ini_limit_after);
929
    }
930
931
    /**
932
     * Tests wp_generate_uuid4().
933
     *
934
     * @covers wp_generate_uuid4()
935
     * @ticket 38164
936
     */
937
    function test_wp_generate_uuid4() 
938
    {
939
        $uuids = array();
940
        for ( $i = 0; $i < 20; $i += 1 ) {
941
            $uuid = wp_generate_uuid4();
942
            $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);
943
            $uuids[] = $uuid;
944
        }
945
946
        $unique_uuids = array_unique($uuids);
947
        $this->assertEquals($uuids, $unique_uuids);
948
    }
949
}
950