Skip to content

Commit

Permalink
Font Library: unregister font collection (#54701)
Browse files Browse the repository at this point in the history
* remove font collection

* add wp_unregister_font_collection function

* format php

* fix function comment

* add tests for unregister_font_collection

* removig unused variable

* update wording on comments

Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com>

* keep track of unregistered font collection ids in an array

* add tests to assure that get_font_collections returns an empty array if all the font collections were unregistered

* fix tests

* test the initial empty value of the font library collections

* format

* simplify unregistering of font collections and add _doing_it_wrong call

* add translation domain

* adding _doing_it_wrong if you are registering collections with the same id or unregistering a non existing collection id

* updating tests

* replace 6.4.0 by 6.5.0 in comments

Co-authored-by: Vicente Canales <1157901+vcanales@users.noreply.github.com>

* update version

Co-authored-by: Grant Kinney <creativecoder@users.noreply.github.com>

* consolidate code as only one function to avoid code repetition

* create base test case

* format php

* assertWPError

* check that collection was not unregistered by mistake

* calling parent test class mehtods

* format php

---------

Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com>
Co-authored-by: Vicente Canales <1157901+vcanales@users.noreply.github.com>
Co-authored-by: Grant Kinney <creativecoder@users.noreply.github.com>
  • Loading branch information
4 people committed Jan 8, 2024
1 parent 80f8d9e commit 302c148
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 33 deletions.
50 changes: 46 additions & 4 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,57 @@ public static function get_expected_font_mime_types_per_php_version( $php_versio
*/
public static function register_font_collection( $config ) {
$new_collection = new WP_Font_Collection( $config );

if ( isset( self::$collections[ $config['id'] ] ) ) {
return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' );
if ( self::is_collection_registered( $config['id'] ) ) {
$error_message = sprintf(
/* translators: %s: Font collection id. */
__( 'Font collection with id: "%s" is already registered.', 'default' ),
$config['id']
);
_doing_it_wrong(
__METHOD__,
$error_message,
'6.5.0'
);
return new WP_Error( 'font_collection_registration_error', $error_message );
}

self::$collections[ $config['id'] ] = $new_collection;
return $new_collection;
}

/**
* Unregisters a previously registered font collection.
*
* @since 6.5.0
*
* @param string $collection_id Font collection ID.
* @return bool True if the font collection was unregistered successfully and false otherwise.
*/
public static function unregister_font_collection( $collection_id ) {
if ( ! self::is_collection_registered( $collection_id ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Font collection id. */
sprintf( __( 'Font collection "%s" not found.', 'default' ), $collection_id ),
'6.5.0'
);
return false;
}
unset( self::$collections[ $collection_id ] );
return true;
}

/**
* Checks if a font collection is registered.
*
* @since 6.5.0
*
* @param string $collection_id Font collection ID.
* @return bool True if the font collection is registered and false otherwise.
*/
private static function is_collection_registered( $collection_id ) {
return array_key_exists( $collection_id, self::$collections );
}

/**
* Gets all the font collections available.
*
Expand Down
13 changes: 13 additions & 0 deletions lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ function wp_register_font_collection( $config ) {
}
}

if ( ! function_exists( 'wp_unregister_font_collection' ) ) {
/**
* Unregisters a font collection from the Font Library.
*
* @since 6.5.0
*
* @param string $collection_id The font collection ID.
*/
function wp_unregister_font_collection( $collection_id ) {
WP_Font_Library::unregister_font_collection( $collection_id );
}

}

$default_font_collection = array(
'id' => 'default-font-collection',
Expand Down
26 changes: 26 additions & 0 deletions phpunit/tests/fonts/font-library/wpFontLibrary/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Test Case for WP_Font_Library tests.
*
* @package WordPress
* @subpackage Font Library
*/
abstract class WP_Font_Library_UnitTestCase extends WP_UnitTestCase {
public function reset_font_collections() {
// Resets the private static property WP_Font_Library::$collections to empty array.
$reflection = new ReflectionClass( 'WP_Font_Library' );
$property = $reflection->getProperty( 'collections' );
$property->setAccessible( true );
$property->setValue( array() );
}

public function set_up() {
parent::set_up();
$this->reset_font_collections();
}

public function tear_down() {
parent::tear_down();
$this->reset_font_collections();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@
*
* @covers WP_Font_Library::get_font_collection
*/
class Tests_Fonts_WpFontLibrary_GetFontCollection extends WP_UnitTestCase {
class Tests_Fonts_WpFontLibrary_GetFontCollection extends WP_Font_Library_UnitTestCase {

public static function set_up_before_class() {
public function test_should_get_font_collection() {
$my_font_collection_config = array(
'id' => 'my-font-collection',
'name' => 'My Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => path_join( __DIR__, 'my-font-collection-data.json' ),
);

wp_register_font_collection( $my_font_collection_config );
}

public function test_should_get_font_collection() {
$font_collection = WP_Font_Library::get_font_collection( 'my-font-collection' );
$this->assertInstanceOf( 'WP_Font_Collection', $font_collection );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,25 @@
*
* @covers WP_Font_Library::get_font_collections
*/
class Tests_Fonts_WpFontLibrary_GetFontCollections extends WP_UnitTestCase {

public static function set_up_before_class() {
$font_library = new WP_Font_Library();
class Tests_Fonts_WpFontLibrary_GetFontCollections extends WP_Font_Library_UnitTestCase {
public function test_should_get_an_empty_list() {
$font_collections = WP_Font_Library::get_font_collections();
$this->assertEmpty( $font_collections, 'Should return an empty array.' );
}

public function test_should_get_mock_font_collection() {
$my_font_collection_config = array(
'id' => 'my-font-collection',
'name' => 'My Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => path_join( __DIR__, 'my-font-collection-data.json' ),
);

$font_library::register_font_collection( $my_font_collection_config );
}

public function test_should_get_the_default_font_collection() {
$font_collections = WP_Font_Library::get_font_collections();
$this->assertArrayHasKey( 'default-font-collection', $font_collections, 'Default Google Fonts collection should be registered' );
$this->assertInstanceOf( 'WP_Font_Collection', $font_collections['default-font-collection'], 'The value of the array $font_collections[id] should be an instance of WP_Font_Collection class.' );
}
WP_Font_Library::register_font_collection( $my_font_collection_config );

public function test_should_get_the_right_number_of_collections() {
$font_collections = WP_Font_Library::get_font_collections();
$this->assertNotEmpty( $font_collections, 'Sould return an array of font collections.' );
$this->assertCount( 2, $font_collections, 'Should return an array with one font collection.' );
}

public function test_should_get_mock_font_collection() {
$font_collections = WP_Font_Library::get_font_collections();
$this->assertCount( 1, $font_collections, 'Should return an array with one font collection.' );
$this->assertArrayHasKey( 'my-font-collection', $font_collections, 'The array should have the key of the registered font collection id.' );
$this->assertInstanceOf( 'WP_Font_Collection', $font_collections['my-font-collection'], 'The value of the array $font_collections[id] should be an instance of WP_Font_Collection class.' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @covers WP_Font_Library::get_fonts_dir
*/
class Tests_Fonts_WpFontLibrary_GetFontsDir extends WP_UnitTestCase {
class Tests_Fonts_WpFontLibrary_GetFontsDir extends WP_Font_Library_UnitTestCase {

public function test_get_fonts_dir() {
$this->assertStringEndsWith( '/wp-content/fonts', WP_Font_Library::get_fonts_dir() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @covers WP_Font_Family_Utils::get_expected_font_mime_types_per_php_version
*/
class Tests_Fonts_WpFontsFamilyUtils_GetMimeTypes extends WP_UnitTestCase {
class Tests_Fonts_WpFontsFamilyUtils_GetMimeTypes extends WP_Font_Library_UnitTestCase {

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @covers WP_Font_Library::register_font_collection
*/
class Tests_Fonts_WpFontLibrary_RegisterFontCollection extends WP_UnitTestCase {
class Tests_Fonts_WpFontLibrary_RegisterFontCollection extends WP_Font_Library_UnitTestCase {

public function test_should_register_font_collection() {
$config = array(
Expand Down Expand Up @@ -70,8 +70,10 @@ public function test_should_return_error_if_id_is_repeated() {
$collection1 = WP_Font_Library::register_font_collection( $config1 );
$this->assertInstanceOf( 'WP_Font_Collection', $collection1, 'A collection should be registered.' );

// Expects a _doing_it_wrong notice.
$this->setExpectedIncorrectUsage( 'WP_Font_Library::register_font_collection' );
// Try to register a second collection with same id.
$collection2 = WP_Font_Library::register_font_collection( $config2 );
$this->assertWPError( $collection2, 'Second collection with the same id should fail.' );
$this->assertWPError( $collection2, 'A WP_Error should be returned.' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @covers WP_Font_Library::set_upload_dir
*/
class Tests_Fonts_WpFontLibrary_SetUploadDir extends WP_UnitTestCase {
class Tests_Fonts_WpFontLibrary_SetUploadDir extends WP_Font_Library_UnitTestCase {

public function test_should_set_fonts_upload_dir() {
$defaults = array(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Test WP_Font_Library::unregister_font_collection().
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*
* @covers WP_Font_Library::unregister_font_collection
*/
class Tests_Fonts_WpFontLibrary_UnregisterFontCollection extends WP_Font_Library_UnitTestCase {

public function test_should_unregister_font_collection() {
// Registers two mock font collections.
$config = array(
'id' => 'mock-font-collection-1',
'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' => 'mock-font-collection-2',
'name' => 'Mock Collection',
'description' => 'A mock font collection.',
'src' => 'my-mock-data.json',
);
WP_Font_Library::register_font_collection( $config );

// Unregister mock font collection.
WP_Font_Library::unregister_font_collection( 'mock-font-collection-1' );
$collections = WP_Font_Library::get_font_collections();
$this->assertArrayNotHasKey( 'mock-font-collection-1', $collections, 'Font collection was not unregistered.' );
$this->assertArrayHasKey( 'mock-font-collection-2', $collections, 'Font collection was unregistered by mistake.' );

// Unregisters remaining mock font collection.
WP_Font_Library::unregister_font_collection( 'mock-font-collection-2' );
$collections = WP_Font_Library::get_font_collections();
$this->assertArrayNotHasKey( 'mock-font-collection-2', $collections, 'Mock font collection was not unregistered.' );

// Checks that all font collections were unregistered.
$this->assertEmpty( $collections, 'Font collections were not unregistered.' );
}

public function unregister_non_existing_collection() {
// Unregisters non existing font collection.
WP_Font_Library::unregister_font_collection( 'non-existing-collection' );
$collections = WP_Font_Library::get_font_collections();
$this->assertEmpty( $collections, 'Should not be registered collections.' );
}
}

0 comments on commit 302c148

Please sign in to comment.