From f2a931e31f2c90812a543e3b648ac8c69f0ca00e Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Thu, 14 Sep 2023 23:59:07 -0700 Subject: [PATCH 01/10] Add get_intersecting_font_faces to determine duplicates to be removed. --- .../font-library/class-wp-font-family.php | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-family.php b/lib/experimental/fonts/font-library/class-wp-font-family.php index cacf504382cce..b123b525d3830 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-family.php +++ b/lib/experimental/fonts/font-library/class-wp-font-family.php @@ -379,6 +379,20 @@ private function download_or_move_font_faces( $files ) { // (for example to install fonts that use a remote url). $new_font_face = $font_face; + $font_face_is_repeated = false; + + // If the font face has the same fontStyle and fontWeight as an existing, continue. + foreach ( $new_font_faces as $font_to_compare ) { + if ( $new_font_face['fontStyle'] === $font_to_compare['fontStyle'] && + $new_font_face['fontWeight'] === $font_to_compare['fontWeight'] ) { + $font_face_is_repeated = true; + } + } + + if ( $font_face_is_repeated ) { + continue; + } + // If installing google fonts, download the font face assets. if ( ! empty( $font_face['downloadFromUrl'] ) ) { $new_font_face = $this->download_font_face_assets( $new_font_face ); @@ -482,6 +496,29 @@ private function create_font_post() { return $post_id; } + /** + * Gets the font faces that are in both the existing and incoming font families. + * + * @since 6.4.0 + * + * @param array $existing The existing font faces. + * @param array $incoming The incoming font faces. + * @return array The font faces that are in both the existing and incoming font families. + */ + private function get_intersecting_font_faces( $existing, $incoming ) { + $intersecting = array(); + foreach ( $existing as $existing_face ) { + foreach ( $incoming as $incoming_face ) { + if ( $incoming_face['fontStyle'] === $existing_face['fontStyle'] && + $incoming_face['fontWeight'] === $existing_face['fontWeight'] && + $incoming_face['src'] !== $existing_face['src'] ) { + $intersecting[] = $existing_face; + } + } + } + return $intersecting; + } + /** * Updates a post for a font family. * @@ -493,7 +530,21 @@ private function create_font_post() { private function update_font_post( $post ) { $post_font_data = json_decode( $post->post_content, true ); $new_data = WP_Font_Family_Utils::merge_fonts_data( $post_font_data, $this->data ); - $this->data = $new_data; + $intersecting = $this->get_intersecting_font_faces( $post_font_data['fontFace'], $new_data['fontFace'] ); + + if ( ! empty( $intersecting ) ) { + $serialized_font_faces = array_map( 'serialize', $new_data['fontFace'] ); + $serialized_intersecting = array_map( 'serialize', $intersecting ); + + $diff = array_diff( $serialized_font_faces, $serialized_intersecting ); + + $new_data['fontFace'] = array_values( array_map( 'unserialize', $diff ) ); + + foreach ( $intersecting as $intersect ) { + $this->delete_font_face_assets( $intersect ); + } + } + $this->data = $new_data; $post = array( 'ID' => $post->ID, From 6a3737616f96b70f8258f4bbf2002cf3f6d4df92 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Fri, 15 Sep 2023 00:24:47 -0700 Subject: [PATCH 02/10] Add duplicant font variant test. --- .../installFonts.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index 1813f535c109f..6b931ab6d7d02 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -414,4 +414,73 @@ public function data_install_with_improper_inputs() { ), ); } + + /** + * Tests that duplicate font faces with same font style and weight but different file extensions are merged. + * + * @dataProvider data_install_with_duplicate_font_faces + * @param array $font_families Font families to install in theme.json format. + * @param array $files Font files to install. + */ + public function test_install_with_duplicate_font_faces( $font_families, $files = array(), $expected_response ) { + $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); + $font_families_json = json_encode( $font_families ); + $install_request->set_param( 'fontFamilies', $font_families_json ); + $install_request->set_file_params( $files ); + + $response = rest_get_server()->dispatch( $install_request ); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + $data = $response->get_data(); + $this->assertCount( 1, $expected_response[0]['fontFace'], 'Duplicate font faces were not removed / added incorrectly.' ); + $this->assertStringEndsWith( $expected_response[0]['fontFace'][ 0 ]['src'], $data[0]['fontFace'][ 0 ]['src'], 'The src attribute does not match the expected font file' ); + } + + /** + * Data provider for test_install_with_duplicate_font_faces + */ + public function data_install_with_duplicate_font_faces() { + return array( + 'with duplicate font faces' => array( + 'font_families' => array( + array( + 'fontFamily' => 'Piazzolla', + 'slug' => 'piazzolla', + 'name' => 'Piazzolla', + 'fontFace' => array( + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '400', + 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', + 'downloadFromUrl' => 'http://example.com/fonts/piazzolla_400_italic.ttf', + ), + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '400', + 'src' => 'http://example.com/fonts/piazzolla_400_italic.woff', + 'downloadFromUrl' => 'http://example.com/fonts/piazzolla_400_italic.woff', + ), + ), + ), + ), + 'expected_response' => array( + array( + 'fontFamily' => 'Piazzolla', + 'slug' => 'piazzolla', + 'name' => 'Piazzolla', + 'fontFace' => array( + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '400', + 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', + ), + ), + ), + ), + 'files' => array(), + ), + ); + } } From 76612fff6da650be66973100ab26aafade93e41a Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Fri, 15 Sep 2023 00:29:38 -0700 Subject: [PATCH 03/10] Make linter happy. --- .../font-library/wpRestFontLibraryController/installFonts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index 6b931ab6d7d02..de83d67727efc 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -432,7 +432,7 @@ public function test_install_with_duplicate_font_faces( $font_families, $files = $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); $data = $response->get_data(); $this->assertCount( 1, $expected_response[0]['fontFace'], 'Duplicate font faces were not removed / added incorrectly.' ); - $this->assertStringEndsWith( $expected_response[0]['fontFace'][ 0 ]['src'], $data[0]['fontFace'][ 0 ]['src'], 'The src attribute does not match the expected font file' ); + $this->assertStringEndsWith( $expected_response[0]['fontFace'][0]['src'], $data[0]['fontFace'][0]['src'], 'The src attribute does not match the expected font file.' ); } /** From 567a403ce8fa448a11ee4084de19952f6a685539 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Fri, 15 Sep 2023 09:54:56 -0700 Subject: [PATCH 04/10] Use local fonts for duplicate font variant test. --- .../installFonts.php | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index de83d67727efc..d729728065047 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -439,6 +439,11 @@ public function test_install_with_duplicate_font_faces( $font_families, $files = * Data provider for test_install_with_duplicate_font_faces */ public function data_install_with_duplicate_font_faces() { + $temp_file_path1 = wp_tempnam( 'Piazzola1-' ); + file_put_contents( $temp_file_path1, 'Mocking file content' ); + $temp_file_path2 = wp_tempnam( 'Piazzola2-' ); + file_put_contents( $temp_file_path2, 'Mocking file content' ); + return array( 'with duplicate font faces' => array( 'font_families' => array( @@ -451,19 +456,33 @@ public function data_install_with_duplicate_font_faces() { 'fontFamily' => 'Piazzolla', 'fontStyle' => 'italic', 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', - 'downloadFromUrl' => 'http://example.com/fonts/piazzolla_400_italic.ttf', + 'uploadedFile' => 'files0', ), array( 'fontFamily' => 'Piazzolla', 'fontStyle' => 'italic', 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.woff', - 'downloadFromUrl' => 'http://example.com/fonts/piazzolla_400_italic.woff', + 'uploadedFile' => 'files1', ), ), ), ), + 'files' => array( + 'files0' => array( + 'name' => 'piazzola1.ttf', + 'type' => 'font/ttf', + 'tmp_name' => $temp_file_path1, + 'error' => 0, + 'size' => 123, + ), + 'files1' => array( + 'name' => 'piazzola1.woff', + 'type' => 'font/woff', + 'tmp_name' => $temp_file_path2, + 'error' => 0, + 'size' => 123, + ), + ), 'expected_response' => array( array( 'fontFamily' => 'Piazzolla', @@ -474,12 +493,11 @@ public function data_install_with_duplicate_font_faces() { 'fontFamily' => 'Piazzolla', 'fontStyle' => 'italic', 'fontWeight' => '400', - 'src' => 'http://example.com/fonts/piazzolla_400_italic.ttf', + 'src' => '/wp-content/fonts/piazzolla_italic_400.ttf', ), ), ), ), - 'files' => array(), ), ); } From 1d01973daf9ff86a03b8b1d32c8fa28fbf37a1e3 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Fri, 15 Sep 2023 09:58:25 -0700 Subject: [PATCH 05/10] Make linter happy. --- .../wpRestFontLibraryController/installFonts.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index d729728065047..41a9d5549bc1d 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -453,16 +453,16 @@ public function data_install_with_duplicate_font_faces() { 'name' => 'Piazzolla', 'fontFace' => array( array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'uploadedFile' => 'files0', + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '400', + 'uploadedFile' => 'files0', ), array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'uploadedFile' => 'files1', + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'italic', + 'fontWeight' => '400', + 'uploadedFile' => 'files1', ), ), ), From b07fdd083687c250a396588cb9b525d6c3cb568c Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Mon, 18 Sep 2023 09:21:46 -0700 Subject: [PATCH 06/10] Ensure fontFace exists and is not empty. --- .../fonts/font-library/class-wp-font-family.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-family.php b/lib/experimental/fonts/font-library/class-wp-font-family.php index b123b525d3830..2175f69678887 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-family.php +++ b/lib/experimental/fonts/font-library/class-wp-font-family.php @@ -530,9 +530,11 @@ private function get_intersecting_font_faces( $existing, $incoming ) { private function update_font_post( $post ) { $post_font_data = json_decode( $post->post_content, true ); $new_data = WP_Font_Family_Utils::merge_fonts_data( $post_font_data, $this->data ); - $intersecting = $this->get_intersecting_font_faces( $post_font_data['fontFace'], $new_data['fontFace'] ); + if ( isset( $post_font_data['fontFace'] ) && ! empty( $post_font_data['fontFace'] ) ) { + $intersecting = $this->get_intersecting_font_faces( $post_font_data['fontFace'], $new_data['fontFace'] ); + } - if ( ! empty( $intersecting ) ) { + if ( isset( $intersecting ) && ! empty( $intersecting ) ) { $serialized_font_faces = array_map( 'serialize', $new_data['fontFace'] ); $serialized_intersecting = array_map( 'serialize', $intersecting ); From 39bb1f9d26477d76cbedddf230b2cefda86f518e Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Mon, 18 Sep 2023 10:03:58 -0700 Subject: [PATCH 07/10] WIP test. --- .../installFonts.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index 41a9d5549bc1d..a32fa6c047e65 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -501,4 +501,29 @@ public function data_install_with_duplicate_font_faces() { ), ); } + + /** + * Tests that font faces with same font style and weight but different extensions, installed on separate requests, are merged. + * + * @dataProvider data_install_with_duplicate_font_faces + * @param array $font_families Font families to install in theme.json format. + * @param array $files Font files to install. + */ + public function test_install_duplicate_fonts_separate_requests( $font_families, $files = array(), $expected_response ) { + $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); + $font_families_json = json_encode( $font_families ); + $install_request->set_param( 'fontFamilies', $font_families_json ); + $install_request->set_file_params( $files ); + + $response = rest_get_server()->dispatch( $install_request ); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + $data = $response->get_data(); + + // Install the same font family again. + $response = rest_get_server()->dispatch( $install_request ); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + $data = $response->get_data(); + $this->assertCount( 1, $expected_response[0]['fontFace'], 'Duplicate font faces were not removed / added incorrectly.' ); + $this->assertStringEndsWith( $expected_response[0]['fontFace'][0]['src'], $data[0]['fontFace'][0]['src'], 'The src attribute does not match the expected font file.' ); + } } From ce5a4a9cd393ebed176c6bf2df9a72be86c1daf5 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Mon, 18 Sep 2023 10:56:08 -0700 Subject: [PATCH 08/10] Move tests to fontFamily class and add another test when installing more than one font face. --- .../font-library/wpFontFamily/install.php | 121 ++++++++++++++++++ .../installFonts.php | 112 ---------------- 2 files changed, 121 insertions(+), 112 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/install.php b/phpunit/tests/fonts/font-library/wpFontFamily/install.php index 42461b1f89e8f..d3315a690218e 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamily/install.php +++ b/phpunit/tests/fonts/font-library/wpFontFamily/install.php @@ -245,4 +245,125 @@ public function data_should_move_local_fontfaces() { ), ); } + + /** + * @dataProvider data_should_not_install_duplicate_fontfaces + * + * @param array $font_data Font family data in theme.json format. + * @param array $files_data Files data in $_FILES format. + * @param array $expected Expected font filename(s). + */ + public function test_should_not_install_duplicate_fontfaces( $font_data, array $files_data, array $expected ) { + // Set up the temporary files. + foreach ( $files_data as $file ) { + file_put_contents( $file['tmp_name'], 'Mocking file content' ); + } + + $font = new WP_Font_Family( $font_data ); + $font->install( $files_data ); + + foreach ( $expected as $font_file ) { + $font_file = path_join( static::$fonts_dir, $font_file ); + $this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" ); + } + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_not_install_duplicate_fontfaces() { + return array( + 'single unique font face' => array( + 'font_data' => array( + 'name' => 'Inter', + 'slug' => 'inter', + 'fontFamily' => 'Inter', + 'fontFace' => array( + array( + 'fontFamily' => 'Inter', + 'fontStyle' => 'italic', + 'fontWeight' => '900', + 'uploadedFile' => 'files0', + ), + array( + 'fontFamily' => 'Inter', + 'fontStyle' => 'italic', + 'fontWeight' => '900', + 'uploadedFile' => 'files1', + ), + ), + ), + 'files_data' => array( + 'files0' => array( + 'name' => 'inter1.ttf', + 'type' => 'font/ttf', + 'tmp_name' => wp_tempnam( 'Inter-' ), + 'error' => 0, + 'size' => 123, + ), + 'files1' => array( + 'name' => 'inter1.woff', + 'type' => 'font/woff', + 'tmp_name' => wp_tempnam( 'Inter-' ), + 'error' => 0, + 'size' => 123, + ), + ), + 'expected' => array( 'inter_italic_900.ttf' ), + ), + 'multiple unique font faces' => array( + 'font_data' => array( + 'name' => 'Lato', + 'slug' => 'lato', + 'fontFamily' => 'Lato', + 'fontFace' => array( + array( + 'fontFamily' => 'Lato', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + 'uploadedFile' => 'files0', + ), + array( + 'fontFamily' => 'Lato', + 'fontStyle' => 'normal', + 'fontWeight' => '500', + 'uploadedFile' => 'files1', + ), + array( + 'fontFamily' => 'Lato', + 'fontStyle' => 'normal', + 'fontWeight' => '500', + 'uploadedFile' => 'files2', + ), + ), + ), + 'files_data' => array( + 'files0' => array( + 'name' => 'lato1.ttf', + 'type' => 'font/ttf', + 'tmp_name' => wp_tempnam( 'Lato-' ), + 'error' => 0, + 'size' => 123, + ), + 'files1' => array( + 'name' => 'lato2.ttf', + 'type' => 'font/ttf', + 'tmp_name' => wp_tempnam( 'Lato-' ), + 'error' => 0, + 'size' => 123, + ), + 'files2' => array( + 'name' => 'lato2.woff', + 'type' => 'font/woff', + 'tmp_name' => wp_tempnam( 'Lato-' ), + 'error' => 0, + 'size' => 123, + ), + ), + 'expected' => array( 'lato_normal_400.ttf', 'lato_normal_500.ttf' ), + ), + ); + } } diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index a32fa6c047e65..1813f535c109f 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -414,116 +414,4 @@ public function data_install_with_improper_inputs() { ), ); } - - /** - * Tests that duplicate font faces with same font style and weight but different file extensions are merged. - * - * @dataProvider data_install_with_duplicate_font_faces - * @param array $font_families Font families to install in theme.json format. - * @param array $files Font files to install. - */ - public function test_install_with_duplicate_font_faces( $font_families, $files = array(), $expected_response ) { - $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); - $font_families_json = json_encode( $font_families ); - $install_request->set_param( 'fontFamilies', $font_families_json ); - $install_request->set_file_params( $files ); - - $response = rest_get_server()->dispatch( $install_request ); - $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); - $data = $response->get_data(); - $this->assertCount( 1, $expected_response[0]['fontFace'], 'Duplicate font faces were not removed / added incorrectly.' ); - $this->assertStringEndsWith( $expected_response[0]['fontFace'][0]['src'], $data[0]['fontFace'][0]['src'], 'The src attribute does not match the expected font file.' ); - } - - /** - * Data provider for test_install_with_duplicate_font_faces - */ - public function data_install_with_duplicate_font_faces() { - $temp_file_path1 = wp_tempnam( 'Piazzola1-' ); - file_put_contents( $temp_file_path1, 'Mocking file content' ); - $temp_file_path2 = wp_tempnam( 'Piazzola2-' ); - file_put_contents( $temp_file_path2, 'Mocking file content' ); - - return array( - 'with duplicate font faces' => array( - 'font_families' => array( - array( - 'fontFamily' => 'Piazzolla', - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'uploadedFile' => 'files0', - ), - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'uploadedFile' => 'files1', - ), - ), - ), - ), - 'files' => array( - 'files0' => array( - 'name' => 'piazzola1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => $temp_file_path1, - 'error' => 0, - 'size' => 123, - ), - 'files1' => array( - 'name' => 'piazzola1.woff', - 'type' => 'font/woff', - 'tmp_name' => $temp_file_path2, - 'error' => 0, - 'size' => 123, - ), - ), - 'expected_response' => array( - array( - 'fontFamily' => 'Piazzolla', - 'slug' => 'piazzolla', - 'name' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => '/wp-content/fonts/piazzolla_italic_400.ttf', - ), - ), - ), - ), - ), - ); - } - - /** - * Tests that font faces with same font style and weight but different extensions, installed on separate requests, are merged. - * - * @dataProvider data_install_with_duplicate_font_faces - * @param array $font_families Font families to install in theme.json format. - * @param array $files Font files to install. - */ - public function test_install_duplicate_fonts_separate_requests( $font_families, $files = array(), $expected_response ) { - $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); - $font_families_json = json_encode( $font_families ); - $install_request->set_param( 'fontFamilies', $font_families_json ); - $install_request->set_file_params( $files ); - - $response = rest_get_server()->dispatch( $install_request ); - $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); - $data = $response->get_data(); - - // Install the same font family again. - $response = rest_get_server()->dispatch( $install_request ); - $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); - $data = $response->get_data(); - $this->assertCount( 1, $expected_response[0]['fontFace'], 'Duplicate font faces were not removed / added incorrectly.' ); - $this->assertStringEndsWith( $expected_response[0]['fontFace'][0]['src'], $data[0]['fontFace'][0]['src'], 'The src attribute does not match the expected font file.' ); - } } From b78f7c351c1b1e37bbfddbf986b78115d31d28d3 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Mon, 18 Sep 2023 11:25:01 -0700 Subject: [PATCH 09/10] Add test that only a single font face extension is maintained, overwriting initial. --- .../font-library/wpFontFamily/install.php | 100 +++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/install.php b/phpunit/tests/fonts/font-library/wpFontFamily/install.php index d3315a690218e..a44b217afa275 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamily/install.php +++ b/phpunit/tests/fonts/font-library/wpFontFamily/install.php @@ -262,6 +262,8 @@ public function test_should_not_install_duplicate_fontfaces( $font_data, array $ $font = new WP_Font_Family( $font_data ); $font->install( $files_data ); + $this->assertCount( count( $expected ), $this->files_in_dir( static::$fonts_dir ), 'Font directory should contain the same number of files as expected' ); + foreach ( $expected as $font_file ) { $font_file = path_join( static::$fonts_dir, $font_file ); $this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" ); @@ -276,7 +278,7 @@ public function test_should_not_install_duplicate_fontfaces( $font_data, array $ public function data_should_not_install_duplicate_fontfaces() { return array( 'single unique font face' => array( - 'font_data' => array( + 'font_data' => array( 'name' => 'Inter', 'slug' => 'inter', 'fontFamily' => 'Inter', @@ -366,4 +368,100 @@ public function data_should_not_install_duplicate_fontfaces() { ), ); } + + public function test_should_overwrite_fontface_with_different_extension(){ + $font_data_initial = array( + 'name' => 'Inter', + 'slug' => 'inter', + 'fontFamily' => 'Inter', + 'fontFace' => array( + array( + 'fontFamily' => 'Inter', + 'fontStyle' => 'italic', + 'fontWeight' => '500', + 'uploadedFile' => 'files0', + ), + array( + 'fontFamily' => 'Inter', + 'fontStyle' => 'italic', + 'fontWeight' => '900', + 'uploadedFile' => 'files1', + ) + ), + ); + $files_data_initial = array( + 'files0' => array( + 'name' => 'inter1.ttf', + 'type' => 'font/woff', + 'tmp_name' => wp_tempnam( 'Inter-' ), + 'error' => 0, + 'size' => 123, + ), + 'files1' => array( + 'name' => 'inter1.woff', + 'type' => 'font/woff', + 'tmp_name' => wp_tempnam( 'Inter-' ), + 'error' => 0, + 'size' => 123, + ), + ); + $font_data_overwrite = array( + 'name' => 'Inter', + 'slug' => 'inter', + 'fontFamily' => 'Inter', + 'fontFace' => array( + array( + 'fontFamily' => 'Inter', + 'fontStyle' => 'italic', + 'fontWeight' => '500', + 'uploadedFile' => 'files0', + ), + array( + 'fontFamily' => 'Inter', + 'fontStyle' => 'italic', + 'fontWeight' => '900', + 'uploadedFile' => 'files1', + ), + ), + ); + $files_data_overwrite = array( + 'files0' => array( + 'name' => 'inter1.woff', + 'type' => 'font/woff', + 'tmp_name' => wp_tempnam( 'Inter-' ), + 'error' => 0, + 'size' => 123, + ), + 'files1' => array( + 'name' => 'inter1.ttf', + 'type' => 'font/ttf', + 'tmp_name' => wp_tempnam( 'Inter-' ), + 'error' => 0, + 'size' => 123, + ), + ); + + $expected = array( 'inter_italic_500.woff', 'inter_italic_900.ttf' ); + + // Set up the temporary files. + foreach ( $files_data_initial as $file ) { + file_put_contents( $file['tmp_name'], 'Mocking file content' ); + } + foreach ( $files_data_overwrite as $file ) { + file_put_contents( $file['tmp_name'], 'Mocking file content' ); + } + + $font = new WP_Font_Family( $font_data_initial ); + $font->install( $files_data_initial ); + + $font = new WP_Font_Family( $font_data_overwrite ); + $font->install( $files_data_overwrite ); + + $this->assertCount( count( $expected ), $this->files_in_dir( static::$fonts_dir ), 'Font directory should contain the same number of files as expected' ); + + foreach ( $expected as $font_file ) { + $font_file = path_join( static::$fonts_dir, $font_file ); + $this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" ); + } + } } From b953b2f0b90bd0dba01cdd1f2e747f22d6fcea44 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Mon, 18 Sep 2023 11:30:10 -0700 Subject: [PATCH 10/10] Format php. --- .../font-library/wpFontFamily/install.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/install.php b/phpunit/tests/fonts/font-library/wpFontFamily/install.php index a44b217afa275..733dc748d5f93 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamily/install.php +++ b/phpunit/tests/fonts/font-library/wpFontFamily/install.php @@ -277,8 +277,8 @@ public function test_should_not_install_duplicate_fontfaces( $font_data, array $ */ public function data_should_not_install_duplicate_fontfaces() { return array( - 'single unique font face' => array( - 'font_data' => array( + 'single unique font face' => array( + 'font_data' => array( 'name' => 'Inter', 'slug' => 'inter', 'fontFamily' => 'Inter', @@ -316,7 +316,7 @@ public function data_should_not_install_duplicate_fontfaces() { 'expected' => array( 'inter_italic_900.ttf' ), ), 'multiple unique font faces' => array( - 'font_data' => array( + 'font_data' => array( 'name' => 'Lato', 'slug' => 'lato', 'fontFamily' => 'Lato', @@ -341,7 +341,7 @@ public function data_should_not_install_duplicate_fontfaces() { ), ), ), - 'files_data' => array( + 'files_data' => array( 'files0' => array( 'name' => 'lato1.ttf', 'type' => 'font/ttf', @@ -364,13 +364,13 @@ public function data_should_not_install_duplicate_fontfaces() { 'size' => 123, ), ), - 'expected' => array( 'lato_normal_400.ttf', 'lato_normal_500.ttf' ), + 'expected' => array( 'lato_normal_400.ttf', 'lato_normal_500.ttf' ), ), ); } - public function test_should_overwrite_fontface_with_different_extension(){ - $font_data_initial = array( + public function test_should_overwrite_fontface_with_different_extension() { + $font_data_initial = array( 'name' => 'Inter', 'slug' => 'inter', 'fontFamily' => 'Inter', @@ -386,10 +386,10 @@ public function test_should_overwrite_fontface_with_different_extension(){ 'fontStyle' => 'italic', 'fontWeight' => '900', 'uploadedFile' => 'files1', - ) + ), ), ); - $files_data_initial = array( + $files_data_initial = array( 'files0' => array( 'name' => 'inter1.ttf', 'type' => 'font/woff', @@ -405,7 +405,7 @@ public function test_should_overwrite_fontface_with_different_extension(){ 'size' => 123, ), ); - $font_data_overwrite = array( + $font_data_overwrite = array( 'name' => 'Inter', 'slug' => 'inter', 'fontFamily' => 'Inter', @@ -450,7 +450,7 @@ public function test_should_overwrite_fontface_with_different_extension(){ foreach ( $files_data_overwrite as $file ) { file_put_contents( $file['tmp_name'], 'Mocking file content' ); } - + $font = new WP_Font_Family( $font_data_initial ); $font->install( $files_data_initial );