From 9078b01cf2fc8f3b97919b03715a39ac54c111c7 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 10 Mar 2022 17:40:27 -0300 Subject: [PATCH] Reset wp_webfonts instance before each test is run --- .../wordpress-6.0/class-wp-webfonts.php | 18 --------------- lib/compat/wordpress-6.0/webfonts.php | 15 ++++++++++++- phpunit/class-wp-webfonts-test.php | 22 ++++++++++++++++++- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index eb4ddb476a22c2..aa3c3199299416 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -9,7 +9,6 @@ * Class WP_Webfonts */ class WP_Webfonts { - private static $instance = null; /** * An array of registered webfonts. * @@ -33,27 +32,10 @@ class WP_Webfonts { */ private $stylesheet_handle = ''; - /** - * Return an instance of the WP_Webfonts class, or create one if none exist yet. - * - * @since 6.0.0 - * - * @return WP_Webfonts|null - */ - public static function get_instance() { - if ( null === self::$instance ) { - self::$instance = new WP_Webfonts(); - self::$instance->init(); - } - - return self::$instance; - } - /** * Init. */ public function init() { - // Register default providers. $this->register_provider( 'local', 'WP_Webfonts_Provider_Local' ); diff --git a/lib/compat/wordpress-6.0/webfonts.php b/lib/compat/wordpress-6.0/webfonts.php index 450fb76d39f705..9a2d4af3fb0ed7 100644 --- a/lib/compat/wordpress-6.0/webfonts.php +++ b/lib/compat/wordpress-6.0/webfonts.php @@ -8,6 +8,19 @@ * @subpackage Webfonts */ +class _WP_Webfonts_Singleton { + private static $instance = null; + + public static function get_instance() { + if ( null === self::$instance ) { + self::$instance = new WP_Webfonts(); + self::$instance->init(); + } + + return self::$instance; + } +} + /** * Instantiates the webfonts controller, if not already set, and returns it. * @@ -16,7 +29,7 @@ * @return WP_Webfonts Instance of the controller. */ function wp_webfonts() { - return WP_Webfonts::get_instance(); + return _WP_Webfonts_Singleton::get_instance(); } /** diff --git a/phpunit/class-wp-webfonts-test.php b/phpunit/class-wp-webfonts-test.php index 239f2fae59fb12..3a267b02473c60 100644 --- a/phpunit/class-wp-webfonts-test.php +++ b/phpunit/class-wp-webfonts-test.php @@ -5,6 +5,14 @@ * @covers WP_Webfonts_Test */ class WP_Webfonts_Test extends WP_UnitTestCase { + function setUp() { + $singleton = new _WP_Webfonts_Singleton(); + $reflection = new ReflectionClass( $singleton ); + $instance = $reflection->getProperty( 'instance' ); + $instance->setAccessible( true ); + $instance->setValue( null, null ); + $instance->setAccessible( false ); + } /** * @covers wp_register_webfonts @@ -106,8 +114,20 @@ public function test_validate_font() { * @covers WP_Webfonts::generate_styles */ public function test_generate_styles() { + $font = array( + 'provider' => 'local', + 'font-family' => 'Source Serif Pro', + 'font-style' => 'normal', + 'font-weight' => '200 900', + 'font-stretch' => 'normal', + 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', + 'font-display' => 'fallback', + ); + + wp_register_webfont( $font ); + $this->assertEquals( - '@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2\') format(\'woff2\');}@font-face{font-family:"Source Serif Pro";font-style:italic;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2\') format(\'woff2\');}', + '@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2\') format(\'woff2\');}', wp_webfonts()->generate_styles() ); }