From ef1a26e69c09d82d91145852811df7493e752810 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 24 Jul 2024 09:55:19 +0800 Subject: [PATCH] Fix the undefined array key "name" warning in ComingSoonRequestHandler.php when the font name is not set (#49795) * Fix undefined array key "name" warning in ComingSoonRequestHandler.php when font name is not set * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: github-actions --- ...on-request-handler-font-undefined-key-name | 4 ++ .../ComingSoon/ComingSoonRequestHandler.php | 4 +- .../ComingSoonRequestHandlerTest.php | 58 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce/changelog/49795-fix-coming-soon-request-handler-font-undefined-key-name diff --git a/plugins/woocommerce/changelog/49795-fix-coming-soon-request-handler-font-undefined-key-name b/plugins/woocommerce/changelog/49795-fix-coming-soon-request-handler-font-undefined-key-name new file mode 100644 index 00000000000..037f3b9d6a3 --- /dev/null +++ b/plugins/woocommerce/changelog/49795-fix-coming-soon-request-handler-font-undefined-key-name @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix the undefined array key "name" warning in ComingSoonRequestHandler.php when the font name is not set \ No newline at end of file diff --git a/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php b/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php index d80f6a4c94a..31057ab11bd 100644 --- a/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php +++ b/plugins/woocommerce/src/Internal/ComingSoon/ComingSoonRequestHandler.php @@ -5,7 +5,7 @@ use Automattic\WooCommerce\Admin\Features\Features; /** * Handles the template_include hook to determine whether the current page needs - * to be replaced with a comiing soon screen. + * to be replaced with a coming soon screen. */ class ComingSoonRequestHandler { @@ -185,7 +185,7 @@ class ComingSoonRequestHandler { foreach ( $fonts_to_add as $font_to_add ) { $found = false; foreach ( $font_data as $font ) { - if ( $font['name'] === $font_to_add['name'] ) { + if ( isset( $font['name'] ) && $font['name'] === $font_to_add['name'] ) { $found = true; break; } diff --git a/plugins/woocommerce/tests/php/src/Internal/ComingSoon/ComingSoonRequestHandlerTest.php b/plugins/woocommerce/tests/php/src/Internal/ComingSoon/ComingSoonRequestHandlerTest.php index 0b6c9f3ba11..6940a62e016 100644 --- a/plugins/woocommerce/tests/php/src/Internal/ComingSoon/ComingSoonRequestHandlerTest.php +++ b/plugins/woocommerce/tests/php/src/Internal/ComingSoon/ComingSoonRequestHandlerTest.php @@ -70,4 +70,62 @@ class ComingSoonRequestHandlerTest extends \WC_Unit_Test_Case { $this->assertSame( $wp->query_vars['page_id'], null ); } + + /** + * @testdox Tests that the method adds the 'Inter' and 'Cardo' fonts to the theme JSON data. + */ + public function test_experimental_filter_theme_json_theme() { + $theme_json = $this->createMock( \WP_Theme_JSON_Data::class ); + $initial_data = array( + 'settings' => array( + 'typography' => array( + 'fontFamilies' => array( + 'theme' => array( + array( + 'fontFamily' => 'Existing Font', + 'name' => 'Existing Font', + 'slug' => 'existing-font', + 'fontFace' => array( + array( + 'fontFamily' => 'Existing Font', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + 'src' => array( 'existing-font.woff2' ), + ), + ), + ), + array( + 'fontFamily' => 'Unnamed Font', + 'slug' => 'unnamed-font', + 'fontFace' => array( + array( + 'fontFamily' => 'Unnamed Font', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + 'src' => array( 'unnamed-font.woff2' ), + ), + ), + ), + ), + ), + ), + ), + ); + + $theme_json->method( 'get_data' )->willReturn( $initial_data ); + + $theme_json->expects( $this->once() ) + ->method( 'update_with' ) + ->with( + $this->callback( + function ( $new_data ) { + $fonts = $new_data['settings']['typography']['fontFamilies']['theme']; + $font_names = array_column( $fonts, 'name' ); + return in_array( 'Inter', $font_names, true ) && in_array( 'Cardo', $font_names, true ); + } + ) + ); + + $this->sut->experimental_filter_theme_json_theme( $theme_json ); + } }