Skip to content

Commit

Permalink
Font Library: ensure merged fontFace data is enconded as an array ins…
Browse files Browse the repository at this point in the history
…tead of an object (#54435)

* ensure merged fontFace data is enconded as an array instead of an object

* format php
  • Loading branch information
matiasbenedetto committed Sep 13, 2023
1 parent 0c77c5b commit fec5ffc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
}

/**
Expand Down Expand Up @@ -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',
),
),
),
),
);
}
}

0 comments on commit fec5ffc

Please sign in to comment.