From ab24c0af740174798e40397f5764bff4dec42e85 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 3 May 2023 16:06:24 -0500 Subject: [PATCH 1/5] Adds user-select fonts enqueuer. --- .../fonts-api/class-wp-fonts-resolver.php | 102 +++++++ lib/load.php | 2 + .../enqueueUserSelectedFonts-test.php | 256 ++++++++++++++++++ 3 files changed, 360 insertions(+) create mode 100644 lib/experimental/fonts-api/class-wp-fonts-resolver.php create mode 100644 phpunit/fonts-api/wpFontsResolver/enqueueUserSelectedFonts-test.php diff --git a/lib/experimental/fonts-api/class-wp-fonts-resolver.php b/lib/experimental/fonts-api/class-wp-fonts-resolver.php new file mode 100644 index 00000000000000..5b8314129726b5 --- /dev/null +++ b/lib/experimental/fonts-api/class-wp-fonts-resolver.php @@ -0,0 +1,102 @@ +user->create( + array( + 'role' => 'administrator', + 'user_email' => 'administrator@example.com', + ) + ); + } + + /** + * @dataProvider data_should_not_enqueue_when_no_user_selected_fonts + * + * @param array $styles Optional. Test styles. Default empty array. + */ + public function test_should_not_enqueue_when_no_user_selected_fonts( $styles = array() ) { + $this->set_up_global_styles( $styles ); + + $mock = $this->set_up_mock( 'enqueue' ); + $mock->expects( $this->never() ) + ->method( 'enqueue' ); + + $expected = array(); + $this->assertSame( $expected, WP_Fonts_Resolver::enqueue_user_selected_fonts() ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_not_enqueue_when_no_user_selected_fonts() { + return array( + 'no user-selected styles' => array(), + 'invalid element' => array( + array( + 'elements' => array( + 'invalid' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + ), + ), + ), + ), + ), + ); + } + + /** + * @dataProvider data_should_enqueue_when_user_selected_fonts + * + * @param array $styles Test styles. + * @param array $expected Expected results. + */ + public function test_should_enqueue_when_user_selected_fonts( $styles, $expected ) { + $mock = $this->set_up_mock( 'enqueue' ); + $mock->expects( $this->once() ) + ->method( 'enqueue' ) + ->with( + $this->identicalTo( $expected ) + ); + + $this->set_up_global_styles( $styles ); + + $this->assertSameSets( $expected, WP_Fonts_Resolver::enqueue_user_selected_fonts() ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_enqueue_when_user_selected_fonts() { + $fonts = array( + 'font1-400-normal' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + ), + 'font1-400-italic' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'italic', + 'fontWeight' => '400', + ), + 'font2-600-normal' => array( + 'fontFamily' => 'var:preset|font-family|font2', + 'fontStyle' => 'normal', + 'fontWeight' => '600', + ), + 'font2-600-italic' => array( + 'fontFamily' => 'var:preset|font-family|font2', + 'fontStyle' => 'italic', + 'fontWeight' => '600', + ), + 'font3-900-normal' => array( + 'fontFamily' => 'var:preset|font-family|font3', + 'fontStyle' => 'normal', + 'fontWeight' => '900', + ), + ); + + return array( + 'link' => array( + 'styles' => array( + 'elements' => array( + 'link' => array( + 'typography' => $fonts['font1-400-italic'], + ), + ), + ), + 'expected' => array( 'font1' ), + ), + 'heading' => array( + 'styles' => array( + 'elements' => array( + 'heading' => array( + 'typography' => $fonts['font2-600-italic'], + ), + ), + ), + 'expected' => array( 'font2' ), + ), + 'caption' => array( + 'styles' => array( + 'elements' => array( + 'caption' => array( + 'typography' => $fonts['font2-600-normal'], + ), + ), + ), + 'expected' => array( 'font2' ), + ), + 'button' => array( + 'styles' => array( + 'elements' => array( + 'button' => array( + 'typography' => $fonts['font1-400-normal'], + ), + ), + ), + 'expected' => array( 'font1' ), + ), + 'text' => array( + 'styles' => array( + 'typography' => $fonts['font1-400-normal'], + ), + 'expected' => array( 'font1' ), + ), + 'all elements' => array( + 'styles' => array( + 'elements' => array( + 'link' => array( + 'typography' => $fonts['font1-400-italic'], + ), + 'heading' => array( + 'typography' => $fonts['font2-600-italic'], + ), + 'caption' => array( + 'typography' => $fonts['font2-600-normal'], + ), + 'button' => array( + 'typography' => $fonts['font1-400-normal'], + ), + ), + ), + 'expected' => array( 'font1', 'font2' ), + ), + 'all elements and text' => array( + 'styles' => array( + 'elements' => array( + 'link' => array( + 'typography' => $fonts['font1-400-italic'], + ), + 'heading' => array( + 'typography' => $fonts['font2-600-italic'], + ), + 'caption' => array( + 'typography' => $fonts['font3-900-normal'], + ), + 'button' => array( + 'typography' => $fonts['font1-400-normal'], + ), + ), + 'typography' => $fonts['font1-400-normal'], + ), + 'expected' => array( 'font1', 'font2', 'font3' ), + ), + 'with invalid element' => array( + 'styles' => array( + 'elements' => array( + 'button' => array( + 'typography' => $fonts['font1-400-normal'], + ), + 'invalid' => array( + 'typography' => $fonts['font3-900-normal'], + ), + ), + ), + 'expected' => array( 'font1' ), + ), + ); + } + + /** + * Sets up the global styles. + * + * @param array $styles User-selected styles structure. + */ + private function set_up_global_styles( array $styles ) { + switch_theme( static::FONTS_THEME ); + + if ( empty( $styles ) ) { + return; + } + + // Make sure there is data from the user origin. + wp_set_current_user( self::$administrator_id ); + $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true ); + $config = json_decode( $user_cpt['post_content'], true ); + + // Add the test styles. + $config['styles'] = $styles; + + // Update the global styles and settings post. + $user_cpt['post_content'] = wp_json_encode( $config ); + wp_update_post( $user_cpt, true, false ); + } +} From a2ee32ed89e6da763ff6097f0abfd16250169ebe Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 4 May 2023 13:23:16 -0500 Subject: [PATCH 2/5] Auto queue before printing --- lib/experimental/fonts-api/fonts-api.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index 5f3f7a60ee8839..806a867fe5c0d4 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -198,8 +198,10 @@ function wp_print_fonts( $handles = false ) { return array(); } - // Skip this reassignment decision-making when using the default of `false`. - if ( false !== $handles ) { + if ( false === $handles ) { + // Automatically enqueue all user-selected fonts. + WP_Fonts_Resolver::enqueue_user_selected_fonts(); + } else { // When `true`, print all registered fonts for the iframed editor. if ( $in_iframed_editor ) { $queue = $wp_fonts->queue; From 4c8b07169e16bbc000b3458c2f833ec7139becc6 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 9 May 2023 12:31:07 -0500 Subject: [PATCH 3/5] Always set $handles to false when empty When invoked as a hooked callback, it receives an empty string. Empty is the same as false. --- lib/experimental/fonts-api/fonts-api.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index 806a867fe5c0d4..7075f20f76a217 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -198,21 +198,17 @@ function wp_print_fonts( $handles = false ) { return array(); } - if ( false === $handles ) { + if ( empty( $handles ) ) { // Automatically enqueue all user-selected fonts. WP_Fonts_Resolver::enqueue_user_selected_fonts(); - } else { - // When `true`, print all registered fonts for the iframed editor. - if ( $in_iframed_editor ) { - $queue = $wp_fonts->queue; - $done = $wp_fonts->done; - $wp_fonts->done = array(); - $wp_fonts->queue = $registered; - $handles = false; - } elseif ( empty( $handles ) ) { - // When falsey, assign `false` to print enqueued fonts. - $handles = false; - } + $handles = false; + } elseif ( $in_iframed_editor ) { + // Print all registered fonts for the iframed editor. + $queue = $wp_fonts->queue; + $done = $wp_fonts->done; + $wp_fonts->done = array(); + $wp_fonts->queue = $registered; + $handles = false; } _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); From fe2bd0d6890d2483a87ca05090e494ece3b0862b Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 10 May 2023 12:14:04 -0500 Subject: [PATCH 4/5] Use WP_Theme_JSON_Resolver_Gutenberg::get_user_data(). From PR #50499. Props @oandregal. --- .../fonts-api/class-wp-fonts-resolver.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/experimental/fonts-api/class-wp-fonts-resolver.php b/lib/experimental/fonts-api/class-wp-fonts-resolver.php index 5b8314129726b5..d3d0ecba992b88 100644 --- a/lib/experimental/fonts-api/class-wp-fonts-resolver.php +++ b/lib/experimental/fonts-api/class-wp-fonts-resolver.php @@ -45,8 +45,12 @@ class WP_Fonts_Resolver { * @return array User selected font-families when exists, else empty array. */ public static function enqueue_user_selected_fonts() { - $global_styles = wp_get_global_styles(); - $user_selected_fonts = static::get_user_selected_fonts( $global_styles ); + $user_selected_fonts = array(); + $user_global_styles = WP_Theme_JSON_Resolver_Gutenberg::get_user_data()->get_raw_data(); + if ( isset( $user_global_styles['styles'] ) ) { + $user_selected_fonts = static::get_user_selected_fonts( $user_global_styles['styles'] ); + } + if ( empty( $user_selected_fonts ) ) { return array(); } @@ -92,11 +96,14 @@ private static function get_value_from_style( $style, $preset_type = 'font-famil return ''; } - $starting_pattern = "var:preset|{$preset_type}|"; + $starting_pattern = "var(--wp--preset--{$preset_type}--"; + $ending_pattern = ')'; if ( ! str_starts_with( $style, $starting_pattern ) ) { return ''; } - return substr( $style, strlen( $starting_pattern ) ); + $offset = strlen( $starting_pattern ); + $length = strpos( $style, $ending_pattern ) - $offset; + return substr( $style, $offset, $length ); } } From f87724b2a267e0112747a4c96014956c00dd75f4 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 11 May 2023 12:54:07 -0500 Subject: [PATCH 5/5] Adds print tests. Moves datasets to trait. --- phpunit/fonts-api/wp-fonts-testcase.php | 42 ++++ phpunit/fonts-api/wp-fonts-tests-dataset.php | 232 ++++++++++++++++++ .../enqueueUserSelectedFonts-test.php | 171 ++----------- phpunit/fonts-api/wpPrintFonts-test.php | 78 ++++-- 4 files changed, 355 insertions(+), 168 deletions(-) diff --git a/phpunit/fonts-api/wp-fonts-testcase.php b/phpunit/fonts-api/wp-fonts-testcase.php index adb70eca98b0fc..f7f7fb04b50625 100644 --- a/phpunit/fonts-api/wp-fonts-testcase.php +++ b/phpunit/fonts-api/wp-fonts-testcase.php @@ -59,6 +59,13 @@ abstract class WP_Fonts_TestCase extends WP_UnitTestCase { */ protected $orig_theme_dir; + /** + * Administrator ID. + * + * @var int + */ + protected static $administrator_id = 0; + public static function set_up_before_class() { parent::set_up_before_class(); @@ -346,4 +353,39 @@ protected function get_handles_for_provider( array $fonts, $provider_id ) { return $handles; } + + protected static function set_up_admin_user() { + self::$administrator_id = self::factory()->user->create( + array( + 'role' => 'administrator', + 'user_email' => 'administrator@example.com', + ) + ); + } + + /** + * Sets up the global styles. + * + * @param array $styles User-selected styles structure. + * @param array $theme Optional. Theme to switch to for the test. Default 'fonts-block-theme'. + */ + protected function set_up_global_styles( array $styles, $theme = 'fonts-block-theme' ) { + switch_theme( $theme ); + + if ( empty( $styles ) ) { + return; + } + + // Make sure there is data from the user origin. + wp_set_current_user( self::$administrator_id ); + $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true ); + $config = json_decode( $user_cpt['post_content'], true ); + + // Add the test styles. + $config['styles'] = $styles; + + // Update the global styles and settings post. + $user_cpt['post_content'] = wp_json_encode( $config ); + wp_update_post( $user_cpt, true, false ); + } } diff --git a/phpunit/fonts-api/wp-fonts-tests-dataset.php b/phpunit/fonts-api/wp-fonts-tests-dataset.php index 79d2387b52cf53..b882965598d360 100644 --- a/phpunit/fonts-api/wp-fonts-tests-dataset.php +++ b/phpunit/fonts-api/wp-fonts-tests-dataset.php @@ -1161,4 +1161,236 @@ protected function get_registered_mock_fonts() { ), ); } + + /** + * Data provider. + * + * @return array + */ + public function data_print_user_selected_fonts() { + $global_styles = $this->get_mock_user_selected_fonts_global_styles(); + $font_faces = $this->get_registered_fonts_css(); + + return array( + 'print font1' => array( + 'global_styles' => $global_styles['font1'], + 'expected_done' => array( + 'font1-300-normal', + 'font1-300-italic', + 'font1-900-normal', + 'font1', + ), + 'expected_output' => sprintf( + '%s; %s; %s\n', + $font_faces['font1-300-normal'], + $font_faces['font1-300-italic'], + $font_faces['font1-900-normal'] + ), + ), + 'print font2' => array( + 'global_styles' => $global_styles['font2'], + 'expected_done' => array( 'font2-200-900-normal', 'font2-200-900-italic', 'font2' ), + 'expected_output' => sprintf( + '%s; %s\n', + $font_faces['font2-200-900-normal'], + $font_faces['font2-200-900-italic'] + ), + ), + 'print font3' => array( + 'global_styles' => $global_styles['font3'], + 'expected_done' => array( 'font3', 'font3-bold-normal' ), + 'expected_output' => sprintf( + '%s\n', + $font_faces['font3-bold-normal'] + ), + ), + 'print all fonts' => array( + 'global_styles' => $global_styles['all'], + 'expected_done' => array( + 'font1-300-normal', + 'font1-300-italic', + 'font1-900-normal', + 'font1', + 'font2-200-900-normal', + 'font2-200-900-italic', + 'font2', + 'font3-bold-normal', + 'font3', + ), + 'expected_output' => sprintf( + '%s; %s; %s; %s; %s; %s\n', + $font_faces['font1-300-normal'], + $font_faces['font1-300-italic'], + $font_faces['font1-900-normal'], + $font_faces['font2-200-900-normal'], + $font_faces['font2-200-900-italic'], + $font_faces['font3-bold-normal'] + ), + ), + 'print all valid fonts' => array( + 'global_styles' => $global_styles['all with invalid element'], + 'expected_done' => array( + 'font1-300-normal', + 'font1-300-italic', + 'font1-900-normal', + 'font1', + 'font2-200-900-normal', + 'font2-200-900-italic', + 'font2', + 'font3-bold-normal', + 'font3', + ), + 'expected_output' => sprintf( + '%s; %s; %s; %s; %s; %s\n', + $font_faces['font1-300-normal'], + $font_faces['font1-300-italic'], + $font_faces['font1-900-normal'], + $font_faces['font2-200-900-normal'], + $font_faces['font2-200-900-italic'], + $font_faces['font3-bold-normal'] + ), + ), + ); + } + + /** + * Gets user-selected fonts for global styles for the mock provider. + * + * @since X.X.X + * + * @return array + */ + protected function get_mock_user_selected_fonts_global_styles() { + return array( + 'font1' => array( + 'elements' => array( + 'heading' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'normal', + 'fontWeight' => '300', + ), + ), + 'caption' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'italic', + 'fontWeight' => '300', + ), + ), + ), + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'normal', + 'fontWeight' => '900', + ), + ), + 'font2' => array( + 'elements' => array( + 'heading' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font2', + 'fontStyle' => 'normal', + 'fontWeight' => '200-900', + ), + ), + 'button' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font2', + 'fontStyle' => 'italic', + 'fontWeight' => '200-900', + ), + ), + ), + ), + 'font3' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font3', + 'fontStyle' => 'normal', + 'fontWeight' => 'bold', + ), + ), + 'all' => array( + 'elements' => array( + 'link' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'italic', + 'fontWeight' => '300', + ), + ), + 'heading' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'normal', + 'fontWeight' => '900', + ), + ), + 'caption' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'italic', + 'fontWeight' => '300', + ), + ), + 'button' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font2', + 'fontStyle' => 'normal', + 'fontWeight' => '200-900', + ), + ), + ), + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font3', + 'fontStyle' => 'normal', + 'fontWeight' => 'bold', + ), + ), + 'all with invalid element' => array( + 'elements' => array( + 'link' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'italic', + 'fontWeight' => '300', + ), + ), + 'heading' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'normal', + 'fontWeight' => '900', + ), + ), + 'caption' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font1', + 'fontStyle' => 'italic', + 'fontWeight' => '300', + ), + ), + 'button' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font2', + 'fontStyle' => 'normal', + 'fontWeight' => '200-900', + ), + ), + 'invalid' => array( + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font2', + 'fontStyle' => 'italic', + 'fontWeight' => '200-900', + ), + ), + ), + 'typography' => array( + 'fontFamily' => 'var:preset|font-family|font3', + 'fontStyle' => 'normal', + 'fontWeight' => 'bold', + ), + ), + ); + } } diff --git a/phpunit/fonts-api/wpFontsResolver/enqueueUserSelectedFonts-test.php b/phpunit/fonts-api/wpFontsResolver/enqueueUserSelectedFonts-test.php index 437c95b698d350..c3ba0fd1f72ac6 100644 --- a/phpunit/fonts-api/wpFontsResolver/enqueueUserSelectedFonts-test.php +++ b/phpunit/fonts-api/wpFontsResolver/enqueueUserSelectedFonts-test.php @@ -13,13 +13,6 @@ * @covers WP_Fonts_Resolver::enqueue_user_selected_fonts */ class Tests_Fonts_WpFontsResolver_EnqueueUserSelectedFonts extends WP_Fonts_TestCase { - const FONTS_THEME = 'fonts-block-theme'; - /** - * Administrator ID. - * - * @var int - */ - private static $administrator_id = 0; public static function set_up_before_class() { self::$requires_switch_theme_fixtures = true; @@ -99,158 +92,40 @@ public function test_should_enqueue_when_user_selected_fonts( $styles, $expected * @return array */ public function data_should_enqueue_when_user_selected_fonts() { - $fonts = array( - 'font1-400-normal' => array( - 'fontFamily' => 'var:preset|font-family|font1', - 'fontStyle' => 'normal', - 'fontWeight' => '400', - ), - 'font1-400-italic' => array( - 'fontFamily' => 'var:preset|font-family|font1', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - ), - 'font2-600-normal' => array( - 'fontFamily' => 'var:preset|font-family|font2', - 'fontStyle' => 'normal', - 'fontWeight' => '600', - ), - 'font2-600-italic' => array( - 'fontFamily' => 'var:preset|font-family|font2', - 'fontStyle' => 'italic', - 'fontWeight' => '600', - ), - 'font3-900-normal' => array( - 'fontFamily' => 'var:preset|font-family|font3', - 'fontStyle' => 'normal', - 'fontWeight' => '900', - ), - ); + $global_styles = $this->get_mock_user_selected_fonts_global_styles(); return array( - 'link' => array( - 'styles' => array( - 'elements' => array( - 'link' => array( - 'typography' => $fonts['font1-400-italic'], - ), - ), - ), + 'heading, caption, text' => array( + 'styles' => $global_styles['font1'], 'expected' => array( 'font1' ), ), - 'heading' => array( - 'styles' => array( - 'elements' => array( - 'heading' => array( - 'typography' => $fonts['font2-600-italic'], - ), - ), - ), + 'heading, button' => array( + 'styles' => $global_styles['font2'], 'expected' => array( 'font2' ), ), - 'caption' => array( - 'styles' => array( - 'elements' => array( - 'caption' => array( - 'typography' => $fonts['font2-600-normal'], - ), - ), - ), - 'expected' => array( 'font2' ), + 'text' => array( + 'styles' => $global_styles['font3'], + 'expected' => array( 'font3' ), ), - 'button' => array( - 'styles' => array( - 'elements' => array( - 'button' => array( - 'typography' => $fonts['font1-400-normal'], - ), - ), + 'all' => array( + 'styles' => $global_styles['all'], + 'expected' => array( + 0 => 'font1', + // font1 occurs 2 more times and gets removed as duplicates. + 3 => 'font2', + 4 => 'font3', ), - 'expected' => array( 'font1' ), ), - 'text' => array( - 'styles' => array( - 'typography' => $fonts['font1-400-normal'], + 'all with invalid element' => array( + 'styles' => $global_styles['all with invalid element'], + 'expected' => array( + 0 => 'font1', + // font1 occurs 2 more times and gets removed as duplicates. + 3 => 'font2', + // Skips font2 for the "invalid" element. + 4 => 'font3', ), - 'expected' => array( 'font1' ), - ), - 'all elements' => array( - 'styles' => array( - 'elements' => array( - 'link' => array( - 'typography' => $fonts['font1-400-italic'], - ), - 'heading' => array( - 'typography' => $fonts['font2-600-italic'], - ), - 'caption' => array( - 'typography' => $fonts['font2-600-normal'], - ), - 'button' => array( - 'typography' => $fonts['font1-400-normal'], - ), - ), - ), - 'expected' => array( 'font1', 'font2' ), - ), - 'all elements and text' => array( - 'styles' => array( - 'elements' => array( - 'link' => array( - 'typography' => $fonts['font1-400-italic'], - ), - 'heading' => array( - 'typography' => $fonts['font2-600-italic'], - ), - 'caption' => array( - 'typography' => $fonts['font3-900-normal'], - ), - 'button' => array( - 'typography' => $fonts['font1-400-normal'], - ), - ), - 'typography' => $fonts['font1-400-normal'], - ), - 'expected' => array( 'font1', 'font2', 'font3' ), - ), - 'with invalid element' => array( - 'styles' => array( - 'elements' => array( - 'button' => array( - 'typography' => $fonts['font1-400-normal'], - ), - 'invalid' => array( - 'typography' => $fonts['font3-900-normal'], - ), - ), - ), - 'expected' => array( 'font1' ), ), ); } - - /** - * Sets up the global styles. - * - * @param array $styles User-selected styles structure. - */ - private function set_up_global_styles( array $styles ) { - switch_theme( static::FONTS_THEME ); - - if ( empty( $styles ) ) { - return; - } - - // Make sure there is data from the user origin. - wp_set_current_user( self::$administrator_id ); - $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true ); - $config = json_decode( $user_cpt['post_content'], true ); - - // Add the test styles. - $config['styles'] = $styles; - - // Update the global styles and settings post. - $user_cpt['post_content'] = wp_json_encode( $config ); - wp_update_post( $user_cpt, true, false ); - } } diff --git a/phpunit/fonts-api/wpPrintFonts-test.php b/phpunit/fonts-api/wpPrintFonts-test.php index cb500aaa0430f4..4f50cf6a8cd5ea 100644 --- a/phpunit/fonts-api/wpPrintFonts-test.php +++ b/phpunit/fonts-api/wpPrintFonts-test.php @@ -15,6 +15,14 @@ */ class Tests_Fonts_WpPrintFonts extends WP_Fonts_TestCase { + public static function set_up_before_class() { + self::$requires_switch_theme_fixtures = true; + + parent::set_up_before_class(); + + static::set_up_admin_user(); + } + public function test_should_return_empty_array_when_no_fonts_registered() { $this->assertSame( array(), wp_print_fonts() ); } @@ -104,26 +112,6 @@ public function test_should_print_handles_when_not_enqueued( $setup, $expected_d $this->assertSameSets( $expected_done, $actual_done, 'Printed handles should match' ); } - /** - * Sets up the dependencies for integration test. - * - * @param array $setup Dependencies to set up. - * @param WP_Fonts $wp_fonts Instance of WP_Fonts. - * @param bool $enqueue Whether to enqueue. Default true. - */ - private function setup_integrated_deps( array $setup, $wp_fonts, $enqueue = true ) { - foreach ( $setup['provider'] as $provider ) { - $wp_fonts->register_provider( $provider['id'], $provider['class'] ); - } - foreach ( $setup['registered'] as $handle => $variations ) { - $this->setup_register( $handle, $variations, $wp_fonts ); - } - - if ( $enqueue ) { - $wp_fonts->enqueue( $setup['enqueued'] ); - } - } - /** * @dataProvider data_should_print_all_registered_fonts_for_iframed_editor * @@ -189,4 +177,54 @@ public function data_should_print_all_registered_fonts_for_iframed_editor() { ), ); } + + /** + * Integration test for printing user-selected global fonts. + * This test registers providers and fonts and then enqueues before testing the printing functionality. + * + * @dataProvider data_print_user_selected_fonts + * + * @param array $global_styles Test set up information for provider, fonts, and enqueued. + * @param array $expected_done Expected array of printed handles. + * @param string $expected_output Expected printed output. + */ + public function test_should_print_user_selected_fonts( $global_styles, $expected_done, $expected_output ) { + $wp_fonts = wp_fonts(); + + $setup = array( + 'provider' => array( 'mock' => $this->get_provider_definitions( 'mock' ) ), + 'registered' => $this->get_registered_mock_fonts(), + 'global_styles' => $global_styles, + ); + $this->setup_integrated_deps( $setup, $wp_fonts, false ); + + $this->expectOutputString( $expected_output ); + $actual_printed_fonts = wp_print_fonts(); + $this->assertSameSets( $expected_done, $actual_printed_fonts, 'Should print font-faces for given user-selected fonts' ); + } + + + /** + * Sets up the dependencies for integration test. + * + * @param array $setup Dependencies to set up. + * @param WP_Fonts $wp_fonts Instance of WP_Fonts. + * @param bool $enqueue Whether to enqueue. Default true. + */ + private function setup_integrated_deps( array $setup, $wp_fonts, $enqueue = true ) { + foreach ( $setup['provider'] as $provider ) { + $wp_fonts->register_provider( $provider['id'], $provider['class'] ); + } + foreach ( $setup['registered'] as $handle => $variations ) { + $this->setup_register( $handle, $variations, $wp_fonts ); + } + + if ( $enqueue ) { + $wp_fonts->enqueue( $setup['enqueued'] ); + } + + if ( ! empty( $setup['global_styles'] ) ) { + $this->set_up_global_styles( $setup['global_styles'] ); + } + } }