diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index 49b45392d3306a..85d0f94ccc96fc 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -183,7 +183,13 @@ function wp_register_font_provider( $name, $classname ) { * An empty array if none were processed. */ function wp_print_fonts( $handles = false ) { - global $wp_fonts; + $wp_fonts = wp_fonts(); + $registered = $wp_fonts->get_registered_font_families(); + + // Nothing to print, as no fonts are registered. + if ( empty( $registered ) ) { + return array(); + } if ( empty( $handles ) ) { $handles = false; @@ -191,13 +197,7 @@ function wp_print_fonts( $handles = false ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); - if ( ! ( $wp_fonts instanceof WP_Fonts ) ) { - if ( ! $handles ) { - return array(); // No need to instantiate if nothing is there. - } - } - - return wp_fonts()->do_items( $handles ); + return $wp_fonts->do_items( $handles ); } } diff --git a/phpunit/fonts-api/wpPrintFonts-test.php b/phpunit/fonts-api/wpPrintFonts-test.php index 932af8e990b31f..d0108eec14faff 100644 --- a/phpunit/fonts-api/wpPrintFonts-test.php +++ b/phpunit/fonts-api/wpPrintFonts-test.php @@ -15,31 +15,29 @@ */ class Tests_Fonts_WpPrintFonts extends WP_Fonts_TestCase { - public function test_should_return_empty_array_when_global_not_instance() { - global $wp_fonts; - wp_fonts(); - $wp_fonts = null; - + public function test_should_return_empty_array_when_no_fonts_registered() { $this->assertSame( array(), wp_print_fonts() ); - $this->assertNotInstanceOf( WP_Webfonts::class, $wp_fonts ); } /** - * Unit test to mock WP_Webfonts::do_items(). + * Unit test which mocks WP_Fonts methods. * * @dataProvider data_mocked_handles * - * @param string|string[]|false $handles Handles to test. - * @param array|string[] $expected Expected array of processed handles. + * @param string|string[] $handles Handles to test. */ - public function test_should_return_mocked_handles( $handles, $expected ) { - $mock = $this->set_up_mock( 'do_items' ); + public function test_should_return_mocked_handles( $handles ) { + $mock = $this->set_up_mock( array( 'get_registered_font_families', 'do_items' ) ); + $mock->expects( $this->once() ) + ->method( 'get_registered_font_families' ) + ->will( $this->returnValue( $handles ) ); + $mock->expects( $this->once() ) ->method( 'do_items' ) ->with( $this->identicalTo( $handles ) ) - ->will( $this->returnValue( $expected ) ); + ->will( $this->returnValue( $handles ) ); wp_print_fonts( $handles ); } @@ -51,13 +49,14 @@ public function test_should_return_mocked_handles( $handles, $expected ) { */ public function data_mocked_handles() { return array( - 'no handles' => array( - 'handles' => false, - 'expected' => array(), + 'font family' => array( + array( 'my-custom-font' ), ), - 'font family handles' => array( - 'handles' => array( 'my-custom-font' ), - 'expected' => array( 'my-custom-font' ), + 'multiple font families' => array( + array( + 'font1', + 'font2', + ), ), ); }