Skip to content

Commit

Permalink
keep track of unregistered font collection ids in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Oct 11, 2023
1 parent 2d6dcd2 commit b7c9113
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
24 changes: 18 additions & 6 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public static function get_expected_font_mime_types_per_php_version( $php_versio
*/
private static $collections = array();

/**
* Unregistered font collections ids.
*
* @since 6.4.0
*
* @var array
*/
private static $unregistered_collection_ids = array();

/**
* Register a new font collection.
*
Expand Down Expand Up @@ -81,11 +90,7 @@ public static function register_font_collection( $config ) {
* @return bool True if the font collection was unregistered successfully and false otherwise.

Check failure on line 90 in lib/experimental/fonts/font-library/class-wp-font-library.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Function return type is not void, but function has no return statement
*/
public static function unregister_font_collection( $collection_id ) {
if ( ! isset( self::$collections[ $collection_id ] ) ) {
return false;
}
unset( self::$collections[ $collection_id ] );
return true;
self::$unregistered_collection_ids[] = $collection_id;
}

/**
Expand All @@ -96,7 +101,14 @@ public static function unregister_font_collection( $collection_id ) {
* @return array List of font collections.
*/
public static function get_font_collections() {
return self::$collections;
// filters collections to remove unregistered collections.
$registered_collections = array();
foreach ( self::$collections as $key => $value ) {
if ( ! in_array( $key, self::$unregistered_collection_ids, true ) ) {
$registered_collections[ $key ] = $value;
}
}
return $registered_collections;
}

/**
Expand Down
8 changes: 2 additions & 6 deletions lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ function wp_register_font_collection( $config ) {
* @param string $collection_id The font collection ID.
*/
function wp_unregister_font_collection( $collection_id ) {
add_action(
'init',
function () use ( $collection_id ) {
WP_Font_Library::unregister_font_collection( $collection_id );
}
);
WP_Font_Library::unregister_font_collection( $collection_id );
}

}

$default_font_collection = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,28 @@
class Tests_Fonts_WpFontLibrary_UnregisterFontCollection extends WP_UnitTestCase {

public function set_up() {
$config = array(
'id' => 'mock-col-to-unregister',
'name' => 'Mock Collection to be unregistered',
'description' => 'A mock font collection to be unregistered.',
'data_json_file' => 'my-collection-data.json',
$config = array(
'id' => 'mock-col-to-unregister',
'name' => 'Mock Collection to be unregistered',
'description' => 'A mock font collection to be unregistered.',
'src' => 'my-collection-data.json',
);
WP_Font_Library::register_font_collection( $config );

$config = array(
'id' => 'another-collecction',
'name' => 'Mock Collection',
'description' => 'A mock font collection.',
'src' => 'my-mock-data.json',
);
WP_Font_Library::register_font_collection( $config );
}

public function test_should_unregister_font_collection() {
// Unregister mock font collection.
$result1 = WP_Font_Library::unregister_font_collection( 'mock-col-to-unregister' );
$this->assertTrue( $result1, 'Should return true if it was unregistered succesfully.' );
// Try to unregister mock font collection again.
$result2 = WP_Font_Library::unregister_font_collection( 'mock-col-to-unregister' );
$this->assertFalse( $result2, 'Should return false because it was already unregistered.' );
}

public function test_should_unregister_default_font_collection() {
// Unregister default font collection.
$result1 = WP_Font_Library::unregister_font_collection( 'default-font-collection' );
$this->assertTrue( $result1, 'Should return true if it was unregistered succesfully.' );
// Try to unregister default font collection again.
$result2 = WP_Font_Library::unregister_font_collection( 'default-font-collection' );
$this->assertFalse( $result2, 'Should return false because it was already unregistered.' );
}

public function test_should_unregister_non_existing_font_collection() {
// Unregister default font collection.
$result = WP_Font_Library::unregister_font_collection( 'fake-collection-id-x1234' );
$this->assertFalse( $result );
WP_Font_Library::unregister_font_collection( 'mock-col-to-unregister' );
$collections = WP_Font_Library::get_font_collections();
$this->assertArrayNotHasKey( 'mock-col-to-unregister', $collections, 'Font collection was not unregistered.' );
$this->assertArrayHasKey( 'default-font-collection', $collections, 'Font collection was unregistered by mistake.' );
}
}

0 comments on commit b7c9113

Please sign in to comment.