Skip to content

Commit

Permalink
Bail out if no fonts to process.
Browse files Browse the repository at this point in the history
1. Adds guard clause to WP_Font_Face::generate_and_print().
2. Adds unhappy test for it.
3. Adds similar unhappy test for wp_print_font_faces().
  • Loading branch information
hellofromtonya committed Jul 6, 2023
1 parent 6359091 commit 6f8e601
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/experimental/fonts/class-wp-font-face.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5'
public function generate_and_print( array $fonts ) {
$fonts = $this->validate_fonts( $fonts );

// Bail out if there are no fonts are given to process.
if ( empty( $fonts ) ) {
return;
}

printf(
$this->get_style_element(),
$this->get_css( $fonts )
Expand Down
8 changes: 8 additions & 0 deletions phpunit/fonts/wpFontFace/generateAndPrint-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
class Tests_Fonts_WPFontFace_GenerateAndPrint extends WP_UnitTestCase {
use WP_Font_Face_Tests_Datasets;

public function test_should_not_generate_and_print_when_no_fonts() {
$font_face = new WP_Font_Face();
$fonts = array();

$this->expectOutputString( '' );
$font_face->generate_and_print( $fonts );
}

/**
* @dataProvider data_should_print_given_fonts
*
Expand Down
7 changes: 7 additions & 0 deletions phpunit/fonts/wpPrintFontFaces-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public static function set_up_before_class() {
parent::set_up_before_class();
}

public function test_should_not_print_when_no_fonts() {
switch_theme( 'block-theme' );

$this->expectOutputString( '' );
wp_print_font_faces();
}

/**
* @dataProvider data_should_print_given_fonts
*
Expand Down

0 comments on commit 6f8e601

Please sign in to comment.