From fec39fabef8284a6c99b7a0bb88c04560993ce61 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 13 Sep 2023 13:16:47 -0300 Subject: [PATCH 1/2] ensure merged fontFace data is enconded as an array instead of an object --- .../class-wp-font-family-utils.php | 2 +- .../wpFontFamilyUtils/mergeFontsData.php | 72 ++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-family-utils.php b/lib/experimental/fonts/font-library/class-wp-font-family-utils.php index 607360d0c7e66..200cfef22289f 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-family-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-family-utils.php @@ -71,7 +71,7 @@ public static function merge_fonts_data( $font1, $font2 ) { $unique_faces = array_map( 'unserialize', $unique_serialized_faces ); $merged_font = array_merge( $font1, $font2 ); - $merged_font['fontFace'] = $unique_faces; + $merged_font['fontFace'] = array_values( $unique_faces ); return $merged_font; } diff --git a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php b/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php index b1e9bdc30aec4..eba2d4c8914e8 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php +++ b/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php @@ -76,9 +76,10 @@ public function data_should_fail_merge() { * @param array $expected_result Expected result. */ public function test_should_merge( array $font1, array $font2, array $expected_result ) { - $actual = WP_Font_Family_Utils::merge_fonts_data( $font1, $font2 ); - - $this->assertSame( $expected_result, $actual ); + $result = WP_Font_Family_Utils::merge_fonts_data( $font1, $font2 ); + $this->assertSame( $expected_result, $result, 'Merged font data should match expected result.' ); + $json_result = wp_json_encode( $result ); + $this->assertStringContainsString('"fontFace":[', $json_result, 'fontFace data should be enconded as an array and not an object.' ); } /** @@ -229,6 +230,71 @@ public function data_should_merge() { ), ), ), + 'repeated font faces with non consecutive index positions' => 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', + ), + + ), + ), + 'font2' => array( + 'slug' => 'piazzolla', + 'fontFamily' => 'Piazzolla', + 'fontFace' => array( + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'normal', + 'fontWeight' => '600', + 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', + ), + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '400', + 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', + ), + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '500', + 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', + ), + ), + ), + 'expected_result' => 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', + ), + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'normal', + 'fontWeight' => '600', + 'src' => 'http://example.com/fonts/piazzolla_600_normal.ttf', + ), + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '500', + 'src' => 'http://example.com/fonts/piazzolla_500_italic.ttf', + ), + ), + ), + ) ); } } From 6d894c4a7e0c19a7a46d0d57da7c187f6f4e3efd Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 13 Sep 2023 13:30:11 -0300 Subject: [PATCH 2/2] format php --- .../font-library/wpFontFamilyUtils/mergeFontsData.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php b/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php index eba2d4c8914e8..21517a0970f93 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php +++ b/phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php @@ -78,8 +78,8 @@ public function data_should_fail_merge() { public function test_should_merge( array $font1, array $font2, array $expected_result ) { $result = WP_Font_Family_Utils::merge_fonts_data( $font1, $font2 ); $this->assertSame( $expected_result, $result, 'Merged font data should match expected result.' ); - $json_result = wp_json_encode( $result ); - $this->assertStringContainsString('"fontFace":[', $json_result, 'fontFace data should be enconded as an array and not an object.' ); + $json_result = wp_json_encode( $result ); + $this->assertStringContainsString( '"fontFace":[', $json_result, 'fontFace data should be enconded as an array and not an object.' ); } /** @@ -230,7 +230,7 @@ public function data_should_merge() { ), ), ), - 'repeated font faces with non consecutive index positions' => array ( + 'repeated font faces with non consecutive index positions' => array( 'font1' => array( 'slug' => 'piazzolla', 'name' => 'Piazzolla', @@ -294,7 +294,7 @@ public function data_should_merge() { ), ), ), - ) + ), ); } }