From caf45ed0e97981b1a5dc93b07a6af8927b1ff7af Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 23 Jun 2026 17:41:30 -0400 Subject: [PATCH 1/7] Add PHPUnit tests for `wp_timezone_string` --- .../tests/functions/wpTimezoneString.php | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpTimezoneString.php diff --git a/tests/phpunit/tests/functions/wpTimezoneString.php b/tests/phpunit/tests/functions/wpTimezoneString.php new file mode 100644 index 0000000000000..cfea6d9ffa5ff --- /dev/null +++ b/tests/phpunit/tests/functions/wpTimezoneString.php @@ -0,0 +1,125 @@ +assertSame( $tz_name, wp_timezone_string() ); + } + + /** + * Data provider to test numeric offset conversion. + * + * @return array + */ + public function data_should_convert_gmt_offset() { + return array( + array( -12, '-12:00' ), + array( -11.5, '-11:30' ), + array( -11, '-11:00' ), + array( -10.5, '-10:30' ), + array( -10, '-10:00' ), + array( -9.5, '-09:30' ), + array( -9, '-09:00' ), + array( -8.5, '-08:30' ), + array( -8, '-08:00' ), + array( -7.5, '-07:30' ), + array( -7, '-07:00' ), + array( -6.5, '-06:30' ), + array( -6, '-06:00' ), + array( -5.5, '-05:30' ), + array( -5, '-05:00' ), + array( -4.5, '-04:30' ), + array( -4, '-04:00' ), + array( -3.5, '-03:30' ), + array( -3, '-03:00' ), + array( -2.5, '-02:30' ), + array( -2, '-02:00' ), + array( '-1.5', '-01:30' ), + array( -1.5, '-01:30' ), + array( -1, '-01:00' ), + array( -0.5, '-00:30' ), + array( 0, '+00:00' ), + array( '0', '+00:00' ), + array( 0.5, '+00:30' ), + array( 1, '+01:00' ), + array( 1.5, '+01:30' ), + array( '1.5', '+01:30' ), + array( 2, '+02:00' ), + array( 2.5, '+02:30' ), + array( 3, '+03:00' ), + array( 3.5, '+03:30' ), + array( 4, '+04:00' ), + array( 4.5, '+04:30' ), + array( 5, '+05:00' ), + array( 5.5, '+05:30' ), + array( 5.75, '+05:45' ), + array( 6, '+06:00' ), + array( 6.5, '+06:30' ), + array( 7, '+07:00' ), + array( 7.5, '+07:30' ), + array( 8, '+08:00' ), + array( 8.5, '+08:30' ), + array( 8.75, '+08:45' ), + array( 9, '+09:00' ), + array( 9.5, '+09:30' ), + array( 10, '+10:00' ), + array( 10.5, '+10:30' ), + array( 11, '+11:00' ), + array( 11.5, '+11:30' ), + array( 12, '+12:00' ), + array( 12.75, '+12:45' ), + array( 13, '+13:00' ), + array( 13.75, '+13:45' ), + array( 14, '+14:00' ), + ); + } + + /** + * @ticket 24730 + */ + public function test_should_return_timezone_string() { + update_option( 'timezone_string', 'Europe/Helsinki' ); + + $this->assertSame( 'Europe/Helsinki', wp_timezone_string() ); + } + + /** + * Ensures that deprecated timezone strings are handled correctly. + * + * @ticket 56468 + */ + public function test_should_return_deprecated_timezone_string() { + $tz_string = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6. + update_option( 'timezone_string', $tz_string ); + + $this->assertSame( $tz_string, wp_timezone_string() ); + } +} From 4318bbd0b5a569a7146c9fccdbe910e931e435d9 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 24 Jun 2026 13:14:04 -0400 Subject: [PATCH 2/7] Remove PHPUnit tests for functions.php --- tests/phpunit/tests/file.php | 79 - tests/phpunit/tests/functions.php | 2301 ----------------- tests/phpunit/tests/functions/addQueryArg.php | 271 ++ tests/phpunit/tests/functions/boolFromYn.php | 17 + .../tests/functions/canonicalCharset.php | 8 +- .../{date => functions}/currentDatetime.php | 0 .../tests/{date => functions}/currentTime.php | 0 .../tests/{date => functions}/dateI18n.php | 0 .../tests/functions/deviceCanUpload.php | 68 + .../tests/functions/getAllowedMimeTypes.php | 28 + tests/phpunit/tests/functions/getFileData.php | 90 + .../tests/functions/humanReadableDuration.php | 90 + .../tests/{date => functions}/mysql2date.php | 0 .../tests/functions/mysqlToRfc3339.php | 31 + .../tests/functions/pathIsAbsolute.php | 47 + tests/phpunit/tests/functions/pathJoin.php | 73 + .../phpunit/tests/functions/validateFile.php | 161 ++ .../tests/functions/wpCheckFiletypeAndExt.php | 258 ++ .../tests/{date => functions}/wpDate.php | 0 tests/phpunit/tests/functions/wpExt2Type.php | 26 + .../phpunit/tests/functions/wpExtractUrls.php | 210 ++ .../wpGetDefaultExtensionForMimeType.php | 22 + .../phpunit/tests/functions/wpGetExtTypes.php | 27 + .../{http => functions}/wpGetHttpHeaders.php | 0 .../tests/functions/wpGetImageMime.php | 109 + .../tests/functions/wpGetImageSize.php | 234 ++ .../tests/functions/wpGetMimeTypes.php | 40 +- .../tests/functions/wpIsJsonMediaType.php | 31 + tests/phpunit/tests/functions/wpIsStream.php | 51 + tests/phpunit/tests/functions/wpIsUuid.php | 58 + .../phpunit/tests/functions/wpJsonEncode.php | 96 + .../tests/functions/wpJsonFileDecode.php | 33 + .../tests/functions/wpNormalizePath.php | 102 + tests/phpunit/tests/functions/wpParseArgs.php | 88 + .../tests/functions/wpRaiseMemoryLimit.php | 30 + .../tests/functions/wpRecursiveKsort.php | 95 + .../tests/{date => functions}/wpTimezone.php | 11 +- .../tests/functions/wpUniqueFilename.php | 193 ++ tests/phpunit/tests/functions/wpUniqueId.php | 36 + tests/phpunit/tests/functions/wpUploadDir.php | 38 + .../wpUploadDirInternal.php} | 22 +- 41 files changed, 2647 insertions(+), 2427 deletions(-) delete mode 100644 tests/phpunit/tests/functions.php create mode 100644 tests/phpunit/tests/functions/addQueryArg.php create mode 100644 tests/phpunit/tests/functions/boolFromYn.php rename tests/phpunit/tests/{date => functions}/currentDatetime.php (100%) rename tests/phpunit/tests/{date => functions}/currentTime.php (100%) rename tests/phpunit/tests/{date => functions}/dateI18n.php (100%) create mode 100644 tests/phpunit/tests/functions/deviceCanUpload.php create mode 100644 tests/phpunit/tests/functions/getAllowedMimeTypes.php create mode 100644 tests/phpunit/tests/functions/getFileData.php create mode 100644 tests/phpunit/tests/functions/humanReadableDuration.php rename tests/phpunit/tests/{date => functions}/mysql2date.php (100%) create mode 100644 tests/phpunit/tests/functions/mysqlToRfc3339.php create mode 100644 tests/phpunit/tests/functions/pathIsAbsolute.php create mode 100644 tests/phpunit/tests/functions/pathJoin.php create mode 100644 tests/phpunit/tests/functions/validateFile.php create mode 100644 tests/phpunit/tests/functions/wpCheckFiletypeAndExt.php rename tests/phpunit/tests/{date => functions}/wpDate.php (100%) create mode 100644 tests/phpunit/tests/functions/wpExt2Type.php create mode 100644 tests/phpunit/tests/functions/wpExtractUrls.php create mode 100644 tests/phpunit/tests/functions/wpGetDefaultExtensionForMimeType.php create mode 100644 tests/phpunit/tests/functions/wpGetExtTypes.php rename tests/phpunit/tests/{http => functions}/wpGetHttpHeaders.php (100%) create mode 100644 tests/phpunit/tests/functions/wpGetImageMime.php create mode 100644 tests/phpunit/tests/functions/wpGetImageSize.php create mode 100644 tests/phpunit/tests/functions/wpIsJsonMediaType.php create mode 100644 tests/phpunit/tests/functions/wpIsStream.php create mode 100644 tests/phpunit/tests/functions/wpIsUuid.php create mode 100644 tests/phpunit/tests/functions/wpJsonEncode.php create mode 100644 tests/phpunit/tests/functions/wpJsonFileDecode.php create mode 100644 tests/phpunit/tests/functions/wpNormalizePath.php create mode 100644 tests/phpunit/tests/functions/wpParseArgs.php create mode 100644 tests/phpunit/tests/functions/wpRaiseMemoryLimit.php create mode 100644 tests/phpunit/tests/functions/wpRecursiveKsort.php rename tests/phpunit/tests/{date => functions}/wpTimezone.php (90%) create mode 100644 tests/phpunit/tests/functions/wpUniqueFilename.php create mode 100644 tests/phpunit/tests/functions/wpUniqueId.php create mode 100644 tests/phpunit/tests/functions/wpUploadDir.php rename tests/phpunit/tests/{upload.php => functions/wpUploadDirInternal.php} (86%) diff --git a/tests/phpunit/tests/file.php b/tests/phpunit/tests/file.php index 9a29e4e67669a..0d7ad78a150e4 100644 --- a/tests/phpunit/tests/file.php +++ b/tests/phpunit/tests/file.php @@ -15,85 +15,6 @@ public function set_up() { $this->dir = untrailingslashit( get_temp_dir() ); } - /** - * @group plugins - * @group themes - */ - public function test_get_file_data() { - $theme_headers = array( - 'Name' => 'Theme Name', - 'ThemeURI' => 'Theme URI', - 'Description' => 'Description', - 'Version' => 'Version', - 'Author' => 'Author', - 'AuthorURI' => 'Author URI', - ); - - $actual = get_file_data( DIR_TESTDATA . '/themedir1/default/style.css', $theme_headers ); - - $expected = array( - 'Name' => 'WordPress Default', - 'ThemeURI' => 'http://wordpress.org/', - 'Description' => 'The default WordPress theme based on the famous Kubrick.', - 'Version' => '1.6', - 'Author' => 'Michael Heilemann', - 'AuthorURI' => 'http://binarybonsai.com/', - ); - - $this->assertNotEmpty( $actual ); - - foreach ( $actual as $header => $value ) { - $this->assertSame( $expected[ $header ], $value, $header ); - } - } - - /** - * @ticket 19854 - * @group plugins - * @group themes - */ - public function test_get_file_data_with_cr_line_endings() { - $headers = array( - 'SomeHeader' => 'Some Header', - 'Description' => 'Description', - 'Author' => 'Author', - ); - - $actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-cr-line-endings.php', $headers ); - $expected = array( - 'SomeHeader' => 'Some header value!', - 'Description' => 'This file is using CR line endings for a testcase.', - 'Author' => 'A Very Old Mac', - ); - - $this->assertNotEmpty( $actual ); - - foreach ( $actual as $header => $value ) { - $this->assertSame( $expected[ $header ], $value, $header ); - } - } - - /** - * @ticket 47186 - * @group plugins - * @group themes - */ - public function test_get_file_data_with_php_open_tag_prefix() { - $headers = array( - 'TemplateName' => 'Template Name', - ); - - $actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-php-open-tag-prefix.php', $headers ); - $expected = array( - 'TemplateName' => 'Something', - ); - - $this->assertNotEmpty( $actual ); - - foreach ( $actual as $header => $value ) { - $this->assertSame( $expected[ $header ], $value, $header ); - } - } private function is_unique_writable_file( $path, $filename ) { $fullpath = $path . DIRECTORY_SEPARATOR . $filename; diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php deleted file mode 100644 index b6080da780ba8..0000000000000 --- a/tests/phpunit/tests/functions.php +++ /dev/null @@ -1,2301 +0,0 @@ -_baba = 5; - $x->yZ = 'baba'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - $x->a = array( 5, 111, 'x' ); - $this->assertSame( - array( - '_baba' => 5, - 'yZ' => 'baba', - 'a' => array( 5, 111, 'x' ), - ), - wp_parse_args( $x ) - ); - $y = new MockClass(); - $this->assertSame( array(), wp_parse_args( $y ) ); - } - - public function test_wp_parse_args_array() { - // Arrays. - $a = array(); - $this->assertSame( array(), wp_parse_args( $a ) ); - $b = array( - '_baba' => 5, - 'yZ' => 'baba', - 'a' => array( 5, 111, 'x' ), - ); - $this->assertSame( - array( - '_baba' => 5, - 'yZ' => 'baba', - 'a' => array( 5, 111, 'x' ), - ), - wp_parse_args( $b ) - ); - } - - public function test_wp_parse_args_defaults() { - $x = new MockClass(); - $x->_baba = 5; - $x->yZ = 'baba'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - $x->a = array( 5, 111, 'x' ); - $d = array( 'pu' => 'bu' ); - $this->assertSame( - array( - 'pu' => 'bu', - '_baba' => 5, - 'yZ' => 'baba', - 'a' => array( 5, 111, 'x' ), - ), - wp_parse_args( $x, $d ) - ); - $e = array( '_baba' => 6 ); - $this->assertSame( - array( - '_baba' => 5, - 'yZ' => 'baba', - 'a' => array( 5, 111, 'x' ), - ), - wp_parse_args( $x, $e ) - ); - } - - public function test_wp_parse_args_other() { - $b = true; - wp_parse_str( $b, $s ); - $this->assertSame( $s, wp_parse_args( $b ) ); - $q = 'x=5&_baba=dudu&'; - wp_parse_str( $q, $ss ); - $this->assertSame( $ss, wp_parse_args( $q ) ); - } - - /** - * @ticket 30753 - */ - public function test_wp_parse_args_boolean_strings() { - $args = wp_parse_args( 'foo=false&bar=true' ); - $this->assertIsString( $args['foo'] ); - $this->assertIsString( $args['bar'] ); - } - - /** - * @ticket 35972 - */ - public function test_bool_from_yn() { - $this->assertTrue( bool_from_yn( 'Y' ) ); - $this->assertTrue( bool_from_yn( 'y' ) ); - $this->assertFalse( bool_from_yn( 'n' ) ); - } - - public function test_path_is_absolute() { - $absolute_paths = array( - '/', - '/foo/', - '/foo', - '/FOO/bar', - '/foo/bar/', - '/foo/../bar/', - '\\WINDOWS', - 'C:\\', - 'C:\\WINDOWS', - '\\\\sambashare\\foo', - ); - foreach ( $absolute_paths as $path ) { - $this->assertTrue( path_is_absolute( $path ), "path_is_absolute('$path') should return true" ); - } - } - - public function test_path_is_not_absolute() { - $relative_paths = array( - '', - '.', - '..', - '../foo', - '../', - '../foo.bar', - 'foo/bar', - 'foo', - 'FOO', - '..\\WINDOWS', - ); - foreach ( $relative_paths as $path ) { - $this->assertFalse( path_is_absolute( $path ), "path_is_absolute('$path') should return false" ); - } - } - - /** - * Tests path_join(). - * - * @ticket 55897 - * @dataProvider data_path_join - */ - public function test_path_join( $base, $path, $expected ) { - $this->assertSame( $expected, path_join( $base, $path ) ); - } - - /** - * Data provider for test_path_join(). - * - * @return string[][] - */ - public function data_path_join() { - return array( - // Absolute paths. - 'absolute path should return path' => array( - 'base' => 'base', - 'path' => '/path', - 'expected' => '/path', - ), - 'windows path with slashes' => array( - 'base' => 'base', - 'path' => '//path', - 'expected' => '//path', - ), - 'windows path with backslashes' => array( - 'base' => 'base', - 'path' => '\\\\path', - 'expected' => '\\\\path', - ), - // Non-absolute paths. - 'join base and path' => array( - 'base' => 'base', - 'path' => 'path', - 'expected' => 'base/path', - ), - 'strip trailing slashes in base' => array( - 'base' => 'base///', - 'path' => 'path', - 'expected' => 'base/path', - ), - 'empty path' => array( - 'base' => 'base', - 'path' => '', - 'expected' => 'base/', - ), - 'empty base' => array( - 'base' => '', - 'path' => 'path', - 'expected' => '/path', - ), - 'empty path and base' => array( - 'base' => '', - 'path' => '', - 'expected' => '/', - ), - ); - } - - /** - * @ticket 33265 - * @ticket 35996 - * - * @dataProvider data_wp_normalize_path - */ - public function test_wp_normalize_path( $path, $expected ) { - $this->assertSame( $expected, wp_normalize_path( $path ) ); - } - - public 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' ), - - // Linux paths. - array( '/www/path/', '/www/path/' ), - array( '/www/path/////', '/www/path/' ), - array( '/www/path', '/www/path' ), - - // 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/' ), - - // Edge cases. - array( '', '' ), // Empty string should return empty string. - array( 123, '123' ), // Integer should be cast to string. - ); - } - - /** - * Tests that wp_normalize_path() works with objects that have __toString(). - * - * This is important because the function uses a static cache, and the input - * must be cast to string before being used as an array key. - * - * @ticket 64538 - */ - public function test_wp_normalize_path_with_stringable_object() { - $file_info = new SplFileInfo( '/var/www/html\\test' ); - - $this->assertSame( '/var/www/html/test', wp_normalize_path( $file_info ) ); - } - - /** - * Tests that wp_normalize_path() returns consistent results on repeated calls. - * - * The function uses a static cache, so this verifies cache behavior. - * - * @ticket 64538 - */ - public function test_wp_normalize_path_returns_consistent_results() { - $path = 'C:\\www\\path\\'; - - $first_call = wp_normalize_path( $path ); - $second_call = wp_normalize_path( $path ); - $third_call = wp_normalize_path( $path ); - - $this->assertSame( $first_call, $second_call, 'Second call should return same result as first.' ); - $this->assertSame( $second_call, $third_call, 'Third call should return same result as second.' ); - $this->assertSame( 'C:/www/path/', $first_call, 'Normalized path should match expected value.' ); - } - - /** - * Tests that wp_normalize_path() static cache stores results. - * - * @ticket 64538 - */ - public function test_wp_normalize_path_static_cache() { - $path = '/var/www/cache-test\\subdir\\'; - $expected = '/var/www/cache-test/subdir/'; - - $result = wp_normalize_path( $path ); - $this->assertSame( $expected, $result ); - - $reflection = new ReflectionFunction( 'wp_normalize_path' ); - $static_vars = $reflection->getStaticVariables(); - - $this->assertArrayHasKey( 'cache', $static_vars, 'Static cache array should exist.' ); - $this->assertArrayHasKey( $path, $static_vars['cache'], 'Cache should contain the normalized path.' ); - $this->assertSame( $expected, $static_vars['cache'][ $path ], 'Cached value should match the expected normalized path.' ); - } - - public function test_wp_unique_filename() { - - $testdir = DIR_TESTDATA . '/images/'; - - // Confidence check. - $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Test non-existing file, file name should be unchanged.' ); - - // Ensure correct images exist. - $this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' ); - $this->assertFileDoesNotExist( $testdir . 'test-image-1.png' ); - - // Check number is appended if file already exists. - $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'File name not unique, number not appended.' ); - - // Check file with uppercase extension. - $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.PNG' ), 'File name with uppercase extension not unique, number not appended.' ); - - // Check file name with already added number. - $this->assertSame( 'test-image-2-1.gif', wp_unique_filename( $testdir, 'test-image-2.gif' ), 'File name not unique, number not appended correctly.' ); - - // Check special chars. - $this->assertSame( 'testtest-image.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' ); - - // Check special chars with potential conflicting name. - $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'tést-imagé.png' ), 'Filename with special chars failed' ); - - // Check with single quotes in name (somehow). - $this->assertSame( 'abcdefgh.png', wp_unique_filename( $testdir, "abcdefg'h.png" ), 'File with quote failed' ); - - // Check with double quotes in name (somehow). - $this->assertSame( 'abcdefgh.png', wp_unique_filename( $testdir, 'abcdefg"h.png' ), 'File with quote failed' ); - - // Test crazy name (useful for regression tests). - $this->assertSame( '12af34567890@.^_qwerty-fghjkl-zx.png', wp_unique_filename( $testdir, '12%af34567890#~!@#$..%^&*()|_+qwerty fgh`jkl zx<>?:"{}[]="\'/?.png' ), 'Failed crazy file name' ); - - // Test slashes in names. - $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\fg.png' ), 'Slash not removed' ); - $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\fg.png' ), 'Double slashed not removed' ); - $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\\fg.png' ), 'Triple slashed not removed' ); - } - - /** - * @ticket 42437 - */ - public function test_unique_filename_with_dimension_like_filename() { - $testdir = DIR_TESTDATA . '/images/'; - - add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); - - // Test collision with "dimension-like" original filename. - $this->assertSame( 'one-blue-pixel-100x100-1.png', wp_unique_filename( $testdir, 'one-blue-pixel-100x100.png' ) ); - // Test collision with existing sub-size filename. - // Existing files: one-blue-pixel-100x100.png, one-blue-pixel-1-100x100.png. - $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.png' ) ); - // Same as above with upper case extension. - $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.PNG' ) ); - - remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); - } - - // Callback to patch "basedir" when used in `wp_unique_filename()`. - public function upload_dir_patch_basedir( $upload_dir ) { - $upload_dir['basedir'] = DIR_TESTDATA . '/images/'; - return $upload_dir; - } - - /** - * @ticket 53668 - */ - public function test_wp_unique_filename_with_additional_image_extension() { - $testdir = DIR_TESTDATA . '/images/'; - - add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); - - // Set conversions for uploaded images. - add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); - - // Ensure the test images exist. - $this->assertFileExists( $testdir . 'test-image-1-100x100.jpg', 'test-image-1-100x100.jpg does not exist' ); - $this->assertFileExists( $testdir . 'test-image-2.gif', 'test-image-2.gif does not exist' ); - $this->assertFileExists( $testdir . 'test-image-3.jpg', 'test-image-3.jpg does not exist' ); - $this->assertFileExists( $testdir . 'test-image-4.png', 'test-image-4.png does not exist' ); - - // Standard test: file does not exist and there are no possible intersections with other files. - $this->assertSame( - 'abcdef.png', - wp_unique_filename( $testdir, 'abcdef.png' ), - 'The abcdef.png, abcdef.gif, and abcdef.jpg images do not exist. The file name should not be changed.' - ); - - // Actual clash recognized. - $this->assertSame( - 'canola-1.jpg', - wp_unique_filename( $testdir, 'canola.jpg' ), - 'The canola.jpg image exists. The file name should be unique.' - ); - - // Same name with different extension and the image will be converted. - $this->assertSame( - 'canola-1.png', - wp_unique_filename( $testdir, 'canola.png' ), - 'The canola.jpg image exists. Uploading canola.png that will be converted to canola.jpg should produce unique file name.' - ); - - // Same name with different uppercase extension and the image will be converted. - $this->assertSame( - 'canola-1.png', - wp_unique_filename( $testdir, 'canola.PNG' ), - 'The canola.jpg image exists. Uploading canola.PNG that will be converted to canola.jpg should produce unique file name.' - ); - - // Actual clash with several images with different extensions. - $this->assertSame( - 'test-image-5.png', - wp_unique_filename( $testdir, 'test-image.png' ), - 'The test-image.png, test-image-1-100x100.jpg, test-image-2.gif, test-image-3.jpg, and test-image-4.png images exist.' . - 'All of them may clash when creating sub-sizes or regenerating thumbnails in the future. The filename should be unique.' - ); - - // Possible clash with regenerated thumbnails in the future. - $this->assertSame( - 'codeispoetry-1.jpg', - wp_unique_filename( $testdir, 'codeispoetry.jpg' ), - 'The codeispoetry.png image exists. When regenerating thumbnails for it they will be converted to JPG.' . - 'The name of the newly uploaded codeispoetry.jpg should be made unique.' - ); - - remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); - remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); - } - - /** - * Changes the output format when editing images. When uploading a PNG file - * it will be converted to JPEG, GIF to JPEG, and PICT to BMP - * (if the image editor in PHP supports it). - * - * @param array $formats - * - * @return array - */ - public function image_editor_output_format_handler( $formats ) { - $formats['image/png'] = 'image/jpeg'; - $formats['image/gif'] = 'image/jpeg'; - $formats['image/pct'] = 'image/bmp'; - - return $formats; - } - - /** - * @group add_query_arg - */ - public 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->assertSame( "$url?foo=1", add_query_arg( 'foo', '1', $url ) ); - $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ), $url ) ); - $this->assertSame( - "$url?foo=2", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ), - $url - ) - ); - $this->assertSame( - "$url?foo=1&bar=2", - add_query_arg( - array( - 'foo' => '1', - 'bar' => '2', - ), - $url - ) - ); - - $_SERVER['REQUEST_URI'] = $url; - - $this->assertSame( "$url?foo=1", add_query_arg( 'foo', '1' ) ); - $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) ); - $this->assertSame( - "$url?foo=2", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ) - ) - ); - $this->assertSame( - "$url?foo=1&bar=2", - add_query_arg( - array( - 'foo' => '1', - 'bar' => '2', - ) - ) - ); - } - - foreach ( $frag_urls as $frag_url ) { - $_SERVER['REQUEST_URI'] = 'nothing'; - $url = str_replace( '#frag', '', $frag_url ); - - $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) ); - $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) ); - $this->assertSame( - "$url?foo=2#frag", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ), - $frag_url - ) - ); - $this->assertSame( - "$url?foo=1&bar=2#frag", - add_query_arg( - array( - 'foo' => '1', - 'bar' => '2', - ), - $frag_url - ) - ); - - $_SERVER['REQUEST_URI'] = $frag_url; - - $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) ); - $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) ); - $this->assertSame( - "$url?foo=2#frag", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ) - ) - ); - $this->assertSame( - "$url?foo=1&bar=2#frag", - add_query_arg( - array( - 'foo' => '1', - 'bar' => '2', - ) - ) - ); - } - - $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->assertSame( "$url&foo=1", add_query_arg( 'foo', '1', $url ) ); - $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ), $url ) ); - $this->assertSame( - "$url&foo=2", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ), - $url - ) - ); - $this->assertSame( - "$url&foo=1&bar=2", - add_query_arg( - array( - 'foo' => '1', - 'bar' => '2', - ), - $url - ) - ); - - $_SERVER['REQUEST_URI'] = $url; - - $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1' ) ); - $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ) ) ); - $this->assertSame( - "$url&foo=2", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ) - ) - ); - $this->assertSame( - "$url&foo=1&bar=2", - add_query_arg( - array( - 'foo' => '1', - 'bar' => '2', - ) - ) - ); - } - - $_SERVER['REQUEST_URI'] = $old_req_uri; - } - - /** - * @ticket 31306 - */ - public function test_add_query_arg_numeric_keys() { - $url = add_query_arg( array( 'foo' => 'bar' ), '1=1' ); - $this->assertSame( '1=1&foo=bar', $url ); - - $url = add_query_arg( - array( - 'foo' => 'bar', - '1' => '2', - ), - '1=1' - ); - $this->assertSame( '1=2&foo=bar', $url ); - - $url = add_query_arg( array( '1' => '2' ), 'foo=bar' ); - $this->assertSame( 'foo=bar&1=2', $url ); - } - - /** - * Tests that add_query_arg removes the question mark when - * a parameter is set to false. - * - * @dataProvider data_add_query_arg_removes_question_mark - * - * @ticket 44499 - * @group add_query_arg - * - * @covers ::add_query_arg - * - * @param string $url Url to test. - * @param string $expected Expected URL. - */ - public function test_add_query_arg_removes_question_mark( $url, $expected, $key = 'param', $value = false ) { - $this->assertSame( $expected, add_query_arg( $key, $value, $url ) ); - } - - /** - * Data provider. - * - * @return array - */ - public function data_add_query_arg_removes_question_mark() { - return array( - 'anchor' => array( - 'url' => 'http://example.org?#anchor', - 'expected' => 'http://example.org#anchor', - ), - '/ then anchor' => array( - 'url' => 'http://example.org/?#anchor', - 'expected' => 'http://example.org/#anchor', - ), - 'invalid query param and anchor' => array( - 'url' => 'http://example.org?param=value#anchor', - 'expected' => 'http://example.org#anchor', - ), - '/ then invalid query param and anchor' => array( - 'url' => 'http://example.org/?param=value#anchor', - 'expected' => 'http://example.org/#anchor', - ), - '?#anchor when adding valid key/value args' => array( - 'url' => 'http://example.org?#anchor', - 'expected' => 'http://example.org?foo=bar#anchor', - 'key' => 'foo', - 'value' => 'bar', - ), - '/?#anchor when adding valid key/value args' => array( - 'url' => 'http://example.org/?#anchor', - 'expected' => 'http://example.org/?foo=bar#anchor', - 'key' => 'foo', - 'value' => 'bar', - ), - ); - } - - /** - * @ticket 21594 - */ - public function test_get_allowed_mime_types() { - $mimes = get_allowed_mime_types(); - - $this->assertIsArray( $mimes ); - $this->assertNotEmpty( $mimes ); - - add_filter( 'upload_mimes', '__return_empty_array' ); - $mimes = get_allowed_mime_types(); - $this->assertIsArray( $mimes ); - $this->assertEmpty( $mimes ); - - remove_filter( 'upload_mimes', '__return_empty_array' ); - $mimes = get_allowed_mime_types(); - $this->assertIsArray( $mimes ); - $this->assertNotEmpty( $mimes ); - } - - /** - * @ticket 21594 - */ - public function test_wp_get_mime_types() { - $mimes = wp_get_mime_types(); - - $this->assertIsArray( $mimes ); - $this->assertNotEmpty( $mimes ); - - add_filter( 'mime_types', '__return_empty_array' ); - $mimes = wp_get_mime_types(); - $this->assertIsArray( $mimes ); - $this->assertEmpty( $mimes ); - - remove_filter( 'mime_types', '__return_empty_array' ); - $mimes = wp_get_mime_types(); - $this->assertIsArray( $mimes ); - $this->assertNotEmpty( $mimes ); - - // 'upload_mimes' should not affect wp_get_mime_types(). - add_filter( 'upload_mimes', '__return_empty_array' ); - $mimes = wp_get_mime_types(); - $this->assertIsArray( $mimes ); - $this->assertNotEmpty( $mimes ); - - remove_filter( 'upload_mimes', '__return_empty_array' ); - $mimes2 = wp_get_mime_types(); - $this->assertIsArray( $mimes2 ); - $this->assertNotEmpty( $mimes2 ); - $this->assertSame( $mimes2, $mimes ); - } - - /** - * @ticket 23688 - */ - public function test_canonical_charset() { - $orig_blog_charset = get_option( 'blog_charset' ); - - update_option( 'blog_charset', 'utf8' ); - $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', 'utf-8' ); - $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', 'UTF8' ); - $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', 'UTF-8' ); - $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', 'ISO-8859-1' ); - $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', 'ISO8859-1' ); - $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', 'iso8859-1' ); - $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', 'iso-8859-1' ); - $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); - - // Arbitrary strings are passed through. - update_option( 'blog_charset', 'foobarbaz' ); - $this->assertSame( 'foobarbaz', get_option( 'blog_charset' ) ); - - update_option( 'blog_charset', $orig_blog_charset ); - } - - /** - * @dataProvider data_device_can_upload - */ - public function test_device_can_upload( $user_agent, $expected ) { - $_SERVER['HTTP_USER_AGENT'] = $user_agent; - $actual = _device_can_upload(); - unset( $_SERVER['HTTP_USER_AGENT'] ); - $this->assertSame( $expected, $actual ); - } - - public 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, - ), - // Android 2.2, Android Webkit Browser. - 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, - ), - // BlackBerry 9900, BlackBerry browser. - 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, - ), - ); - } - - /** - * @ticket 9064 - */ - public 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', - 'http://127.0.0.1', - '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', - 'http://việtnam.icom.museum', - 'ftp://127.0.0.1/', - 'http://www.woo.com/video?v=exvUH2qKLTU', - 'http://taco.com?burrito=enchilada#guac', - 'http://example.org/?post_type=post&p=4', - 'http://example.org/?post_type=post&p=5', - 'http://example.org/?post_type=post&p=6', - 'http://typo-in-query.org/?foo=bar&baz=missing_semicolon', - ); - - $blob = ' - http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html - - http://this.com - - http://127.0.0.1 - - 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://č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 - ftp://127.0.0.1/ - http://www.woo.com/video?v=exvUH2qKLTU - - http://taco.com?burrito=enchilada#guac - - http://example.org/?post_type=post&p=4 - http://example.org/?post_type=post&p=5 - http://example.org/?post_type=post&p=6 - - http://typo-in-query.org/?foo=bar&baz=missing_semicolon - '; - - $urls = wp_extract_urls( $blob ); - $this->assertNotEmpty( $urls ); - $this->assertIsArray( $urls ); - $this->assertCount( count( $original_urls ), $urls ); - $this->assertSame( $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->assertSame( $decoded, $urls ); - $this->assertSame( $original_urls, $decoded ); - - $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor - incididunt ut labore http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html et dolore magna aliqua. - Ut http://this.com enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco http://127.0.0.1 - laboris nisi ut aliquip ex http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&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 - velit esse http://wordpress-core.com:8080/ cillum dolore eu fugiat nulla http://www.website.com:5000 pariatur. Excepteur sint occaecat cupidatat non proident, - sunt in culpa qui officia deserunt mollit http://wordpress-core/?346236346326&2134362574863.437 anim id est laborum.'; - - $urls = wp_extract_urls( $blob ); - $this->assertNotEmpty( $urls ); - $this->assertIsArray( $urls ); - $this->assertCount( 8, $urls ); - $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls ); - - $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor - incididunt ut labore 343462^ et dolore magna aliqua. - Ut &3640i6p1yi499 enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco localhost - laboris nisi ut aliquip ex 343462^ ea - commodo consequat. 343462^ Duis aute irure dolor in reprehenderit in voluptate - velit esse -3-4--321-64-4@#!$^$!@^@^ cillum dolore eu http://www.website.com:5000 fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, - sunt in culpa qui officia deserunt mollit )(*&^%$ anim id est laborum.'; - - $urls = wp_extract_urls( $blob ); - $this->assertNotEmpty( $urls ); - $this->assertIsArray( $urls ); - $this->assertCount( 8, $urls ); - $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls ); - } - - /** - * Tests for backward compatibility of `wp_extract_urls` to remove unused semicolons. - * - * @ticket 30580 - * - * @covers ::wp_extract_urls - */ - public function test_wp_extract_urls_remove_semicolon() { - $expected = array( - 'http://typo.com', - 'http://example.org/?post_type=post&p=8', - ); - $actual = wp_extract_urls( - ' - http://typo.com; - http://example.org/?post_type=;p;o;s;t;&p=8; - ' - ); - - $this->assertSame( $expected, $actual ); - } - - /** - * @ticket 28786 - */ - public function test_wp_json_encode() { - $this->assertSame( wp_json_encode( 'a' ), '"a"' ); - } - - /** - * @ticket 28786 - */ - public function test_wp_json_encode_utf8() { - $this->assertSame( wp_json_encode( '这' ), '"\u8fd9"' ); - } - - /** - * @ticket 28786 - * @requires function mb_detect_order - */ - public function test_wp_json_encode_non_utf8() { - $charsets = mb_detect_order(); - $old_charsets = $charsets; - if ( ! in_array( 'EUC-JP', $charsets, true ) ) { - $charsets[] = 'EUC-JP'; - mb_detect_order( $charsets ); - } - - $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); - $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); - - $this->assertSame( 'aあb', $utf8 ); - - $this->assertSame( '"a\u3042b"', wp_json_encode( $eucjp ) ); - - mb_detect_order( $old_charsets ); - } - - /** - * @ticket 28786 - * @requires function mb_detect_order - */ - public function test_wp_json_encode_non_utf8_in_array() { - $charsets = mb_detect_order(); - $old_charsets = $charsets; - if ( ! in_array( 'EUC-JP', $charsets, true ) ) { - $charsets[] = 'EUC-JP'; - mb_detect_order( $charsets ); - } - - $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); - $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); - - $this->assertSame( 'aあb', $utf8 ); - - $this->assertSame( '["c","a\u3042b"]', wp_json_encode( array( 'c', $eucjp ) ) ); - - mb_detect_order( $old_charsets ); - } - - /** - * @ticket 28786 - */ - public function test_wp_json_encode_array() { - $this->assertSame( wp_json_encode( array( 'a' ) ), '["a"]' ); - } - - /** - * @ticket 28786 - */ - public function test_wp_json_encode_object() { - $object = new stdClass(); - $object->a = 'b'; - $this->assertSame( wp_json_encode( $object ), '{"a":"b"}' ); - } - - /** - * @ticket 28786 - */ - public 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 ); - } - - /** - * @ticket 53238 - */ - public function test_wp_json_file_decode() { - $result = wp_json_file_decode( - DIR_TESTDATA . '/blocks/notice/block.json' - ); - - $this->assertIsObject( $result ); - $this->assertSame( 'tests/notice', $result->name ); - } - - /** - * @ticket 53238 - */ - public function test_wp_json_file_decode_associative_array() { - $result = wp_json_file_decode( - DIR_TESTDATA . '/blocks/notice/block.json', - array( 'associative' => true ) - ); - - $this->assertIsArray( $result ); - $this->assertSame( 'tests/notice', $result['name'] ); - } - - /** - * @ticket 36054 - * @dataProvider data_mysql_to_rfc3339 - */ - public function test_mysql_to_rfc3339( $expected, $actual ) { - $date_return = mysql_to_rfc3339( $actual ); - - $this->assertIsString( $date_return, 'The date return must be a string' ); - $this->assertNotEmpty( $date_return, 'The date return could not be an empty string' ); - $this->assertSame( $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' ); - } - - public function data_mysql_to_rfc3339() { - 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' ), - array( '2016-03-02T19:13:00', '16-03-02 19:13' ), - ); - } - - /** - * @ticket 35987 - */ - public function test_wp_get_ext_types() { - $extensions = wp_get_ext_types(); - - $this->assertIsArray( $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->assertIsArray( $extensions ); - $this->assertNotEmpty( $extensions ); - } - - /** - * @ticket 35987 - */ - public function test_wp_ext2type() { - $extensions = wp_get_ext_types(); - - $this->assertNotEmpty( $extensions ); - - foreach ( $extensions as $type => $extension_list ) { - foreach ( $extension_list as $extension ) { - $this->assertSame( $type, wp_ext2type( $extension ) ); - $this->assertSame( $type, wp_ext2type( strtoupper( $extension ) ) ); - } - } - - $this->assertNull( wp_ext2type( 'unknown_format' ) ); - } - - /** - * 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 - */ - public function test_wp_raise_memory_limit() { - if ( -1 !== WP_MAX_MEMORY_LIMIT ) { - $this->markTestSkipped( 'WP_MAX_MEMORY_LIMIT should be set to -1.' ); - } - - $ini_limit_before = ini_get( 'memory_limit' ); - $raised_limit = wp_raise_memory_limit(); - $ini_limit_after = ini_get( 'memory_limit' ); - - $this->assertSame( $ini_limit_before, $ini_limit_after ); - $this->assertFalse( $raised_limit ); - $this->assertEquals( WP_MAX_MEMORY_LIMIT, $ini_limit_after ); - } - - /** - * Tests wp_generate_uuid4(). - * - * @covers ::wp_generate_uuid4 - * @ticket 38164 - */ - public function test_wp_generate_uuid4() { - $uuids = array(); - for ( $i = 0; $i < 20; $i += 1 ) { - $uuid = wp_generate_uuid4(); - $this->assertTrue( wp_is_uuid( $uuid, 4 ) ); - $uuids[] = $uuid; - } - - $unique_uuids = array_unique( $uuids ); - $this->assertSame( $uuids, $unique_uuids ); - } - - /** - * Tests wp_is_uuid(). - * - * @covers ::wp_is_uuid - * @ticket 39778 - */ - public 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 ) ); - } - } - - /** - * Tests wp_unique_id(). - * - * @covers ::wp_unique_id - * @ticket 44883 - */ - public function test_wp_unique_id() { - - // Test without prefix. - $ids = array(); - for ( $i = 0; $i < 20; $i += 1 ) { - $id = wp_unique_id(); - $this->assertIsString( $id ); - $this->assertIsNumeric( $id ); - $ids[] = $id; - } - $this->assertSame( $ids, array_unique( $ids ) ); - - // Test with prefix. - $ids = array(); - for ( $i = 0; $i < 20; $i += 1 ) { - $id = wp_unique_id( 'foo-' ); - $this->assertMatchesRegularExpression( '/^foo-\d+$/', $id ); - $ids[] = $id; - } - $this->assertSame( $ids, array_unique( $ids ) ); - } - - /** - * @ticket 40017 - * @dataProvider data_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.' ); - } - - if ( is_array( $expected ) ) { - $this->assertContains( wp_get_image_mime( $file ), $expected ); - } else { - $this->assertSame( $expected, wp_get_image_mime( $file ) ); - } - } - - /** - * Data provider for test_wp_get_image_mime(). - */ - public function data_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', - ), - // Animated WebP. - array( - DIR_TESTDATA . '/images/webp-animated.webp', - 'image/webp', - ), - // Lossless WebP. - array( - DIR_TESTDATA . '/images/webp-lossless.webp', - 'image/webp', - ), - // Lossy WebP. - array( - DIR_TESTDATA . '/images/webp-lossy.webp', - 'image/webp', - ), - // Transparent WebP. - array( - DIR_TESTDATA . '/images/webp-transparent.webp', - 'image/webp', - ), - // Not an image. - array( - DIR_TESTDATA . '/uploads/dashicons.woff', - false, - ), - // Animated AVIF. - array( - DIR_TESTDATA . '/images/avif-animated.avif', - 'image/avif', - ), - // Lossless AVIF. - array( - DIR_TESTDATA . '/images/avif-lossless.avif', - 'image/avif', - ), - // Lossy AVIF. - array( - DIR_TESTDATA . '/images/avif-lossy.avif', - 'image/avif', - ), - // Transparent AVIF. - array( - DIR_TESTDATA . '/images/avif-transparent.avif', - 'image/avif', - ), - // HEIC. - array( - DIR_TESTDATA . '/images/test-image.heic', - // In PHP 8.5, it returns 'image/heif'. Before that, it returns 'image/heic'. - array( 'image/heic', 'image/heif' ), - ), - ); - - return $data; - } - - /** - * @ticket 35725 - * @dataProvider data_wp_getimagesize - */ - public function test_wp_getimagesize( $file, $expected ) { - if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) { - $this->markTestSkipped( 'The exif PHP extension is not loaded.' ); - } - - $result = wp_getimagesize( $file ); - - // The getimagesize() function varies in its response, so - // let's restrict comparison to expected keys only. - if ( is_array( $expected ) ) { - foreach ( $expected as $k => $v ) { - $this->assertArrayHasKey( $k, $result ); - $this->assertSame( $expected[ $k ], $result[ $k ] ); - } - } else { - $this->assertSame( $expected, $result ); - } - } - - /** - * Data provider for test_wp_getimagesize(). - */ - public function data_wp_getimagesize() { - $data = array( - // Standard JPEG. - array( - DIR_TESTDATA . '/images/test-image.jpg', - array( - 50, - 50, - IMAGETYPE_JPEG, - 'width="50" height="50"', - 'mime' => 'image/jpeg', - ), - ), - // Standard GIF. - array( - DIR_TESTDATA . '/images/test-image.gif', - array( - 50, - 50, - IMAGETYPE_GIF, - 'width="50" height="50"', - 'mime' => 'image/gif', - ), - ), - // Standard PNG. - array( - DIR_TESTDATA . '/images/test-image.png', - array( - 50, - 50, - IMAGETYPE_PNG, - 'width="50" height="50"', - 'mime' => 'image/png', - ), - ), - // Image with wrong extension. - array( - DIR_TESTDATA . '/images/test-image-mime-jpg.png', - array( - 50, - 50, - IMAGETYPE_JPEG, - 'width="50" height="50"', - 'mime' => 'image/jpeg', - ), - ), - // Animated WebP. - array( - DIR_TESTDATA . '/images/webp-animated.webp', - array( - 100, - 100, - IMAGETYPE_WEBP, - 'width="100" height="100"', - 'mime' => 'image/webp', - ), - ), - // Lossless WebP. - array( - DIR_TESTDATA . '/images/webp-lossless.webp', - array( - 1200, - 675, - IMAGETYPE_WEBP, - 'width="1200" height="675"', - 'mime' => 'image/webp', - ), - ), - // Lossy WebP. - array( - DIR_TESTDATA . '/images/webp-lossy.webp', - array( - 1200, - 675, - IMAGETYPE_WEBP, - 'width="1200" height="675"', - 'mime' => 'image/webp', - ), - ), - // Transparent WebP. - array( - DIR_TESTDATA . '/images/webp-transparent.webp', - array( - 1200, - 675, - IMAGETYPE_WEBP, - 'width="1200" height="675"', - 'mime' => 'image/webp', - ), - ), - // Not an image. - array( - DIR_TESTDATA . '/uploads/dashicons.woff', - false, - ), - // Animated AVIF. - array( - DIR_TESTDATA . '/images/avif-animated.avif', - array( - 150, - 150, - IMAGETYPE_AVIF, - 'width="150" height="150"', - 'mime' => 'image/avif', - ), - ), - // Lossless AVIF. - array( - DIR_TESTDATA . '/images/avif-lossless.avif', - array( - 400, - 400, - IMAGETYPE_AVIF, - 'width="400" height="400"', - 'mime' => 'image/avif', - ), - ), - // Lossy AVIF. - array( - DIR_TESTDATA . '/images/avif-lossy.avif', - array( - 400, - 400, - IMAGETYPE_AVIF, - 'width="400" height="400"', - 'mime' => 'image/avif', - ), - ), - // Transparent AVIF. - array( - DIR_TESTDATA . '/images/avif-transparent.avif', - array( - 128, - 128, - IMAGETYPE_AVIF, - 'width="128" height="128"', - 'mime' => 'image/avif', - ), - ), - // Grid AVIF. - array( - DIR_TESTDATA . '/images/avif-alpha-grid2x1.avif', - array( - 199, - 200, - IMAGETYPE_AVIF, - 'width="199" height="200"', - 'mime' => 'image/avif', - ), - ), - ); - - return $data; - } - - /** - * Tests that wp_getimagesize() correctly handles HEIC image files. - * - * @ticket 53645 - */ - public function test_wp_getimagesize_heic() { - if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) { - $this->markTestSkipped( 'The exif PHP extension is not loaded.' ); - } - - $file = DIR_TESTDATA . '/images/test-image.heic'; - - $editor = wp_get_image_editor( $file ); - if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/heic' ) ) { - $this->markTestSkipped( 'No HEIC support in the editor engine on this system.' ); - } - - $expected = array( - 1180, - 1180, - IMAGETYPE_HEIF, - 'width="1180" height="1180"', - ); - - // As of PHP 8.5.0, getimagesize() supports HEIF/HEIC files. - if ( PHP_VERSION_ID >= 80500 ) { - $expected = array_merge( - $expected, - array( - 'bits' => 8, - 'channels' => 3, - 'mime' => 'image/heif', - 'width_unit' => 'px', - 'height_unit' => 'px', - ) - ); - } else { - $expected['mime'] = 'image/heic'; - } - - $result = wp_getimagesize( $file ); - $this->assertSame( $expected, $result ); - } - - - /** - * @ticket 39550 - * @dataProvider data_wp_check_filetype_and_ext - * @requires extension fileinfo - */ - public function test_wp_check_filetype_and_ext( $file, $filename, $expected ) { - $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); - } - - public function data_wp_check_filetype_and_ext() { - $data = array( - // Standard image. - array( - DIR_TESTDATA . '/images/canola.jpg', - 'canola.jpg', - array( - 'ext' => 'jpg', - 'type' => 'image/jpeg', - 'proper_filename' => false, - ), - ), - // Image with wrong extension. - array( - DIR_TESTDATA . '/images/test-image-mime-jpg.png', - 'test-image-mime-jpg.png', - array( - 'ext' => 'jpg', - 'type' => 'image/jpeg', - 'proper_filename' => 'test-image-mime-jpg.jpg', - ), - ), - // Image without extension. - array( - DIR_TESTDATA . '/images/test-image-no-extension', - 'test-image-no-extension', - array( - 'ext' => false, - 'type' => false, - 'proper_filename' => false, - ), - ), - // Valid non-image file with an image extension. - array( - DIR_TESTDATA . '/formatting/big5.txt', - 'big5.jpg', - array( - 'ext' => false, - 'type' => false, - 'proper_filename' => false, - ), - ), - // Non-image file not allowed. - array( - DIR_TESTDATA . '/export/crazy-cdata.xml', - 'crazy-cdata.xml', - array( - 'ext' => false, - 'type' => false, - 'proper_filename' => false, - ), - ), - // 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, - ), - ), - // 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, - ), - ), - ); - - // Test a few additional file types on single sites. - if ( ! is_multisite() ) { - $data = array_merge( - $data, - array( - // Standard non-image file. - array( - DIR_TESTDATA . '/formatting/big5.txt', - 'big5.txt', - array( - 'ext' => 'txt', - 'type' => 'text/plain', - 'proper_filename' => false, - ), - ), - // Google Docs file for which finfo_file() returns a duplicate mime type. - array( - DIR_TESTDATA . '/uploads/double-mime-type.docx', - 'double-mime-type.docx', - array( - 'ext' => 'docx', - 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'proper_filename' => false, - ), - ), - // Non-image file with wrong sub-type. - array( - DIR_TESTDATA . '/uploads/pages-to-word.docx', - 'pages-to-word.docx', - array( - 'ext' => 'docx', - 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'proper_filename' => false, - ), - ), - // FLAC file. - array( - DIR_TESTDATA . '/uploads/small-audio.flac', - 'small-audio.flac', - array( - 'ext' => 'flac', - 'type' => 'audio/flac', - 'proper_filename' => false, - ), - ), - // 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, - ), - ), - ) - ); - } - - return $data; - } - - /** - * @ticket 39550 - * @group ms-excluded - * @requires extension fileinfo - */ - public function test_wp_check_filetype_and_ext_with_filtered_svg() { - $file = DIR_TESTDATA . '/uploads/video-play.svg'; - $filename = 'video-play.svg'; - - $expected = array( - 'ext' => 'svg', - 'type' => 'image/svg+xml', - 'proper_filename' => false, - ); - - add_filter( - 'upload_mimes', - static function ( $mimes ) { - $mimes['svg'] = 'image/svg+xml'; - return $mimes; - } - ); - - $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); - } - - /** - * @ticket 39550 - * @group ms-excluded - * @requires extension fileinfo - */ - public function test_wp_check_filetype_and_ext_with_filtered_woff() { - $file = DIR_TESTDATA . '/uploads/dashicons.woff'; - $filename = 'dashicons.woff'; - - $woff_mime_type = 'application/font-woff'; - - /* - * As of PHP 8.1.12, which includes libmagic/file update to version 5.42, - * the expected mime type for WOFF files is 'font/woff'. - * - * See https://github.com/php/php-src/issues/8805. - */ - if ( PHP_VERSION_ID >= 80112 ) { - $woff_mime_type = 'font/woff'; - } - - $expected = array( - 'ext' => 'woff', - 'type' => $woff_mime_type, - 'proper_filename' => false, - ); - - add_filter( - 'upload_mimes', - static function ( $mimes ) use ( $woff_mime_type ) { - $mimes['woff'] = $woff_mime_type; - return $mimes; - } - ); - - $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); - } - - /** - * Test file path validation - * - * @ticket 42016 - * @ticket 61488 - * @dataProvider data_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_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, - ), - - // Windows Path with allowed file - array( - 'Apache24\htdocs\wordpress/wp-content/themes/twentyten/style.css', - array( 'Apache24\htdocs\wordpress/wp-content/themes/twentyten/style.css' ), - 0, - ), - - // Disallowed files: - array( - 'foo.ext', - array( 'bar.ext' ), - 3, - ), - array( - 'foo.ext', - array( '.ext' ), - 3, - ), - array( - 'path/foo.ext', - array( 'foo.ext' ), - 3, - ), - - ); - } - - /** - * Test stream URL validation. - * - * @dataProvider data_wp_is_stream - * - * @param string $path The resource path or URL. - * @param bool $expected Expected result. - */ - public function test_wp_is_stream( $path, $expected ) { - if ( ! extension_loaded( 'openssl' ) && false !== strpos( $path, 'https://' ) ) { - $this->markTestSkipped( 'The openssl PHP extension is not loaded.' ); - } - - $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_wp_is_stream() { - return array( - // Legitimate stream examples. - array( 'http://example.com', true ), - 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 ), - ); - } - - /** - * Test human_readable_duration(). - * - * @ticket 39667 - * @dataProvider data_human_readable_duration - * - * @param string $input Duration. - * @param string $expected Expected human readable duration. - */ - public function test_human_readable_duration( $input, $expected ) { - $this->assertSame( $expected, human_readable_duration( $input ) ); - } - - /** - * Data provider for test_duration_format(). - * - * @return array { - * @type array { - * @type string $input Duration. - * @type string $expect Expected human readable duration. - * } - * } - */ - public function data_human_readable_duration() { - return array( - // 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' ), - array( '30:00', '30 minutes, 0 seconds' ), - 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 ), - array( 'Batman Begins !', false ), - array( '', false ), - array( '-1', false ), - array( -1, false ), - array( 0, false ), - array( 1, false ), - array( '00', false ), - 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. - ); - } - - /** - * @ticket 49404 - * @dataProvider data_wp_is_json_media_type - */ - public function test_wp_is_json_media_type( $input, $expected ) { - $this->assertSame( $expected, wp_is_json_media_type( $input ) ); - } - - - public function data_wp_is_json_media_type() { - return array( - array( 'application/ld+json', true ), - array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', true ), - array( 'application/activity+json', true ), - array( 'application/json+oembed', true ), - array( 'application/json', true ), - array( 'application/nojson', false ), - array( 'application/no.json', false ), - array( 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8', false ), - array( 'application/activity+json, application/nojson', true ), - ); - } - - /** - * @ticket 53668 - */ - public function test_wp_get_default_extension_for_mime_type() { - $this->assertSame( 'jpg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' ); - $this->assertNotEquals( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' ); - $this->assertSame( 'png', wp_get_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' ); - $this->assertFalse( wp_get_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' ); - $this->assertFalse( wp_get_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' ); - $this->assertFalse( wp_get_default_extension_for_mime_type( ' ' ), 'false not returned when empty string as mime type supplied' ); - $this->assertFalse( wp_get_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' ); - $this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' ); - } - - /** - * @ticket 55505 - * @covers ::wp_recursive_ksort - */ - public function test_wp_recursive_ksort() { - // Create an array to test. - $theme_json = array( - 'version' => 1, - 'settings' => array( - 'typography' => array( - 'fontFamilies' => array( - 'fontFamily' => 'DM Sans, sans-serif', - 'slug' => 'dm-sans', - 'name' => 'DM Sans', - ), - ), - 'color' => array( - 'palette' => array( - array( - 'slug' => 'foreground', - 'color' => '#242321', - 'name' => 'Foreground', - ), - array( - 'slug' => 'background', - 'color' => '#FCFBF8', - 'name' => 'Background', - ), - array( - 'slug' => 'primary', - 'color' => '#71706E', - 'name' => 'Primary', - ), - array( - 'slug' => 'tertiary', - 'color' => '#CFCFCF', - 'name' => 'Tertiary', - ), - ), - ), - ), - ); - - // Sort the array. - wp_recursive_ksort( $theme_json ); - - // Expected result. - $expected_theme_json = array( - 'settings' => array( - 'color' => array( - 'palette' => array( - array( - 'color' => '#242321', - 'name' => 'Foreground', - 'slug' => 'foreground', - ), - array( - 'color' => '#FCFBF8', - 'name' => 'Background', - 'slug' => 'background', - ), - array( - 'color' => '#71706E', - 'name' => 'Primary', - 'slug' => 'primary', - ), - array( - 'color' => '#CFCFCF', - 'name' => 'Tertiary', - 'slug' => 'tertiary', - ), - ), - ), - 'typography' => array( - 'fontFamilies' => array( - 'fontFamily' => 'DM Sans, sans-serif', - 'name' => 'DM Sans', - 'slug' => 'dm-sans', - ), - ), - ), - 'version' => 1, - ); - $this->assertSameSetsWithIndex( $theme_json, $expected_theme_json ); - } -} diff --git a/tests/phpunit/tests/functions/addQueryArg.php b/tests/phpunit/tests/functions/addQueryArg.php new file mode 100644 index 0000000000000..d6d808145de3a --- /dev/null +++ b/tests/phpunit/tests/functions/addQueryArg.php @@ -0,0 +1,271 @@ +assertSame( "$url?foo=1", add_query_arg( 'foo', '1', $url ) ); + $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ), $url ) ); + $this->assertSame( + "$url?foo=2", + add_query_arg( + array( + 'foo' => '1', + 'foo' => '2', + ), + $url + ) + ); + $this->assertSame( + "$url?foo=1&bar=2", + add_query_arg( + array( + 'foo' => '1', + 'bar' => '2', + ), + $url + ) + ); + + $_SERVER['REQUEST_URI'] = $url; + + $this->assertSame( "$url?foo=1", add_query_arg( 'foo', '1' ) ); + $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) ); + $this->assertSame( + "$url?foo=2", + add_query_arg( + array( + 'foo' => '1', + 'foo' => '2', + ) + ) + ); + $this->assertSame( + "$url?foo=1&bar=2", + add_query_arg( + array( + 'foo' => '1', + 'bar' => '2', + ) + ) + ); + } + + foreach ( $frag_urls as $frag_url ) { + $_SERVER['REQUEST_URI'] = 'nothing'; + $url = str_replace( '#frag', '', $frag_url ); + + $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) ); + $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) ); + $this->assertSame( + "$url?foo=2#frag", + add_query_arg( + array( + 'foo' => '1', + 'foo' => '2', + ), + $frag_url + ) + ); + $this->assertSame( + "$url?foo=1&bar=2#frag", + add_query_arg( + array( + 'foo' => '1', + 'bar' => '2', + ), + $frag_url + ) + ); + + $_SERVER['REQUEST_URI'] = $frag_url; + + $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) ); + $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) ); + $this->assertSame( + "$url?foo=2#frag", + add_query_arg( + array( + 'foo' => '1', + 'foo' => '2', + ) + ) + ); + $this->assertSame( + "$url?foo=1&bar=2#frag", + add_query_arg( + array( + 'foo' => '1', + 'bar' => '2', + ) + ) + ); + } + + $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->assertSame( "$url&foo=1", add_query_arg( 'foo', '1', $url ) ); + $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ), $url ) ); + $this->assertSame( + "$url&foo=2", + add_query_arg( + array( + 'foo' => '1', + 'foo' => '2', + ), + $url + ) + ); + $this->assertSame( + "$url&foo=1&bar=2", + add_query_arg( + array( + 'foo' => '1', + 'bar' => '2', + ), + $url + ) + ); + + $_SERVER['REQUEST_URI'] = $url; + + $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1' ) ); + $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ) ) ); + $this->assertSame( + "$url&foo=2", + add_query_arg( + array( + 'foo' => '1', + 'foo' => '2', + ) + ) + ); + $this->assertSame( + "$url&foo=1&bar=2", + add_query_arg( + array( + 'foo' => '1', + 'bar' => '2', + ) + ) + ); + } + + $_SERVER['REQUEST_URI'] = $old_req_uri; + } + + /** + * @ticket 31306 + */ + public function test_add_query_arg_numeric_keys() { + $url = add_query_arg( array( 'foo' => 'bar' ), '1=1' ); + $this->assertSame( '1=1&foo=bar', $url ); + + $url = add_query_arg( + array( + 'foo' => 'bar', + '1' => '2', + ), + '1=1' + ); + $this->assertSame( '1=2&foo=bar', $url ); + + $url = add_query_arg( array( '1' => '2' ), 'foo=bar' ); + $this->assertSame( 'foo=bar&1=2', $url ); + } + + /** + * Tests that add_query_arg removes the question mark when + * a parameter is set to false. + * + * @dataProvider data_add_query_arg_removes_question_mark + * + * @ticket 44499 + * @group add_query_arg + * + * @covers ::add_query_arg + * + * @param string $url Url to test. + * @param string $expected Expected URL. + */ + public function test_add_query_arg_removes_question_mark( $url, $expected, $key = 'param', $value = false ) { + $this->assertSame( $expected, add_query_arg( $key, $value, $url ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_add_query_arg_removes_question_mark() { + return array( + 'anchor' => array( + 'url' => 'http://example.org?#anchor', + 'expected' => 'http://example.org#anchor', + ), + '/ then anchor' => array( + 'url' => 'http://example.org/?#anchor', + 'expected' => 'http://example.org/#anchor', + ), + 'invalid query param and anchor' => array( + 'url' => 'http://example.org?param=value#anchor', + 'expected' => 'http://example.org#anchor', + ), + '/ then invalid query param and anchor' => array( + 'url' => 'http://example.org/?param=value#anchor', + 'expected' => 'http://example.org/#anchor', + ), + '?#anchor when adding valid key/value args' => array( + 'url' => 'http://example.org?#anchor', + 'expected' => 'http://example.org?foo=bar#anchor', + 'key' => 'foo', + 'value' => 'bar', + ), + '/?#anchor when adding valid key/value args' => array( + 'url' => 'http://example.org/?#anchor', + 'expected' => 'http://example.org/?foo=bar#anchor', + 'key' => 'foo', + 'value' => 'bar', + ), + ); + } +} diff --git a/tests/phpunit/tests/functions/boolFromYn.php b/tests/phpunit/tests/functions/boolFromYn.php new file mode 100644 index 0000000000000..129b83de54eb4 --- /dev/null +++ b/tests/phpunit/tests/functions/boolFromYn.php @@ -0,0 +1,17 @@ +assertTrue( bool_from_yn( 'Y' ) ); + $this->assertTrue( bool_from_yn( 'y' ) ); + $this->assertFalse( bool_from_yn( 'n' ) ); + } +} diff --git a/tests/phpunit/tests/functions/canonicalCharset.php b/tests/phpunit/tests/functions/canonicalCharset.php index b46b6f5ce5b42..16207456f7cad 100644 --- a/tests/phpunit/tests/functions/canonicalCharset.php +++ b/tests/phpunit/tests/functions/canonicalCharset.php @@ -1,10 +1,6 @@ assertSame( $expected, $actual ); + } + + public 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, + ), + // Android 2.2, Android Webkit Browser. + 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, + ), + // BlackBerry 9900, BlackBerry browser. + 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, + ), + ); + } +} diff --git a/tests/phpunit/tests/functions/getAllowedMimeTypes.php b/tests/phpunit/tests/functions/getAllowedMimeTypes.php new file mode 100644 index 0000000000000..dd387498d2893 --- /dev/null +++ b/tests/phpunit/tests/functions/getAllowedMimeTypes.php @@ -0,0 +1,28 @@ +assertIsArray( $mimes ); + $this->assertNotEmpty( $mimes ); + + add_filter( 'upload_mimes', '__return_empty_array' ); + $mimes = get_allowed_mime_types(); + $this->assertIsArray( $mimes ); + $this->assertEmpty( $mimes ); + + remove_filter( 'upload_mimes', '__return_empty_array' ); + $mimes = get_allowed_mime_types(); + $this->assertIsArray( $mimes ); + $this->assertNotEmpty( $mimes ); + } +} diff --git a/tests/phpunit/tests/functions/getFileData.php b/tests/phpunit/tests/functions/getFileData.php new file mode 100644 index 0000000000000..7ac923bc2a984 --- /dev/null +++ b/tests/phpunit/tests/functions/getFileData.php @@ -0,0 +1,90 @@ + 'Theme Name', + 'ThemeURI' => 'Theme URI', + 'Description' => 'Description', + 'Version' => 'Version', + 'Author' => 'Author', + 'AuthorURI' => 'Author URI', + ); + + $actual = get_file_data( DIR_TESTDATA . '/themedir1/default/style.css', $theme_headers ); + + $expected = array( + 'Name' => 'WordPress Default', + 'ThemeURI' => 'http://wordpress.org/', + 'Description' => 'The default WordPress theme based on the famous Kubrick.', + 'Version' => '1.6', + 'Author' => 'Michael Heilemann', + 'AuthorURI' => 'http://binarybonsai.com/', + ); + + $this->assertNotEmpty( $actual ); + + foreach ( $actual as $header => $value ) { + $this->assertSame( $expected[ $header ], $value, $header ); + } + } + + /** + * @ticket 19854 + * @group plugins + * @group themes + */ + public function test_get_file_data_with_cr_line_endings() { + $headers = array( + 'SomeHeader' => 'Some Header', + 'Description' => 'Description', + 'Author' => 'Author', + ); + + $actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-cr-line-endings.php', $headers ); + $expected = array( + 'SomeHeader' => 'Some header value!', + 'Description' => 'This file is using CR line endings for a testcase.', + 'Author' => 'A Very Old Mac', + ); + + $this->assertNotEmpty( $actual ); + + foreach ( $actual as $header => $value ) { + $this->assertSame( $expected[ $header ], $value, $header ); + } + } + + /** + * @ticket 47186 + * @group plugins + * @group themes + */ + public function test_get_file_data_with_php_open_tag_prefix() { + $headers = array( + 'TemplateName' => 'Template Name', + ); + + $actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-php-open-tag-prefix.php', $headers ); + $expected = array( + 'TemplateName' => 'Something', + ); + + $this->assertNotEmpty( $actual ); + + foreach ( $actual as $header => $value ) { + $this->assertSame( $expected[ $header ], $value, $header ); + } + } +} diff --git a/tests/phpunit/tests/functions/humanReadableDuration.php b/tests/phpunit/tests/functions/humanReadableDuration.php new file mode 100644 index 0000000000000..a567d568aaa93 --- /dev/null +++ b/tests/phpunit/tests/functions/humanReadableDuration.php @@ -0,0 +1,90 @@ +assertSame( $expected, human_readable_duration( $input ) ); + } + + /** + * Data provider for test_human_readable_duration(). + * + * @return array { + * @type array { + * @type string $input Duration. + * @type string $expect Expected human readable duration. + * } + * } + */ + public function data_human_readable_duration() { + return array( + // 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' ), + array( '30:00', '30 minutes, 0 seconds' ), + 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 ), + array( 'Batman Begins !', false ), + array( '', false ), + array( '-1', false ), + array( -1, false ), + array( 0, false ), + array( 1, false ), + array( '00', false ), + 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. + ); + } +} diff --git a/tests/phpunit/tests/date/mysql2date.php b/tests/phpunit/tests/functions/mysql2date.php similarity index 100% rename from tests/phpunit/tests/date/mysql2date.php rename to tests/phpunit/tests/functions/mysql2date.php diff --git a/tests/phpunit/tests/functions/mysqlToRfc3339.php b/tests/phpunit/tests/functions/mysqlToRfc3339.php new file mode 100644 index 0000000000000..6c71d5991f047 --- /dev/null +++ b/tests/phpunit/tests/functions/mysqlToRfc3339.php @@ -0,0 +1,31 @@ +assertIsString( $date_return, 'The date return must be a string' ); + $this->assertNotEmpty( $date_return, 'The date return could not be an empty string' ); + $this->assertSame( $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' ); + } + + public function data_mysql_to_rfc3339() { + 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' ), + array( '2016-03-02T19:13:00', '16-03-02 19:13' ), + ); + } +} diff --git a/tests/phpunit/tests/functions/pathIsAbsolute.php b/tests/phpunit/tests/functions/pathIsAbsolute.php new file mode 100644 index 0000000000000..edd0acd2cc393 --- /dev/null +++ b/tests/phpunit/tests/functions/pathIsAbsolute.php @@ -0,0 +1,47 @@ +assertTrue( path_is_absolute( $path ), "path_is_absolute('$path') should return true" ); + } + } + + public function test_path_is_not_absolute() { + $relative_paths = array( + '', + '.', + '..', + '../foo', + '../', + '../foo.bar', + 'foo/bar', + 'foo', + 'FOO', + '..\\WINDOWS', + ); + foreach ( $relative_paths as $path ) { + $this->assertFalse( path_is_absolute( $path ), "path_is_absolute('$path') should return false" ); + } + } +} diff --git a/tests/phpunit/tests/functions/pathJoin.php b/tests/phpunit/tests/functions/pathJoin.php new file mode 100644 index 0000000000000..406e36919b88f --- /dev/null +++ b/tests/phpunit/tests/functions/pathJoin.php @@ -0,0 +1,73 @@ +assertSame( $expected, path_join( $base, $path ) ); + } + + /** + * Data provider for test_path_join(). + * + * @return string[][] + */ + public function data_path_join() { + return array( + // Absolute paths. + 'absolute path should return path' => array( + 'base' => 'base', + 'path' => '/path', + 'expected' => '/path', + ), + 'windows path with slashes' => array( + 'base' => 'base', + 'path' => '//path', + 'expected' => '//path', + ), + 'windows path with backslashes' => array( + 'base' => 'base', + 'path' => '\\\\path', + 'expected' => '\\\\path', + ), + // Non-absolute paths. + 'join base and path' => array( + 'base' => 'base', + 'path' => 'path', + 'expected' => 'base/path', + ), + 'strip trailing slashes in base' => array( + 'base' => 'base///', + 'path' => 'path', + 'expected' => 'base/path', + ), + 'empty path' => array( + 'base' => 'base', + 'path' => '', + 'expected' => 'base/', + ), + 'empty base' => array( + 'base' => '', + 'path' => 'path', + 'expected' => '/path', + ), + 'empty path and base' => array( + 'base' => '', + 'path' => '', + 'expected' => '/', + ), + ); + } +} diff --git a/tests/phpunit/tests/functions/validateFile.php b/tests/phpunit/tests/functions/validateFile.php new file mode 100644 index 0000000000000..8c634563ef6cc --- /dev/null +++ b/tests/phpunit/tests/functions/validateFile.php @@ -0,0 +1,161 @@ +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_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, + ), + + // Windows Path with allowed file + array( + 'Apache24\htdocs\wordpress/wp-content/themes/twentyten/style.css', + array( 'Apache24\htdocs\wordpress/wp-content/themes/twentyten/style.css' ), + 0, + ), + + // Disallowed files: + array( + 'foo.ext', + array( 'bar.ext' ), + 3, + ), + array( + 'foo.ext', + array( '.ext' ), + 3, + ), + array( + 'path/foo.ext', + array( 'foo.ext' ), + 3, + ), + + ); + } +} diff --git a/tests/phpunit/tests/functions/wpCheckFiletypeAndExt.php b/tests/phpunit/tests/functions/wpCheckFiletypeAndExt.php new file mode 100644 index 0000000000000..4b30be2891334 --- /dev/null +++ b/tests/phpunit/tests/functions/wpCheckFiletypeAndExt.php @@ -0,0 +1,258 @@ +assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); + } + + public function data_wp_check_filetype_and_ext() { + $data = array( + // Standard image. + array( + DIR_TESTDATA . '/images/canola.jpg', + 'canola.jpg', + array( + 'ext' => 'jpg', + 'type' => 'image/jpeg', + 'proper_filename' => false, + ), + ), + // Image with wrong extension. + array( + DIR_TESTDATA . '/images/test-image-mime-jpg.png', + 'test-image-mime-jpg.png', + array( + 'ext' => 'jpg', + 'type' => 'image/jpeg', + 'proper_filename' => 'test-image-mime-jpg.jpg', + ), + ), + // Image without extension. + array( + DIR_TESTDATA . '/images/test-image-no-extension', + 'test-image-no-extension', + array( + 'ext' => false, + 'type' => false, + 'proper_filename' => false, + ), + ), + // Valid non-image file with an image extension. + array( + DIR_TESTDATA . '/formatting/big5.txt', + 'big5.jpg', + array( + 'ext' => false, + 'type' => false, + 'proper_filename' => false, + ), + ), + // Non-image file not allowed. + array( + DIR_TESTDATA . '/export/crazy-cdata.xml', + 'crazy-cdata.xml', + array( + 'ext' => false, + 'type' => false, + 'proper_filename' => false, + ), + ), + // 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, + ), + ), + // 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, + ), + ), + ); + + // Test a few additional file types on single sites. + if ( ! is_multisite() ) { + $data = array_merge( + $data, + array( + // Standard non-image file. + array( + DIR_TESTDATA . '/formatting/big5.txt', + 'big5.txt', + array( + 'ext' => 'txt', + 'type' => 'text/plain', + 'proper_filename' => false, + ), + ), + // Google Docs file for which finfo_file() returns a duplicate mime type. + array( + DIR_TESTDATA . '/uploads/double-mime-type.docx', + 'double-mime-type.docx', + array( + 'ext' => 'docx', + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'proper_filename' => false, + ), + ), + // Non-image file with wrong sub-type. + array( + DIR_TESTDATA . '/uploads/pages-to-word.docx', + 'pages-to-word.docx', + array( + 'ext' => 'docx', + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'proper_filename' => false, + ), + ), + // FLAC file. + array( + DIR_TESTDATA . '/uploads/small-audio.flac', + 'small-audio.flac', + array( + 'ext' => 'flac', + 'type' => 'audio/flac', + 'proper_filename' => false, + ), + ), + // 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, + ), + ), + ) + ); + } + + return $data; + } + + /** + * @ticket 39550 + * @group ms-excluded + * @requires extension fileinfo + */ + public function test_wp_check_filetype_and_ext_with_filtered_svg() { + $file = DIR_TESTDATA . '/uploads/video-play.svg'; + $filename = 'video-play.svg'; + + $expected = array( + 'ext' => 'svg', + 'type' => 'image/svg+xml', + 'proper_filename' => false, + ); + + add_filter( + 'upload_mimes', + static function ( $mimes ) { + $mimes['svg'] = 'image/svg+xml'; + return $mimes; + } + ); + + $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); + } + + /** + * @ticket 39550 + * @group ms-excluded + * @requires extension fileinfo + */ + public function test_wp_check_filetype_and_ext_with_filtered_woff() { + $file = DIR_TESTDATA . '/uploads/dashicons.woff'; + $filename = 'dashicons.woff'; + + $woff_mime_type = 'application/font-woff'; + + /* + * As of PHP 8.1.12, which includes libmagic/file update to version 5.42, + * the expected mime type for WOFF files is 'font/woff'. + * + * See https://github.com/php/php-src/issues/8805. + */ + if ( PHP_VERSION_ID >= 80112 ) { + $woff_mime_type = 'font/woff'; + } + + $expected = array( + 'ext' => 'woff', + 'type' => $woff_mime_type, + 'proper_filename' => false, + ); + + add_filter( + 'upload_mimes', + static function ( $mimes ) use ( $woff_mime_type ) { + $mimes['woff'] = $woff_mime_type; + return $mimes; + } + ); + + $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); + } +} diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/functions/wpDate.php similarity index 100% rename from tests/phpunit/tests/date/wpDate.php rename to tests/phpunit/tests/functions/wpDate.php diff --git a/tests/phpunit/tests/functions/wpExt2Type.php b/tests/phpunit/tests/functions/wpExt2Type.php new file mode 100644 index 0000000000000..d6de698f449a8 --- /dev/null +++ b/tests/phpunit/tests/functions/wpExt2Type.php @@ -0,0 +1,26 @@ +assertNotEmpty( $extensions ); + + foreach ( $extensions as $type => $extension_list ) { + foreach ( $extension_list as $extension ) { + $this->assertSame( $type, wp_ext2type( $extension ) ); + $this->assertSame( $type, wp_ext2type( strtoupper( $extension ) ) ); + } + } + + $this->assertNull( wp_ext2type( 'unknown_format' ) ); + } +} diff --git a/tests/phpunit/tests/functions/wpExtractUrls.php b/tests/phpunit/tests/functions/wpExtractUrls.php new file mode 100644 index 0000000000000..9e36ea42cd578 --- /dev/null +++ b/tests/phpunit/tests/functions/wpExtractUrls.php @@ -0,0 +1,210 @@ +assertNotEmpty( $urls ); + $this->assertIsArray( $urls ); + $this->assertCount( count( $original_urls ), $urls ); + $this->assertSame( $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->assertSame( $decoded, $urls ); + $this->assertSame( $original_urls, $decoded ); + + $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor + incididunt ut labore http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html et dolore magna aliqua. + Ut http://this.com enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco http://127.0.0.1 + laboris nisi ut aliquip ex http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&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 + velit esse http://wordpress-core.com:8080/ cillum dolore eu fugiat nulla http://www.website.com:5000 pariatur. Excepteur sint occaecat cupidatat non proident, + sunt in culpa qui officia deserunt mollit http://wordpress-core/?346236346326&2134362574863.437 anim id est laborum.'; + + $urls = wp_extract_urls( $blob ); + $this->assertNotEmpty( $urls ); + $this->assertIsArray( $urls ); + $this->assertCount( 8, $urls ); + $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls ); + + $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor + incididunt ut labore 343462^ et dolore magna aliqua. + Ut &3640i6p1yi499 enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco localhost + laboris nisi ut aliquip ex 343462^ ea + commodo consequat. 343462^ Duis aute irure dolor in reprehenderit in voluptate + velit esse -3-4--321-64-4@#!$^$!@^@^ cillum dolore eu http://www.website.com:5000 fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, + sunt in culpa qui officia deserunt mollit )(*&^%$ anim id est laborum.'; + + $urls = wp_extract_urls( $blob ); + $this->assertNotEmpty( $urls ); + $this->assertIsArray( $urls ); + $this->assertCount( 8, $urls ); + $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls ); + } + + /** + * Tests for backward compatibility of `wp_extract_urls` to remove unused semicolons. + * + * @ticket 30580 + * + * @covers ::wp_extract_urls + */ + public function test_wp_extract_urls_remove_semicolon() { + $expected = array( + 'http://typo.com', + 'http://example.org/?post_type=post&p=8', + ); + $actual = wp_extract_urls( + ' + http://typo.com; + http://example.org/?post_type=;p;o;s;t;&p=8; + ' + ); + + $this->assertSame( $expected, $actual ); + } +} diff --git a/tests/phpunit/tests/functions/wpGetDefaultExtensionForMimeType.php b/tests/phpunit/tests/functions/wpGetDefaultExtensionForMimeType.php new file mode 100644 index 0000000000000..88e2549ad27bd --- /dev/null +++ b/tests/phpunit/tests/functions/wpGetDefaultExtensionForMimeType.php @@ -0,0 +1,22 @@ +assertSame( 'jpg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' ); + $this->assertNotEquals( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' ); + $this->assertSame( 'png', wp_get_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' ); + $this->assertFalse( wp_get_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' ); + $this->assertFalse( wp_get_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' ); + $this->assertFalse( wp_get_default_extension_for_mime_type( ' ' ), 'false not returned when empty string as mime type supplied' ); + $this->assertFalse( wp_get_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' ); + $this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' ); + } +} diff --git a/tests/phpunit/tests/functions/wpGetExtTypes.php b/tests/phpunit/tests/functions/wpGetExtTypes.php new file mode 100644 index 0000000000000..fd0d2d1f41123 --- /dev/null +++ b/tests/phpunit/tests/functions/wpGetExtTypes.php @@ -0,0 +1,27 @@ +assertIsArray( $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->assertIsArray( $extensions ); + $this->assertNotEmpty( $extensions ); + } +} diff --git a/tests/phpunit/tests/http/wpGetHttpHeaders.php b/tests/phpunit/tests/functions/wpGetHttpHeaders.php similarity index 100% rename from tests/phpunit/tests/http/wpGetHttpHeaders.php rename to tests/phpunit/tests/functions/wpGetHttpHeaders.php diff --git a/tests/phpunit/tests/functions/wpGetImageMime.php b/tests/phpunit/tests/functions/wpGetImageMime.php new file mode 100644 index 0000000000000..ab403f48e8bb4 --- /dev/null +++ b/tests/phpunit/tests/functions/wpGetImageMime.php @@ -0,0 +1,109 @@ +markTestSkipped( 'The exif PHP extension is not loaded.' ); + } + + if ( is_array( $expected ) ) { + $this->assertContains( wp_get_image_mime( $file ), $expected ); + } else { + $this->assertSame( $expected, wp_get_image_mime( $file ) ); + } + } + + /** + * Data provider for test_wp_get_image_mime(). + * + * @return array[] + */ + public function data_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', + ), + // Animated WebP. + array( + DIR_TESTDATA . '/images/webp-animated.webp', + 'image/webp', + ), + // Lossless WebP. + array( + DIR_TESTDATA . '/images/webp-lossless.webp', + 'image/webp', + ), + // Lossy WebP. + array( + DIR_TESTDATA . '/images/webp-lossy.webp', + 'image/webp', + ), + // Transparent WebP. + array( + DIR_TESTDATA . '/images/webp-transparent.webp', + 'image/webp', + ), + // Not an image. + array( + DIR_TESTDATA . '/uploads/dashicons.woff', + false, + ), + // Animated AVIF. + array( + DIR_TESTDATA . '/images/avif-animated.avif', + 'image/avif', + ), + // Lossless AVIF. + array( + DIR_TESTDATA . '/images/avif-lossless.avif', + 'image/avif', + ), + // Lossy AVIF. + array( + DIR_TESTDATA . '/images/avif-lossy.avif', + 'image/avif', + ), + // Transparent AVIF. + array( + DIR_TESTDATA . '/images/avif-transparent.avif', + 'image/avif', + ), + // HEIC. + array( + DIR_TESTDATA . '/images/test-image.heic', + // In PHP 8.5, it returns 'image/heif'. Before that, it returns 'image/heic'. + array( 'image/heic', 'image/heif' ), + ), + ); + + return $data; + } +} diff --git a/tests/phpunit/tests/functions/wpGetImageSize.php b/tests/phpunit/tests/functions/wpGetImageSize.php new file mode 100644 index 0000000000000..eb48638df8f30 --- /dev/null +++ b/tests/phpunit/tests/functions/wpGetImageSize.php @@ -0,0 +1,234 @@ +markTestSkipped( 'The exif PHP extension is not loaded.' ); + } + + $result = wp_getimagesize( $file ); + + // The getimagesize() function varies in its response, so + // let's restrict comparison to expected keys only. + if ( is_array( $expected ) ) { + foreach ( $expected as $k => $v ) { + $this->assertArrayHasKey( $k, $result ); + $this->assertSame( $expected[ $k ], $result[ $k ] ); + } + } else { + $this->assertSame( $expected, $result ); + } + } + + /** + * Data provider for test_wp_getimagesize(). + */ + public function data_wp_getimagesize() { + $data = array( + // Standard JPEG. + array( + DIR_TESTDATA . '/images/test-image.jpg', + array( + 50, + 50, + IMAGETYPE_JPEG, + 'width="50" height="50"', + 'mime' => 'image/jpeg', + ), + ), + // Standard GIF. + array( + DIR_TESTDATA . '/images/test-image.gif', + array( + 50, + 50, + IMAGETYPE_GIF, + 'width="50" height="50"', + 'mime' => 'image/gif', + ), + ), + // Standard PNG. + array( + DIR_TESTDATA . '/images/test-image.png', + array( + 50, + 50, + IMAGETYPE_PNG, + 'width="50" height="50"', + 'mime' => 'image/png', + ), + ), + // Image with wrong extension. + array( + DIR_TESTDATA . '/images/test-image-mime-jpg.png', + array( + 50, + 50, + IMAGETYPE_JPEG, + 'width="50" height="50"', + 'mime' => 'image/jpeg', + ), + ), + // Animated WebP. + array( + DIR_TESTDATA . '/images/webp-animated.webp', + array( + 100, + 100, + IMAGETYPE_WEBP, + 'width="100" height="100"', + 'mime' => 'image/webp', + ), + ), + // Lossless WebP. + array( + DIR_TESTDATA . '/images/webp-lossless.webp', + array( + 1200, + 675, + IMAGETYPE_WEBP, + 'width="1200" height="675"', + 'mime' => 'image/webp', + ), + ), + // Lossy WebP. + array( + DIR_TESTDATA . '/images/webp-lossy.webp', + array( + 1200, + 675, + IMAGETYPE_WEBP, + 'width="1200" height="675"', + 'mime' => 'image/webp', + ), + ), + // Transparent WebP. + array( + DIR_TESTDATA . '/images/webp-transparent.webp', + array( + 1200, + 675, + IMAGETYPE_WEBP, + 'width="1200" height="675"', + 'mime' => 'image/webp', + ), + ), + // Not an image. + array( + DIR_TESTDATA . '/uploads/dashicons.woff', + false, + ), + // Animated AVIF. + array( + DIR_TESTDATA . '/images/avif-animated.avif', + array( + 150, + 150, + IMAGETYPE_AVIF, + 'width="150" height="150"', + 'mime' => 'image/avif', + ), + ), + // Lossless AVIF. + array( + DIR_TESTDATA . '/images/avif-lossless.avif', + array( + 400, + 400, + IMAGETYPE_AVIF, + 'width="400" height="400"', + 'mime' => 'image/avif', + ), + ), + // Lossy AVIF. + array( + DIR_TESTDATA . '/images/avif-lossy.avif', + array( + 400, + 400, + IMAGETYPE_AVIF, + 'width="400" height="400"', + 'mime' => 'image/avif', + ), + ), + // Transparent AVIF. + array( + DIR_TESTDATA . '/images/avif-transparent.avif', + array( + 128, + 128, + IMAGETYPE_AVIF, + 'width="128" height="128"', + 'mime' => 'image/avif', + ), + ), + // Grid AVIF. + array( + DIR_TESTDATA . '/images/avif-alpha-grid2x1.avif', + array( + 199, + 200, + IMAGETYPE_AVIF, + 'width="199" height="200"', + 'mime' => 'image/avif', + ), + ), + ); + + return $data; + } + + /** + * Tests that wp_getimagesize() correctly handles HEIC image files. + * + * @ticket 53645 + */ + public function test_wp_getimagesize_heic() { + if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) { + $this->markTestSkipped( 'The exif PHP extension is not loaded.' ); + } + + $file = DIR_TESTDATA . '/images/test-image.heic'; + + $editor = wp_get_image_editor( $file ); + if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/heic' ) ) { + $this->markTestSkipped( 'No HEIC support in the editor engine on this system.' ); + } + + $expected = array( + 1180, + 1180, + IMAGETYPE_HEIF, + 'width="1180" height="1180"', + ); + + // As of PHP 8.5.0, getimagesize() supports HEIF/HEIC files. + if ( PHP_VERSION_ID >= 80500 ) { + $expected = array_merge( + $expected, + array( + 'bits' => 8, + 'channels' => 3, + 'mime' => 'image/heif', + 'width_unit' => 'px', + 'height_unit' => 'px', + ) + ); + } else { + $expected['mime'] = 'image/heic'; + } + + $result = wp_getimagesize( $file ); + $this->assertSame( $expected, $result ); + } +} diff --git a/tests/phpunit/tests/functions/wpGetMimeTypes.php b/tests/phpunit/tests/functions/wpGetMimeTypes.php index 8bcd0aed8a9dc..7e69b35861f89 100644 --- a/tests/phpunit/tests/functions/wpGetMimeTypes.php +++ b/tests/phpunit/tests/functions/wpGetMimeTypes.php @@ -1,32 +1,40 @@ assertIsArray( $mime_types_start ); - $this->assertNotEmpty( $mime_types_start ); + $this->assertIsArray( $mimes ); + $this->assertNotEmpty( $mimes ); add_filter( 'mime_types', '__return_empty_array' ); - $mime_types_empty = wp_get_mime_types(); - $this->assertSame( array(), $mime_types_empty ); + $mimes = wp_get_mime_types(); + $this->assertIsArray( $mimes ); + $this->assertEmpty( $mimes ); remove_filter( 'mime_types', '__return_empty_array' ); - $mime_types = wp_get_mime_types(); - $this->assertIsArray( $mime_types ); - $this->assertNotEmpty( $mime_types ); - // Did it revert to the original after filter remove? - $this->assertSame( $mime_types_start, $mime_types ); + $mimes = wp_get_mime_types(); + $this->assertIsArray( $mimes ); + $this->assertNotEmpty( $mimes ); + + // 'upload_mimes' should not affect wp_get_mime_types(). + add_filter( 'upload_mimes', '__return_empty_array' ); + $mimes = wp_get_mime_types(); + $this->assertIsArray( $mimes ); + $this->assertNotEmpty( $mimes ); + + remove_filter( 'upload_mimes', '__return_empty_array' ); + $mimes2 = wp_get_mime_types(); + $this->assertIsArray( $mimes2 ); + $this->assertNotEmpty( $mimes2 ); + $this->assertSame( $mimes2, $mimes ); } } diff --git a/tests/phpunit/tests/functions/wpIsJsonMediaType.php b/tests/phpunit/tests/functions/wpIsJsonMediaType.php new file mode 100644 index 0000000000000..aeda89167706c --- /dev/null +++ b/tests/phpunit/tests/functions/wpIsJsonMediaType.php @@ -0,0 +1,31 @@ +assertSame( $expected, wp_is_json_media_type( $input ) ); + } + + + public function data_wp_is_json_media_type() { + return array( + array( 'application/ld+json', true ), + array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', true ), + array( 'application/activity+json', true ), + array( 'application/json+oembed', true ), + array( 'application/json', true ), + array( 'application/nojson', false ), + array( 'application/no.json', false ), + array( 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8', false ), + array( 'application/activity+json, application/nojson', true ), + ); + } +} diff --git a/tests/phpunit/tests/functions/wpIsStream.php b/tests/phpunit/tests/functions/wpIsStream.php new file mode 100644 index 0000000000000..9e8699bec0836 --- /dev/null +++ b/tests/phpunit/tests/functions/wpIsStream.php @@ -0,0 +1,51 @@ +markTestSkipped( 'The openssl PHP extension is not loaded.' ); + } + + $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_wp_is_stream() { + return array( + // Legitimate stream examples. + array( 'http://example.com', true ), + 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 ), + ); + } +} diff --git a/tests/phpunit/tests/functions/wpIsUuid.php b/tests/phpunit/tests/functions/wpIsUuid.php new file mode 100644 index 0000000000000..1dff89f161d11 --- /dev/null +++ b/tests/phpunit/tests/functions/wpIsUuid.php @@ -0,0 +1,58 @@ +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 ) ); + } + } +} diff --git a/tests/phpunit/tests/functions/wpJsonEncode.php b/tests/phpunit/tests/functions/wpJsonEncode.php new file mode 100644 index 0000000000000..8d91a212b07bc --- /dev/null +++ b/tests/phpunit/tests/functions/wpJsonEncode.php @@ -0,0 +1,96 @@ +assertSame( wp_json_encode( 'a' ), '"a"' ); + } + + /** + * @ticket 28786 + */ + public function test_wp_json_encode_utf8() { + $this->assertSame( wp_json_encode( '这' ), '"\u8fd9"' ); + } + + /** + * @ticket 28786 + * @requires function mb_detect_order + */ + public function test_wp_json_encode_non_utf8() { + $charsets = mb_detect_order(); + $old_charsets = $charsets; + if ( ! in_array( 'EUC-JP', $charsets, true ) ) { + $charsets[] = 'EUC-JP'; + mb_detect_order( $charsets ); + } + + $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); + $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); + + $this->assertSame( 'aあb', $utf8 ); + + $this->assertSame( '"a\u3042b"', wp_json_encode( $eucjp ) ); + + mb_detect_order( $old_charsets ); + } + + /** + * @ticket 28786 + * @requires function mb_detect_order + */ + public function test_wp_json_encode_non_utf8_in_array() { + $charsets = mb_detect_order(); + $old_charsets = $charsets; + if ( ! in_array( 'EUC-JP', $charsets, true ) ) { + $charsets[] = 'EUC-JP'; + mb_detect_order( $charsets ); + } + + $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); + $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); + + $this->assertSame( 'aあb', $utf8 ); + + $this->assertSame( '["c","a\u3042b"]', wp_json_encode( array( 'c', $eucjp ) ) ); + + mb_detect_order( $old_charsets ); + } + + /** + * @ticket 28786 + */ + public function test_wp_json_encode_array() { + $this->assertSame( wp_json_encode( array( 'a' ) ), '["a"]' ); + } + + /** + * @ticket 28786 + */ + public function test_wp_json_encode_object() { + $object = new stdClass(); + $object->a = 'b'; + $this->assertSame( wp_json_encode( $object ), '{"a":"b"}' ); + } + + /** + * @ticket 28786 + */ + public 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 ); + } +} diff --git a/tests/phpunit/tests/functions/wpJsonFileDecode.php b/tests/phpunit/tests/functions/wpJsonFileDecode.php new file mode 100644 index 0000000000000..ce265ed3fd557 --- /dev/null +++ b/tests/phpunit/tests/functions/wpJsonFileDecode.php @@ -0,0 +1,33 @@ +assertIsObject( $result ); + $this->assertSame( 'tests/notice', $result->name ); + } + + /** + * @ticket 53238 + */ + public function test_wp_json_file_decode_associative_array() { + $result = wp_json_file_decode( + DIR_TESTDATA . '/blocks/notice/block.json', + array( 'associative' => true ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( 'tests/notice', $result['name'] ); + } +} diff --git a/tests/phpunit/tests/functions/wpNormalizePath.php b/tests/phpunit/tests/functions/wpNormalizePath.php new file mode 100644 index 0000000000000..1b106f02e3531 --- /dev/null +++ b/tests/phpunit/tests/functions/wpNormalizePath.php @@ -0,0 +1,102 @@ +assertSame( $expected, wp_normalize_path( $path ) ); + } + + public 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' ), + + // Linux paths. + array( '/www/path/', '/www/path/' ), + array( '/www/path/////', '/www/path/' ), + array( '/www/path', '/www/path' ), + + // 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/' ), + + // Edge cases. + array( '', '' ), // Empty string should return empty string. + array( 123, '123' ), // Integer should be cast to string. + ); + } + + /** + * Tests that wp_normalize_path() works with objects that have __toString(). + * + * This is important because the function uses a static cache, and the input + * must be cast to string before being used as an array key. + * + * @ticket 64538 + */ + public function test_wp_normalize_path_with_stringable_object() { + $file_info = new SplFileInfo( '/var/www/html\\test' ); + + $this->assertSame( '/var/www/html/test', wp_normalize_path( $file_info ) ); + } + + /** + * Tests that wp_normalize_path() returns consistent results on repeated calls. + * + * The function uses a static cache, so this verifies cache behavior. + * + * @ticket 64538 + */ + public function test_wp_normalize_path_returns_consistent_results() { + $path = 'C:\\www\\path\\'; + + $first_call = wp_normalize_path( $path ); + $second_call = wp_normalize_path( $path ); + $third_call = wp_normalize_path( $path ); + + $this->assertSame( $first_call, $second_call, 'Second call should return same result as first.' ); + $this->assertSame( $second_call, $third_call, 'Third call should return same result as second.' ); + $this->assertSame( 'C:/www/path/', $first_call, 'Normalized path should match expected value.' ); + } + + /** + * Tests that wp_normalize_path() static cache stores results. + * + * @ticket 64538 + */ + public function test_wp_normalize_path_static_cache() { + $path = '/var/www/cache-test\\subdir\\'; + $expected = '/var/www/cache-test/subdir/'; + + $result = wp_normalize_path( $path ); + $this->assertSame( $expected, $result ); + + $reflection = new ReflectionFunction( 'wp_normalize_path' ); + $static_vars = $reflection->getStaticVariables(); + + $this->assertArrayHasKey( 'cache', $static_vars, 'Static cache array should exist.' ); + $this->assertArrayHasKey( $path, $static_vars['cache'], 'Cache should contain the normalized path.' ); + $this->assertSame( $expected, $static_vars['cache'][ $path ], 'Cached value should match the expected normalized path.' ); + } +} diff --git a/tests/phpunit/tests/functions/wpParseArgs.php b/tests/phpunit/tests/functions/wpParseArgs.php new file mode 100644 index 0000000000000..1f29208c37c1a --- /dev/null +++ b/tests/phpunit/tests/functions/wpParseArgs.php @@ -0,0 +1,88 @@ +_baba = 5; + $x->yZ = 'baba'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $x->a = array( 5, 111, 'x' ); + $this->assertSame( + array( + '_baba' => 5, + 'yZ' => 'baba', + 'a' => array( 5, 111, 'x' ), + ), + wp_parse_args( $x ) + ); + $y = new MockClass(); + $this->assertSame( array(), wp_parse_args( $y ) ); + } + + public function test_wp_parse_args_array() { + // Arrays. + $a = array(); + $this->assertSame( array(), wp_parse_args( $a ) ); + $b = array( + '_baba' => 5, + 'yZ' => 'baba', + 'a' => array( 5, 111, 'x' ), + ); + $this->assertSame( + array( + '_baba' => 5, + 'yZ' => 'baba', + 'a' => array( 5, 111, 'x' ), + ), + wp_parse_args( $b ) + ); + } + + public function test_wp_parse_args_defaults() { + $x = new MockClass(); + $x->_baba = 5; + $x->yZ = 'baba'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $x->a = array( 5, 111, 'x' ); + $d = array( 'pu' => 'bu' ); + $this->assertSame( + array( + 'pu' => 'bu', + '_baba' => 5, + 'yZ' => 'baba', + 'a' => array( 5, 111, 'x' ), + ), + wp_parse_args( $x, $d ) + ); + $e = array( '_baba' => 6 ); + $this->assertSame( + array( + '_baba' => 5, + 'yZ' => 'baba', + 'a' => array( 5, 111, 'x' ), + ), + wp_parse_args( $x, $e ) + ); + } + + public function test_wp_parse_args_other() { + $b = true; + wp_parse_str( $b, $s ); + $this->assertSame( $s, wp_parse_args( $b ) ); + $q = 'x=5&_baba=dudu&'; + wp_parse_str( $q, $ss ); + $this->assertSame( $ss, wp_parse_args( $q ) ); + } + + /** + * @ticket 30753 + */ + public function test_wp_parse_args_boolean_strings() { + $args = wp_parse_args( 'foo=false&bar=true' ); + $this->assertIsString( $args['foo'] ); + $this->assertIsString( $args['bar'] ); + } +} diff --git a/tests/phpunit/tests/functions/wpRaiseMemoryLimit.php b/tests/phpunit/tests/functions/wpRaiseMemoryLimit.php new file mode 100644 index 0000000000000..4bb9dcc016e2c --- /dev/null +++ b/tests/phpunit/tests/functions/wpRaiseMemoryLimit.php @@ -0,0 +1,30 @@ +markTestSkipped( 'WP_MAX_MEMORY_LIMIT should be set to -1.' ); + } + + $ini_limit_before = ini_get( 'memory_limit' ); + $raised_limit = wp_raise_memory_limit(); + $ini_limit_after = ini_get( 'memory_limit' ); + + $this->assertSame( $ini_limit_before, $ini_limit_after ); + $this->assertFalse( $raised_limit ); + $this->assertEquals( WP_MAX_MEMORY_LIMIT, $ini_limit_after ); + } +} diff --git a/tests/phpunit/tests/functions/wpRecursiveKsort.php b/tests/phpunit/tests/functions/wpRecursiveKsort.php new file mode 100644 index 0000000000000..278199dd70196 --- /dev/null +++ b/tests/phpunit/tests/functions/wpRecursiveKsort.php @@ -0,0 +1,95 @@ + 1, + 'settings' => array( + 'typography' => array( + 'fontFamilies' => array( + 'fontFamily' => 'DM Sans, sans-serif', + 'slug' => 'dm-sans', + 'name' => 'DM Sans', + ), + ), + 'color' => array( + 'palette' => array( + array( + 'slug' => 'foreground', + 'color' => '#242321', + 'name' => 'Foreground', + ), + array( + 'slug' => 'background', + 'color' => '#FCFBF8', + 'name' => 'Background', + ), + array( + 'slug' => 'primary', + 'color' => '#71706E', + 'name' => 'Primary', + ), + array( + 'slug' => 'tertiary', + 'color' => '#CFCFCF', + 'name' => 'Tertiary', + ), + ), + ), + ), + ); + + // Sort the array. + wp_recursive_ksort( $theme_json ); + + // Expected result. + $expected_theme_json = array( + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'color' => '#242321', + 'name' => 'Foreground', + 'slug' => 'foreground', + ), + array( + 'color' => '#FCFBF8', + 'name' => 'Background', + 'slug' => 'background', + ), + array( + 'color' => '#71706E', + 'name' => 'Primary', + 'slug' => 'primary', + ), + array( + 'color' => '#CFCFCF', + 'name' => 'Tertiary', + 'slug' => 'tertiary', + ), + ), + ), + 'typography' => array( + 'fontFamilies' => array( + 'fontFamily' => 'DM Sans, sans-serif', + 'name' => 'DM Sans', + 'slug' => 'dm-sans', + ), + ), + ), + 'version' => 1, + ); + $this->assertSameSetsWithIndex( $theme_json, $expected_theme_json ); + } +} diff --git a/tests/phpunit/tests/date/wpTimezone.php b/tests/phpunit/tests/functions/wpTimezone.php similarity index 90% rename from tests/phpunit/tests/date/wpTimezone.php rename to tests/phpunit/tests/functions/wpTimezone.php index d4e7e7e220fdd..17e27e0fe4462 100644 --- a/tests/phpunit/tests/date/wpTimezone.php +++ b/tests/phpunit/tests/functions/wpTimezone.php @@ -4,7 +4,6 @@ * @group date * @group datetime * - * @covers ::wp_timezone_string * @covers ::wp_timezone */ class Tests_Date_wpTimezone extends WP_UnitTestCase { @@ -32,8 +31,6 @@ public function test_should_convert_gmt_offset( $gmt_offset, $tz_name ) { delete_option( 'timezone_string' ); update_option( 'gmt_offset', $gmt_offset ); - $this->assertSame( $tz_name, wp_timezone_string() ); - $timezone = wp_timezone(); $this->assertSame( $tz_name, $timezone->getName() ); @@ -110,11 +107,9 @@ public function data_should_convert_gmt_offset() { /** * @ticket 24730 */ - public function test_should_return_timezone_string() { + public function test_should_return_timezone_object() { update_option( 'timezone_string', 'Europe/Helsinki' ); - $this->assertSame( 'Europe/Helsinki', wp_timezone_string() ); - $timezone = wp_timezone(); $this->assertSame( 'Europe/Helsinki', $timezone->getName() ); @@ -125,12 +120,10 @@ public function test_should_return_timezone_string() { * * @ticket 56468 */ - public function test_should_return_deprecated_timezone_string() { + public function test_should_return_deprecated_timezone_object() { $tz_string = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6. update_option( 'timezone_string', $tz_string ); - $this->assertSame( $tz_string, wp_timezone_string() ); - $timezone = wp_timezone(); $this->assertSame( $tz_string, $timezone->getName() ); diff --git a/tests/phpunit/tests/functions/wpUniqueFilename.php b/tests/phpunit/tests/functions/wpUniqueFilename.php new file mode 100644 index 0000000000000..d638006af9d13 --- /dev/null +++ b/tests/phpunit/tests/functions/wpUniqueFilename.php @@ -0,0 +1,193 @@ +assertSame( $expected, wp_unique_filename( $testdir, $filename ), $message ); + } + + /** + * Data provider for test_wp_unique_filename. + * + * @return array[] + */ + public function data_wp_unique_filename() { + return array( + 'non-existing file' => array( + 'filename' => 'abcdefg.png', + 'expected' => 'abcdefg.png', + 'message' => 'Test non-existing file, file name should be unchanged.', + ), + 'file already exists' => array( + 'filename' => 'test-image.png', + 'expected' => 'test-image-1.png', + 'message' => 'File name not unique, number not appended.', + ), + 'uppercase extension' => array( + 'filename' => 'test-image.PNG', + 'expected' => 'test-image-1.png', + 'message' => 'File name with uppercase extension not unique, number not appended.', + ), + 'already added number' => array( + 'filename' => 'test-image-2.gif', + 'expected' => 'test-image-2-1.gif', + 'message' => 'File name not unique, number not appended correctly.', + ), + 'special chars' => array( + 'filename' => 'testtést-imagé.png', + 'expected' => 'testtest-image.png', + 'message' => 'Filename with special chars failed', + ), + 'special chars with potential conflict' => array( + 'filename' => 'tést-imagé.png', + 'expected' => 'test-image-1.png', + 'message' => 'Filename with special chars failed', + ), + 'single quotes in name' => array( + 'filename' => "abcdefg'h.png", + 'expected' => 'abcdefgh.png', + 'message' => 'File with quote failed', + ), + 'double quotes in name' => array( + 'filename' => 'abcdefg"h.png', + 'expected' => 'abcdefgh.png', + 'message' => 'File with quote failed', + ), + 'crazy name' => array( + 'filename' => '12%af34567890#~!@#$..%^&*()|_+qwerty fgh`jkl zx<>?:"{}[]="\'/?.png', + 'expected' => '12af34567890@.^_qwerty-fghjkl-zx.png', + 'message' => 'Failed crazy file name', + ), + 'single slash' => array( + 'filename' => 'abcde\fg.png', + 'expected' => 'abcdefg.png', + 'message' => 'Slash not removed', + ), + 'double slash' => array( + 'filename' => 'abcde\\fg.png', + 'expected' => 'abcdefg.png', + 'message' => 'Double slashed not removed', + ), + 'triple slash' => array( + 'filename' => 'abcde\\\fg.png', + 'expected' => 'abcdefg.png', + 'message' => 'Triple slashed not removed', + ), + ); + } + + /** + * @ticket 42437 + */ + public function test_unique_filename_with_dimension_like_filename() { + $testdir = DIR_TESTDATA . '/images/'; + + add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); + + // Test collision with "dimension-like" original filename. + $this->assertSame( 'one-blue-pixel-100x100-1.png', wp_unique_filename( $testdir, 'one-blue-pixel-100x100.png' ) ); + // Test collision with existing sub-size filename. + // Existing files: one-blue-pixel-100x100.png, one-blue-pixel-1-100x100.png. + $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.png' ) ); + // Same as above with upper case extension. + $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.PNG' ) ); + + remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); + } + + // Callback to patch "basedir" when used in `wp_unique_filename()`. + public function upload_dir_patch_basedir( $upload_dir ) { + $upload_dir['basedir'] = DIR_TESTDATA . '/images/'; + return $upload_dir; + } + + /** + * @ticket 53668 + */ + public function test_wp_unique_filename_with_additional_image_extension() { + $testdir = DIR_TESTDATA . '/images/'; + + add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); + + // Set conversions for uploaded images. + add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); + + // Ensure the test images exist. + $this->assertFileExists( $testdir . 'test-image-1-100x100.jpg', 'test-image-1-100x100.jpg does not exist' ); + $this->assertFileExists( $testdir . 'test-image-2.gif', 'test-image-2.gif does not exist' ); + $this->assertFileExists( $testdir . 'test-image-3.jpg', 'test-image-3.jpg does not exist' ); + $this->assertFileExists( $testdir . 'test-image-4.png', 'test-image-4.png does not exist' ); + + // Standard test: file does not exist and there are no possible intersections with other files. + $this->assertSame( + 'abcdef.png', + wp_unique_filename( $testdir, 'abcdef.png' ), + 'The abcdef.png, abcdef.gif, and abcdef.jpg images do not exist. The file name should not be changed.' + ); + + // Actual clash recognized. + $this->assertSame( + 'canola-1.jpg', + wp_unique_filename( $testdir, 'canola.jpg' ), + 'The canola.jpg image exists. The file name should be unique.' + ); + + // Same name with different uppercase extension and the image will be converted. + $this->assertSame( + 'canola-1.png', + wp_unique_filename( $testdir, 'canola.PNG' ), + 'The canola.jpg image exists. Uploading canola.PNG that will be converted to canola.jpg should produce unique file name.' + ); + + // Actual clash with several images with different extensions. + $this->assertSame( + 'test-image-5.png', + wp_unique_filename( $testdir, 'test-image.png' ), + 'The test-image.png, test-image-1-100x100.jpg, test-image-2.gif, test-image-3.jpg, and test-image-4.png images exist.' . + 'All of them may clash when creating sub-sizes or regenerating thumbnails in the future. The filename should be unique.' + ); + + // Possible clash with regenerated thumbnails in the future. + $this->assertSame( + 'codeispoetry-1.jpg', + wp_unique_filename( $testdir, 'codeispoetry.jpg' ), + 'The codeispoetry.png image exists. When regenerating thumbnails for it they will be converted to JPG.' . + 'The name of the newly uploaded codeispoetry.jpg should be made unique.' + ); + + remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); + remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); + } + + /** + * Changes the output format when editing images. When uploading a PNG file + * it will be converted to JPEG, GIF to JPEG, and PICT to BMP + * (if the image editor in PHP supports it). + * + * @param array $formats + * + * @return array + */ + public function image_editor_output_format_handler( $formats ) { + $formats['image/png'] = 'image/jpeg'; + $formats['image/gif'] = 'image/jpeg'; + $formats['image/pct'] = 'image/bmp'; + + return $formats; + } +} diff --git a/tests/phpunit/tests/functions/wpUniqueId.php b/tests/phpunit/tests/functions/wpUniqueId.php new file mode 100644 index 0000000000000..931ea82b136f3 --- /dev/null +++ b/tests/phpunit/tests/functions/wpUniqueId.php @@ -0,0 +1,36 @@ +assertIsString( $id ); + $this->assertIsNumeric( $id ); + $ids[] = $id; + } + $this->assertSame( $ids, array_unique( $ids ) ); + + // Test with prefix. + $ids = array(); + for ( $i = 0; $i < 20; $i += 1 ) { + $id = wp_unique_id( 'foo-' ); + $this->assertMatchesRegularExpression( '/^foo-\d+$/', $id ); + $ids[] = $id; + } + $this->assertSame( $ids, array_unique( $ids ) ); + } +} diff --git a/tests/phpunit/tests/functions/wpUploadDir.php b/tests/phpunit/tests/functions/wpUploadDir.php new file mode 100644 index 0000000000000..31a2f647ef4ed --- /dev/null +++ b/tests/phpunit/tests/functions/wpUploadDir.php @@ -0,0 +1,38 @@ +reset_options(); + } + + private function reset_options() { + // System defaults. + update_option( 'upload_path', 'wp-content/uploads' ); + update_option( 'upload_url_path', '' ); + update_option( 'uploads_use_yearmonth_folders', 1 ); + } + + /** + * @covers ::wp_upload_dir + */ + public function test_upload_dir_default() { + // wp_upload_dir() with default parameters. + $info = wp_upload_dir(); + $subdir = date_format( date_create( 'now' ), '/Y/m' ); + + $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] ); + $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); + $this->assertSame( $subdir, $info['subdir'] ); + $this->assertFalse( $info['error'] ); + } +} diff --git a/tests/phpunit/tests/upload.php b/tests/phpunit/tests/functions/wpUploadDirInternal.php similarity index 86% rename from tests/phpunit/tests/upload.php rename to tests/phpunit/tests/functions/wpUploadDirInternal.php index 46fcea7099097..822fcd580891b 100644 --- a/tests/phpunit/tests/upload.php +++ b/tests/phpunit/tests/functions/wpUploadDirInternal.php @@ -1,11 +1,14 @@ assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); - $this->assertSame( $subdir, $info['subdir'] ); - $this->assertFalse( $info['error'] ); - } - public function test_upload_dir_relative() { // wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'. update_option( 'upload_path', 'foo/bar' ); From 9dda338532bf1fcd11dc972338ff56d363abe2d1 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 24 Jun 2026 13:41:20 -0400 Subject: [PATCH 3/7] Add PHPUnit tests for `wp_generate_uuid4` --- .../tests/functions/wpGenerateUuid4.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpGenerateUuid4.php diff --git a/tests/phpunit/tests/functions/wpGenerateUuid4.php b/tests/phpunit/tests/functions/wpGenerateUuid4.php new file mode 100644 index 0000000000000..95569a020e6bb --- /dev/null +++ b/tests/phpunit/tests/functions/wpGenerateUuid4.php @@ -0,0 +1,27 @@ +assertTrue( wp_is_uuid( $uuid, 4 ) ); + $uuids[] = $uuid; + } + + $unique_uuids = array_unique( $uuids ); + $this->assertSame( $uuids, $unique_uuids ); + } +} From cd2f31744a2cdd1e09e27ab85f7789c4a1ed9949 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 24 Jun 2026 13:43:41 -0400 Subject: [PATCH 4/7] Update and align array key indentation in `wpUniqueFilename` PHPUnit tests --- .../tests/functions/wpUniqueFilename.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/tests/functions/wpUniqueFilename.php b/tests/phpunit/tests/functions/wpUniqueFilename.php index d638006af9d13..ed48f242ea805 100644 --- a/tests/phpunit/tests/functions/wpUniqueFilename.php +++ b/tests/phpunit/tests/functions/wpUniqueFilename.php @@ -28,27 +28,27 @@ public function test_wp_unique_filename( $filename, $expected, $message ) { */ public function data_wp_unique_filename() { return array( - 'non-existing file' => array( + 'non-existing file' => array( 'filename' => 'abcdefg.png', 'expected' => 'abcdefg.png', 'message' => 'Test non-existing file, file name should be unchanged.', ), - 'file already exists' => array( + 'file already exists' => array( 'filename' => 'test-image.png', 'expected' => 'test-image-1.png', 'message' => 'File name not unique, number not appended.', ), - 'uppercase extension' => array( + 'uppercase extension' => array( 'filename' => 'test-image.PNG', 'expected' => 'test-image-1.png', 'message' => 'File name with uppercase extension not unique, number not appended.', ), - 'already added number' => array( + 'already added number' => array( 'filename' => 'test-image-2.gif', 'expected' => 'test-image-2-1.gif', 'message' => 'File name not unique, number not appended correctly.', ), - 'special chars' => array( + 'special chars' => array( 'filename' => 'testtést-imagé.png', 'expected' => 'testtest-image.png', 'message' => 'Filename with special chars failed', @@ -58,32 +58,32 @@ public function data_wp_unique_filename() { 'expected' => 'test-image-1.png', 'message' => 'Filename with special chars failed', ), - 'single quotes in name' => array( + 'single quotes in name' => array( 'filename' => "abcdefg'h.png", 'expected' => 'abcdefgh.png', 'message' => 'File with quote failed', ), - 'double quotes in name' => array( + 'double quotes in name' => array( 'filename' => 'abcdefg"h.png', 'expected' => 'abcdefgh.png', 'message' => 'File with quote failed', ), - 'crazy name' => array( + 'crazy name' => array( 'filename' => '12%af34567890#~!@#$..%^&*()|_+qwerty fgh`jkl zx<>?:"{}[]="\'/?.png', 'expected' => '12af34567890@.^_qwerty-fghjkl-zx.png', 'message' => 'Failed crazy file name', ), - 'single slash' => array( + 'single slash' => array( 'filename' => 'abcde\fg.png', 'expected' => 'abcdefg.png', 'message' => 'Slash not removed', ), - 'double slash' => array( + 'double slash' => array( 'filename' => 'abcde\\fg.png', 'expected' => 'abcdefg.png', 'message' => 'Double slashed not removed', ), - 'triple slash' => array( + 'triple slash' => array( 'filename' => 'abcde\\\fg.png', 'expected' => 'abcdefg.png', 'message' => 'Triple slashed not removed', From 088b4dcc72fcecb47b5fd913da4f5920f656ac2c Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 6 Aug 2026 16:49:03 -0400 Subject: [PATCH 5/7] Delete tests/phpunit/tests/functions.php --- tests/phpunit/tests/functions.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/phpunit/tests/functions.php diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php deleted file mode 100644 index e69de29bb2d1d..0000000000000 From 72d67e67f3fd1b52b441dc6dde1f5e90274451b6 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 10 Sep 2026 09:29:09 -0400 Subject: [PATCH 6/7] Simplify `add_query_arg` PHPUnit tests by removing redundant array key declarations. Copied from https://github.com/pbearne/wordpress-develop/commit/8cb025ef4e1c55b6a4e9785046450f5d83ec3000 --- tests/phpunit/tests/functions/addQueryArg.php | 52 ++----------------- 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/tests/phpunit/tests/functions/addQueryArg.php b/tests/phpunit/tests/functions/addQueryArg.php index d6d808145de3a..640635213f743 100644 --- a/tests/phpunit/tests/functions/addQueryArg.php +++ b/tests/phpunit/tests/functions/addQueryArg.php @@ -57,15 +57,7 @@ public function test_add_query_arg() { $this->assertSame( "$url?foo=1", add_query_arg( 'foo', '1' ) ); $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) ); - $this->assertSame( - "$url?foo=2", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ) - ) - ); + $this->assertSame( "$url?foo=2", add_query_arg( array( 'foo' => '2' ), $url ) ); $this->assertSame( "$url?foo=1&bar=2", add_query_arg( @@ -83,16 +75,7 @@ public function test_add_query_arg() { $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) ); $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) ); - $this->assertSame( - "$url?foo=2#frag", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ), - $frag_url - ) - ); + $this->assertSame( "$url?foo=2#frag", add_query_arg( array( 'foo' => '2' ), $frag_url ) ); $this->assertSame( "$url?foo=1&bar=2#frag", add_query_arg( @@ -108,15 +91,7 @@ public function test_add_query_arg() { $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) ); $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) ); - $this->assertSame( - "$url?foo=2#frag", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ) - ) - ); + $this->assertSame( "$url?foo=2#frag", add_query_arg( array( 'foo' => '2' ) ) ); $this->assertSame( "$url?foo=1&bar=2#frag", add_query_arg( @@ -145,16 +120,7 @@ public function test_add_query_arg() { $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1', $url ) ); $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ), $url ) ); - $this->assertSame( - "$url&foo=2", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ), - $url - ) - ); + $this->assertSame( "$url&foo=2", add_query_arg( array( 'foo' => '2' ), $url ) ); $this->assertSame( "$url&foo=1&bar=2", add_query_arg( @@ -170,15 +136,7 @@ public function test_add_query_arg() { $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1' ) ); $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ) ) ); - $this->assertSame( - "$url&foo=2", - add_query_arg( - array( - 'foo' => '1', - 'foo' => '2', - ) - ) - ); + $this->assertSame( "$url&foo=2", add_query_arg( array( 'foo' => '2' ) ) ); $this->assertSame( "$url&foo=1&bar=2", add_query_arg( From 319f4d28cae6cb03064a0f629dfb99ab319d5e87 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 17 Sep 2026 11:51:00 -0400 Subject: [PATCH 7/7] Move PHPUnit tests for `functions.php` into individual files by function name --- .../tests/functions/underscoreReturn.php | 52 ---- tests/phpunit/tests/functions/xmlrpc.php | 70 ----- .../functions/AbsintTest.php} | 7 +- .../functions/AddMagicQuotesTest.php} | 7 +- .../functions/AddQueryArgTest.php} | 8 +- .../functions/BoolFromYnTest.php} | 7 +- .../functions/CanonicalCharsetTest.php} | 7 +- .../functions/CleanDirsizeCacheTest.php} | 44 +-- .../functions/CleanupHeaderCommentTest.php} | 8 +- .../functions/CurrentDatetimeTest.php} | 9 +- .../functions/CurrentTimeTest.php} | 10 +- .../functions/DateI18nTest.php} | 10 +- .../functions/DeleteOptionFreshSiteTest.php} | 7 +- .../functions/DeviceCanUploadTest.php} | 7 +- .../functions/DoEncloseTest.php} | 8 +- .../functions/ForceSslAdminTest.php} | 8 +- .../functions/GetAllowedMimeTypesTest.php} | 7 +- .../functions/GetFileDataTest.php} | 8 +- .../functions/GetNonCachedIdsTest.php} | 10 +- .../functions/GetStatusHeaderDescTest.php} | 9 +- .../functions/GetWeekstartendTest.php} | 7 +- .../functions/HumanReadableDurationTest.php} | 7 +- .../functions/IsNewDayTest.php} | 8 +- .../functions/IsPhpVersionCompatibleTest.php} | 7 +- .../functions/IsSerializedStringTest.php} | 8 +- .../functions/IsSerializedTest.php} | 8 +- .../functions/IsWpVersionCompatibleTest.php} | 7 +- .../functions/ListFilesTest.php} | 7 +- .../functions/MaybeSerializeTest.php} | 64 ++--- .../functions/MaybeUnserializeTest.php | 191 +++++++++++++ .../functions/MceSetDirectionTest.php} | 7 +- .../functions/Mysql2dateTest.php} | 9 +- .../functions/MysqlToRfc3339Test.php} | 8 +- .../functions/NumberFormatI18nTest.php} | 7 +- .../functions/PathIsAbsoluteTest.php} | 7 +- .../functions/PathJoinTest.php} | 7 +- .../functions/PluginBasenameTest.php} | 7 +- .../functions/RecurseDirsizeTest.php | 45 ++++ .../functions/RemoveQueryArgTest.php} | 7 +- .../functions/ReturnEmptyArrayTest.php | 21 ++ .../functions/ReturnEmptyStringTest.php | 21 ++ .../wp-includes/functions/ReturnFalseTest.php | 21 ++ .../wp-includes/functions/ReturnNullTest.php | 21 ++ .../wp-includes/functions/ReturnTrueTest.php | 21 ++ .../wp-includes/functions/ReturnZeroTest.php | 21 ++ .../functions/SizeFormatTest.php} | 7 +- .../functions/ValidateFileTest.php} | 7 +- .../functions/WpAdminNoticeTest.php} | 9 +- .../functions/WpAllowedProtocolsTest.php} | 7 +- .../functions/WpArrayGetTest.php} | 7 +- .../functions/WpArraySetTest.php} | 7 +- .../functions/WpArraySliceAssocTest.php} | 7 +- .../functions/WpAuthCheckTest.php} | 8 +- .../WpCacheGetMultipleSaltedTest.php} | 7 +- .../functions/WpCacheGetSaltedTest.php} | 10 +- .../functions/WpCacheSetLastChangedTest.php} | 8 +- .../WpCacheSetMultipleSaltedTest.php} | 10 +- .../functions/WpCacheSetSaltedTest.php} | 8 +- .../WpCheckAlternateFileNamesTest.php} | 7 +- .../functions/WpCheckFiletypeAndExtTest.php} | 7 +- .../functions/WpCheckFiletypeTest.php} | 7 +- .../functions/WpDateTest.php} | 11 +- .../functions/WpDeleteFileTest.php} | 7 +- .../functions/WpExt2typeTest.php} | 7 +- .../functions/WpExtractUrlsTest.php} | 8 +- .../functions/WpFilesizeTest.php} | 7 +- .../functions/WpFilterObjectListTest.php} | 7 +- .../functions/WpFuzzyNumberMatchTest.php} | 7 +- .../functions/WpGenerateUuid4Test.php} | 8 +- .../functions/WpGetAdminNoticeTest.php} | 9 +- .../functions/WpGetArchivesTest.php} | 8 +- .../WpGetDefaultExtensionForMimeTypeTest.php} | 7 +- .../functions/WpGetExtTypesTest.php} | 7 +- .../functions/WpGetHttpHeadersTest.php} | 8 +- .../functions/WpGetImageMimeTest.php} | 7 +- .../functions/WpGetImageSizeTest.php} | 7 +- .../functions/WpGetMimeTypesTest.php} | 7 +- .../functions/WpGetRawRefererTest.php | 71 +++++ .../functions/WpGetRefererTest.php} | 51 +--- .../functions/WpGetWpVersionTest.php} | 7 +- .../functions/WpGuessUrlTest.php} | 7 +- .../functions/WpHashTest.php} | 8 +- .../functions/WpIsJsonMediaTypeTest.php} | 7 +- .../functions/WpIsNumericArrayTest.php} | 7 +- .../functions/WpIsStreamTest.php} | 7 +- .../functions/WpIsUuidTest.php} | 8 +- .../functions/WpJsonEncodeTest.php} | 8 +- .../functions/WpJsonFileDecodeTest.php} | 7 +- .../functions/WpListFilterTest.php} | 7 +- .../functions/WpListPluckTest.php} | 9 +- .../functions/WpListSortTest.php} | 7 +- .../functions/WpListUtilGetInputTest.php | 24 ++ .../functions/WpListUtilGetOutputTest.php | 49 ++++ .../functions/WpListUtilPluckTest.php | 124 +++++++++ .../functions/WpListUtilSortTest.php} | 178 +----------- .../functions/WpMysqlWeekTest.php} | 7 +- .../functions/WpNonceAysTest.php} | 7 +- .../functions/WpNonceFieldTest.php} | 7 +- .../functions/WpNonceUrlTest.php} | 7 +- .../functions/WpNormalizePathTest.php} | 9 +- .../functions/WpParseArgsTest.php} | 8 +- .../functions/WpParseIdListTest.php} | 7 +- .../functions/WpParseListTest.php} | 8 +- .../functions/WpParseSlugListTest.php} | 7 +- .../functions/WpPrivacyAnonymizeDataTest.php} | 18 +- .../functions/WpPrivacyAnonymizeIpTest.php | 255 ++++++++++++++++++ .../functions/WpRaiseMemoryLimitTest.php} | 7 +- .../functions/WpRecursiveKsortTest.php} | 8 +- .../functions/WpRefererFieldTest.php} | 7 +- .../functions/WpRemoteFopenTest.php} | 8 +- .../functions/WpScheduledDeleteTest.php} | 8 +- .../functions/WpTest.php} | 7 +- .../functions/WpTimezoneChoiceTest.php} | 7 +- .../WpTimezoneChoiceUsortCallbackTest.php} | 7 +- .../WpTimezoneOverrideOffsetTest.php} | 8 +- .../functions/WpTimezoneStringTest.php} | 8 +- .../functions/WpTimezoneTest.php} | 8 +- .../functions/WpToKebabCaseTest.php} | 7 +- .../functions/WpTriggerErrorTest.php} | 9 +- .../functions/WpUniqueFilenameTest.php} | 7 +- .../functions/WpUniqueIdFromValuesTest.php} | 18 +- .../functions/WpUniqueIdTest.php} | 8 +- .../functions/WpUniquePrefixedIdTest.php} | 8 +- .../functions/WpUploadDirInternalTest.php} | 8 +- .../functions/WpUploadDirTest.php} | 9 +- .../functions/WpValidateBooleanTest.php} | 10 +- .../functions/WpVerifyFastHashTest.php} | 8 +- .../functions/XmlrpcGetpostcategoryTest.php | 42 +++ .../functions/XmlrpcGetposttitleTest.php | 42 +++ .../functions/XmlrpcRemovepostdataTest.php | 30 +++ 130 files changed, 1765 insertions(+), 542 deletions(-) delete mode 100644 tests/phpunit/tests/functions/underscoreReturn.php delete mode 100644 tests/phpunit/tests/functions/xmlrpc.php rename tests/phpunit/tests/{functions/absint.php => wp-includes/functions/AbsintTest.php} (93%) rename tests/phpunit/tests/{functions/addMagicQuotes.php => wp-includes/functions/AddMagicQuotesTest.php} (89%) rename tests/phpunit/tests/{functions/addQueryArg.php => wp-includes/functions/AddQueryArgTest.php} (97%) rename tests/phpunit/tests/{functions/boolFromYn.php => wp-includes/functions/BoolFromYnTest.php} (68%) rename tests/phpunit/tests/{functions/canonicalCharset.php => wp-includes/functions/CanonicalCharsetTest.php} (95%) rename tests/phpunit/tests/{functions/cleanDirsizeCache.php => wp-includes/functions/CleanDirsizeCacheTest.php} (74%) rename tests/phpunit/tests/{functions/cleanupHeaderComment.php => wp-includes/functions/CleanupHeaderCommentTest.php} (95%) rename tests/phpunit/tests/{functions/currentDatetime.php => wp-includes/functions/CurrentDatetimeTest.php} (59%) rename tests/phpunit/tests/{functions/currentTime.php => wp-includes/functions/CurrentTimeTest.php} (98%) rename tests/phpunit/tests/{functions/dateI18n.php => wp-includes/functions/DateI18nTest.php} (97%) rename tests/phpunit/tests/{functions/deleteOptionFreshSite.php => wp-includes/functions/DeleteOptionFreshSiteTest.php} (77%) rename tests/phpunit/tests/{functions/deviceCanUpload.php => wp-includes/functions/DeviceCanUploadTest.php} (94%) rename tests/phpunit/tests/{functions/doEnclose.php => wp-includes/functions/DoEncloseTest.php} (98%) rename tests/phpunit/tests/{functions/forceSslAdmin.php => wp-includes/functions/ForceSslAdminTest.php} (91%) rename tests/phpunit/tests/{functions/getAllowedMimeTypes.php => wp-includes/functions/GetAllowedMimeTypesTest.php} (81%) rename tests/phpunit/tests/{functions/getFileData.php => wp-includes/functions/GetFileDataTest.php} (94%) rename tests/phpunit/tests/{functions/getNonCachedIds.php => wp-includes/functions/GetNonCachedIdsTest.php} (94%) rename tests/phpunit/tests/{functions/getStatusHeaderDesc.php => wp-includes/functions/GetStatusHeaderDescTest.php} (89%) rename tests/phpunit/tests/{functions/getWeekstartend.php => wp-includes/functions/GetWeekstartendTest.php} (89%) rename tests/phpunit/tests/{functions/humanReadableDuration.php => wp-includes/functions/HumanReadableDurationTest.php} (95%) rename tests/phpunit/tests/{functions/isNewDay.php => wp-includes/functions/IsNewDayTest.php} (87%) rename tests/phpunit/tests/{functions/isPhpVersionCompatible.php => wp-includes/functions/IsPhpVersionCompatibleTest.php} (93%) rename tests/phpunit/tests/{functions/isSerializedString.php => wp-includes/functions/IsSerializedStringTest.php} (93%) rename tests/phpunit/tests/{functions/isSerialized.php => wp-includes/functions/IsSerializedTest.php} (97%) rename tests/phpunit/tests/{functions/isWpVersionCompatible.php => wp-includes/functions/IsWpVersionCompatibleTest.php} (98%) rename tests/phpunit/tests/{functions/listFiles.php => wp-includes/functions/ListFilesTest.php} (95%) rename tests/phpunit/tests/{functions/maybeSerialize.php => wp-includes/functions/MaybeSerializeTest.php} (86%) create mode 100644 tests/phpunit/tests/wp-includes/functions/MaybeUnserializeTest.php rename tests/phpunit/tests/{functions/mceSetDirection.php => wp-includes/functions/MceSetDirectionTest.php} (88%) rename tests/phpunit/tests/{functions/mysql2date.php => wp-includes/functions/Mysql2dateTest.php} (95%) rename tests/phpunit/tests/{functions/mysqlToRfc3339.php => wp-includes/functions/MysqlToRfc3339Test.php} (87%) rename tests/phpunit/tests/{functions/numberFormatI18n.php => wp-includes/functions/NumberFormatI18nTest.php} (92%) rename tests/phpunit/tests/{functions/pathIsAbsolute.php => wp-includes/functions/PathIsAbsoluteTest.php} (86%) rename tests/phpunit/tests/{functions/pathJoin.php => wp-includes/functions/PathJoinTest.php} (92%) rename tests/phpunit/tests/{functions/pluginBasename.php => wp-includes/functions/PluginBasenameTest.php} (92%) create mode 100644 tests/phpunit/tests/wp-includes/functions/RecurseDirsizeTest.php rename tests/phpunit/tests/{functions/removeQueryArg.php => wp-includes/functions/RemoveQueryArgTest.php} (90%) create mode 100644 tests/phpunit/tests/wp-includes/functions/ReturnEmptyArrayTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/ReturnEmptyStringTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/ReturnFalseTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/ReturnNullTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/ReturnTrueTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/ReturnZeroTest.php rename tests/phpunit/tests/{functions/sizeFormat.php => wp-includes/functions/SizeFormatTest.php} (95%) rename tests/phpunit/tests/{functions/validateFile.php => wp-includes/functions/ValidateFileTest.php} (94%) rename tests/phpunit/tests/{functions/wpAdminNotice.php => wp-includes/functions/WpAdminNoticeTest.php} (98%) rename tests/phpunit/tests/{functions/allowedProtocols.php => wp-includes/functions/WpAllowedProtocolsTest.php} (95%) rename tests/phpunit/tests/{functions/wpArrayGet.php => wp-includes/functions/WpArrayGetTest.php} (96%) rename tests/phpunit/tests/{functions/wpArraySet.php => wp-includes/functions/WpArraySetTest.php} (95%) rename tests/phpunit/tests/{functions/wpArraySliceAssoc.php => wp-includes/functions/WpArraySliceAssocTest.php} (94%) rename tests/phpunit/tests/{functions/wpAuthCheck.php => wp-includes/functions/WpAuthCheckTest.php} (91%) rename tests/phpunit/tests/{functions/wpCacheGetMultipleSalted.php => wp-includes/functions/WpCacheGetMultipleSaltedTest.php} (95%) rename tests/phpunit/tests/{functions/wpCacheGetSalted.php => wp-includes/functions/WpCacheGetSaltedTest.php} (93%) rename tests/phpunit/tests/{functions/wpCacheSetLastChanged.php => wp-includes/functions/WpCacheSetLastChangedTest.php} (83%) rename tests/phpunit/tests/{functions/wpCacheSetMultipleSalted.php => wp-includes/functions/WpCacheSetMultipleSaltedTest.php} (87%) rename tests/phpunit/tests/{functions/wpCacheSetSalted.php => wp-includes/functions/WpCacheSetSaltedTest.php} (90%) rename tests/phpunit/tests/{functions/wpCheckAlternateFileNames.php => wp-includes/functions/WpCheckAlternateFileNamesTest.php} (94%) rename tests/phpunit/tests/{functions/wpCheckFiletypeAndExt.php => wp-includes/functions/WpCheckFiletypeAndExtTest.php} (97%) rename tests/phpunit/tests/{functions/wpCheckFiletype.php => wp-includes/functions/WpCheckFiletypeTest.php} (95%) rename tests/phpunit/tests/{functions/wpDate.php => wp-includes/functions/WpDateTest.php} (95%) rename tests/phpunit/tests/{functions/wpDeleteFile.php => wp-includes/functions/WpDeleteFileTest.php} (87%) rename tests/phpunit/tests/{functions/wpExt2Type.php => wp-includes/functions/WpExt2typeTest.php} (80%) rename tests/phpunit/tests/{functions/wpExtractUrls.php => wp-includes/functions/WpExtractUrlsTest.php} (98%) rename tests/phpunit/tests/{functions/wpFilesize.php => wp-includes/functions/WpFilesizeTest.php} (95%) rename tests/phpunit/tests/{functions/wpFilterObjectList.php => wp-includes/functions/WpFilterObjectListTest.php} (97%) rename tests/phpunit/tests/{functions/wpFuzzyNumberMatch.php => wp-includes/functions/WpFuzzyNumberMatchTest.php} (95%) rename tests/phpunit/tests/{functions/wpGenerateUuid4.php => wp-includes/functions/WpGenerateUuid4Test.php} (77%) rename tests/phpunit/tests/{functions/wpGetAdminNotice.php => wp-includes/functions/WpGetAdminNoticeTest.php} (98%) rename tests/phpunit/tests/{functions/wpGetArchives.php => wp-includes/functions/WpGetArchivesTest.php} (97%) rename tests/phpunit/tests/{functions/wpGetDefaultExtensionForMimeType.php => wp-includes/functions/WpGetDefaultExtensionForMimeTypeTest.php} (89%) rename tests/phpunit/tests/{functions/wpGetExtTypes.php => wp-includes/functions/WpGetExtTypesTest.php} (81%) rename tests/phpunit/tests/{functions/wpGetHttpHeaders.php => wp-includes/functions/WpGetHttpHeadersTest.php} (91%) rename tests/phpunit/tests/{functions/wpGetImageMime.php => wp-includes/functions/WpGetImageMimeTest.php} (94%) rename tests/phpunit/tests/{functions/wpGetImageSize.php => wp-includes/functions/WpGetImageSizeTest.php} (97%) rename tests/phpunit/tests/{functions/wpGetMimeTypes.php => wp-includes/functions/WpGetMimeTypesTest.php} (88%) create mode 100644 tests/phpunit/tests/wp-includes/functions/WpGetRawRefererTest.php rename tests/phpunit/tests/{functions/referer.php => wp-includes/functions/WpGetRefererTest.php} (79%) rename tests/phpunit/tests/{functions/wpGetWpVersion.php => wp-includes/functions/WpGetWpVersionTest.php} (85%) rename tests/phpunit/tests/{functions/wpGuessUrl.php => wp-includes/functions/WpGuessUrlTest.php} (89%) rename tests/phpunit/tests/{functions/wpHash.php => wp-includes/functions/WpHashTest.php} (83%) rename tests/phpunit/tests/{functions/wpIsJsonMediaType.php => wp-includes/functions/WpIsJsonMediaTypeTest.php} (87%) rename tests/phpunit/tests/{functions/wpIsNumericArray.php => wp-includes/functions/WpIsNumericArrayTest.php} (93%) rename tests/phpunit/tests/{functions/wpIsStream.php => wp-includes/functions/WpIsStreamTest.php} (91%) rename tests/phpunit/tests/{functions/wpIsUuid.php => wp-includes/functions/WpIsUuidTest.php} (91%) rename tests/phpunit/tests/{functions/wpJsonEncode.php => wp-includes/functions/WpJsonEncodeTest.php} (93%) rename tests/phpunit/tests/{functions/wpJsonFileDecode.php => wp-includes/functions/WpJsonFileDecodeTest.php} (83%) rename tests/phpunit/tests/{functions/wpListFilter.php => wp-includes/functions/WpListFilterTest.php} (97%) rename tests/phpunit/tests/{functions/wpListPluck.php => wp-includes/functions/WpListPluckTest.php} (97%) rename tests/phpunit/tests/{functions/wpListSort.php => wp-includes/functions/WpListSortTest.php} (99%) create mode 100644 tests/phpunit/tests/wp-includes/functions/WpListUtilGetInputTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/WpListUtilGetOutputTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/WpListUtilPluckTest.php rename tests/phpunit/tests/{functions/wpListUtil.php => wp-includes/functions/WpListUtilSortTest.php} (84%) rename tests/phpunit/tests/{functions/wpMysqlWeek.php => wp-includes/functions/WpMysqlWeekTest.php} (90%) rename tests/phpunit/tests/{functions/wpNonceAys.php => wp-includes/functions/WpNonceAysTest.php} (86%) rename tests/phpunit/tests/{functions/wpNonceField.php => wp-includes/functions/WpNonceFieldTest.php} (95%) rename tests/phpunit/tests/{functions/wpNonceUrl.php => wp-includes/functions/WpNonceUrlTest.php} (97%) rename tests/phpunit/tests/{functions/wpNormalizePath.php => wp-includes/functions/WpNormalizePathTest.php} (95%) rename tests/phpunit/tests/{functions/wpParseArgs.php => wp-includes/functions/WpParseArgsTest.php} (93%) rename tests/phpunit/tests/{functions/wpParseIdList.php => wp-includes/functions/WpParseIdListTest.php} (96%) rename tests/phpunit/tests/{functions/wpParseList.php => wp-includes/functions/WpParseListTest.php} (95%) rename tests/phpunit/tests/{functions/wpParseSlugList.php => wp-includes/functions/WpParseSlugListTest.php} (96%) rename tests/phpunit/tests/{functions/anonymization.php => wp-includes/functions/WpPrivacyAnonymizeDataTest.php} (97%) create mode 100644 tests/phpunit/tests/wp-includes/functions/WpPrivacyAnonymizeIpTest.php rename tests/phpunit/tests/{functions/wpRaiseMemoryLimit.php => wp-includes/functions/WpRaiseMemoryLimitTest.php} (85%) rename tests/phpunit/tests/{functions/wpRecursiveKsort.php => wp-includes/functions/WpRecursiveKsortTest.php} (93%) rename tests/phpunit/tests/{functions/wpRefererField.php => wp-includes/functions/WpRefererFieldTest.php} (93%) rename tests/phpunit/tests/{functions/wpRemoteFopen.php => wp-includes/functions/WpRemoteFopenTest.php} (84%) rename tests/phpunit/tests/{functions/wpScheduledDelete.php => wp-includes/functions/WpScheduledDeleteTest.php} (97%) rename tests/phpunit/tests/{functions/wp.php => wp-includes/functions/WpTest.php} (73%) rename tests/phpunit/tests/{functions/wpTimezoneChoice.php => wp-includes/functions/WpTimezoneChoiceTest.php} (97%) rename tests/phpunit/tests/{functions/wpTimezoneChoiceUsortCallback.php => wp-includes/functions/WpTimezoneChoiceUsortCallbackTest.php} (99%) rename tests/phpunit/tests/{functions/wpTimezoneOverrideOffset.php => wp-includes/functions/WpTimezoneOverrideOffsetTest.php} (91%) rename tests/phpunit/tests/{functions/wpTimezoneString.php => wp-includes/functions/WpTimezoneStringTest.php} (95%) rename tests/phpunit/tests/{functions/wpTimezone.php => wp-includes/functions/WpTimezoneTest.php} (95%) rename tests/phpunit/tests/{functions/wpToKebabCase.php => wp-includes/functions/WpToKebabCaseTest.php} (93%) rename tests/phpunit/tests/{functions/wpTriggerError.php => wp-includes/functions/WpTriggerErrorTest.php} (96%) rename tests/phpunit/tests/{functions/wpUniqueFilename.php => wp-includes/functions/WpUniqueFilenameTest.php} (98%) rename tests/phpunit/tests/{functions/wpUniqueIdFromValues.php => wp-includes/functions/WpUniqueIdFromValuesTest.php} (91%) rename tests/phpunit/tests/{functions/wpUniqueId.php => wp-includes/functions/WpUniqueIdTest.php} (84%) rename tests/phpunit/tests/{functions/wpUniquePrefixedId.php => wp-includes/functions/WpUniquePrefixedIdTest.php} (97%) rename tests/phpunit/tests/{functions/wpUploadDirInternal.php => wp-includes/functions/WpUploadDirInternalTest.php} (96%) rename tests/phpunit/tests/{functions/wpUploadDir.php => wp-includes/functions/WpUploadDirTest.php} (86%) rename tests/phpunit/tests/{functions/wpValidateBoolean.php => wp-includes/functions/WpValidateBooleanTest.php} (89%) rename tests/phpunit/tests/{functions/wpVerifyFastHash.php => wp-includes/functions/WpVerifyFastHashTest.php} (88%) create mode 100644 tests/phpunit/tests/wp-includes/functions/XmlrpcGetpostcategoryTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/XmlrpcGetposttitleTest.php create mode 100644 tests/phpunit/tests/wp-includes/functions/XmlrpcRemovepostdataTest.php diff --git a/tests/phpunit/tests/functions/underscoreReturn.php b/tests/phpunit/tests/functions/underscoreReturn.php deleted file mode 100644 index 86c6ca1e45aec..0000000000000 --- a/tests/phpunit/tests/functions/underscoreReturn.php +++ /dev/null @@ -1,52 +0,0 @@ -assertTrue( __return_true() ); - } - - /** - * @covers ::__return_false - */ - public function test__return_false() { - $this->assertFalse( __return_false() ); - } - - /** - * @covers ::__return_zero - */ - public function test__return_zero() { - $this->assertSame( 0, __return_zero() ); - } - - /** - * @covers ::__return_empty_array - */ - public function test__return_empty_array() { - $this->assertSame( array(), __return_empty_array() ); - } - - /** - * @covers ::__return_null - */ - public function test__return_null() { - $this->assertNull( __return_null() ); - } - - /** - * @covers ::__return_empty_string - */ - public function test__return_empty_string() { - $this->assertSame( '', __return_empty_string() ); - } -} diff --git a/tests/phpunit/tests/functions/xmlrpc.php b/tests/phpunit/tests/functions/xmlrpc.php deleted file mode 100644 index 925801326f0af..0000000000000 --- a/tests/phpunit/tests/functions/xmlrpc.php +++ /dev/null @@ -1,70 +0,0 @@ -title - category,category1 - content - '; - - /** - * Tests that xmlrpc_getposttitle() returns the post title if found in the XML. - * - * @covers ::xmlrpc_getposttitle - */ - public function test_xmlrpc_getposttitle() { - $this->assertSame( 'title', xmlrpc_getposttitle( $this->test_content ) ); - } - - /** - * Tests that xmlrpc_getposttitle() defaults to the `$post_default_title` global. - * - * @covers ::xmlrpc_getposttitle - */ - public function test_xmlrpc_getposttitle_default() { - global $post_default_title; - - $post_default_title = 'post_default_title'; - - $this->assertSame( 'post_default_title', xmlrpc_getposttitle( '' ) ); - } - - - /** - * Tests that xmlrpc_getpostcategory() returns post categories if found in the XML. - * - * @covers ::xmlrpc_getpostcategory - */ - public function test_xmlrpc_getpostcategory() { - $this->assertSame( array( 'category', 'category1' ), xmlrpc_getpostcategory( $this->test_content ) ); - } - - /** - * Tests that xmlrpc_getpostcategory() defaults to the `$post_default_category` global. - * - * @covers ::xmlrpc_getpostcategory - */ - public function test_xmlrpc_getpostcategory_default() { - global $post_default_category; - - $post_default_category = 'post_default_category'; - - $this->assertSame( 'post_default_category', xmlrpc_getpostcategory( '' ) ); - } - - /** - * Tests that xmlrpc_removepostdata() returns XML content without title and category elements. - * - * @covers ::xmlrpc_removepostdata - */ - public function test_xmlrpc_removepostdata() { - $this->assertSame( 'content', xmlrpc_removepostdata( $this->test_content ) ); - } -} diff --git a/tests/phpunit/tests/functions/absint.php b/tests/phpunit/tests/wp-includes/functions/AbsintTest.php similarity index 93% rename from tests/phpunit/tests/functions/absint.php rename to tests/phpunit/tests/wp-includes/functions/AbsintTest.php index 5a9edd6961077..4d5ccbac7e008 100644 --- a/tests/phpunit/tests/functions/absint.php +++ b/tests/phpunit/tests/wp-includes/functions/AbsintTest.php @@ -1,13 +1,18 @@ array( 'size' => 42 ), ); } - - /** - * Tests the behavior of the function when the transient doesn't exist. - * - * @ticket 52241 - * @ticket 53635 - * - * @covers ::recurse_dirsize - */ - public function test_recurse_dirsize_without_transient() { - delete_transient( 'dirsize_cache' ); - - $size = recurse_dirsize( DIR_TESTDATA . '/functions' ); - - $this->assertGreaterThan( 10, $size ); - } - - /** - * Tests the behavior of the function when the transient does exist, but is not an array. - * - * In particular, this tests that no PHP TypeErrors are being thrown. - * - * @ticket 52241 - * @ticket 53635 - * - * @covers ::recurse_dirsize - */ - public function test_recurse_dirsize_with_invalid_transient() { - set_transient( 'dirsize_cache', 'this is not a valid transient for dirsize cache' ); - - $size = recurse_dirsize( DIR_TESTDATA . '/functions' ); - - $this->assertGreaterThan( 10, $size ); - } -} +} \ No newline at end of file diff --git a/tests/phpunit/tests/functions/cleanupHeaderComment.php b/tests/phpunit/tests/wp-includes/functions/CleanupHeaderCommentTest.php similarity index 95% rename from tests/phpunit/tests/functions/cleanupHeaderComment.php rename to tests/phpunit/tests/wp-includes/functions/CleanupHeaderCommentTest.php index 95c1b88d20bf1..7a48613c8fa32 100644 --- a/tests/phpunit/tests/functions/cleanupHeaderComment.php +++ b/tests/phpunit/tests/wp-includes/functions/CleanupHeaderCommentTest.php @@ -1,4 +1,9 @@ getProperty( 'callback' ); + if ( PHP_VERSION_ID < 80100 ) { + $property->setAccessible( true ); + } + $callback_value = $property->getValue( $new_value ); - if ( is_object( $expected ) ) { - $this->assertEquals( $expected, maybe_unserialize( $value ) ); + $this->assertSame( null, $callback_value ); } else { - $this->assertSame( $expected, maybe_unserialize( $value ) ); + $this->assertSame( $value->count(), unserialize( $serialized )->count() ); } } /** - * Data provider for `test_maybe_unserialize()`. + * Data provider for `test_maybe_serialize()`. * * @return array[] */ @@ -208,26 +216,6 @@ public function data_is_not_serialized() { ); } - /** - * @dataProvider data_serialize_deserialize_objects - */ - public function test_deserialize_request_utility_filtered_iterator_objects( $value ) { - $serialized = maybe_serialize( $value ); - - if ( get_class( $value ) === 'WpOrg\Requests\Utility\FilteredIterator' ) { - $new_value = unserialize( $serialized ); - $property = ( new ReflectionClass( 'WpOrg\Requests\Utility\FilteredIterator' ) )->getProperty( 'callback' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - $callback_value = $property->getValue( $new_value ); - - $this->assertSame( null, $callback_value ); - } else { - $this->assertSame( $value->count(), unserialize( $serialized )->count() ); - } - } - /** * Data provider for test_deserialize_request_utility_filtered_iterator_objects(). * @@ -236,10 +224,10 @@ public function test_deserialize_request_utility_filtered_iterator_objects( $val public function data_serialize_deserialize_objects() { return array( 'filtered iterator using md5' => array( - new WpOrg\Requests\Utility\FilteredIterator( array( 1 ), 'md5' ), + new FilteredIterator( array( 1 ), 'md5' ), ), 'filtered iterator using sha1' => array( - new WpOrg\Requests\Utility\FilteredIterator( array( 1, 2 ), 'sha1' ), + new FilteredIterator( array( 1, 2 ), 'sha1' ), ), 'array iterator' => array( new ArrayIterator( array( 1, 2, 3 ) ), diff --git a/tests/phpunit/tests/wp-includes/functions/MaybeUnserializeTest.php b/tests/phpunit/tests/wp-includes/functions/MaybeUnserializeTest.php new file mode 100644 index 0000000000000..00f8aaa1f75e3 --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/MaybeUnserializeTest.php @@ -0,0 +1,191 @@ +assertEquals( $expected, maybe_unserialize( $value ) ); + } else { + $this->assertSame( $expected, maybe_unserialize( $value ) ); + } + } + + /** + * Data provider for `test_maybe_unserialize()`. + * + * @return array[] + */ + public function data_is_serialized() { + return array( + 'serialized empty array' => array( + 'data' => serialize( array() ), + 'expected' => true, + ), + 'serialized non-empty array' => array( + 'data' => serialize( array( 1, 1, 2, 3, 5, 8, 13 ) ), + 'expected' => true, + ), + 'serialized empty object' => array( + 'data' => serialize( new stdClass() ), + 'expected' => true, + ), + 'serialized non-empty object' => array( + 'data' => serialize( + (object) array( + 'test' => true, + '1', + 2, + ) + ), + 'expected' => true, + ), + 'serialized null' => array( + 'data' => serialize( null ), + 'expected' => true, + ), + 'serialized boolean true' => array( + 'data' => serialize( true ), + 'expected' => true, + ), + 'serialized boolean false' => array( + 'data' => serialize( false ), + 'expected' => true, + ), + 'serialized integer -1' => array( + 'data' => serialize( -1 ), + 'expected' => true, + ), + 'serialized integer 1' => array( + 'data' => serialize( -1 ), + 'expected' => true, + ), + 'serialized float 1.1' => array( + 'data' => serialize( 1.1 ), + 'expected' => true, + ), + 'serialized string' => array( + 'data' => serialize( 'this string will be serialized' ), + 'expected' => true, + ), + 'serialized string with line break' => array( + 'data' => serialize( "a\nb" ), + 'expected' => true, + ), + 'serialized string with leading and trailing spaces' => array( + 'data' => ' s:25:"this string is serialized"; ', + 'expected' => true, + ), + ); + } + + /** + * Data provider for `test_maybe_serialize()`. + * + * @return array[] + */ + public function data_is_not_serialized() { + return array( + 'an empty array' => array( + 'data' => array(), + 'expected' => false, + ), + 'a non-empty array' => array( + 'data' => array( 1, 1, 2, 3, 5, 8, 13 ), + 'expected' => false, + ), + 'an empty object' => array( + 'data' => new stdClass(), + 'expected' => false, + ), + 'a non-empty object' => array( + 'data' => (object) array( + 'test' => true, + '1', + 2, + ), + 'expected' => false, + ), + 'null' => array( + 'data' => null, + 'expected' => false, + ), + 'a boolean true' => array( + 'data' => true, + 'expected' => false, + ), + 'a boolean false' => array( + 'data' => false, + 'expected' => false, + ), + 'an integer -1' => array( + 'data' => -1, + 'expected' => false, + ), + 'an integer 0' => array( + 'data' => 0, + 'expected' => false, + ), + 'an integer 1' => array( + 'data' => 1, + 'expected' => false, + ), + 'a float 0.0' => array( + 'data' => 0.0, + 'expected' => false, + ), + 'a float 1.1' => array( + 'data' => 1.1, + 'expected' => false, + ), + 'a string' => array( + 'data' => 'a string', + 'expected' => false, + ), + 'a string with line break' => array( + 'data' => "a\nb", + 'expected' => false, + ), + 'a string with leading and trailing garbage' => array( + 'data' => 'garbage:a:0:garbage;', + 'expected' => false, + ), + 'a string with missing double quotes' => array( + 'data' => 's:4:test;', + 'expected' => false, + ), + 'a string that is too short' => array( + 'data' => 's:3', + 'expected' => false, + ), + 'not a colon in second position' => array( + 'data' => 's!3:"foo";', + 'expected' => false, + ), + 'no trailing semicolon (strict check)' => array( + 'data' => 's:3:"foo"', + 'expected' => false, + ), + ); + } +} diff --git a/tests/phpunit/tests/functions/mceSetDirection.php b/tests/phpunit/tests/wp-includes/functions/MceSetDirectionTest.php similarity index 88% rename from tests/phpunit/tests/functions/mceSetDirection.php rename to tests/phpunit/tests/wp-includes/functions/MceSetDirectionTest.php index 485dd06dcc71a..ad87ab44b7368 100644 --- a/tests/phpunit/tests/functions/mceSetDirection.php +++ b/tests/phpunit/tests/wp-includes/functions/MceSetDirectionTest.php @@ -1,13 +1,18 @@ assertGreaterThan( 10, $size ); + } + + /** + * Tests the behavior of the function when the transient does exist, but is not an array. + * + * In particular, this tests that no PHP TypeErrors are being thrown. + * + * @ticket 52241 + * @ticket 53635 + */ + public function test_recurse_dirsize_with_invalid_transient() { + set_transient( 'dirsize_cache', 'this is not a valid transient for dirsize cache' ); + + $size = recurse_dirsize( DIR_TESTDATA . '/functions' ); + + $this->assertGreaterThan( 10, $size ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/functions/removeQueryArg.php b/tests/phpunit/tests/wp-includes/functions/RemoveQueryArgTest.php similarity index 90% rename from tests/phpunit/tests/functions/removeQueryArg.php rename to tests/phpunit/tests/wp-includes/functions/RemoveQueryArgTest.php index 43252fc11c4e8..d98989c8587e1 100644 --- a/tests/phpunit/tests/functions/removeQueryArg.php +++ b/tests/phpunit/tests/wp-includes/functions/RemoveQueryArgTest.php @@ -1,11 +1,16 @@ assertSame( array(), __return_empty_array() ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/wp-includes/functions/ReturnEmptyStringTest.php b/tests/phpunit/tests/wp-includes/functions/ReturnEmptyStringTest.php new file mode 100644 index 0000000000000..da3ebe910b2d8 --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/ReturnEmptyStringTest.php @@ -0,0 +1,21 @@ +assertSame( '', __return_empty_string() ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/wp-includes/functions/ReturnFalseTest.php b/tests/phpunit/tests/wp-includes/functions/ReturnFalseTest.php new file mode 100644 index 0000000000000..8e118f40a1c53 --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/ReturnFalseTest.php @@ -0,0 +1,21 @@ +assertFalse( __return_false() ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/wp-includes/functions/ReturnNullTest.php b/tests/phpunit/tests/wp-includes/functions/ReturnNullTest.php new file mode 100644 index 0000000000000..dc3afca0ba775 --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/ReturnNullTest.php @@ -0,0 +1,21 @@ +assertNull( __return_null() ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/wp-includes/functions/ReturnTrueTest.php b/tests/phpunit/tests/wp-includes/functions/ReturnTrueTest.php new file mode 100644 index 0000000000000..ad21453ac47dc --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/ReturnTrueTest.php @@ -0,0 +1,21 @@ +assertTrue( __return_true() ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/wp-includes/functions/ReturnZeroTest.php b/tests/phpunit/tests/wp-includes/functions/ReturnZeroTest.php new file mode 100644 index 0000000000000..7a831fb3ba860 --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/ReturnZeroTest.php @@ -0,0 +1,21 @@ +assertSame( 0, __return_zero() ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/functions/sizeFormat.php b/tests/phpunit/tests/wp-includes/functions/SizeFormatTest.php similarity index 95% rename from tests/phpunit/tests/functions/sizeFormat.php rename to tests/phpunit/tests/wp-includes/functions/SizeFormatTest.php index 77134188634fe..642f5371fab0b 100644 --- a/tests/phpunit/tests/functions/sizeFormat.php +++ b/tests/phpunit/tests/wp-includes/functions/SizeFormatTest.php @@ -1,5 +1,9 @@ '123' ) ), array( array( diff --git a/tests/phpunit/tests/functions/wpCacheSetLastChanged.php b/tests/phpunit/tests/wp-includes/functions/WpCacheSetLastChangedTest.php similarity index 83% rename from tests/phpunit/tests/functions/wpCacheSetLastChanged.php rename to tests/phpunit/tests/wp-includes/functions/WpCacheSetLastChangedTest.php index f40980bcc3010..1d151e48a46ff 100644 --- a/tests/phpunit/tests/functions/wpCacheSetLastChanged.php +++ b/tests/phpunit/tests/wp-includes/functions/WpCacheSetLastChangedTest.php @@ -1,14 +1,20 @@ assertFalse( wp_get_raw_referer() ); + } + + /** + * @ticket 27152 + */ + public function test_raw_referer() { + $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.com/foo?bar' ); + $this->assertSame( 'http://example.com/foo?bar', wp_get_raw_referer() ); + } + + /** + * @ticket 27152 + */ + public function test_raw_referer_from_request() { + $_REQUEST['_wp_http_referer'] = addslashes( 'http://foo.bar/baz' ); + $this->assertSame( 'http://foo.bar/baz', wp_get_raw_referer() ); + } + + /** + * @ticket 27152 + */ + public function test_raw_referer_both() { + $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.com/foo?bar' ); + $_REQUEST['_wp_http_referer'] = addslashes( 'http://foo.bar/baz' ); + $this->assertSame( 'http://foo.bar/baz', wp_get_raw_referer() ); + } + + /** + * @ticket 57670 + */ + public function test_raw_referer_is_false_on_invalid_request_parameter() { + $_REQUEST['_wp_http_referer'] = array( 'demo' ); + $this->assertFalse( wp_get_raw_referer() ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/functions/referer.php b/tests/phpunit/tests/wp-includes/functions/WpGetRefererTest.php similarity index 79% rename from tests/phpunit/tests/functions/referer.php rename to tests/phpunit/tests/wp-includes/functions/WpGetRefererTest.php index 7d0efb31a1697..26bfda4c348d5 100644 --- a/tests/phpunit/tests/functions/referer.php +++ b/tests/phpunit/tests/wp-includes/functions/WpGetRefererTest.php @@ -1,14 +1,17 @@ assertSame( 'http://another.' . WP_TESTS_DOMAIN . '/test.php?id=123', wp_get_referer() ); remove_filter( 'allowed_redirect_hosts', array( $this, 'filter_allowed_redirect_hosts' ) ); } - - /** - * @ticket 27152 - */ - public function test_raw_referer_empty() { - $this->assertFalse( wp_get_raw_referer() ); - } - - /** - * @ticket 27152 - */ - public function test_raw_referer() { - $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.com/foo?bar' ); - $this->assertSame( 'http://example.com/foo?bar', wp_get_raw_referer() ); - } - - /** - * @ticket 27152 - */ - public function test_raw_referer_from_request() { - $_REQUEST['_wp_http_referer'] = addslashes( 'http://foo.bar/baz' ); - $this->assertSame( 'http://foo.bar/baz', wp_get_raw_referer() ); - } - - /** - * @ticket 27152 - */ - public function test_raw_referer_both() { - $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.com/foo?bar' ); - $_REQUEST['_wp_http_referer'] = addslashes( 'http://foo.bar/baz' ); - $this->assertSame( 'http://foo.bar/baz', wp_get_raw_referer() ); - } - - /** - * @ticket 57670 - */ - public function test_raw_referer_is_false_on_invalid_request_parameter() { - $_REQUEST['_wp_http_referer'] = array( 'demo' ); - $this->assertFalse( wp_get_raw_referer() ); - } -} +} \ No newline at end of file diff --git a/tests/phpunit/tests/functions/wpGetWpVersion.php b/tests/phpunit/tests/wp-includes/functions/WpGetWpVersionTest.php similarity index 85% rename from tests/phpunit/tests/functions/wpGetWpVersion.php rename to tests/phpunit/tests/wp-includes/functions/WpGetWpVersionTest.php index d10d946e3baea..923a3d5bb9538 100644 --- a/tests/phpunit/tests/functions/wpGetWpVersion.php +++ b/tests/phpunit/tests/wp-includes/functions/WpGetWpVersionTest.php @@ -1,13 +1,18 @@ assertSameSets( $input, $util->get_input() ); + } +} diff --git a/tests/phpunit/tests/wp-includes/functions/WpListUtilGetOutputTest.php b/tests/phpunit/tests/wp-includes/functions/WpListUtilGetOutputTest.php new file mode 100644 index 0000000000000..76cfd74850a89 --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/WpListUtilGetOutputTest.php @@ -0,0 +1,49 @@ +assertSameSets( $input, $util->get_output() ); + } + + /** + */ + public function test_wp_list_util_get_output() { + $expected = array( + (object) array( + 'foo' => 'bar', + 'bar' => 'baz', + ), + ); + + $util = new WP_List_Util( + array( + (object) array( + 'foo' => 'bar', + 'bar' => 'baz', + ), + (object) array( 'bar' => 'baz' ), + ) + ); + $actual = $util->filter( array( 'foo' => 'bar' ) ); + + $this->assertEqualSets( $expected, $actual ); + $this->assertEqualSets( $expected, $util->get_output() ); + } +} diff --git a/tests/phpunit/tests/wp-includes/functions/WpListUtilPluckTest.php b/tests/phpunit/tests/wp-includes/functions/WpListUtilPluckTest.php new file mode 100644 index 0000000000000..ec182ae6c10f8 --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/WpListUtilPluckTest.php @@ -0,0 +1,124 @@ +pluck( $target_key, $index_key ); + + $this->assertEqualSetsWithIndex( + $expected, + $actual, + 'The plucked value did not match the expected value.' + ); + + $this->assertEqualSetsWithIndex( + $expected, + $util->get_output(), + '::get_output() did not return the expected value.' + ); + } + + /** + * Data provider for test_wp_list_util_pluck(). + * + * @return array[] + */ + public function data_wp_list_util_pluck() { + return array( + 'simple' => array( + 'target_array' => array( + 0 => array( 'foo' => 'bar' ), + ), + 'target_key' => 'foo', + 'expected' => array( 'bar' ), + ), + 'simple_object' => array( + 'target_array' => array( + 0 => (object) array( 'foo' => 'bar' ), + ), + 'target_key' => 'foo', + 'expected' => array( 'bar' ), + ), + ); + } + + /** + * Tests that wp_list_pluck() throws _doing_it_wrong() with invalid input. + * + * @ticket 56650 + * + * @dataProvider data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input + * + * + * @expectedIncorrectUsage WP_List_Util::pluck + * + * @param array $input An invalid input array. + */ + public function test_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input( $input ) { + $this->assertSame( array(), wp_list_pluck( $input, 'a_field' ) ); + } + + /** + * Tests that wp_list_pluck() throws _doing_it_wrong() with an index key and invalid input. + * + * @ticket 56650 + * + * @dataProvider data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input + * + * + * @expectedIncorrectUsage WP_List_Util::pluck + * + * @param array $input An invalid input array. + */ + public function test_wp_list_pluck_should_throw_doing_it_wrong_with_index_key_and_invalid_input( $input ) { + $this->assertSame( array(), wp_list_pluck( $input, 'a_field', 'an_index_key' ) ); + } + + /** + * Data provider that provides invalid input arrays. + * + * @return array[] + */ + public function data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input() { + return array( + 'int[] 0' => array( array( 0 ) ), + 'int[] 1' => array( array( 1 ) ), + 'int[] -1' => array( array( -1 ) ), + 'float[] 0.0' => array( array( 0.0 ) ), + 'float[] 1.0' => array( array( 1.0 ) ), + 'float[] -1.0' => array( array( -1.0 ) ), + 'string[] and empty string' => array( array( '' ) ), + 'string[] and "0"' => array( array( '0' ) ), + 'string[] and "1"' => array( array( '1' ) ), + 'string[] and "-1"' => array( array( '-1' ) ), + 'array and null' => array( array( null ) ), + 'array and false' => array( array( false ) ), + 'array and true' => array( array( true ) ), + ); + } +} diff --git a/tests/phpunit/tests/functions/wpListUtil.php b/tests/phpunit/tests/wp-includes/functions/WpListUtilSortTest.php similarity index 84% rename from tests/phpunit/tests/functions/wpListUtil.php rename to tests/phpunit/tests/wp-includes/functions/WpListUtilSortTest.php index fd068387fd64c..135c53269ad12 100644 --- a/tests/phpunit/tests/functions/wpListUtil.php +++ b/tests/phpunit/tests/wp-includes/functions/WpListUtilSortTest.php @@ -1,177 +1,23 @@ assertSameSets( $input, $util->get_input() ); - } - - /** - * @covers WP_List_Util::get_output - */ - public function test_wp_list_util_get_output_immediately() { - $input = array( 'foo', 'bar' ); - $util = new WP_List_Util( $input ); - - $this->assertSameSets( $input, $util->get_output() ); - } - - /** - * @covers WP_List_Util::get_output - */ - public function test_wp_list_util_get_output() { - $expected = array( - (object) array( - 'foo' => 'bar', - 'bar' => 'baz', - ), - ); - - $util = new WP_List_Util( - array( - (object) array( - 'foo' => 'bar', - 'bar' => 'baz', - ), - (object) array( 'bar' => 'baz' ), - ) - ); - $actual = $util->filter( array( 'foo' => 'bar' ) ); - - $this->assertEqualSets( $expected, $actual ); - $this->assertEqualSets( $expected, $util->get_output() ); - } - - /** - * @ticket 55300 - * - * @dataProvider data_wp_list_util_pluck - * - * @covers WP_List_Util::pluck - * @covers ::wp_list_pluck - * - * @param array $target_array The array to create the list from. - * @param string $target_key The key to pluck. - * @param array $expected The expected array. - * @param string $index_key Optional. Field from the element to use as keys for the new array. - * Default null. - */ - public function test_wp_list_util_pluck( $target_array, $target_key, $expected, $index_key = null ) { - $util = new WP_List_Util( $target_array ); - $actual = $util->pluck( $target_key, $index_key ); - - $this->assertEqualSetsWithIndex( - $expected, - $actual, - 'The plucked value did not match the expected value.' - ); - - $this->assertEqualSetsWithIndex( - $expected, - $util->get_output(), - '::get_output() did not return the expected value.' - ); - } - - /** - * Data provider for test_wp_list_util_pluck(). - * - * @return array[] - */ - public function data_wp_list_util_pluck() { - return array( - 'simple' => array( - 'target_array' => array( - 0 => array( 'foo' => 'bar' ), - ), - 'target_key' => 'foo', - 'expected' => array( 'bar' ), - ), - 'simple_object' => array( - 'target_array' => array( - 0 => (object) array( 'foo' => 'bar' ), - ), - 'target_key' => 'foo', - 'expected' => array( 'bar' ), - ), - ); - } - - /** - * Tests that wp_list_pluck() throws _doing_it_wrong() with invalid input. - * - * @ticket 56650 - * - * @dataProvider data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input - * - * @covers WP_List_Util::pluck - * @covers ::wp_list_pluck - * - * @expectedIncorrectUsage WP_List_Util::pluck - * - * @param array $input An invalid input array. - */ - public function test_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input( $input ) { - $this->assertSame( array(), wp_list_pluck( $input, 'a_field' ) ); - } - - /** - * Tests that wp_list_pluck() throws _doing_it_wrong() with an index key and invalid input. - * - * @ticket 56650 - * - * @dataProvider data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input - * - * @covers WP_List_Util::pluck - * @covers ::wp_list_pluck - * - * @expectedIncorrectUsage WP_List_Util::pluck - * - * @param array $input An invalid input array. - */ - public function test_wp_list_pluck_should_throw_doing_it_wrong_with_index_key_and_invalid_input( $input ) { - $this->assertSame( array(), wp_list_pluck( $input, 'a_field', 'an_index_key' ) ); - } - - /** - * Data provider that provides invalid input arrays. - * - * @return array[] - */ - public function data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input() { - return array( - 'int[] 0' => array( array( 0 ) ), - 'int[] 1' => array( array( 1 ) ), - 'int[] -1' => array( array( -1 ) ), - 'float[] 0.0' => array( array( 0.0 ) ), - 'float[] 1.0' => array( array( 1.0 ) ), - 'float[] -1.0' => array( array( -1.0 ) ), - 'string[] and empty string' => array( array( '' ) ), - 'string[] and "0"' => array( array( '0' ) ), - 'string[] and "1"' => array( array( '1' ) ), - 'string[] and "-1"' => array( array( '-1' ) ), - 'array and null' => array( array( null ) ), - 'array and false' => array( array( false ) ), - 'array and true' => array( array( true ) ), - ); - } - +class WpListUtilSortTest extends WP_UnitTestCase { /** * @ticket 55300 * - * @covers WP_List_Util::sort - * @covers ::wp_list_sort */ public function test_wp_list_util_sort_simple() { $expected = array( @@ -212,8 +58,6 @@ public function test_wp_list_util_sort_simple() { * @dataProvider data_wp_list_util_sort_object_arrays * @dataProvider data_wp_list_util_sort_non_existent_orderby_fields * - * @covers WP_List_Util::sort - * @covers ::wp_list_sort * * @param array $expected The expected array. * @param array $target_array The array to create a list from. diff --git a/tests/phpunit/tests/functions/wpMysqlWeek.php b/tests/phpunit/tests/wp-includes/functions/WpMysqlWeekTest.php similarity index 90% rename from tests/phpunit/tests/functions/wpMysqlWeek.php rename to tests/phpunit/tests/wp-includes/functions/WpMysqlWeekTest.php index 3649ee6c4c98c..8ee4e0dad9eb5 100644 --- a/tests/phpunit/tests/functions/wpMysqlWeek.php +++ b/tests/phpunit/tests/wp-includes/functions/WpMysqlWeekTest.php @@ -1,13 +1,18 @@ _baba = 5; diff --git a/tests/phpunit/tests/functions/wpParseIdList.php b/tests/phpunit/tests/wp-includes/functions/WpParseIdListTest.php similarity index 96% rename from tests/phpunit/tests/functions/wpParseIdList.php rename to tests/phpunit/tests/wp-includes/functions/WpParseIdListTest.php index 326fc2d2adffb..cc336c3728464 100644 --- a/tests/phpunit/tests/functions/wpParseIdList.php +++ b/tests/phpunit/tests/wp-includes/functions/WpParseIdListTest.php @@ -1,13 +1,18 @@ assertSame( $expected_result, $actual_result ); + } + + /** + * Data provider for `test_wp_privacy_anonymize_ip()`. + * + * @since 4.9.6 Moved from `Test_WP_Community_Events::data_get_unsafe_client_ip_anonymization()`. + * + * @return array { + * @type array { + * @type string $raw_ip Raw IP address. + * @type string $expected_result Expected result. + * } + * } + */ + public function data_wp_privacy_anonymize_ip() { + return array( + // Invalid IP. + array( + null, + '0.0.0.0', + ), + array( + false, + '0.0.0.0', + ), + array( + true, + '0.0.0.0', + ), + array( + 0, + '0.0.0.0', + ), + array( + 1, + '0.0.0.0', + ), + array( + '', + '0.0.0.0', + ), + array( + '0.0.0.0.0', + '0.0.0.0', + ), + array( + '0000:0000:0000:0000:0000:0000:0127:2258', + '::', + ), + // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. + array( + 'unknown', + '0.0.0.0', + ), + // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. + array( + 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001', + '::', + ), + // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. + array( + 'or=\"1000:0000:0000:0000:0000:0000:0000:0001', + '::', + ), + // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. + array( + '1000:0000:0000:0000:0000:0000:0000:0001or=\"', + '::', + ), + // IPv4, no port. + array( + '10.20.30.45', + '10.20.30.0', + ), + // IPv4, port. + array( + '10.20.30.45:20000', + '10.20.30.0', + ), + // IPv4, netmask. + array( + '10.20.30.45/24', + '10.20.30.0', + ), + // IPv6, no port, reducible representation. + array( + '0000:0000:0000:0000:0000:0000:0000:0001', + '::', + ), + // IPv6, port, reducible representation. + array( + '[0000:0000:0000:0000:0000:0000:0000:0001]:1234', + '::', + ), + // IPv6, no port, reduced representation. + array( + '::', + '::', + ), + // IPv6, no port, reduced representation. + array( + '::1', + '::', + ), + // IPv6, port, reduced representation. + array( + '[::]:20000', + '::', + ), + // IPv6, address brackets without port delimiter and number, reduced representation. + array( + '[::1]', + '::', + ), + // IPv6, no port, compatibility mode. + array( + '::ffff:10.15.20.25', + '::ffff:10.15.20.0', + ), + // IPv6, port, compatibility mode. + array( + '[::FFFF:10.15.20.25]:30000', + '::ffff:10.15.20.0', + ), + // IPv6, no port, compatibility mode shorthand. + array( + '::127.0.0.1', + '::ffff:127.0.0.0', + ), + // IPv6, port, compatibility mode shorthand. + array( + '[::127.0.0.1]:30000', + '::ffff:127.0.0.0', + ), + ); + } + + /** + * Tests that wp_privacy_anonymize_ip() properly anonymizes all possible IP address formats. + * + * @dataProvider data_wp_privacy_anonymize_ip_with_inet_dependency + * + * @ticket 41083 + * @ticket 43545 + * @requires function inet_ntop + * @requires function inet_pton + * + * + * @param string $raw_ip Raw IP address. + * @param string $expected_result Expected result. + */ + public function test_wp_privacy_anonymize_ip_with_inet_dependency( $raw_ip, $expected_result ) { + $this->test_wp_privacy_anonymize_ip( $raw_ip, $expected_result ); + } + + /** + * Data provider for `test_wp_privacy_anonymize_ip()`. + * + * @since 4.9.6 Moved from `Test_WP_Community_Events::data_get_unsafe_client_ip_anonymization()`. + * + * @return array { + * @type array { + * @type string $raw_ip Raw IP address. + * @type string $expected_result Expected result. + * } + * } + */ + public function data_wp_privacy_anonymize_ip_with_inet_dependency() { + return array( + // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. + array( + 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]:400', + '1000::', + ), + // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. + array( + 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]', + '1000::', + ), + // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. + array( + 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]400', + '1000::', + ), + // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. + array( + '[1000:0000:0000:0000:0000:0000:0000:0001]:235\"or=', + '1000::', + ), + // IPv6, no port. + array( + '2a03:2880:2110:df07:face:b00c::1', + '2a03:2880:2110:df07::', + ), + // IPv6, port. + array( + '[2a03:2880:2110:df07:face:b00c::1]:20000', + '2a03:2880:2110:df07::', + ), + // IPv6, no port, partially reducible representation. + array( + '1000:0000:0000:0000:0000:0000:0000:0001', + '1000::', + ), + // IPv6, port, partially reducible representation. + array( + '[1000:0000:0000:0000:0000:0000:0000:0001]:5678', + '1000::', + ), + // IPv6 with reachability scope. + array( + 'fe80::b059:65f4:e877:c40%16', + 'fe80::', + ), + // IPv6 with reachability scope. + array( + 'FE80::B059:65F4:E877:C40%eth0', + 'fe80::', + ), + ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/functions/wpRaiseMemoryLimit.php b/tests/phpunit/tests/wp-includes/functions/WpRaiseMemoryLimitTest.php similarity index 85% rename from tests/phpunit/tests/functions/wpRaiseMemoryLimit.php rename to tests/phpunit/tests/wp-includes/functions/WpRaiseMemoryLimitTest.php index 4bb9dcc016e2c..18c61d991f5ad 100644 --- a/tests/phpunit/tests/functions/wpRaiseMemoryLimit.php +++ b/tests/phpunit/tests/wp-includes/functions/WpRaiseMemoryLimitTest.php @@ -1,11 +1,16 @@ array( array( 'value' => 123 ) ), 'float' => array( array( 'value' => 1.23 ) ), 'boolean' => array( array( 'value' => true ) ), - 'object' => array( array( 'value' => new StdClass() ) ), + 'object' => array( array( 'value' => new stdClass() ) ), 'null' => array( array( 'value' => null ) ), 'multiple values' => array( array( @@ -87,7 +95,7 @@ public function data_wp_unique_id_from_values() { 'value2' => 123, 'value3' => 1.23, 'value4' => true, - 'value5' => new StdClass(), + 'value5' => new stdClass(), 'value6' => null, ), ), @@ -100,7 +108,7 @@ public function data_wp_unique_id_from_values() { ), 'list2' => array( 'value4' => true, - 'value5' => new StdClass(), + 'value5' => new stdClass(), 'value6' => null, ), ), @@ -147,7 +155,7 @@ public function data_wp_unique_id_from_values_invalid_data() { 'integer' => array( 123 ), 'float' => array( 1.23 ), 'boolean' => array( true ), - 'object' => array( new StdClass() ), + 'object' => array( new stdClass() ), 'null' => array( null ), ); } diff --git a/tests/phpunit/tests/functions/wpUniqueId.php b/tests/phpunit/tests/wp-includes/functions/WpUniqueIdTest.php similarity index 84% rename from tests/phpunit/tests/functions/wpUniqueId.php rename to tests/phpunit/tests/wp-includes/functions/WpUniqueIdTest.php index 931ea82b136f3..b144f7290b922 100644 --- a/tests/phpunit/tests/functions/wpUniqueId.php +++ b/tests/phpunit/tests/wp-includes/functions/WpUniqueIdTest.php @@ -1,15 +1,19 @@ title + category,category1 + content + '; + + /** + * Tests that xmlrpc_getpostcategory() returns post categories if found in the XML. + */ + public function test_xmlrpc_getpostcategory() { + $this->assertSame( array( 'category', 'category1' ), xmlrpc_getpostcategory( $this->test_content ) ); + } + + /** + * Tests that xmlrpc_getpostcategory() defaults to the `$post_default_category` global. + */ + public function test_xmlrpc_getpostcategory_default() { + global $post_default_category; + + $post_default_category = 'post_default_category'; + + $this->assertSame( 'post_default_category', xmlrpc_getpostcategory( '' ) ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/wp-includes/functions/XmlrpcGetposttitleTest.php b/tests/phpunit/tests/wp-includes/functions/XmlrpcGetposttitleTest.php new file mode 100644 index 0000000000000..8bebd8fba2daf --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/XmlrpcGetposttitleTest.php @@ -0,0 +1,42 @@ +title + category,category1 + content + '; + + /** + * Tests that xmlrpc_getposttitle() returns the post title if found in the XML. + */ + public function test_xmlrpc_getposttitle() { + $this->assertSame( 'title', xmlrpc_getposttitle( $this->test_content ) ); + } + + /** + * Tests that xmlrpc_getposttitle() defaults to the `$post_default_title` global. + */ + public function test_xmlrpc_getposttitle_default() { + global $post_default_title; + + $post_default_title = 'post_default_title'; + + $this->assertSame( 'post_default_title', xmlrpc_getposttitle( '' ) ); + } +} \ No newline at end of file diff --git a/tests/phpunit/tests/wp-includes/functions/XmlrpcRemovepostdataTest.php b/tests/phpunit/tests/wp-includes/functions/XmlrpcRemovepostdataTest.php new file mode 100644 index 0000000000000..d640ef9bf15bc --- /dev/null +++ b/tests/phpunit/tests/wp-includes/functions/XmlrpcRemovepostdataTest.php @@ -0,0 +1,30 @@ +title + category,category1 + content + '; + + /** + * Tests that xmlrpc_removepostdata() returns XML content without title and category elements. + */ + public function test_xmlrpc_removepostdata() { + $this->assertSame( 'content', xmlrpc_removepostdata( $this->test_content ) ); + } +} \ No newline at end of file