Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webfonts API: expose enqueueing method instead of directly enqueueing fonts on registration #39327

Closed
wants to merge 14 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add wp_enqueue_webfonts and wp_enqueue_webfont tests
  • Loading branch information
zaguiini committed Mar 17, 2022
commit 0888132d55beb52fca1af85daa3e8aeb0ec17906
121 changes: 121 additions & 0 deletions phpunit/class-wp-webfonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,127 @@ public function test_wp_register_webfont_does_not_enqueue_on_registration() {
$this->assertEquals( array(), wp_webfonts()->get_enqueued_fonts() );
}

/**
* Test wp_enqueue_webfonts() bulk enqueue webfonts.
*
* @covers wp_enqueue_webfonts
* @covers WP_Webfonts::enqueue_font
* @covers WP_Webfonts::get_enqueued_fonts
* @covers WP_Webfonts::get_registered_fonts
*/
public function test_wp_enqueue_webfonts() {
$fonts = array(
array(
'id' => 'source-serif-pro-200-900-normal-local',
'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',
),
array(
'id' => 'source-serif-pro-200-900-italic-local',
'provider' => 'local',
'font-family' => 'Source Serif Pro',
'font-style' => 'italic',
'font-weight' => '200 900',
'font-stretch' => 'normal',
'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2',
'font-display' => 'fallback',
),
);

$expected = array(
$fonts[0]['id'] => array_filter( $fonts[0], fn( $key ) => 'id' !== $key, ARRAY_FILTER_USE_KEY ),
$fonts[1]['id'] => array_filter( $fonts[1], fn( $key ) => 'id' !== $key, ARRAY_FILTER_USE_KEY ),
);

wp_enqueue_webfonts( $fonts );
$this->assertEquals( $expected, wp_webfonts()->get_enqueued_fonts() );
$this->assertEquals( array(), wp_webfonts()->get_registered_fonts() );
}

/**
* Test wp_enqueue_font() enqueues a registered webfont.
*
* @covers wp_enqueue_webfont
* @covers WP_Webfonts::enqueued_font
* @covers WP_Webfonts::get_enqueued_fonts
* @covers WP_Webfonts::get_registered_fonts
*/
public function test_wp_enqueue_webfont_enqueues_registered_webfont() {
$id = 'source-serif-pro-200-900-normal-local';

$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',
);

$expected = array(
$id => $font,
);

wp_register_webfont( $id, $font );
wp_enqueue_webfont( $id );

$this->assertEquals( array(), wp_webfonts()->get_registered_fonts() );
$this->assertEquals( $expected, wp_webfonts()->get_enqueued_fonts() );
}

/**
* Test wp_enqueue_font() register and enqueues a webfont if given as an argument.
*
* @covers wp_enqueue_webfont
* @covers WP_Webfonts::enqueued_font
* @covers WP_Webfonts::get_enqueued_fonts
*/
public function test_wp_enqueue_webfont_register_and_enqueues_if_webfont_given_as_argument() {
$id = 'source-serif-pro-200-900-normal-local';

$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',
);

$expected = array(
$id => $font,
);

wp_enqueue_webfont( $id, $font );

$this->assertEquals( array(), wp_webfonts()->get_registered_fonts() );
$this->assertEquals( $expected, wp_webfonts()->get_enqueued_fonts() );
}

/**
* Test wp_enqueue_font() does not enqueue a webfont that was not registered.
*
* @covers wp_enqueue_webfont
* @covers WP_Webfonts::enqueued_font
* @covers WP_Webfonts::get_enqueued_fonts
* @covers WP_Webfonts::get_registered_fonts
*/
public function test_wp_enqueue_webfont_does_not_enqueue_unregistered_webfont() {
$id = 'source-serif-pro-200-900-normal-local';

wp_enqueue_webfont( $id );

$this->assertEquals( array(), wp_webfonts()->get_registered_fonts() );
$this->assertEquals( array(), wp_webfonts()->get_enqueued_fonts() );
}

/**
* @covers wp_register_webfont
* @covers WP_Webfonts::register_provider
Expand Down