2013-08-07 08:38:38 +02:00
< ? php
/**
* @ group functions . php
*/
class Tests_Functions extends WP_UnitTestCase {
function test_wp_parse_args_object () {
2017-12-01 00:09:33 +01:00
$x = new MockClass ;
2013-08-07 08:38:38 +02:00
$x -> _baba = 5 ;
2019-01-18 03:14:24 +01:00
$x -> yZ = 'baba' ; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2017-12-01 00:09:33 +01:00
$x -> a = array ( 5 , 111 , 'x' );
$this -> assertEquals (
array (
'_baba' => 5 ,
'yZ' => 'baba' ,
'a' => array ( 5 , 111 , 'x' ),
2018-08-17 03:50:26 +02:00
),
wp_parse_args ( $x )
2017-12-01 00:09:33 +01:00
);
2013-08-07 08:38:38 +02:00
$y = new MockClass ;
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( array (), wp_parse_args ( $y ) );
2013-08-07 08:38:38 +02:00
}
2015-01-31 03:01:46 +01:00
2017-12-01 00:09:33 +01:00
function test_wp_parse_args_array () {
2013-08-07 08:38:38 +02:00
// arrays
$a = array ();
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( array (), wp_parse_args ( $a ) );
$b = array (
'_baba' => 5 ,
'yZ' => 'baba' ,
'a' => array ( 5 , 111 , 'x' ),
);
$this -> assertEquals (
array (
'_baba' => 5 ,
'yZ' => 'baba' ,
'a' => array ( 5 , 111 , 'x' ),
2018-08-17 03:50:26 +02:00
),
wp_parse_args ( $b )
2017-12-01 00:09:33 +01:00
);
2013-08-07 08:38:38 +02:00
}
2015-01-31 03:01:46 +01:00
2013-08-07 08:38:38 +02:00
function test_wp_parse_args_defaults () {
2017-12-01 00:09:33 +01:00
$x = new MockClass ;
2013-08-07 08:38:38 +02:00
$x -> _baba = 5 ;
2019-01-18 03:14:24 +01:00
$x -> yZ = 'baba' ; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2017-12-01 00:09:33 +01:00
$x -> a = array ( 5 , 111 , 'x' );
$d = array ( 'pu' => 'bu' );
$this -> assertEquals (
array (
'pu' => 'bu' ,
'_baba' => 5 ,
'yZ' => 'baba' ,
'a' => array ( 5 , 111 , 'x' ),
2018-08-17 03:50:26 +02:00
),
wp_parse_args ( $x , $d )
2017-12-01 00:09:33 +01:00
);
$e = array ( '_baba' => 6 );
$this -> assertEquals (
array (
'_baba' => 5 ,
'yZ' => 'baba' ,
'a' => array ( 5 , 111 , 'x' ),
2018-08-17 03:50:26 +02:00
),
wp_parse_args ( $x , $e )
2017-12-01 00:09:33 +01:00
);
2013-08-07 08:38:38 +02:00
}
2015-01-31 03:01:46 +01:00
2013-08-07 08:38:38 +02:00
function test_wp_parse_args_other () {
$b = true ;
2017-12-01 00:09:33 +01:00
wp_parse_str ( $b , $s );
$this -> assertEquals ( $s , wp_parse_args ( $b ) );
2013-08-07 08:38:38 +02:00
$q = 'x=5&_baba=dudu&' ;
2017-12-01 00:09:33 +01:00
wp_parse_str ( $q , $ss );
$this -> assertEquals ( $ss , wp_parse_args ( $q ) );
2013-08-07 08:38:38 +02:00
}
2015-01-31 03:01:46 +01:00
/**
* @ ticket 30753
*/
function test_wp_parse_args_boolean_strings () {
2015-01-31 03:11:46 +01:00
$args = wp_parse_args ( 'foo=false&bar=true' );
2015-01-31 03:01:46 +01:00
$this -> assertInternalType ( 'string' , $args [ 'foo' ] );
$this -> assertInternalType ( 'string' , $args [ 'bar' ] );
}
2016-02-28 20:55:48 +01:00
/**
* @ ticket 35972
*/
function test_bool_from_yn () {
$this -> assertTrue ( bool_from_yn ( 'Y' ) );
$this -> assertTrue ( bool_from_yn ( 'y' ) );
$this -> assertFalse ( bool_from_yn ( 'n' ) );
}
2013-08-07 08:38:38 +02:00
function test_path_is_absolute () {
$absolute_paths = array (
'/' ,
'/foo/' ,
'/foo' ,
'/FOO/bar' ,
'/foo/bar/' ,
'/foo/../bar/' ,
'\\WINDOWS' ,
'C:\\' ,
'C:\\WINDOWS' ,
'\\\\sambashare\\foo' ,
2017-12-01 00:09:33 +01:00
);
foreach ( $absolute_paths as $path ) {
$this -> assertTrue ( path_is_absolute ( $path ), " path_is_absolute(' $path ') should return true " );
}
2013-08-07 08:38:38 +02:00
}
function test_path_is_not_absolute () {
$relative_paths = array (
'' ,
'.' ,
'..' ,
'../foo' ,
'../' ,
'../foo.bar' ,
'foo/bar' ,
'foo' ,
'FOO' ,
'..\\WINDOWS' ,
2017-12-01 00:09:33 +01:00
);
foreach ( $relative_paths as $path ) {
$this -> assertFalse ( path_is_absolute ( $path ), " path_is_absolute(' $path ') should return false " );
}
2013-08-07 08:38:38 +02:00
}
2015-09-14 03:58:03 +02:00
/**
* @ ticket 33265
2016-03-08 08:06:12 +01:00
* @ ticket 35996
*
* @ dataProvider data_wp_normalize_path
2015-09-14 03:58:03 +02:00
*/
2016-03-08 08:06:12 +01:00
function test_wp_normalize_path ( $path , $expected ) {
$this -> assertEquals ( $expected , wp_normalize_path ( $path ) );
}
function data_wp_normalize_path () {
return array (
// Windows paths
array ( 'C:\\www\\path\\' , 'C:/www/path/' ),
array ( 'C:\\www\\\\path\\' , 'C:/www/path/' ),
array ( 'c:/www/path' , 'C:/www/path' ),
array ( 'c:\\www\\path\\' , 'C:/www/path/' ), // uppercase drive letter
array ( 'c:\\\\www\\path\\' , 'C:/www/path/' ),
array ( '\\\\Domain\\DFSRoots\\share\\path\\' , '//Domain/DFSRoots/share/path/' ),
array ( '\\\\Server\\share\\path' , '//Server/share/path' ),
array ( '\\\\Server\\share' , '//Server/share' ),
2015-09-14 03:58:03 +02:00
2016-03-08 08:06:12 +01:00
// Linux paths
array ( '/www/path/' , '/www/path/' ),
array ( '/www/path/////' , '/www/path/' ),
array ( '/www/path' , '/www/path' ),
2017-12-12 05:15:54 +01:00
// PHP Stream wrappers
array ( 'php://input' , 'php://input' ),
array ( 'http://example.com//path.ext' , 'http://example.com/path.ext' ),
array ( 'file://c:\\www\\path\\' , 'file://C:/www/path/' ),
2016-03-08 08:06:12 +01:00
);
2015-09-14 03:58:03 +02:00
}
2013-08-07 08:38:38 +02:00
function test_wp_unique_filename () {
2013-09-10 05:17:51 +02:00
2013-08-07 08:38:38 +02:00
$testdir = DIR_TESTDATA . '/images/' ;
2013-09-10 05:17:51 +02:00
2013-08-07 08:38:38 +02:00
// sanity check
$this -> assertEquals ( 'abcdefg.png' , wp_unique_filename ( $testdir , 'abcdefg.png' ), 'Sanitiy check failed' );
// check number is appended for file already exists
$this -> assertFileExists ( $testdir . 'test-image.png' , 'Test image does not exist' );
2015-10-20 06:04:26 +02:00
$this -> assertEquals ( 'test-image-1.png' , wp_unique_filename ( $testdir , 'test-image.png' ), 'Number not appended correctly' );
$this -> assertFileNotExists ( $testdir . 'test-image-1.png' );
2013-08-07 08:38:38 +02:00
// check special chars
$this -> assertEquals ( 'testtést-imagé.png' , wp_unique_filename ( $testdir , 'testtést-imagé.png' ), 'Filename with special chars failed' );
2013-09-10 05:17:51 +02:00
2013-08-07 08:38:38 +02:00
// check special chars with potential conflicting name
$this -> assertEquals ( 'tést-imagé.png' , wp_unique_filename ( $testdir , 'tést-imagé.png' ), 'Filename with special chars failed' );
2013-09-10 05:17:51 +02:00
2013-08-07 08:38:38 +02:00
// check with single quotes in name (somehow)
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'abcdefgh.png' , wp_unique_filename ( $testdir , " abcdefg'h.png " ), 'File with quote failed' );
2013-08-07 08:38:38 +02:00
// check with single quotes in name (somehow)
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'abcdefgh.png' , wp_unique_filename ( $testdir , 'abcdefg"h.png' ), 'File with quote failed' );
2013-08-07 08:38:38 +02:00
// test crazy name (useful for regression tests)
2015-10-13 05:41:11 +02:00
$this -> assertEquals ( '12af34567890@..^_qwerty-fghjkl-zx.png' , wp_unique_filename ( $testdir , '12%af34567890#~!@#$..%^&*()|_+qwerty fgh`jkl zx<>?:"{}[]="\'/?.png' ), 'Failed crazy file name' );
2013-09-10 05:17:51 +02:00
2013-08-07 08:38:38 +02:00
// test slashes in names
$this -> assertEquals ( 'abcdefg.png' , wp_unique_filename ( $testdir , 'abcde\fg.png' ), 'Slash not removed' );
$this -> assertEquals ( 'abcdefg.png' , wp_unique_filename ( $testdir , 'abcde\\fg.png' ), 'Double slashed not removed' );
$this -> assertEquals ( 'abcdefg.png' , wp_unique_filename ( $testdir , 'abcde\\\fg.png' ), 'Tripple slashed not removed' );
}
function test_is_serialized () {
$cases = array (
2017-12-01 00:09:33 +01:00
serialize ( null ),
serialize ( true ),
serialize ( false ),
serialize ( - 25 ),
serialize ( 25 ),
serialize ( 1.1 ),
serialize ( 'this string will be serialized' ),
serialize ( " a \n b " ),
serialize ( array () ),
serialize ( array ( 1 , 1 , 2 , 3 , 5 , 8 , 13 ) ),
serialize (
( object ) array (
'test' => true ,
'3' ,
4 ,
)
),
2013-08-07 08:38:38 +02:00
);
2017-12-01 00:09:33 +01:00
foreach ( $cases as $case ) {
$this -> assertTrue ( is_serialized ( $case ), " Serialized data: $case " );
}
2013-08-07 08:38:38 +02:00
$not_serialized = array (
'a string' ,
'garbage:a:0:garbage;' ,
2017-12-01 00:09:33 +01:00
's:4:test;' ,
2013-08-07 08:38:38 +02:00
);
2017-12-01 00:09:33 +01:00
foreach ( $not_serialized as $case ) {
$this -> assertFalse ( is_serialized ( $case ), " Test data: $case " );
}
2013-08-07 08:38:38 +02:00
}
2019-08-05 15:54:43 +02:00
/**
* @ ticket 46570
*/
function test_is_serialized_should_return_true_for_large_floats () {
$cases = array (
serialize ( 1.7976931348623157E+308 ),
2019-08-05 20:09:20 +02:00
serialize ( array ( 1.7976931348623157E+308 , 1.23e50 ) ),
2019-08-05 15:54:43 +02:00
);
foreach ( $cases as $case ) {
$this -> assertTrue ( is_serialized ( $case ), " Serialized data: $case " );
}
}
2015-05-28 07:22:14 +02:00
/**
* @ ticket 17375
*/
function test_no_new_serializable_types () {
$this -> assertFalse ( is_serialized ( 'C:16:"Serialized_Class":6:{a:0:{}}' ) );
}
2016-05-04 05:29:47 +02:00
2013-08-07 08:38:38 +02:00
/**
* @ group add_query_arg
*/
function test_add_query_arg () {
$old_req_uri = $_SERVER [ 'REQUEST_URI' ];
$urls = array (
'/' ,
'/2012/07/30/' ,
'edit.php' ,
admin_url ( 'edit.php' ),
admin_url ( 'edit.php' , 'https' ),
);
$frag_urls = array (
'/#frag' ,
'/2012/07/30/#frag' ,
'edit.php#frag' ,
admin_url ( 'edit.php#frag' ),
admin_url ( 'edit.php#frag' , 'https' ),
);
foreach ( $urls as $url ) {
$_SERVER [ 'REQUEST_URI' ] = 'nothing' ;
$this -> assertEquals ( " $url ?foo=1 " , add_query_arg ( 'foo' , '1' , $url ) );
$this -> assertEquals ( " $url ?foo=1 " , add_query_arg ( array ( 'foo' => '1' ), $url ) );
2017-12-01 00:09:33 +01:00
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'foo' => '2' ,
2018-08-17 03:50:26 +02:00
),
$url
2017-12-01 00:09:33 +01:00
)
);
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=1&bar=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'bar' => '2' ,
2018-08-17 03:50:26 +02:00
),
$url
2017-12-01 00:09:33 +01:00
)
);
2013-08-07 08:38:38 +02:00
$_SERVER [ 'REQUEST_URI' ] = $url ;
$this -> assertEquals ( " $url ?foo=1 " , add_query_arg ( 'foo' , '1' ) );
$this -> assertEquals ( " $url ?foo=1 " , add_query_arg ( array ( 'foo' => '1' ) ) );
2017-12-01 00:09:33 +01:00
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'foo' => '2' ,
)
)
);
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=1&bar=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'bar' => '2' ,
)
)
);
2013-08-07 08:38:38 +02:00
}
foreach ( $frag_urls as $frag_url ) {
$_SERVER [ 'REQUEST_URI' ] = 'nothing' ;
2017-12-01 00:09:33 +01:00
$url = str_replace ( '#frag' , '' , $frag_url );
2013-08-07 08:38:38 +02:00
$this -> assertEquals ( " $url ?foo=1#frag " , add_query_arg ( 'foo' , '1' , $frag_url ) );
$this -> assertEquals ( " $url ?foo=1#frag " , add_query_arg ( array ( 'foo' => '1' ), $frag_url ) );
2017-12-01 00:09:33 +01:00
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=2#frag " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'foo' => '2' ,
2018-08-17 03:50:26 +02:00
),
$frag_url
2017-12-01 00:09:33 +01:00
)
);
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=1&bar=2#frag " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'bar' => '2' ,
2018-08-17 03:50:26 +02:00
),
$frag_url
2017-12-01 00:09:33 +01:00
)
);
2013-08-07 08:38:38 +02:00
$_SERVER [ 'REQUEST_URI' ] = $frag_url ;
$this -> assertEquals ( " $url ?foo=1#frag " , add_query_arg ( 'foo' , '1' ) );
$this -> assertEquals ( " $url ?foo=1#frag " , add_query_arg ( array ( 'foo' => '1' ) ) );
2017-12-01 00:09:33 +01:00
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=2#frag " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'foo' => '2' ,
)
)
);
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url ?foo=1&bar=2#frag " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'bar' => '2' ,
)
)
);
2013-08-07 08:38:38 +02:00
}
$qs_urls = array (
'baz=1' , // #WP4903
'/?baz' ,
'/2012/07/30/?baz' ,
'edit.php?baz' ,
admin_url ( 'edit.php?baz' ),
admin_url ( 'edit.php?baz' , 'https' ),
admin_url ( 'edit.php?baz&za=1' ),
admin_url ( 'edit.php?baz=1&za=1' ),
admin_url ( 'edit.php?baz=0&za=0' ),
);
foreach ( $qs_urls as $url ) {
$_SERVER [ 'REQUEST_URI' ] = 'nothing' ;
$this -> assertEquals ( " $url &foo=1 " , add_query_arg ( 'foo' , '1' , $url ) );
$this -> assertEquals ( " $url &foo=1 " , add_query_arg ( array ( 'foo' => '1' ), $url ) );
2017-12-01 00:09:33 +01:00
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url &foo=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'foo' => '2' ,
2018-08-17 03:50:26 +02:00
),
$url
2017-12-01 00:09:33 +01:00
)
);
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url &foo=1&bar=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'bar' => '2' ,
2018-08-17 03:50:26 +02:00
),
$url
2017-12-01 00:09:33 +01:00
)
);
2013-08-07 08:38:38 +02:00
$_SERVER [ 'REQUEST_URI' ] = $url ;
$this -> assertEquals ( " $url &foo=1 " , add_query_arg ( 'foo' , '1' ) );
$this -> assertEquals ( " $url &foo=1 " , add_query_arg ( array ( 'foo' => '1' ) ) );
2017-12-01 00:09:33 +01:00
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url &foo=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'foo' => '2' ,
)
)
);
$this -> assertEquals (
2018-08-17 03:50:26 +02:00
" $url &foo=1&bar=2 " ,
add_query_arg (
2017-12-01 00:09:33 +01:00
array (
'foo' => '1' ,
'bar' => '2' ,
)
)
);
2013-08-07 08:38:38 +02:00
}
$_SERVER [ 'REQUEST_URI' ] = $old_req_uri ;
}
2015-04-01 21:14:46 +02:00
/**
* @ ticket 31306
*/
function test_add_query_arg_numeric_keys () {
$url = add_query_arg ( array ( 'foo' => 'bar' ), '1=1' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( '1=1&foo=bar' , $url );
2015-04-01 21:14:46 +02:00
2017-12-01 00:09:33 +01:00
$url = add_query_arg (
array (
'foo' => 'bar' ,
'1' => '2' ,
2018-08-17 03:50:26 +02:00
),
'1=1'
2017-12-01 00:09:33 +01:00
);
$this -> assertEquals ( '1=2&foo=bar' , $url );
2015-04-01 21:14:46 +02:00
$url = add_query_arg ( array ( '1' => '2' ), 'foo=bar' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'foo=bar&1=2' , $url );
2015-04-01 21:14:46 +02:00
}
2013-08-07 08:38:38 +02:00
/**
* @ ticket 21594
*/
function test_get_allowed_mime_types () {
$mimes = get_allowed_mime_types ();
$this -> assertInternalType ( 'array' , $mimes );
$this -> assertNotEmpty ( $mimes );
add_filter ( 'upload_mimes' , '__return_empty_array' );
$mimes = get_allowed_mime_types ();
$this -> assertInternalType ( 'array' , $mimes );
$this -> assertEmpty ( $mimes );
remove_filter ( 'upload_mimes' , '__return_empty_array' );
$mimes = get_allowed_mime_types ();
$this -> assertInternalType ( 'array' , $mimes );
$this -> assertNotEmpty ( $mimes );
}
/**
* @ ticket 21594
*/
function test_wp_get_mime_types () {
$mimes = wp_get_mime_types ();
$this -> assertInternalType ( 'array' , $mimes );
$this -> assertNotEmpty ( $mimes );
add_filter ( 'mime_types' , '__return_empty_array' );
$mimes = wp_get_mime_types ();
$this -> assertInternalType ( 'array' , $mimes );
$this -> assertEmpty ( $mimes );
remove_filter ( 'mime_types' , '__return_empty_array' );
$mimes = wp_get_mime_types ();
$this -> assertInternalType ( 'array' , $mimes );
$this -> assertNotEmpty ( $mimes );
// upload_mimes shouldn't affect wp_get_mime_types()
add_filter ( 'upload_mimes' , '__return_empty_array' );
$mimes = wp_get_mime_types ();
$this -> assertInternalType ( 'array' , $mimes );
$this -> assertNotEmpty ( $mimes );
remove_filter ( 'upload_mimes' , '__return_empty_array' );
$mimes2 = wp_get_mime_types ();
$this -> assertInternalType ( 'array' , $mimes2 );
$this -> assertNotEmpty ( $mimes2 );
$this -> assertEquals ( $mimes2 , $mimes );
}
/**
* @ ticket 23688
*/
function test_canonical_charset () {
$orig_blog_charset = get_option ( 'blog_charset' );
update_option ( 'blog_charset' , 'utf8' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'UTF-8' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , 'utf-8' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'UTF-8' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , 'UTF8' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'UTF-8' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , 'UTF-8' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'UTF-8' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , 'ISO-8859-1' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'ISO-8859-1' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , 'ISO8859-1' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'ISO-8859-1' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , 'iso8859-1' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'ISO-8859-1' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , 'iso-8859-1' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'ISO-8859-1' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
// Arbitrary strings are passed through.
update_option ( 'blog_charset' , 'foobarbaz' );
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'foobarbaz' , get_option ( 'blog_charset' ) );
2013-08-07 08:38:38 +02:00
update_option ( 'blog_charset' , $orig_blog_charset );
}
2013-08-29 20:58:40 +02:00
2019-01-10 22:05:50 +01:00
/**
* @ ticket 43977
* @ dataProvider data_wp_parse_list
*/
function test_wp_parse_list ( $expected , $actual ) {
$this -> assertSame ( $expected , array_values ( wp_parse_list ( $actual ) ) );
}
function data_wp_parse_list () {
return array (
array ( array ( '1' , '2' , '3' , '4' ), '1,2,3,4' ),
array ( array ( 'apple' , 'banana' , 'carrot' , 'dog' ), 'apple,banana,carrot,dog' ),
array ( array ( '1' , '2' , 'apple' , 'banana' ), '1,2,apple,banana' ),
array ( array ( '1' , '2' , 'apple' , 'banana' ), '1, 2,apple,banana' ),
array ( array ( '1' , '2' , 'apple' , 'banana' ), '1,2,apple,,banana' ),
array ( array ( '1' , '2' , 'apple' , 'banana' ), ',1,2,apple,banana' ),
array ( array ( '1' , '2' , 'apple' , 'banana' ), '1,2,apple,banana,' ),
array ( array ( '1' , '2' , 'apple' , 'banana' ), '1,2 ,apple,banana' ),
array ( array (), '' ),
array ( array (), ',' ),
array ( array (), ',,' ),
);
}
2013-08-29 20:58:40 +02:00
/**
* @ dataProvider data_wp_parse_id_list
*/
function test_wp_parse_id_list ( $expected , $actual ) {
$this -> assertSame ( $expected , array_values ( wp_parse_id_list ( $actual ) ) );
}
function data_wp_parse_id_list () {
return array (
array ( array ( 1 , 2 , 3 , 4 ), '1,2,3,4' ),
array ( array ( 1 , 2 , 3 , 4 ), '1, 2,,3,4' ),
array ( array ( 1 , 2 , 3 , 4 ), '1,2,2,3,4' ),
array ( array ( 1 , 2 , 3 , 4 ), array ( '1' , '2' , '3' , '4' , '3' ) ),
array ( array ( 1 , 2 , 3 , 4 ), array ( 1 , '2' , 3 , '4' ) ),
array ( array ( 1 , 2 , 3 , 4 ), '-1,2,-3,4' ),
array ( array ( 1 , 2 , 3 , 4 ), array ( - 1 , 2 , '-3' , '4' ) ),
);
}
2013-09-08 21:41:14 +02:00
2017-02-05 21:34:12 +01:00
/**
* @ dataProvider data_wp_parse_slug_list
*/
function test_wp_parse_slug_list ( $expected , $actual ) {
$this -> assertSame ( $expected , array_values ( wp_parse_slug_list ( $actual ) ) );
}
function data_wp_parse_slug_list () {
return array (
array ( array ( 'apple' , 'banana' , 'carrot' , 'dog' ), 'apple,banana,carrot,dog' ),
array ( array ( 'apple' , 'banana' , 'carrot' , 'dog' ), 'apple, banana,,carrot,dog' ),
array ( array ( 'apple' , 'banana' , 'carrot' , 'dog' ), 'apple banana carrot dog' ),
array ( array ( 'apple' , 'banana-carrot' , 'd-o-g' ), array ( 'apple ' , 'banana carrot' , 'd o g' ) ),
);
}
2016-03-03 03:00:06 +01:00
/**
* @ dataProvider data_device_can_upload
*/
function test_device_can_upload ( $user_agent , $expected ) {
$_SERVER [ 'HTTP_USER_AGENT' ] = $user_agent ;
2017-12-01 00:09:33 +01:00
$actual = _device_can_upload ();
2016-03-03 03:00:06 +01:00
unset ( $_SERVER [ 'HTTP_USER_AGENT' ] );
$this -> assertEquals ( $expected , $actual );
}
function data_device_can_upload () {
return array (
// iPhone iOS 5.0.1, Safari 5.1
array (
'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A406)' ,
false ,
),
// iPad iOS 3.2, Safari 4.0.4
array (
'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' ,
false ,
),
// iPod iOS 4.3.3, Safari 5.0.2
array (
'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' ,
false ,
),
// iPhone iOS 6.0.0, Safari 6.0
array (
'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' ,
true ,
),
// iPad iOS 6.0.0, Safari 6.0
array (
'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' ,
true ,
),
2016-07-08 16:36:37 +02:00
// Android 2.2, Android Webkit Browser
2016-03-03 03:00:06 +01:00
array (
'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' ,
true ,
),
2016-07-08 16:36:37 +02:00
// BlackBerry 9900, BlackBerry browser
2016-03-03 03:00:06 +01:00
array (
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+' ,
true ,
),
// Windows Phone 8.0, Internet Explorer 10.0;
array (
'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)' ,
true ,
),
// Ubuntu desktop, Firefox 41.0
array (
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0' ,
true ,
),
);
}
2013-09-10 05:17:51 +02:00
/**
* @ ticket 9064
*/
function test_wp_extract_urls () {
$original_urls = array (
'http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html' ,
'http://this.com' ,
2014-06-28 06:15:30 +02:00
'http://127.0.0.1' ,
2013-09-10 05:17:51 +02:00
'http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437' ,
'http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html' ,
'http://wordpress-core.com:8080/' ,
'http://www.website.com:5000' ,
'http://wordpress-core/?346236346326&2134362574863.437' ,
'http://افغانستا.icom.museum' ,
'http://الجزائر.icom.museum' ,
'http://österreich.icom.museum' ,
'http://বাংলাদেশ.icom.museum' ,
'http://беларусь.icom.museum' ,
'http://belgië.icom.museum' ,
'http://българия.icom.museum' ,
'http://تشادر.icom.museum' ,
'http://中国.icom.museum' ,
#'http://القمر.icom.museum', // Comoros http://القمر.icom.museum
#'http://κυπρος.icom.museum', Cyprus http://κυπρος.icom.museum
'http://českárepublika.icom.museum' ,
#'http://مصر.icom.museum', // Egypt http://مصر.icom.museum
'http://ελλάδα.icom.museum' ,
'http://magyarország.icom.museum' ,
'http://ísland.icom.museum' ,
'http://भारत.icom.museum' ,
'http://ايران.icom.museum' ,
'http://éire.icom.museum' ,
'http://איקו״ם.ישראל.museum' ,
'http://日本.icom.museum' ,
'http://الأردن.icom.museum' ,
'http://қазақстан.icom.museum' ,
'http://한국.icom.museum' ,
'http://кыргызстан.icom.museum' ,
'http://ລາວ.icom.museum' ,
'http://لبنان.icom.museum' ,
'http://македонија.icom.museum' ,
#'http://موريتانيا.icom.museum', // Mauritania http://موريتانيا.icom.museum
'http://méxico.icom.museum' ,
'http://монголулс.icom.museum' ,
#'http://المغرب.icom.museum', // Morocco http://المغرب.icom.museum
'http://नेपाल.icom.museum' ,
#'http://عمان.icom.museum', // Oman http://عمان.icom.museum
'http://قطر.icom.museum' ,
'http://românia.icom.museum' ,
'http://россия.иком.museum' ,
'http://србијаицрнагора.иком.museum' ,
'http://இலங்கை.icom.museum' ,
'http://españa.icom.museum' ,
'http://ไทย.icom.museum' ,
'http://تونس.icom.museum' ,
'http://türkiye.icom.museum' ,
'http://украина.icom.museum' ,
2014-06-28 06:15:30 +02:00
'http://việtnam.icom.museum' ,
2014-09-23 04:49:22 +02:00
'ftp://127.0.0.1/' ,
'http://www.woo.com/video?v=exvUH2qKLTU' ,
2017-12-01 00:09:33 +01:00
'http://taco.com?burrito=enchilada#guac' ,
2013-09-10 05:17:51 +02:00
);
2017-12-01 00:09:33 +01:00
$blob = '
2013-09-10 05:17:51 +02:00
http :// woo . com / 1 , 2 , 3 , 4 , 5 , 6 /- 1 - 2 - 3 - 4 -/ woo . html
http :// this . com
2014-06-28 06:15:30 +02:00
http :// 127.0 . 0.1
2013-09-10 05:17:51 +02:00
http :// www111 . urwyeoweytwutreyytqytwetowteuiiu . com / ? 346236346326 & amp ; 2134362574863.437
http :// wordpress - core / 1 , 2 , 3 , 4 , 5 , 6 /- 1 - 2 - 3 - 4 -/ woo . html
http :// wordpress - core . com : 8080 /
http :// www . website . com : 5000
http :// wordpress - core / ? 346236346326 & amp ; 2134362574863.437
http :// افغانستا . icom . museum
http :// الجزائر . icom . museum
http :// österreich . icom . museum
http :// বাংলাদেশ . icom . museum
http :// беларусь . icom . museum
http :// belgië . icom . museum
http :// българия . icom . museum
http :// تشادر . icom . museum
http :// 中国 . icom . museum
http :// českárepublika . icom . museum
http :// ελλάδα . icom . museum
http :// magyarország . icom . museum
http :// ísland . icom . museum
http :// भारत . icom . museum
http :// ايران . icom . museum
http :// éire . icom . museum
http :// איקו״ם . ישראל . museum
http :// 日本 . icom . museum
http :// الأردن . icom . museum
http :// қазақстан . icom . museum
http :// 한국 . icom . museum
http :// кыргызстан . icom . museum
http :// ລາວ . icom . museum
http :// لبنان . icom . museum
http :// македонија . icom . museum
http :// méxico . icom . museum
http :// монголулс . icom . museum
http :// नेपाल . icom . museum
http :// قطر . icom . museum
http :// românia . icom . museum
http :// россия . иком . museum
http :// србијаицрнагора . иком . museum
http :// இலங்கை . icom . museum
http :// españa . icom . museum
http :// ไทย . icom . museum
http :// تونس . icom . museum
http :// türkiye . icom . museum
http :// украина . icom . museum
http :// việtnam . icom . museum
2014-06-28 06:15:30 +02:00
ftp :// 127.0 . 0.1 /
2014-09-23 04:49:22 +02:00
http :// www . woo . com / video ? v = exvUH2qKLTU
http :// taco . com ? burrito = enchilada #guac
2017-12-01 00:09:33 +01:00
' ;
2013-09-10 05:17:51 +02:00
$urls = wp_extract_urls ( $blob );
$this -> assertNotEmpty ( $urls );
$this -> assertInternalType ( 'array' , $urls );
$this -> assertCount ( count ( $original_urls ), $urls );
$this -> assertEquals ( $original_urls , $urls );
$exploded = array_values ( array_filter ( array_map ( 'trim' , explode ( " \n " , $blob ) ) ) );
// wp_extract_urls calls html_entity_decode
$decoded = array_map ( 'html_entity_decode' , $exploded );
$this -> assertEquals ( $decoded , $urls );
$this -> assertEquals ( $original_urls , $decoded );
2017-12-01 00:09:33 +01:00
$blob = ' Lorem ipsum dolor sit amet , consectetur adipisicing elit , sed do eiusmod tempor
2013-09-10 05:17:51 +02:00
incididunt ut labore http :// woo . com / 1 , 2 , 3 , 4 , 5 , 6 /- 1 - 2 - 3 - 4 -/ woo . html et dolore magna aliqua .
2014-06-28 06:15:30 +02:00
Ut http :// this . com enim ad minim veniam , quis nostrud exercitation 16.06 . to 18.06 . 2014 ullamco http :// 127.0 . 0.1
2013-09-10 05:17:51 +02:00
laboris nisi ut aliquip ex http :// www111 . urwyeoweytwutreyytqytwetowteuiiu . com / ? 346236346326 & amp ; 2134362574863.437 ea
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
2017-12-01 00:09:33 +01:00
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 ,
sunt in culpa qui officia deserunt mollit http :// wordpress - core / ? 346236346326 & amp ; 2134362574863.437 anim id est laborum . ' ;
2013-09-10 05:17:51 +02:00
$urls = wp_extract_urls ( $blob );
$this -> assertNotEmpty ( $urls );
$this -> assertInternalType ( 'array' , $urls );
2014-06-28 06:15:30 +02:00
$this -> assertCount ( 8 , $urls );
$this -> assertEquals ( array_slice ( $original_urls , 0 , 8 ), $urls );
2013-09-10 05:17:51 +02:00
$blob = ' Lorem ipsum dolor sit amet , consectetur adipisicing elit , sed do eiusmod tempor
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 .
2014-06-28 06:15:30 +02:00
Ut < a href = " http://this.com " >& amp ; 3640 i6p1yi499 </ a > enim ad minim veniam , quis nostrud exercitation 16.06 . to 18.06 . 2014 ullamco < a href = " http://127.0.0.1 " > localhost </ a >
2013-09-10 05:17:51 +02:00
laboris nisi ut aliquip ex < a href = " http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437 " > 343462 ^</ a > ea
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
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,
sunt in culpa qui officia deserunt mollit < a href = " http://wordpress-core/?346236346326&2134362574863.437 " > )( *&^% $ </ a > anim id est laborum . ' ;
$urls = wp_extract_urls ( $blob );
$this -> assertNotEmpty ( $urls );
$this -> assertInternalType ( 'array' , $urls );
2014-06-28 06:15:30 +02:00
$this -> assertCount ( 8 , $urls );
$this -> assertEquals ( array_slice ( $original_urls , 0 , 8 ), $urls );
2013-09-10 05:17:51 +02:00
}
2014-10-28 19:34:16 +01:00
/**
* @ ticket 28786
*/
function test_wp_json_encode () {
$this -> assertEquals ( wp_json_encode ( 'a' ), '"a"' );
2014-11-24 00:51:13 +01:00
}
/**
* @ ticket 28786
*/
function test_wp_json_encode_utf8 () {
2014-10-28 19:34:16 +01:00
$this -> assertEquals ( wp_json_encode ( '这' ), '"\u8fd9"' );
2014-11-24 00:51:13 +01:00
}
/**
* @ ticket 28786
*/
function test_wp_json_encode_non_utf8 () {
2015-10-02 10:54:52 +02:00
if ( ! function_exists ( 'mb_detect_order' ) ) {
$this -> markTestSkipped ( 'mbstring extension not available.' );
}
2015-10-13 05:41:11 +02:00
2019-07-02 06:43:01 +02:00
$charsets = mb_detect_order ();
$old_charsets = $charsets ;
2019-07-08 02:55:20 +02:00
if ( ! in_array ( 'EUC-JP' , $charsets , true ) ) {
2014-11-24 02:55:42 +01:00
$charsets [] = 'EUC-JP' ;
mb_detect_order ( $charsets );
}
$eucjp = mb_convert_encoding ( 'aあb' , 'EUC-JP' , 'UTF-8' );
2017-12-01 00:09:33 +01:00
$utf8 = mb_convert_encoding ( $eucjp , 'UTF-8' , 'EUC-JP' );
2014-11-24 02:55:42 +01:00
$this -> assertEquals ( 'aあb' , $utf8 );
2014-10-28 19:34:16 +01:00
2014-11-25 06:00:36 +01:00
$this -> assertEquals ( '"a\u3042b"' , wp_json_encode ( $eucjp ) );
2014-11-24 02:55:42 +01:00
mb_detect_order ( $old_charsets );
}
/**
* @ ticket 28786
*/
function test_wp_json_encode_non_utf8_in_array () {
2015-10-02 10:54:52 +02:00
if ( ! function_exists ( 'mb_detect_order' ) ) {
$this -> markTestSkipped ( 'mbstring extension not available.' );
}
2019-07-02 06:43:01 +02:00
$charsets = mb_detect_order ();
$old_charsets = $charsets ;
2019-07-08 02:55:20 +02:00
if ( ! in_array ( 'EUC-JP' , $charsets , true ) ) {
2014-10-28 19:34:16 +01:00
$charsets [] = 'EUC-JP' ;
mb_detect_order ( $charsets );
}
$eucjp = mb_convert_encoding ( 'aあb' , 'EUC-JP' , 'UTF-8' );
2017-12-01 00:09:33 +01:00
$utf8 = mb_convert_encoding ( $eucjp , 'UTF-8' , 'EUC-JP' );
2014-10-28 19:34:16 +01:00
$this -> assertEquals ( 'aあb' , $utf8 );
2014-11-25 06:00:36 +01:00
$this -> assertEquals ( '["c","a\u3042b"]' , wp_json_encode ( array ( 'c' , $eucjp ) ) );
2014-10-28 19:34:16 +01:00
2014-11-24 00:51:13 +01:00
mb_detect_order ( $old_charsets );
}
/**
* @ ticket 28786
*/
function test_wp_json_encode_array () {
2014-10-28 19:34:16 +01:00
$this -> assertEquals ( wp_json_encode ( array ( 'a' ) ), '["a"]' );
2014-11-24 00:51:13 +01:00
}
2014-10-28 19:34:16 +01:00
2014-11-24 00:51:13 +01:00
/**
* @ ticket 28786
*/
function test_wp_json_encode_object () {
2017-12-01 00:09:33 +01:00
$object = new stdClass ;
2014-10-28 19:34:16 +01:00
$object -> a = 'b' ;
$this -> assertEquals ( wp_json_encode ( $object ), '{"a":"b"}' );
}
/**
* @ ticket 28786
*/
function test_wp_json_encode_depth () {
$data = array ( array ( array ( 1 , 2 , 3 ) ) );
$json = wp_json_encode ( $data , 0 , 1 );
$this -> assertFalse ( $json );
$data = array ( 'あ' , array ( array ( 1 , 2 , 3 ) ) );
$json = wp_json_encode ( $data , 0 , 1 );
$this -> assertFalse ( $json );
}
2015-09-24 05:33:21 +02:00
2016-03-03 14:16:31 +01:00
/**
* @ ticket 36054
* @ dataProvider datetime_provider
*/
function test_mysql_to_rfc3339 ( $expected , $actual ) {
$date_return = mysql_to_rfc3339 ( $actual );
$this -> assertTrue ( is_string ( $date_return ), 'The date return must be a string' );
$this -> assertNotEmpty ( $date_return , 'The date return could not be an empty string' );
$this -> assertEquals ( $expected , $date_return , 'The date does not match' );
$this -> assertEquals ( new DateTime ( $expected ), new DateTime ( $date_return ), 'The date is not the same after the call method' );
}
function datetime_provider () {
return array (
array ( '2016-03-15T18:54:46' , '15-03-2016 18:54:46' ),
array ( '2016-03-02T19:13:25' , '2016-03-02 19:13:25' ),
array ( '2016-03-02T19:13:00' , '2016-03-02 19:13' ),
array ( '2016-03-02T19:13:00' , '16-03-02 19:13' ),
2017-12-01 00:09:33 +01:00
array ( '2016-03-02T19:13:00' , '16-03-02 19:13' ),
2016-03-03 14:16:31 +01:00
);
}
2016-04-13 05:11:25 +02:00
/**
* @ ticket 35987
*/
public function test_wp_get_ext_types () {
$extensions = wp_get_ext_types ();
$this -> assertInternalType ( 'array' , $extensions );
$this -> assertNotEmpty ( $extensions );
add_filter ( 'ext2type' , '__return_empty_array' );
$extensions = wp_get_ext_types ();
$this -> assertSame ( array (), $extensions );
remove_filter ( 'ext2type' , '__return_empty_array' );
$extensions = wp_get_ext_types ();
$this -> assertInternalType ( 'array' , $extensions );
$this -> assertNotEmpty ( $extensions );
}
/**
* @ ticket 35987
*/
public function test_wp_ext2type () {
$extensions = wp_get_ext_types ();
2019-07-08 02:55:20 +02:00
foreach ( $extensions as $type => $extension_list ) {
foreach ( $extension_list as $extension ) {
2016-04-13 05:11:25 +02:00
$this -> assertEquals ( $type , wp_ext2type ( $extension ) );
$this -> assertEquals ( $type , wp_ext2type ( strtoupper ( $extension ) ) );
}
}
$this -> assertNull ( wp_ext2type ( 'unknown_format' ) );
}
2016-07-08 16:36:37 +02:00
/**
* Tests raising the memory limit .
*
* Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the
* test suite is - 1 , we can not test the memory limit negotiations .
*
* @ ticket 32075
*/
function test_wp_raise_memory_limit () {
2016-07-08 20:16:07 +02:00
if ( - 1 !== WP_MAX_MEMORY_LIMIT ) {
$this -> markTestSkipped ( 'WP_MAX_MEMORY_LIMIT should be set to -1' );
}
$ini_limit_before = ini_get ( 'memory_limit' );
2017-12-01 00:09:33 +01:00
$raised_limit = wp_raise_memory_limit ();
$ini_limit_after = ini_get ( 'memory_limit' );
2016-07-08 20:16:07 +02:00
$this -> assertSame ( $ini_limit_before , $ini_limit_after );
$this -> assertSame ( false , $raised_limit );
$this -> assertEquals ( WP_MAX_MEMORY_LIMIT , $ini_limit_after );
2016-07-08 16:36:37 +02:00
}
Customize: Implement customized state persistence with changesets.
Includes infrastructure developed in the Customize Snapshots feature plugin.
See https://make.wordpress.org/core/2016/10/12/customize-changesets-technical-design-decisions/
Props westonruter, valendesigns, utkarshpatel, stubgo, lgedeon, ocean90, ryankienstra, mihai2u, dlh, aaroncampbell, jonathanbardo, jorbin.
See #28721.
See #31089.
Fixes #30937.
Fixes #31517.
Fixes #30028.
Fixes #23225.
Fixes #34142.
Fixes #36485.
git-svn-id: https://develop.svn.wordpress.org/trunk@38810 602fd350-edb4-49c9-b593-d223f7449a82
2016-10-18 22:04:36 +02:00
/**
* Tests wp_generate_uuid4 () .
*
2017-01-17 03:43:25 +01:00
* @ covers :: wp_generate_uuid4
Customize: Implement customized state persistence with changesets.
Includes infrastructure developed in the Customize Snapshots feature plugin.
See https://make.wordpress.org/core/2016/10/12/customize-changesets-technical-design-decisions/
Props westonruter, valendesigns, utkarshpatel, stubgo, lgedeon, ocean90, ryankienstra, mihai2u, dlh, aaroncampbell, jonathanbardo, jorbin.
See #28721.
See #31089.
Fixes #30937.
Fixes #31517.
Fixes #30028.
Fixes #23225.
Fixes #34142.
Fixes #36485.
git-svn-id: https://develop.svn.wordpress.org/trunk@38810 602fd350-edb4-49c9-b593-d223f7449a82
2016-10-18 22:04:36 +02:00
* @ ticket 38164
*/
function test_wp_generate_uuid4 () {
$uuids = array ();
for ( $i = 0 ; $i < 20 ; $i += 1 ) {
$uuid = wp_generate_uuid4 ();
2017-09-19 01:03:06 +02:00
$this -> assertTrue ( wp_is_uuid ( $uuid , 4 ) );
Customize: Implement customized state persistence with changesets.
Includes infrastructure developed in the Customize Snapshots feature plugin.
See https://make.wordpress.org/core/2016/10/12/customize-changesets-technical-design-decisions/
Props westonruter, valendesigns, utkarshpatel, stubgo, lgedeon, ocean90, ryankienstra, mihai2u, dlh, aaroncampbell, jonathanbardo, jorbin.
See #28721.
See #31089.
Fixes #30937.
Fixes #31517.
Fixes #30028.
Fixes #23225.
Fixes #34142.
Fixes #36485.
git-svn-id: https://develop.svn.wordpress.org/trunk@38810 602fd350-edb4-49c9-b593-d223f7449a82
2016-10-18 22:04:36 +02:00
$uuids [] = $uuid ;
}
$unique_uuids = array_unique ( $uuids );
$this -> assertEquals ( $uuids , $unique_uuids );
}
2017-02-25 17:07:25 +01:00
2017-09-19 01:03:06 +02:00
/**
* Tests wp_is_uuid () .
*
* @ covers :: wp_is_uuid
* @ ticket 39778
*/
function test_wp_is_valid_uuid () {
$uuids_v4 = array (
'27fe2421-780c-44c5-b39b-fff753092b55' ,
'b7c7713a-4ee9-45a1-87ed-944a90390fc7' ,
'fbedbe35-7bf5-49cc-a5ac-0343bd94360a' ,
'4c58e67e-123b-4290-a41c-5eeb6970fa3e' ,
'f54f5b78-e414-4637-84a9-a6cdc94a1beb' ,
'd1c533ac-abcf-44b6-9b0e-6477d2c91b09' ,
'7fcd683f-e5fd-454a-a8b9-ed15068830da' ,
'7962c750-e58c-470a-af0d-ec1eae453ff2' ,
'a59878ce-9a67-4493-8ca0-a756b52804b3' ,
'6faa519d-1e13-4415-bd6f-905ae3689d1d' ,
);
foreach ( $uuids_v4 as $uuid ) {
$this -> assertTrue ( wp_is_uuid ( $uuid , 4 ) );
}
$uuids = array (
'00000000-0000-0000-0000-000000000000' , // Nil.
'9e3a0460-d72d-11e4-a631-c8e0eb141dab' , // Version 1.
'2c1d43b8-e6d7-376e-af7f-d4bde997cc3f' , // Version 3.
'39888f87-fb62-5988-a425-b2ea63f5b81e' , // Version 5.
);
foreach ( $uuids as $uuid ) {
$this -> assertTrue ( wp_is_uuid ( $uuid ) );
$this -> assertFalse ( wp_is_uuid ( $uuid , 4 ) );
}
$invalid_uuids = array (
'a19d5192-ea41-41e6-b006' ,
'this-is-not-valid' ,
1234 ,
true ,
array (),
);
foreach ( $invalid_uuids as $invalid_uuid ) {
$this -> assertFalse ( wp_is_uuid ( $invalid_uuid , 4 ) );
$this -> assertFalse ( wp_is_uuid ( $invalid_uuid ) );
}
}
2018-09-24 22:56:30 +02:00
/**
* Tests wp_unique_id () .
*
* @ covers :: wp_unique_id
* @ ticket 44883
*/
function test_wp_unique_id () {
// Test without prefix.
$ids = array ();
for ( $i = 0 ; $i < 20 ; $i += 1 ) {
$id = wp_unique_id ();
$this -> assertInternalType ( 'string' , $id );
$this -> assertTrue ( is_numeric ( $id ) );
$ids [] = $id ;
}
$this -> assertEquals ( $ids , array_unique ( $ids ) );
// Test with prefix.
$ids = array ();
for ( $i = 0 ; $i < 20 ; $i += 1 ) {
$id = wp_unique_id ( 'foo-' );
$this -> assertRegExp ( '/^foo-\d+$/' , $id );
$ids [] = $id ;
}
$this -> assertEquals ( $ids , array_unique ( $ids ) );
}
2017-04-10 00:43:04 +02:00
/**
* @ ticket 40017
* @ dataProvider _wp_get_image_mime
*/
public function test_wp_get_image_mime ( $file , $expected ) {
if ( ! is_callable ( 'exif_imagetype' ) && ! function_exists ( 'getimagesize' ) ) {
$this -> markTestSkipped ( 'The exif PHP extension is not loaded.' );
}
$this -> assertEquals ( $expected , wp_get_image_mime ( $file ) );
}
2017-02-25 17:07:25 +01:00
/**
* @ ticket 39550
* @ dataProvider _wp_check_filetype_and_ext_data
*/
function test_wp_check_filetype_and_ext ( $file , $filename , $expected ) {
if ( ! extension_loaded ( 'fileinfo' ) ) {
$this -> markTestSkipped ( 'The fileinfo PHP extension is not loaded.' );
}
$this -> assertEquals ( $expected , wp_check_filetype_and_ext ( $file , $filename ) );
}
/**
* @ ticket 39550
2017-04-22 21:21:11 +02:00
* @ group ms - excluded
2017-02-25 17:07:25 +01:00
*/
function test_wp_check_filetype_and_ext_with_filtered_svg () {
2017-02-26 17:05:25 +01:00
if ( ! extension_loaded ( 'fileinfo' ) ) {
$this -> markTestSkipped ( 'The fileinfo PHP extension is not loaded.' );
}
2017-12-01 00:09:33 +01:00
$file = DIR_TESTDATA . '/uploads/video-play.svg' ;
2017-02-25 17:07:25 +01:00
$filename = 'video-play.svg' ;
$expected = array (
2017-12-01 00:09:33 +01:00
'ext' => 'svg' ,
'type' => 'image/svg+xml' ,
2017-02-25 17:07:25 +01:00
'proper_filename' => false ,
);
add_filter ( 'upload_mimes' , array ( $this , '_filter_mime_types_svg' ) );
$this -> assertEquals ( $expected , wp_check_filetype_and_ext ( $file , $filename ) );
// Cleanup.
remove_filter ( 'upload_mimes' , array ( $this , '_test_add_mime_types_svg' ) );
}
/**
* @ ticket 39550
2017-04-23 23:57:31 +02:00
* @ group ms - excluded
2017-02-25 17:07:25 +01:00
*/
function test_wp_check_filetype_and_ext_with_filtered_woff () {
2017-02-26 17:05:25 +01:00
if ( ! extension_loaded ( 'fileinfo' ) ) {
$this -> markTestSkipped ( 'The fileinfo PHP extension is not loaded.' );
}
2017-12-01 00:09:33 +01:00
$file = DIR_TESTDATA . '/uploads/dashicons.woff' ;
2017-02-25 17:07:25 +01:00
$filename = 'dashicons.woff' ;
$expected = array (
2017-12-01 00:09:33 +01:00
'ext' => 'woff' ,
'type' => 'application/font-woff' ,
2017-02-25 17:07:25 +01:00
'proper_filename' => false ,
);
add_filter ( 'upload_mimes' , array ( $this , '_filter_mime_types_woff' ) );
$this -> assertEquals ( $expected , wp_check_filetype_and_ext ( $file , $filename ) );
// Cleanup.
remove_filter ( 'upload_mimes' , array ( $this , '_test_add_mime_types_woff' ) );
}
public function _filter_mime_types_svg ( $mimes ) {
$mimes [ 'svg' ] = 'image/svg+xml' ;
return $mimes ;
}
public function _filter_mime_types_woff ( $mimes ) {
$mimes [ 'woff' ] = 'application/font-woff' ;
return $mimes ;
}
2017-04-10 00:43:04 +02:00
/**
2019-01-07 21:47:56 +01:00
* Data provider for test_wp_get_image_mime ();
2017-04-10 00:43:04 +02:00
*/
public function _wp_get_image_mime () {
$data = array (
// Standard JPEG.
array (
DIR_TESTDATA . '/images/test-image.jpg' ,
'image/jpeg' ,
),
// Standard GIF.
array (
DIR_TESTDATA . '/images/test-image.gif' ,
'image/gif' ,
),
// Standard PNG.
array (
DIR_TESTDATA . '/images/test-image.png' ,
'image/png' ,
),
// Image with wrong extension.
array (
DIR_TESTDATA . '/images/test-image-mime-jpg.png' ,
'image/jpeg' ,
),
// Not an image.
array (
DIR_TESTDATA . '/uploads/dashicons.woff' ,
false ,
),
);
return $data ;
}
2017-02-25 17:07:25 +01:00
public function _wp_check_filetype_and_ext_data () {
2017-02-26 17:05:25 +01:00
$data = array (
2017-02-25 17:07:25 +01:00
// Standard image.
array (
DIR_TESTDATA . '/images/canola.jpg' ,
'canola.jpg' ,
array (
2017-12-01 00:09:33 +01:00
'ext' => 'jpg' ,
'type' => 'image/jpeg' ,
2017-02-25 17:07:25 +01:00
'proper_filename' => false ,
),
),
// Image with wrong extension.
array (
DIR_TESTDATA . '/images/test-image-mime-jpg.png' ,
'test-image-mime-jpg.png' ,
array (
2017-12-01 00:09:33 +01:00
'ext' => 'jpg' ,
'type' => 'image/jpeg' ,
2017-02-25 17:07:25 +01:00
'proper_filename' => 'test-image-mime-jpg.jpg' ,
),
),
// Image without extension.
array (
DIR_TESTDATA . '/images/test-image-no-extension' ,
'test-image-no-extension' ,
array (
2017-12-01 00:09:33 +01:00
'ext' => false ,
'type' => false ,
2017-02-25 17:07:25 +01:00
'proper_filename' => false ,
),
),
// Valid non-image file with an image extension.
array (
DIR_TESTDATA . '/formatting/big5.txt' ,
'big5.jpg' ,
array (
2018-12-18 17:34:17 +01:00
'ext' => false ,
'type' => false ,
2017-02-25 17:07:25 +01:00
'proper_filename' => false ,
),
),
// Non-image file not allowed.
array (
DIR_TESTDATA . '/export/crazy-cdata.xml' ,
'crazy-cdata.xml' ,
array (
2017-12-01 00:09:33 +01:00
'ext' => false ,
'type' => false ,
2017-02-25 17:07:25 +01:00
'proper_filename' => false ,
),
),
2018-12-18 17:34:17 +01:00
// Non-image file not allowed even if it's named like one.
array (
DIR_TESTDATA . '/export/crazy-cdata.xml' ,
'crazy-cdata.jpg' ,
array (
'ext' => false ,
'type' => false ,
'proper_filename' => false ,
),
),
// Non-image file not allowed if it's named like something else.
array (
DIR_TESTDATA . '/export/crazy-cdata.xml' ,
'crazy-cdata.doc' ,
array (
'ext' => false ,
'type' => false ,
'proper_filename' => false ,
),
),
2019-01-07 21:47:56 +01:00
// Non-image file not allowed even if it's named like one.
array (
DIR_TESTDATA . '/export/crazy-cdata.xml' ,
'crazy-cdata.jpg' ,
array (
2019-01-07 22:08:03 +01:00
'ext' => false ,
'type' => false ,
2019-01-07 21:47:56 +01:00
'proper_filename' => false ,
),
),
// Non-image file not allowed if it's named like something else.
array (
DIR_TESTDATA . '/export/crazy-cdata.xml' ,
'crazy-cdata.doc' ,
array (
2019-01-07 22:08:03 +01:00
'ext' => false ,
'type' => false ,
2019-01-07 21:47:56 +01:00
'proper_filename' => false ,
),
),
2017-02-25 17:07:25 +01:00
);
2017-02-26 17:05:25 +01:00
// Test a few additional file types on single sites.
if ( ! is_multisite () ) {
2017-12-01 00:09:33 +01:00
$data = array_merge (
2018-08-17 03:50:26 +02:00
$data ,
array (
2017-12-01 00:09:33 +01:00
// Standard non-image file.
2019-07-05 07:20:52 +02:00
array (
DIR_TESTDATA . '/formatting/big5.txt' ,
'big5.txt' ,
array (
'ext' => 'txt' ,
'type' => 'text/plain' ,
'proper_filename' => false ,
),
),
2017-12-01 00:09:33 +01:00
// Non-image file with wrong sub-type.
2017-02-26 17:05:25 +01:00
array (
2017-12-01 00:09:33 +01:00
DIR_TESTDATA . '/uploads/pages-to-word.docx' ,
'pages-to-word.docx' ,
array (
'ext' => 'docx' ,
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ,
'proper_filename' => false ,
2018-01-15 23:16:33 +01:00
),
),
// FLAC file.
array (
DIR_TESTDATA . '/uploads/small-audio.flac' ,
'small-audio.flac' ,
array (
'ext' => 'flac' ,
'type' => 'audio/flac' ,
'proper_filename' => false ,
2017-12-01 00:09:33 +01:00
),
2017-02-26 17:05:25 +01:00
),
2019-01-07 22:41:40 +01:00
// Assorted text/* sample files
array (
DIR_TESTDATA . '/uploads/test.vtt' ,
'test.vtt' ,
array (
'ext' => 'vtt' ,
'type' => 'text/vtt' ,
'proper_filename' => false ,
),
),
array (
DIR_TESTDATA . '/uploads/test.csv' ,
'test.csv' ,
array (
'ext' => 'csv' ,
'type' => 'text/csv' ,
'proper_filename' => false ,
),
),
// RTF files.
array (
DIR_TESTDATA . '/uploads/test.rtf' ,
'test.rtf' ,
array (
'ext' => 'rtf' ,
'type' => 'application/rtf' ,
'proper_filename' => false ,
),
),
2017-12-01 00:09:33 +01:00
)
);
2017-02-26 17:05:25 +01:00
}
return $data ;
2017-02-25 17:07:25 +01:00
}
2017-09-27 15:03:03 +02:00
2017-10-25 01:14:33 +02:00
/**
* Test file path validation
*
* @ ticket 42016
* @ dataProvider data_test_validate_file ()
*
* @ param string $file File path .
* @ param array $allowed_files List of allowed files .
* @ param int $expected Expected result .
*/
public function test_validate_file ( $file , $allowed_files , $expected ) {
$this -> assertSame ( $expected , validate_file ( $file , $allowed_files ) );
}
/**
* Data provider for file validation .
*
* @ return array {
* @ type array $ 0. .. {
* @ type string $ 0 File path .
* @ type array $ 1 List of allowed files .
* @ type int $ 2 Expected result .
* }
* }
*/
public function data_test_validate_file () {
return array (
// Allowed files:
array (
null ,
array (),
0 ,
),
array (
'' ,
array (),
0 ,
),
array (
' ' ,
array (),
0 ,
),
array (
'.' ,
array (),
0 ,
),
array (
'..' ,
array (),
0 ,
),
array (
'./' ,
array (),
0 ,
),
array (
'foo.ext' ,
array ( 'foo.ext' ),
0 ,
),
array (
'dir/foo.ext' ,
array (),
0 ,
),
array (
'foo..ext' ,
array (),
0 ,
),
array (
'dir/dir/../' ,
array (),
0 ,
),
// Directory traversal:
array (
'../' ,
array (),
1 ,
),
array (
'../../' ,
array (),
1 ,
),
array (
'../file.ext' ,
array (),
1 ,
),
array (
'../dir/../' ,
array (),
1 ,
),
array (
'/dir/dir/../../' ,
array (),
1 ,
),
array (
'/dir/dir/../../' ,
array ( '/dir/dir/../../' ),
1 ,
),
// Windows drives:
array (
'c:' ,
array (),
2 ,
),
array (
'C:/WINDOWS/system32' ,
array ( 'C:/WINDOWS/system32' ),
2 ,
),
// Disallowed files:
array (
'foo.ext' ,
array ( 'bar.ext' ),
3 ,
),
array (
'foo.ext' ,
array ( '.ext' ),
3 ,
),
array (
'path/foo.ext' ,
array ( 'foo.ext' ),
3 ,
),
);
}
2018-07-18 03:01:28 +02:00
/**
* Test stream URL validation .
*
* @ dataProvider data_test_wp_is_stream
*
* @ param string $path The resource path or URL .
* @ param bool $expected Expected result .
*/
public function test_wp_is_stream ( $path , $expected ) {
2018-07-18 11:51:53 +02:00
if ( ! extension_loaded ( 'openssl' ) && false !== strpos ( $path , 'https://' ) ) {
$this -> markTestSkipped ( 'The openssl PHP extension is not loaded.' );
}
2018-07-18 03:01:28 +02:00
$this -> assertSame ( $expected , wp_is_stream ( $path ) );
}
/**
* Data provider for stream URL validation .
*
* @ return array {
* @ type array $ 0. .. {
* @ type string $ 0 The resource path or URL .
* @ type bool $ 1 Expected result .
* }
* }
*/
public function data_test_wp_is_stream () {
return array (
// Legitimate stream examples.
2018-07-18 11:51:53 +02:00
array ( 'http://example.com' , true ),
2018-07-18 03:01:28 +02:00
array ( 'https://example.com' , true ),
array ( 'ftp://example.com' , true ),
array ( 'file:///path/to/some/file' , true ),
array ( 'php://some/php/file.php' , true ),
// Non-stream examples.
array ( 'fakestream://foo/bar/baz' , false ),
array ( '../../some/relative/path' , false ),
array ( 'some/other/relative/path' , false ),
array ( '/leading/relative/path' , false ),
);
}
2018-09-08 06:19:40 +02:00
/**
2019-01-08 20:55:31 +01:00
* Test human_readable_duration () .
2018-09-08 06:19:40 +02:00
*
* @ ticket 39667
2019-01-08 20:55:31 +01:00
* @ dataProvider data_test_human_readable_duration
2018-09-08 06:19:40 +02:00
*
2019-01-08 20:55:31 +01:00
* @ param string $input Duration .
* @ param string $expected Expected human readable duration .
2018-09-08 06:19:40 +02:00
*/
2019-01-08 20:55:31 +01:00
public function test_human_readable_duration ( $input , $expected ) {
2018-09-08 06:19:40 +02:00
$this -> assertSame ( $expected , human_readable_duration ( $input ) );
}
2019-01-08 20:55:31 +01:00
/**
* Dataprovider for test_duration_format () .
*
* @ return array {
* @ type array {
* @ type string $input Duration .
* @ type string $expect Expected human readable duration .
* }
* }
*/
public function data_test_human_readable_duration () {
2018-09-08 06:19:40 +02:00
return array (
2019-01-08 20:55:31 +01:00
// Valid ii:ss cases.
array ( '0:0' , '0 minutes, 0 seconds' ),
array ( '00:00' , '0 minutes, 0 seconds' ),
array ( '0:5' , '0 minutes, 5 seconds' ),
array ( '0:05' , '0 minutes, 5 seconds' ),
array ( '01:01' , '1 minute, 1 second' ),
2018-09-08 06:19:40 +02:00
array ( '30:00' , '30 minutes, 0 seconds' ),
2019-01-08 20:55:31 +01:00
array ( ' 30:00 ' , '30 minutes, 0 seconds' ),
// Valid HH:ii:ss cases.
array ( '0:0:0' , '0 hours, 0 minutes, 0 seconds' ),
array ( '00:00:00' , '0 hours, 0 minutes, 0 seconds' ),
array ( '00:30:34' , '0 hours, 30 minutes, 34 seconds' ),
array ( '01:01:01' , '1 hour, 1 minute, 1 second' ),
array ( '1:02:00' , '1 hour, 2 minutes, 0 seconds' ),
array ( '10:30:34' , '10 hours, 30 minutes, 34 seconds' ),
array ( '1234567890:59:59' , '1234567890 hours, 59 minutes, 59 seconds' ),
// Valid ii:ss cases with negative sign.
array ( '-00:00' , '0 minutes, 0 seconds' ),
array ( '-3:00' , '3 minutes, 0 seconds' ),
array ( '-03:00' , '3 minutes, 0 seconds' ),
array ( '-30:00' , '30 minutes, 0 seconds' ),
// Valid HH:ii:ss cases with negative sign.
array ( '-00:00:00' , '0 hours, 0 minutes, 0 seconds' ),
array ( '-1:02:00' , '1 hour, 2 minutes, 0 seconds' ),
// Invalid cases.
array ( null , false ),
array ( '' , false ),
array ( ':' , false ),
array ( '::' , false ),
array ( array (), false ),
2018-09-08 06:19:40 +02:00
array ( 'Batman Begins !' , false ),
array ( '' , false ),
array ( '-1' , false ),
array ( - 1 , false ),
array ( 0 , false ),
array ( 1 , false ),
array ( '00' , false ),
2019-01-08 20:55:31 +01:00
array ( '30:-10' , false ),
array ( ':30:00' , false ), // Missing HH.
array ( 'MM:30:00' , false ), // Invalid HH.
array ( '30:MM:00' , false ), // Invalid ii.
array ( '30:30:MM' , false ), // Invalid ss.
array ( '30:MM' , false ), // Invalid ss.
array ( 'MM:00' , false ), // Invalid ii.
array ( 'MM:MM' , false ), // Invalid ii and ss.
array ( '10 :30' , false ), // Containing a space.
array ( '59:61' , false ), // Out of bound.
array ( '61:59' , false ), // Out of bound.
array ( '3:59:61' , false ), // Out of bound.
array ( '03:61:59' , false ), // Out of bound.
2018-09-08 06:19:40 +02:00
);
}
2013-08-07 08:38:38 +02:00
}