Skip to content

Commit

Permalink
Add wp_print_fonts() tests.
Browse files Browse the repository at this point in the history
1. Adds test for when no fonts are passed into the function, i.e. meaning it will pull from the resolver.

2. Improves resolver test to validate the full returned array structure.

3. Moves the expected fonts to the dataset trait to share it between these 2 tests.

4. A wee bit of girlscouting.
  • Loading branch information
hellofromtonya committed Jul 6, 2023
1 parent 8ef75f1 commit 6359091
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 18 deletions.
73 changes: 73 additions & 0 deletions phpunit/fonts/wp-font-face-tests-dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,77 @@ public function data_should_print_given_fonts() {
),
);
}

public function get_expected_fonts_for_fonts_block_theme( $key = '' ) {
static $data = null;

if ( null === $data ) {
$uri = get_stylesheet_directory_uri() . '/assets/fonts/';
$data = array(
'fonts' => array(
'DM Sans' => array(
array(
'src' => array( $uri . 'dm-sans/DMSans-Regular.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '400',
),
array(
'src' => array( $uri . 'dm-sans/DMSans-Regular-Italic.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '400',
),
array(
'src' => array( $uri . 'dm-sans/DMSans-Bold.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '700',
),
array(
'src' => array( $uri . 'dm-sans/DMSans-Bold-Italic.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '700',
),
),
'Source Serif Pro' => array(
array(
'src' => array( $uri . 'source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ),
'font-family' => 'Source Serif Pro',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '200 900',
),
array(
'src' => array( $uri . 'source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2' ),
'font-family' => 'Source Serif Pro',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '200 900',
),
),
),
'font_face_styles' => <<<CSS
@font-face{font-family:"DM Sans";font-style:normal;font-weight:400;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Regular.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:italic;font-weight:400;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Regular-Italic.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:normal;font-weight:700;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Bold.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:italic;font-weight:700;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Bold-Italic.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;src:url('{$uri}source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"Source Serif Pro";font-style:italic;font-weight:200 900;font-display:fallback;src:url('{$uri}source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2') format('woff2');font-stretch:normal;}
CSS
,
);
}

if ( isset( $data[ $key ] ) ) {
return $data[ $key ];
}

return $data;
}
}
19 changes: 6 additions & 13 deletions phpunit/fonts/wpFontFaceResolver/getFontsFromThemeJson-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @group fontface
* @covers WP_Font_Face_Resolver::get_fonts_from_theme_json
*/
class Tests_Fonts_WPFontFaceResolver_GetFontsFromThemeJson extends WP_Fonts_TestCase {
class Tests_Fonts_WPFontFaceResolver_GetFontsFromThemeJson extends WP_Font_Face_TestCase {
const FONTS_THEME = 'fonts-block-theme';

public static function set_up_before_class() {
Expand All @@ -38,27 +38,20 @@ public static function set_up_before_class() {
parent::set_up_before_class();
}

public function test_should_return_empty_array_when_no_fonts_defined() {
public function test_should_return_empty_array_when_no_fonts_defined_in_theme() {
switch_theme( 'block-theme' );

$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
$this->assertIsArray( $fonts, 'Should return an array data type' );
$this->assertEmpty( $fonts, 'Should return an empty array' );
}

/**
* Tests all font families are registered and enqueued. "All" means all font families from
* the theme's theme.json.
*/
public function test_should_return_all_defined_font_families() {
public function test_should_return_all_fonts_from_theme() {
switch_theme( static::FONTS_THEME );

$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();

$this->assertSameSetsWithIndex(
array( 'DM Sans', 'Source Serif Pro' ),
array_keys( $fonts )
);
$actual = WP_Font_Face_Resolver::get_fonts_from_theme_json();
$expected = $this->get_expected_fonts_for_fonts_block_theme( 'fonts' );
$this->assertSame( $expected, $actual );
}

/**
Expand Down
30 changes: 25 additions & 5 deletions phpunit/fonts/wpPrintFontFaces-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @subpackage Fonts
*/

require_once __DIR__ . '/wp-font-face-tests-dataset.php';
require_once __DIR__ . '/wp-font-face-testcase.php';

/*
* This code is only needed if the Font API is enabled.
Expand All @@ -29,8 +29,14 @@
* @group fontface
* @covers wp_print_font_faces
*/
class Tests_Fonts_WPPrintFontFaces extends WP_UnitTestCase {
use WP_Font_Face_Tests_Datasets;
class Tests_Fonts_WPPrintFontFaces extends WP_Font_Face_TestCase {
const FONTS_THEME = 'fonts-block-theme';

public static function set_up_before_class() {
self::$requires_switch_theme_fixtures = true;

parent::set_up_before_class();
}

/**
* @dataProvider data_should_print_given_fonts
Expand All @@ -39,10 +45,24 @@ class Tests_Fonts_WPPrintFontFaces extends WP_UnitTestCase {
* @param string $expected Expected CSS.
*/
public function test_should_print_given_fonts( array $fonts, $expected ) {
$style_element = "<style id='wp-fonts-local' type='text/css'>\n%s\n</style>\n";
$expected_output = sprintf( $style_element, $expected );
$expected_output = $this->get_expected_styles_output( $expected );

$this->expectOutputString( $expected_output );
wp_print_font_faces( $fonts );
}

public function test_should_print_fonts_in_merged_data() {
switch_theme( static::FONTS_THEME );

$expected = $this->get_expected_fonts_for_fonts_block_theme( 'font_face_styles' );
$expected_output = $this->get_expected_styles_output( $expected );

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

private function get_expected_styles_output( $styles ) {
$style_element = "<style id='wp-fonts-local' type='text/css'>\n%s\n</style>\n";
return sprintf( $style_element, $styles );
}
}

0 comments on commit 6359091

Please sign in to comment.