Skip to content

Commit

Permalink
WP_Font_Family_Utils: split tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Aug 16, 2023
1 parent b153978 commit b9d87ad
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Test WP_Font_Family_Utils::get_filename_from_font_face().
*
* @package WordPress
* @subpackage Fonts Library
*
* @group fonts
* @group fonts-library
*
* @covers WP_Font_Family_Utils::get_filename_from_font_face
*/
class Tests_Fonts_WpFontsFamilyUtils_GetFilenameFromFontFace extends WP_UnitTestCase {

/**
* @dataProvider data_should_get_filename
*
* @param string $slug Font slug.
* @param array $font_face Font face data in theme.json format.
* @param string $suffix Suffix added to the resulting filename.
* @param string $expected Expected filename.
*/
public function test_should_get_filename( $slug, $font_face, $suffix, $expected ) {
$this->assertSame(
$expected,
WP_Font_Family_Utils::get_filename_from_font_face(
$slug,
$font_face,
$font_face['src'],
$suffix
)
);
}

/**
* Data provider.
*
* @return array[]
*/
public function data_should_get_filename() {
return array(
'piazzolla' => array(
'slug' => 'piazzolla',
'font_face' => array(
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'http://example.com/fonts/font_file.ttf',
),
'suffix' => '',
'expected_file_name' => 'piazzolla_italic_400.ttf',
),
'inter' => array(
'slug' => 'inter',
'font_face' => array(
'fontStyle' => 'normal',
'fontWeight' => '600',
'src' => 'http://example.com/fonts/font_file.otf',
),
'suffix' => '',
'expected_file_name' => 'inter_normal_600.otf',
),
);
}
}
61 changes: 61 additions & 0 deletions phpunit/fonts-library/wpFonFamilyUtils/hasFontMimeType-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Test WP_Font_Family_Utils::has_font_mime_type().
*
* @package WordPress
* @subpackage Fonts Library
*
* @group fonts
* @group fonts-library
*
* @covers WP_Font_Family_Utils::has_font_mime_type
*/
class Tests_Fonts_WpFontsFamilyUtils_HasFontMimeType extends WP_UnitTestCase {

/**
* @dataProvider data_should_succeed_when_has_mime_type
*
* @param string $font_file Font file path.
*/
public function test_should_succeed_when_has_mime_type( $font_file ) {
$this->assertTrue( WP_Font_Family_Utils::has_font_mime_type( $font_file ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_should_succeed_when_has_mime_type() {
return array(
'ttf' => array( '/temp/piazzolla_400_italic.ttf' ),
'otf' => array( '/temp/piazzolla_400_italic.otf' ),
'woff' => array( '/temp/piazzolla_400_italic.woff' ),
'woff2' => array( '/temp/piazzolla_400_italic.woff2' ),
);
}

/**
* @dataProvider data_should_fail_when_mime_not_supported
*
* @param string $font_file Font file path.
*/
public function test_should_fail_when_mime_not_supported( $font_file ) {
$this->assertFalse( WP_Font_Family_Utils::has_font_mime_type( $font_file ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_should_fail_when_mime_not_supported() {
return array(
'exe' => array( '/temp/test.exe' ),
'md' => array( '/temp/license.md' ),
'php' => array( '/temp/test.php' ),
'txt' => array( '/temp/test.txt' ),
'zip' => array( '/temp/lato.zip' ),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,82 +1,31 @@
<?php
/**
* Tests for Font Family class
* Test WP_Font_Family_Utils::merge_fonts_data().
*
* @package Gutenberg
* @package WordPress
* @subpackage Fonts Library
*
* @group fonts
* @group fonts-library
*
* @covers WP_Font_Family_Utils::merge_fonts_data
*/

/**
* @coversDefaultClass WP_Font_Family_Utils
*/
class WP_Font_Family_Utils_Test extends WP_UnitTestCase {

/**
* @covers ::has_font_mime_type
*
* @dataProvider data_has_font_mime_type_fixtures
*
* @param string $font_file Font file path.
* @param bool $expected Expected result.
*/
public function test_has_font_mime_type( $font_file, $expected ) {
$this->assertSame( $expected, WP_Font_Family_Utils::has_font_mime_type( $font_file ) );
}
class Tests_Fonts_WpFontsFamilyUtils_MergeFontsData extends WP_UnitTestCase {

/**
* Data provider.
* @dataProvider data_should_fail_merge
*
* @return array[]
* @param array $font1 First font data in theme.json format.
* @param array $font2 Second font data in theme.json format.
*/
public function data_has_font_mime_type_fixtures() {
return array(
'ttf' => array(
'font_file' => '/temp/piazzolla_400_italic.ttf',
'expected' => true,
),
'otf' => array(
'font_file' => '/temp/piazzolla_400_italic.otf',
'expected' => true,
),
'woff' => array(
'font_file' => '/temp/piazzolla_400_italic.woff',
'expected' => true,
),
'woff2' => array(
'font_file' => '/temp/piazzolla_400_italic.woff2',
'expected' => true,
),
'exe' => array(
'font_file' => '/temp/piazzolla_400_italic.exe',
'expected' => false,
),
'php' => array(
'font_file' => '/temp/piazzolla_400_italic.php',
'expected' => false,
),
);
}

/**
* @covers ::get_filename_from_font_face
*
* @dataProvider data_get_filename_from_font_face_fixtures
*
* @param string $slug Font slug.
* @param array $font_face Font face data in theme.json format.
* @param string $suffix Suffix added to the resulting filename. Default empty string.
* @param string $expected_file_name Expected file name.
*/
public function test_get_filename_from_font_face( $slug, $font_face, $suffix, $expected_file_name ) {
public function test_should_fail_merge( $font1, $font2 ) {
$actual = WP_Font_Family_Utils::merge_fonts_data( $font1, $font2 );

$this->assertWPError( $actual, 'WP_Error should have been returned' );
$this->assertSame(
$expected_file_name,
WP_Font_Family_Utils::get_filename_from_font_face(
$slug,
$font_face,
$font_face['src'],
$suffix
)
array( 'fonts_must_have_same_slug' => array( 'Fonts must have the same slug to be merged.' ) ),
$actual->errors,
'WP_Error should have "fonts_must_have_same_slug" error'
);
}

Expand All @@ -85,63 +34,61 @@ public function test_get_filename_from_font_face( $slug, $font_face, $suffix, $e
*
* @return array[]
*/
public function data_get_filename_from_font_face_fixtures() {
public function data_should_fail_merge() {
return array(
'piazzolla' => array(
'slug' => 'piazzolla',
'font_face' => array(
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'http://example.com/fonts/font_file.ttf',
'different slugs' => array(
'font1' => array(
'slug' => 'piazzolla',
'name' => 'Piazzolla',
'fontFamily' => 'Piazzolla',
'fontFace' => array(
array(
'fontFamily' => 'Piazzolla',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf',
),
),
),
'suffix' => '',
'expected_file_name' => 'piazzolla_italic_400.ttf',
),
'inter' => array(
'slug' => 'inter',
'font_face' => array(
'fontStyle' => 'normal',
'fontWeight' => '600',
'src' => 'http://example.com/fonts/font_file.otf',
'font2' => array(
'slug' => 'inter',
'fontFamily' => 'Inter',
'fontFace' => array(
array(
'fontFamily' => 'Inter',
'fontStyle' => 'normal',
'fontWeight' => '700',
'src' => 'http://example.com/fonts/inter_700_normal.ttf',
),
),
),
'suffix' => '',
'expected_file_name' => 'inter_normal_600.otf',
'expected_result' => 'WP_Error',
),
);
}


/**
* @covers ::merge_fonts_data
*
* @dataProvider data_merge_fonts_data_fixtures
* @dataProvider data_should_merge
*
* @param bool $are_mergeable Whether the fonts are mergeable.
* @param array $font1 First font data in theme.json format.
* @param array $font2 Second font data in theme.json format.
* @param array $expected_result Expected result.
*/
public function test_merge_fonts_data( $are_mergeable, $font1, $font2, $expected_result ) {
// Fonts with same slug should be merged.
$merged_font = WP_Font_Family_Utils::merge_fonts_data( $font1, $font2 );
public function test_should_merge( array $font1, array $font2, array $expected_result ) {
$actual = WP_Font_Family_Utils::merge_fonts_data( $font1, $font2 );

if ( $are_mergeable ) {
$this->assertNotWPError( $merged_font, 'Fonts could not be merged' );
$this->assertSame( $expected_result, $merged_font, 'The font family data and font faces merged not as expected' );
} else {
$this->assertWPError( $merged_font, 'Merging non mergeable fonts (diifferent slug) should have failed.' );
}
$this->assertSame( $expected_result, $actual );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_merge_fonts_data_fixtures() {
public function data_should_merge() {
return array(

'mergeable_fonts' => array(
'are_mergeable' => true,
'with different font faces' => array(
'font1' => array(
'slug' => 'piazzolla',
'name' => 'Piazzolla',
Expand Down Expand Up @@ -212,8 +159,7 @@ public function data_merge_fonts_data_fixtures() {
),
),

'mergeable_fonts_with_repeated_font_faces' => array(
'are_mergeable' => true,
'repeated font faces' => array(
'font1' => array(
'slug' => 'piazzolla',
'name' => 'Piazzolla',
Expand Down Expand Up @@ -283,38 +229,6 @@ public function data_merge_fonts_data_fixtures() {
),
),
),

'non_mergeable_fonts' => array(
'are_mergeable' => false,
'font1' => array(
'slug' => 'piazzolla',
'name' => 'Piazzolla',
'fontFamily' => 'Piazzolla',
'fontFace' => array(
array(
'fontFamily' => 'Piazzolla',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf',
),
),
),
'font2' => array(
'slug' => 'inter',
'fontFamily' => 'Inter',
'fontFace' => array(
array(
'fontFamily' => 'Inter',
'fontStyle' => 'normal',
'fontWeight' => '700',
'src' => 'http://example.com/fonts/inter_700_normal.ttf',
),
),
),
'expected_result' => 'WP_Error',
),

);
}

}

0 comments on commit b9d87ad

Please sign in to comment.