Skip to content

Commit

Permalink
Move mime-type collection generation to a function that can be tested… (
Browse files Browse the repository at this point in the history
#54844)

* Move mime-type collection generation to a function that can be tested.  Refactored to use that function.

* linting changes

* Add unit tests to mime type getter

* Fixed linting errors

* test the entire output array and replace assertTrue by assertEquals

* fixing docs

---------

Co-authored-by: Matias Benedetto <[email protected]>
  • Loading branch information
2 people authored and mikachan committed Sep 29, 2023
1 parent 949b28c commit 23ad86f
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public static function merge_fonts_data( $font1, $font2 ) {
* @return bool True if the file has a font MIME type, false otherwise.
*/
public static function has_font_mime_type( $filepath ) {
$filetype = wp_check_filetype( $filepath, WP_Font_Library::ALLOWED_FONT_MIME_TYPES );
$allowed_mime_types = WP_Font_Library::get_expected_font_mime_types_per_php_version();
$filetype = wp_check_filetype( $filepath, $allowed_mime_types );

return in_array( $filetype['type'], WP_Font_Library::ALLOWED_FONT_MIME_TYPES, true );
return in_array( $filetype['type'], $allowed_mime_types, true );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private function get_upload_overrides( $filename ) {
// Seems mime type for files that are not images cannot be tested.
// See wp_check_filetype_and_ext().
'test_type' => true,
'mimes' => WP_Font_Library::ALLOWED_FONT_MIME_TYPES,
'mimes' => WP_Font_Library::get_expected_font_mime_types_per_php_version(),
'unique_filename_callback' => static function () use ( $filename ) {
// Keep the original filename.
return $filename;
Expand Down
30 changes: 22 additions & 8 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@
*/
class WP_Font_Library {

const PHP_7_TTF_MIME_TYPE = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf';
/**
* Provide the expected mime-type value for font files per-PHP release. Due to differences in the values returned these values differ between PHP versions.
*
* This is necessary until a collection of valid mime-types per-file extension can be provided to 'upload_mimes' filter.
*
* @since 6.4.0
*
* @param array $php_version_id The version of PHP to provide mime types for. The default is the current PHP version.
*
* @return Array A collection of mime types keyed by file extension.
*/
public static function get_expected_font_mime_types_per_php_version( $php_version_id = PHP_VERSION_ID ) {

const ALLOWED_FONT_MIME_TYPES = array(
'otf' => 'font/otf',
'ttf' => PHP_VERSION_ID >= 70400 ? 'font/sfnt' : self::PHP_7_TTF_MIME_TYPE,
'woff' => PHP_VERSION_ID >= 80100 ? 'font/woff' : 'application/font-woff',
'woff2' => PHP_VERSION_ID >= 80100 ? 'font/woff2' : 'application/font-woff2',
);
$php_7_ttf_mime_type = $php_version_id >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf';

return array(
'otf' => 'font/otf',
'ttf' => $php_version_id >= 70400 ? 'font/sfnt' : $php_7_ttf_mime_type,
'woff' => $php_version_id >= 80100 ? 'font/woff' : 'application/font-woff',
'woff2' => $php_version_id >= 80100 ? 'font/woff2' : 'application/font-woff2',
);
}

/**
* Font collections.
Expand Down Expand Up @@ -130,6 +144,6 @@ public static function set_upload_dir( $defaults ) {
* @return array Modified upload directory.
*/
public static function set_allowed_mime_types( $mime_types ) {
return array_merge( $mime_types, self::ALLOWED_FONT_MIME_TYPES );
return array_merge( $mime_types, self::get_expected_font_mime_types_per_php_version() );
}
}
90 changes: 90 additions & 0 deletions phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Test WP_Font_Family_Utils::get_expected_font_mime_types_per_php_version().
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*
* @covers WP_Font_Family_Utils::get_expected_font_mime_types_per_php_version
*/
class Tests_Fonts_WpFontsFamilyUtils_GetMimeTypes extends WP_UnitTestCase {

/**
*
* @dataProvider data_should_supply_correct_mime_type_for_php_version
*
* @param array $php_version_id PHP_VERSION_ID value.
* @param array $expected Expected mime types.
*/
public function test_should_supply_correct_mime_type_for_php_version( $php_version_id, $expected ) {
$mimes = WP_Font_Library::get_expected_font_mime_types_per_php_version( $php_version_id );
$this->assertEquals( $mimes, $expected );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_should_supply_correct_mime_type_for_php_version() {
return array(
'version 7.2' => array(
'php_version_id' => 70200,
'expected' => array(
'otf' => 'font/otf',
'ttf' => 'application/x-font-ttf',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
),
),
'version 7.3' => array(
'php_version_id' => 70300,
'expected' => array(
'otf' => 'font/otf',
'ttf' => 'application/font-sfnt',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
),
),
'version 7.4' => array(
'php_version_id' => 70400,
'expected' => array(
'otf' => 'font/otf',
'ttf' => 'font/sfnt',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
),
),
'version 8.0' => array(
'php_version_id' => 80000,
'expected' => array(
'otf' => 'font/otf',
'ttf' => 'font/sfnt',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
),
),
'version 8.1' => array(
'php_version_id' => 80100,
'expected' => array(
'otf' => 'font/otf',
'ttf' => 'font/sfnt',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
),
),
'version 8.2' => array(
'php_version_id' => 80200,
'expected' => array(
'otf' => 'font/otf',
'ttf' => 'font/sfnt',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
),
),
);
}
}

0 comments on commit 23ad86f

Please sign in to comment.