From b9d87adbd6bc783aab029ed2fa218ea2384bdf12 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 16 Aug 2023 10:12:46 -0500 Subject: [PATCH] WP_Font_Family_Utils: split tests --- .../getFilenameFromFontFace-test.php | 64 ++++++ .../wpFonFamilyUtils/hasFontMimeType-test.php | 61 ++++++ .../mergeFontsData-test.php} | 188 +++++------------- 3 files changed, 176 insertions(+), 137 deletions(-) create mode 100644 phpunit/fonts-library/wpFonFamilyUtils/getFilenameFromFontFace-test.php create mode 100644 phpunit/fonts-library/wpFonFamilyUtils/hasFontMimeType-test.php rename phpunit/fonts-library/{class-wp-font-family-utils-test.php => wpFonFamilyUtils/mergeFontsData-test.php} (59%) diff --git a/phpunit/fonts-library/wpFonFamilyUtils/getFilenameFromFontFace-test.php b/phpunit/fonts-library/wpFonFamilyUtils/getFilenameFromFontFace-test.php new file mode 100644 index 0000000000000..2ae4c3e5cedcf --- /dev/null +++ b/phpunit/fonts-library/wpFonFamilyUtils/getFilenameFromFontFace-test.php @@ -0,0 +1,64 @@ +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', + ), + ); + } +} diff --git a/phpunit/fonts-library/wpFonFamilyUtils/hasFontMimeType-test.php b/phpunit/fonts-library/wpFonFamilyUtils/hasFontMimeType-test.php new file mode 100644 index 0000000000000..582ff8dd650e2 --- /dev/null +++ b/phpunit/fonts-library/wpFonFamilyUtils/hasFontMimeType-test.php @@ -0,0 +1,61 @@ +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' ), + ); + } +} diff --git a/phpunit/fonts-library/class-wp-font-family-utils-test.php b/phpunit/fonts-library/wpFonFamilyUtils/mergeFontsData-test.php similarity index 59% rename from phpunit/fonts-library/class-wp-font-family-utils-test.php rename to phpunit/fonts-library/wpFonFamilyUtils/mergeFontsData-test.php index 775c88ce8b596..d3fc0d26e406a 100644 --- a/phpunit/fonts-library/class-wp-font-family-utils-test.php +++ b/phpunit/fonts-library/wpFonFamilyUtils/mergeFontsData-test.php @@ -1,82 +1,31 @@ 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' ); } @@ -85,51 +34,51 @@ 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 ); } /** @@ -137,11 +86,9 @@ public function test_merge_fonts_data( $are_mergeable, $font1, $font2, $expected * * @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', @@ -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', @@ -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', - ), - ); } - }