From 79d6af78415c5588acb0d8fdd7444b6f11f5ed51 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 18 Aug 2023 12:35:08 -0300 Subject: [PATCH 01/50] Adding Font Collection class --- .../font-library/class-wp-font-collection.php | 77 +++++++++++++++++++ .../font-library/class-wp-font-library.php | 54 +++++++++++++ .../class-wp-rest-font-library-controller.php | 63 +++++++++++++++ .../fonts/font-library/font-library.php | 10 ++- lib/load.php | 1 + 5 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 lib/experimental/fonts/font-library/class-wp-font-collection.php diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php new file mode 100644 index 0000000000000..96c02bbb3bd91 --- /dev/null +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -0,0 +1,77 @@ +config = $config; + } + + /** + * Gets the font collection config. + * + * @since 6.4.0 + * + * @return array An array contaning the font collection config. + */ + public function get_config() { + return $this->config; + } + + /** + * Gets the font collection data. + * + * @since 6.4.0 + * + * @return array An array contaning the list of font families in theme.json format + */ + public function get_data() { + if ( ! empty( $this->config['data_json_file'] ) ) { + if ( file_exists( $this->config['data_json_file'] ) ) { + $data = file_get_contents( $this->config['data_json_file'] ); + $collection_data = $this->get_config(); + $collection_data['data'] = $data; + unset( $collection_data['data_json_file'] ); + return $collection_data; + } + } + return new WP_Error( 'font_collection_error', 'Font Collection data is missing the data_json_file.' ); + } + +} diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 6459a91873fea..83c5b2da7c403 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -27,6 +27,60 @@ class WP_Font_Library { 'woff2' => 'font/woff2', ); + /** + * Font collections. + * + * @since 6.4.0 + * + * @var array + */ + public static $collections = array(); + + /** + * Register filter to extend the library with font collections. + * + * @since 6.4.0 + */ + public static function register_filters() { + add_filter( + 'register_font_collection', + array( 'WP_Font_Library', 'register_font_collection' ), + 10, + 2 + ); + } + + /** + * Register a new font collection. + * + * @since 6.4.0 + * + * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. + */ + public static function register_font_collection( $id, $config ) { + if ( ! array_key_exists( $id, self::$collections ) ) { + try { + + $new_collection = new WP_Font_Collection( $id, $config ); + self::$collections[ $id ] = $new_collection; + return $new_collection; + } catch ( Exception $e ) { + return new WP_Error( 'font_collection_error', $e->getMessage() ); + } + } + } + + /** + * Gets the font collections available. + * + * @since 6.4.0 + * + * @return array List of font collections. + */ + public static function get_font_collections() { + return self::$collections; + } + /** * Gets the upload directory for fonts. * diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 59e42d8716124..4184b620d64a9 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -67,6 +67,69 @@ public function register_routes() { ), ) ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/collections', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_font_collections' ), + 'permission_callback' => array( $this, 'update_font_library_permissions_check' ), + ), + ) + ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/collections' . '/(?P[\/\w-]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_font_collection' ), + 'permission_callback' => array( $this, 'update_font_library_permissions_check' ), + ), + ) + ); + } + + /** + * Gets a font collection. + * + * @since 6.4.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + public function get_font_collection( $request ) { + $id = $request->get_param( 'id' ); + if ( ! array_key_exists( $id, WP_Font_Library::get_font_collections() ) ) { + return new WP_Error( + 'font_collection_not_found', + __( 'Font collection not found.', 'gutenberg' ), + array( 'status' => 404 ) + ); + } + + $collection = WP_Font_Library::get_font_collections()[ $id ]; + return new WP_REST_Response( $collection->get_data() ); + } + + /** + * Gets the font collections available. + * + * @since 6.4.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + public function get_font_collections( $request ) { + $collections = array(); + foreach ( WP_Font_Library::get_font_collections() as $collection ) { + $collections[] = $collection->get_config(); + } + + return new WP_REST_Response( $collections ); } /** diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index 96a33923917ec..a2559b69dc69d 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -19,7 +19,7 @@ * * @since 6.4.0 */ -function gutenberg_init_font_library() { +function gutenberg_init_font_library_routes() { // @core-merge: This code will go into Core's `create_initial_post_types()`. $args = array( 'public' => true, @@ -33,5 +33,11 @@ function gutenberg_init_font_library() { $font_library_controller->register_routes(); } -add_action( 'rest_api_init', 'gutenberg_init_font_library' ); +add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' ); + + +function gutenberg_init_font_library() { + WP_Font_Library::register_filters(); +} +add_action( 'init', 'gutenberg_init_font_library' ); diff --git a/lib/load.php b/lib/load.php index cfd5c4f38d0c5..2dbcfeb114379 100644 --- a/lib/load.php +++ b/lib/load.php @@ -164,6 +164,7 @@ function gutenberg_is_experiment_enabled( $name ) { ( defined( 'FONTS_LIBRARY_ENABLE' ) && FONTS_LIBRARY_ENABLE ) ) { // Loads the Font Library. + require __DIR__ . '/experimental/fonts/font-library/class-wp-font-collection.php'; require __DIR__ . '/experimental/fonts/font-library/class-wp-font-library.php'; require __DIR__ . '/experimental/fonts/font-library/class-wp-font-family-utils.php'; require __DIR__ . '/experimental/fonts/font-library/class-wp-font-family.php'; From fd1c967c7c687bc294a21c49d67f6e5dcd6b4a36 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 18 Aug 2023 13:21:31 -0300 Subject: [PATCH 02/50] php formatting and linting --- .../fonts/font-library/class-wp-font-collection.php | 9 +++++++++ .../fonts/font-library/class-wp-font-library.php | 2 ++ .../class-wp-rest-font-library-controller.php | 2 +- lib/experimental/fonts/font-library/font-library.php | 6 +++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 96c02bbb3bd91..e2fe5cb9a8c75 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -29,6 +29,15 @@ class WP_Font_Collection { */ private $config; + /** + * WP_Font_Collection constructor. + * + * @since 6.4.0 + * + * @param string $id Font collection id. + * @param array $config Font collection config options. + * @throws Exception If some of the required parameters are missing. + */ public function __construct( $id, $config ) { if ( empty( $id ) ) { diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 83c5b2da7c403..48fca8120bec6 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -55,6 +55,8 @@ public static function register_filters() { * * @since 6.4.0 * + * @param string $id Font collection id. + * @param array $config Font collection config options. * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. */ public static function register_font_collection( $id, $config ) { diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 4184b620d64a9..541de5b45dda1 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -123,7 +123,7 @@ public function get_font_collection( $request ) { * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ - public function get_font_collections( $request ) { + public function get_font_collections() { $collections = array(); foreach ( WP_Font_Library::get_font_collections() as $collection ) { $collections[] = $collection->get_config(); diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index a2559b69dc69d..d52b382f72ae0 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -35,7 +35,11 @@ function gutenberg_init_font_library_routes() { add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' ); - +/** + * Registers the font collection filter for font library. + * + * @since 6.4.0 + */ function gutenberg_init_font_library() { WP_Font_Library::register_filters(); } From 0cfac6cc2a0214b4e68ef454968479fcc5502bdd Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 18 Aug 2023 15:08:43 -0300 Subject: [PATCH 03/50] Adding tests for collections routes --- .../class-wp-rest-font-library-controller-test.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpunit/fonts/font-library/class-wp-rest-font-library-controller-test.php b/phpunit/fonts/font-library/class-wp-rest-font-library-controller-test.php index f55d221acba96..a5d90a31028d4 100644 --- a/phpunit/fonts/font-library/class-wp-rest-font-library-controller-test.php +++ b/phpunit/fonts/font-library/class-wp-rest-font-library-controller-test.php @@ -35,8 +35,13 @@ public function test_register_routes() { $routes = rest_get_server()->get_routes(); $this->assertArrayHasKey( '/wp/v2/fonts', $routes, 'Rest server has not the fonts path intialized.' ); $this->assertCount( 2, $routes['/wp/v2/fonts'], 'Rest server has not the 2 fonts paths initialized.' ); + $this->assertCount( 1, $routes['/wp/v2/fonts/collections'], 'Rest server has not the collections path initialized.' ); + $this->assertCount( 1, $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'], 'Rest server has not the collection path initialized.' ); + $this->assertArrayHasKey( 'POST', $routes['/wp/v2/fonts'][0]['methods'], 'Rest server has not the POST method for fonts intialized.' ); $this->assertArrayHasKey( 'DELETE', $routes['/wp/v2/fonts'][1]['methods'], 'Rest server has not the DELETE method for fonts intialized.' ); + $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections'][0]['methods'], 'Rest server has not the GET method for collections intialized.' ); + $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' ); } /** From 6bb2f8b0490e049e800e4debdf7115424116d020 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 18 Aug 2023 15:45:53 -0300 Subject: [PATCH 04/50] Adding tests for WP_Font_Collection constructor --- .../font-library/class-wp-font-collection.php | 4 +- .../wpFontCollection/__construct-test.php | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 phpunit/fonts/font-library/wpFontCollection/__construct-test.php diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index e2fe5cb9a8c75..61ce6133d8a9a 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -41,11 +41,11 @@ class WP_Font_Collection { public function __construct( $id, $config ) { if ( empty( $id ) ) { - throw new Exception( 'Font Collection data is missing the id.' ); + throw new Exception( 'Font Collection is missing the id.' ); } if ( empty( $config ) ) { - throw new Exception( 'Font Collection data is missing the config.' ); + throw new Exception( 'Font Collection is missing the config.' ); } $config['id'] = $id; diff --git a/phpunit/fonts/font-library/wpFontCollection/__construct-test.php b/phpunit/fonts/font-library/wpFontCollection/__construct-test.php new file mode 100644 index 0000000000000..2bd32487e9ce2 --- /dev/null +++ b/phpunit/fonts/font-library/wpFontCollection/__construct-test.php @@ -0,0 +1,93 @@ +setAccessible( true ); + + $id = 'my-collection'; + $config = array( + 'name' => 'My Collection', + 'description' => 'My collection description', + 'data_json_file' => 'my-collection-data.json', + ); + $font_collection = new WP_Font_Collection( $id, $config ); + + $actual = $property->getValue( $font_collection ); + $property->setAccessible( false ); + + $expected = $config; + $expected['id'] = $id; + + $this->assertSame( $expected, $actual ); + } + + /** + * @dataProvider data_should_throw_exception + * + * @param mixed $id Id of the font collection. + * @param mixed $config Config of the font collection. + * @param string $expected_exception_message Expected exception message. + */ + public function test_should_throw_exception( $id, $config, $expected_exception_message ) { + $this->expectException( 'Exception' ); + $this->expectExceptionMessage( $expected_exception_message ); + + new WP_Font_Collection( $id, $config ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_throw_exception() { + return array( + 'no id' => array( + '', + array( + 'name' => 'My Collection', + 'description' => 'My collection description', + 'data_json_file' => 'my-collection-data.json', + ), + 'Font Collection is missing the id.', + ), + + 'no config' => array( + 'my-collection', + '', + 'Font Collection is missing the config.', + ), + + 'empty array' => array( + 'my-collection', + array(), + 'Font Collection is missing the config.', + ), + + 'boolean instead of config array' => array( + 'my-collection', + false, + 'Font Collection is missing the config.', + ), + + 'null instead of config array' => array( + 'my-collection', + null, + 'Font Collection is missing the config.', + ), + + ); + } +} From 4a67d0b2385b8446342b531c9e162acca7544551 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 18 Aug 2023 16:52:47 -0300 Subject: [PATCH 05/50] adding tests for WP_Font_Collection get_data() --- .../wpFontCollection/getData-test.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 phpunit/fonts/font-library/wpFontCollection/getData-test.php diff --git a/phpunit/fonts/font-library/wpFontCollection/getData-test.php b/phpunit/fonts/font-library/wpFontCollection/getData-test.php new file mode 100644 index 0000000000000..1b2b415ee892d --- /dev/null +++ b/phpunit/fonts/font-library/wpFontCollection/getData-test.php @@ -0,0 +1,53 @@ +assertSame( $expected_data, $collection->get_data() ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_get_data() { + $mock_file = wp_tempnam( 'my-collection-data-' ); + file_put_contents( $mock_file, '{"this is mock data":true}' ); + + return array( + 'with a data_json_file' => array( + 'id' => 'my-collection', + 'config' => array( + 'name' => 'My Collection', + 'description' => 'My collection description', + 'data_json_file' => $mock_file, + ), + 'expected_data' => array( + 'name' => 'My Collection', + 'description' => 'My collection description', + 'id' => 'my-collection', + 'data' => '{"this is mock data":true}', + ), + ), + ); + } +} From b26cb638ca51b2537ebdc9c24c5fe9cd25270f20 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 18 Aug 2023 17:39:17 -0300 Subject: [PATCH 06/50] adding tests for WP_Font_Library register_font_collection() --- .../font-library/class-wp-font-library.php | 17 ++--- .../registerFontCollection-test.php | 69 +++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 phpunit/fonts/font-library/wpFontLibrary/registerFontCollection-test.php diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 48fca8120bec6..9037b983611f4 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -60,15 +60,16 @@ public static function register_filters() { * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. */ public static function register_font_collection( $id, $config ) { - if ( ! array_key_exists( $id, self::$collections ) ) { - try { + if ( array_key_exists( $id, self::$collections ) ) { + return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' ); + } - $new_collection = new WP_Font_Collection( $id, $config ); - self::$collections[ $id ] = $new_collection; - return $new_collection; - } catch ( Exception $e ) { - return new WP_Error( 'font_collection_error', $e->getMessage() ); - } + try { + $new_collection = new WP_Font_Collection( $id, $config ); + self::$collections[ $id ] = $new_collection; + return $new_collection; + } catch ( Exception $e ) { + return new WP_Error( 'font_collection_error', $e->getMessage() ); } } diff --git a/phpunit/fonts/font-library/wpFontLibrary/registerFontCollection-test.php b/phpunit/fonts/font-library/wpFontLibrary/registerFontCollection-test.php new file mode 100644 index 0000000000000..8cb3252835c3e --- /dev/null +++ b/phpunit/fonts/font-library/wpFontLibrary/registerFontCollection-test.php @@ -0,0 +1,69 @@ + 'My Collection', + 'description' => 'My Collection Description', + 'data_file_json' => 'my-collection-data.json', + ); + $collection = WP_Font_Library::register_font_collection( $id, $config ); + $this->assertInstanceOf( 'WP_Font_Collection', $collection ); + } + + public function test_should_return_error_if_id_is_missing() { + $config = array( + 'name' => 'My Collection', + 'description' => 'My Collection Description', + 'data_file_json' => 'my-collection-data.json', + ); + $collection = WP_Font_Library::register_font_collection( '', $config ); + $this->assertInstanceOf( 'WP_Error', $collection ); + } + + public function test_should_return_error_if_config_is_missing() { + $id = 'my-other-collection'; + $collection = WP_Font_Library::register_font_collection( $id, '' ); + $this->assertInstanceOf( 'WP_Error', $collection ); + } + + public function test_should_return_error_if_config_is_empty() { + $id = 'my-other-collection'; + $collection = WP_Font_Library::register_font_collection( $id, array() ); + $this->assertInstanceOf( 'WP_Error', $collection ); + } + + public function test_should_return_error_if_id_is_repeated() { + $id1 = 'my-collection-1'; + $config1 = array( + 'name' => 'My Collection 1', + 'description' => 'My Collection 1 Description', + 'data_file_json' => 'my-collection-1-data.json', + ); + $config2 = array( + 'name' => 'My Collection 2', + 'description' => 'My Collection 2 Description', + 'data_file_json' => 'my-collection-2-data.json', + ); + + // Register first collection + $collection1 = WP_Font_Library::register_font_collection( $id1, $config1 ); + $this->assertInstanceOf( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); + + // Try to register a second collection with same id + $collection2 = WP_Font_Library::register_font_collection( $id1, $config2 ); + $this->assertInstanceOf( 'WP_Error', $collection2, 'Second collection with the same id should fail.' ); + } +} From 0cfddb268ad804c6a9d79ec9e5da734a145c04f6 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 21 Aug 2023 13:37:24 -0300 Subject: [PATCH 07/50] get font collection tests --- .../font-library/class-wp-font-library.php | 17 ++++++- .../class-wp-rest-font-library-controller.php | 7 +-- .../wpFontLibrary/getFontCollection-test.php | 35 +++++++++++++++ .../wpFontLibrary/getFontCollections-test.php | 44 +++++++++++++++++++ 4 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php create mode 100644 phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 9037b983611f4..6e7bb27c91bd2 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -74,7 +74,7 @@ public static function register_font_collection( $id, $config ) { } /** - * Gets the font collections available. + * Gets all the font collections available. * * @since 6.4.0 * @@ -84,6 +84,21 @@ public static function get_font_collections() { return self::$collections; } + /** + * Gets a font collection. + * + * @since 6.4.0 + * + * @param string $id Font collection id. + * @return array List of font collections. + */ + public static function get_font_collection( $id ) { + if ( array_key_exists( $id, self::$collections ) ) { + return self::$collections[ $id ]; + } + return new WP_Error( 'font_collection_not_found', 'Font collection not found.' ); + } + /** * Gets the upload directory for fonts. * diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 541de5b45dda1..16c5d794ec801 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -103,15 +103,16 @@ public function register_routes() { */ public function get_font_collection( $request ) { $id = $request->get_param( 'id' ); - if ( ! array_key_exists( $id, WP_Font_Library::get_font_collections() ) ) { + $collection = WP_Font_Library::get_font_collection( $id ); + + if ( is_wp_error( $collection ) ) { return new WP_Error( 'font_collection_not_found', __( 'Font collection not found.', 'gutenberg' ), array( 'status' => 404 ) ); } - - $collection = WP_Font_Library::get_font_collections()[ $id ]; + return new WP_REST_Response( $collection->get_data() ); } diff --git a/phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php b/phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php new file mode 100644 index 0000000000000..ee7c1a754de63 --- /dev/null +++ b/phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php @@ -0,0 +1,35 @@ + 'My Font Collection', + 'description' => 'Demo about how to a font collection to your WordPress Font Library.', + 'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ), + ); + + apply_filters( 'register_font_collection', 'my-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 ); + } + + public function test_should_get_no_font_collection_if_the_id_is_not_registered() { + $font_collection = WP_Font_Library::get_font_collection( 'not-registered-font-collection' ); + $this->assertWPError( $font_collection ); + } + +} diff --git a/phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php b/phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php new file mode 100644 index 0000000000000..0cfc8fe82fbde --- /dev/null +++ b/phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php @@ -0,0 +1,44 @@ + 'My Font Collection', + 'description' => 'Demo about how to a font collection to your WordPress Font Library.', + 'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ), + ); + + apply_filters( 'register_font_collection', 'my-font-collection', $my_font_collection_config ); + + $another_font_collection_config = array( + 'name' => 'Another Font Collection', + 'description' => 'Demo about how to a font collection to your WordPress Font Library.', + 'data_json_file' => path_join( __DIR__, 'another-font-collection-data.json' ), + ); + + apply_filters( 'register_font_collection', 'another-font-collection', $another_font_collection_config ); + } + + public function test_should_get_font_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.' ); + + $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.' ); + $this->assertArrayHasKey( 'another-font-collection', $font_collections, 'The array should have the key of the registered font collection id.' ); + $this->assertInstanceOf( 'WP_Font_Collection', $font_collections['another-font-collection'], 'The value of the array $font_collections[id] should be an instance of WP_Font_Collection class.' ); + } + +} From 7ba6f61a442b5298c231f47bda88226a3631e154 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 21 Aug 2023 17:01:46 -0300 Subject: [PATCH 08/50] adding 'wp_' prefix to the 'register_font_collection' filter name --- lib/experimental/fonts/font-library/class-wp-font-library.php | 4 ++-- .../font-library/wpFontLibrary/getFontCollection-test.php | 2 +- .../font-library/wpFontLibrary/getFontCollections-test.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 6e7bb27c91bd2..cbaa28d079140 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -43,8 +43,8 @@ class WP_Font_Library { */ public static function register_filters() { add_filter( - 'register_font_collection', - array( 'WP_Font_Library', 'register_font_collection' ), + 'wp_register_font_collection', + array( 'WP_Font_Library', 'wp_register_font_collection' ), 10, 2 ); diff --git a/phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php b/phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php index ee7c1a754de63..767fc55f62cd8 100644 --- a/phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php +++ b/phpunit/fonts/font-library/wpFontLibrary/getFontCollection-test.php @@ -19,7 +19,7 @@ public static function set_up_before_class() { 'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ), ); - apply_filters( 'register_font_collection', 'my-font-collection', $my_font_collection_config ); + apply_filters( 'wp_register_font_collection', 'my-font-collection', $my_font_collection_config ); } public function test_should_get_font_collection() { diff --git a/phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php b/phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php index 0cfc8fe82fbde..efdc7517c635b 100644 --- a/phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php +++ b/phpunit/fonts/font-library/wpFontLibrary/getFontCollections-test.php @@ -19,7 +19,7 @@ public static function set_up_before_class() { 'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ), ); - apply_filters( 'register_font_collection', 'my-font-collection', $my_font_collection_config ); + apply_filters( 'wp_register_font_collection', 'my-font-collection', $my_font_collection_config ); $another_font_collection_config = array( 'name' => 'Another Font Collection', @@ -27,7 +27,7 @@ public static function set_up_before_class() { 'data_json_file' => path_join( __DIR__, 'another-font-collection-data.json' ), ); - apply_filters( 'register_font_collection', 'another-font-collection', $another_font_collection_config ); + apply_filters( 'wp_register_font_collection', 'another-font-collection', $another_font_collection_config ); } public function test_should_get_font_collections() { From 454f9a5a5e7dd020d80e6b804db656b92d5f4155 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 21 Aug 2023 17:06:11 -0300 Subject: [PATCH 09/50] fix callback name --- lib/experimental/fonts/font-library/class-wp-font-library.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index cbaa28d079140..f760ca32caa69 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -44,7 +44,7 @@ class WP_Font_Library { public static function register_filters() { add_filter( 'wp_register_font_collection', - array( 'WP_Font_Library', 'wp_register_font_collection' ), + array( 'WP_Font_Library', 'register_font_collection' ), 10, 2 ); From 932064fa60a6afad445fb3f876ca9ba70669a612 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 21 Aug 2023 17:08:03 -0300 Subject: [PATCH 10/50] making class property private --- lib/experimental/fonts/font-library/class-wp-font-library.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index f760ca32caa69..13f9beedbdd5d 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -34,7 +34,7 @@ class WP_Font_Library { * * @var array */ - public static $collections = array(); + private static $collections = array(); /** * Register filter to extend the library with font collections. From b18439888291c840d3e8b53bf4c6a445326232c0 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 21 Aug 2023 17:18:06 -0300 Subject: [PATCH 11/50] registering filter from font-library.php file --- .../fonts/font-library/class-wp-font-library.php | 14 -------------- .../class-wp-rest-font-library-controller.php | 4 ++-- .../fonts/font-library/font-library.php | 11 ++++++++--- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 13f9beedbdd5d..86b64542ebed8 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -36,20 +36,6 @@ class WP_Font_Library { */ private static $collections = array(); - /** - * Register filter to extend the library with font collections. - * - * @since 6.4.0 - */ - public static function register_filters() { - add_filter( - 'wp_register_font_collection', - array( 'WP_Font_Library', 'register_font_collection' ), - 10, - 2 - ); - } - /** * Register a new font collection. * diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 16c5d794ec801..12f21c94a0e52 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -102,7 +102,7 @@ public function register_routes() { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_font_collection( $request ) { - $id = $request->get_param( 'id' ); + $id = $request->get_param( 'id' ); $collection = WP_Font_Library::get_font_collection( $id ); if ( is_wp_error( $collection ) ) { @@ -112,7 +112,7 @@ public function get_font_collection( $request ) { array( 'status' => 404 ) ); } - + return new WP_REST_Response( $collection->get_data() ); } diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index d52b382f72ae0..a1e555940c6a1 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -40,8 +40,13 @@ function gutenberg_init_font_library_routes() { * * @since 6.4.0 */ -function gutenberg_init_font_library() { - WP_Font_Library::register_filters(); +function gutenberg_add_register_font_collection_filter() { + add_filter( + 'wp_register_font_collection', + array( 'WP_Font_Library', 'register_font_collection' ), + 10, + 2 + ); } -add_action( 'init', 'gutenberg_init_font_library' ); +add_action( 'init', 'gutenberg_add_register_font_collection_filter' ); From 36ab1889227feb4d1f8fd6377dc47e44eaa1c3bc Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 22 Aug 2023 16:00:42 -0300 Subject: [PATCH 12/50] removing superfluous comment --- .../fonts/font-library/class-wp-rest-font-library-controller.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 12f21c94a0e52..4208684fedd68 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -121,7 +121,6 @@ public function get_font_collection( $request ) { * * @since 6.4.0 * - * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_font_collections() { From ac938c94869b60959bec9e836f847d04a975213f Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 22 Aug 2023 16:05:31 -0300 Subject: [PATCH 13/50] moving files to according changes in trunk --- .../fonts/font-library/wpFontCollection/__construct-test.php | 0 .../fonts/font-library/wpFontCollection/getData-test.php | 0 .../wpFontLibrary/registerFontCollection-test.php | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename phpunit/{ => tests}/fonts/font-library/wpFontCollection/__construct-test.php (100%) rename phpunit/{ => tests}/fonts/font-library/wpFontCollection/getData-test.php (100%) diff --git a/phpunit/fonts/font-library/wpFontCollection/__construct-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php similarity index 100% rename from phpunit/fonts/font-library/wpFontCollection/__construct-test.php rename to phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php diff --git a/phpunit/fonts/font-library/wpFontCollection/getData-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php similarity index 100% rename from phpunit/fonts/font-library/wpFontCollection/getData-test.php rename to phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php index 8cb3252835c3e..351fc1e10efeb 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php @@ -58,11 +58,11 @@ public function test_should_return_error_if_id_is_repeated() { 'data_file_json' => 'my-collection-2-data.json', ); - // Register first collection + // Register first collection. $collection1 = WP_Font_Library::register_font_collection( $id1, $config1 ); $this->assertInstanceOf( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); - // Try to register a second collection with same id + // Try to register a second collection with same id. $collection2 = WP_Font_Library::register_font_collection( $id1, $config2 ); $this->assertInstanceOf( 'WP_Error', $collection2, 'Second collection with the same id should fail.' ); } From a9c7b6a96ba17fc93d9ffb3fdb635650713261b7 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 22 Aug 2023 16:15:12 -0300 Subject: [PATCH 14/50] config without a json file should fail --- .../fonts/font-library/class-wp-font-collection.php | 4 ++++ .../font-library/wpFontCollection/__construct-test.php | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 61ce6133d8a9a..37c8d7881a672 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -48,6 +48,10 @@ public function __construct( $id, $config ) { throw new Exception( 'Font Collection is missing the config.' ); } + if ( empty( $config['data_json_file'] ) ) { + throw new Exception( 'Font Collection is missing the data_json_file.' ); + } + $config['id'] = $id; $this->config = $config; } diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php index 2bd32487e9ce2..f43048ba0fa01 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php @@ -88,6 +88,15 @@ public function data_should_throw_exception() { 'Font Collection is missing the config.', ), + 'missing data_json_file' => array( + 'my-collection', + array( + 'name' => 'My Collection', + 'description' => 'My collection description', + ), + 'Font Collection is missing the data_json_file.', + ), + ); } } From b2deb1410aa2c811c2919f3a69260ace3a953a1e Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 22 Aug 2023 16:20:10 -0300 Subject: [PATCH 15/50] fix property name in tests --- .../wpFontLibrary/registerFontCollection-test.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php index 351fc1e10efeb..364e0ca044e9e 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php @@ -17,7 +17,7 @@ public function test_should_register_font_collection() { $config = array( 'name' => 'My Collection', 'description' => 'My Collection Description', - 'data_file_json' => 'my-collection-data.json', + 'data_json_file' => 'my-collection-data.json', ); $collection = WP_Font_Library::register_font_collection( $id, $config ); $this->assertInstanceOf( 'WP_Font_Collection', $collection ); @@ -27,7 +27,7 @@ public function test_should_return_error_if_id_is_missing() { $config = array( 'name' => 'My Collection', 'description' => 'My Collection Description', - 'data_file_json' => 'my-collection-data.json', + 'data_json_file' => 'my-collection-data.json', ); $collection = WP_Font_Library::register_font_collection( '', $config ); $this->assertInstanceOf( 'WP_Error', $collection ); @@ -50,12 +50,12 @@ public function test_should_return_error_if_id_is_repeated() { $config1 = array( 'name' => 'My Collection 1', 'description' => 'My Collection 1 Description', - 'data_file_json' => 'my-collection-1-data.json', + 'data_json_file' => 'my-collection-1-data.json', ); $config2 = array( 'name' => 'My Collection 2', 'description' => 'My Collection 2 Description', - 'data_file_json' => 'my-collection-2-data.json', + 'data_json_file' => 'my-collection-2-data.json', ); // Register first collection. From 47655388de62250d7eb70c27ac9d4d1e143d24d8 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 23 Aug 2023 13:53:34 -0300 Subject: [PATCH 16/50] name fix Co-authored-by: Tonya Mork --- .../fonts/font-library/class-wp-font-collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 37c8d7881a672..4db41f11a7c76 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -5,7 +5,7 @@ * This file contains the Font Collection class definition. * * @package WordPress - * @subpackage Font Collection + * @subpackage Font Library * @since 6.4.0 */ From 3fb8b0adb7686dbe3116de6ea4e432b97cf29e70 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 23 Aug 2023 13:54:04 -0300 Subject: [PATCH 17/50] comment update Co-authored-by: Tonya Mork --- .../fonts/font-library/class-wp-font-collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 4db41f11a7c76..5093779547427 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -36,7 +36,7 @@ class WP_Font_Collection { * * @param string $id Font collection id. * @param array $config Font collection config options. - * @throws Exception If some of the required parameters are missing. + * @throws Exception If the required parameters are missing. */ public function __construct( $id, $config ) { From 3fadd9d1f6b51d3e28a5a79fa15f993a8990428d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 23 Aug 2023 12:09:55 -0500 Subject: [PATCH 18/50] Adds WP_Error to return type --- .../fonts/font-library/class-wp-font-collection.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 5093779547427..be384ddb45abe 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -34,7 +34,7 @@ class WP_Font_Collection { * * @since 6.4.0 * - * @param string $id Font collection id. + * @param string $id Font collection id. * @param array $config Font collection config options. * @throws Exception If the required parameters are missing. */ @@ -61,7 +61,7 @@ public function __construct( $id, $config ) { * * @since 6.4.0 * - * @return array An array contaning the font collection config. + * @return array An array containing the font collection config. */ public function get_config() { return $this->config; @@ -72,7 +72,8 @@ public function get_config() { * * @since 6.4.0 * - * @return array An array contaning the list of font families in theme.json format + * @return array|WP_Error An array containing the list of font families in theme.json format on success, + * else an instance of WP_Error on failure. */ public function get_data() { if ( ! empty( $this->config['data_json_file'] ) ) { From bdaacf8505a4f6f10c81b7923c3eb6dc0d9f5065 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 23 Aug 2023 12:13:11 -0500 Subject: [PATCH 19/50] Improves contructor error handling. * Ensures each required param is of the right data type. * Improves each param check error to include expected data type. --- .../fonts/font-library/class-wp-font-collection.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index be384ddb45abe..4ac9721ac649e 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -40,16 +40,16 @@ class WP_Font_Collection { */ public function __construct( $id, $config ) { - if ( empty( $id ) ) { - throw new Exception( 'Font Collection is missing the id.' ); + if ( empty( $id ) && is_string( $id ) ) { + throw new Exception( 'Font Collection ID is required as a non-empty string.' ); } if ( empty( $config ) ) { - throw new Exception( 'Font Collection is missing the config.' ); + throw new Exception( 'Font Collection config options is required as a non-empty array.' ); } - if ( empty( $config['data_json_file'] ) ) { - throw new Exception( 'Font Collection is missing the data_json_file.' ); + if ( empty( $config['data_json_file'] ) && is_string( $config['data_json_file'] ) ) { + throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); } $config['id'] = $id; From 85fe753e506bbf043b32d6cd14022fc83e0ecf21 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 23 Aug 2023 12:18:39 -0500 Subject: [PATCH 20/50] FontCollection::get_data(): Improves error handling * Rechecking if "data_json_file" exists in the $config property shouldn't be necessary, as it's checked in the constructor. * If the file does not exist, bail out immediately as there's nothing more to do. * Adds a check and WP_Error for file_get_contents(): file_get_contents() returns false on failure. If there's nothing in the file, an empty string is returned. This change checks for both of these conditions and returns a WP_Error if either happens. * Internationalizes WP_Error error message for consistency in Core. --- .../font-library/class-wp-font-collection.php | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 4ac9721ac649e..9803bad31065e 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -76,16 +76,18 @@ public function get_config() { * else an instance of WP_Error on failure. */ public function get_data() { - if ( ! empty( $this->config['data_json_file'] ) ) { - if ( file_exists( $this->config['data_json_file'] ) ) { - $data = file_get_contents( $this->config['data_json_file'] ); - $collection_data = $this->get_config(); - $collection_data['data'] = $data; - unset( $collection_data['data_json_file'] ); - return $collection_data; - } + if ( ! file_exists( $this->config['data_json_file'] ) ) { + return new WP_Error( 'font_collection_file_error', __( 'Font Collection data JSON file does not exist.', 'gutenberg' ) ); + } + + $data = file_get_contents( $this->config['data_json_file'] ); + if ( empty( $data ) ) { + return new WP_Error( 'font_collection_read_error', __( 'Error reading the Font Collection data JSON file contents.', 'gutenberg' ) ); } - return new WP_Error( 'font_collection_error', 'Font Collection data is missing the data_json_file.' ); - } + $collection_data = $this->get_config(); + $collection_data['data'] = $data; + unset( $collection_data['data_json_file'] ); + return $collection_data; + } } From 4ae9af6b9b57cda6f5790c1d1722c05009b103f7 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 23 Aug 2023 12:25:52 -0500 Subject: [PATCH 21/50] Removes empty space for wpcs --- .../fonts/font-library/class-wp-font-collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 9803bad31065e..46897f0a99a90 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -48,7 +48,7 @@ public function __construct( $id, $config ) { throw new Exception( 'Font Collection config options is required as a non-empty array.' ); } - if ( empty( $config['data_json_file'] ) && is_string( $config['data_json_file'] ) ) { + if ( empty( $config['data_json_file'] ) && is_string( $config['data_json_file'] ) ) { throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); } From de053da376ddf3f8fbce825696f3f9f57a26e9b8 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 23 Aug 2023 14:38:26 -0300 Subject: [PATCH 22/50] adding filter in a simpler way Co-authored-by: Tonya Mork --- .../fonts/font-library/font-library.php | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index a1e555940c6a1..1b4cc3bb6d5f5 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -35,18 +35,4 @@ function gutenberg_init_font_library_routes() { add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' ); -/** - * Registers the font collection filter for font library. - * - * @since 6.4.0 - */ -function gutenberg_add_register_font_collection_filter() { - add_filter( - 'wp_register_font_collection', - array( 'WP_Font_Library', 'register_font_collection' ), - 10, - 2 - ); -} - -add_action( 'init', 'gutenberg_add_register_font_collection_filter' ); +add_filter( 'wp_register_font_collection', array( 'WP_Font_Library', 'register_font_collection' ), 10, 2 ); From 21dfbe3fee8ac49688dd90934373b918c70f5ab0 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 23 Aug 2023 14:43:19 -0300 Subject: [PATCH 23/50] micro-optimization Co-authored-by: Tonya Mork --- lib/experimental/fonts/font-library/class-wp-font-library.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 86b64542ebed8..a342076ae7f26 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -46,7 +46,7 @@ class WP_Font_Library { * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. */ public static function register_font_collection( $id, $config ) { - if ( array_key_exists( $id, self::$collections ) ) { + if ( isset( self::$collections[ $id ] ) ) { return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' ); } From fb913af813ba32cc4ef0bd22e58016477aed607a Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 23 Aug 2023 15:07:54 -0300 Subject: [PATCH 24/50] reuse WP_Error response instead of creating a new one Co-authored-by: Tonya Mork --- .../font-library/class-wp-rest-font-library-controller.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 4208684fedd68..2bf1c9dd67148 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -106,11 +106,7 @@ public function get_font_collection( $request ) { $collection = WP_Font_Library::get_font_collection( $id ); if ( is_wp_error( $collection ) ) { - return new WP_Error( - 'font_collection_not_found', - __( 'Font collection not found.', 'gutenberg' ), - array( 'status' => 404 ) - ); + return $collection; } return new WP_REST_Response( $collection->get_data() ); From 0e6c026c87933f64dae78049cc9bb53c7fa8dffb Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 23 Aug 2023 13:17:24 -0500 Subject: [PATCH 25/50] Eliminates try/catch --- .../font-library/class-wp-font-collection.php | 17 +++++++++++++---- .../font-library/class-wp-font-library.php | 10 +++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 46897f0a99a90..05206086fa0e3 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -36,20 +36,29 @@ class WP_Font_Collection { * * @param string $id Font collection id. * @param array $config Font collection config options. - * @throws Exception If the required parameters are missing. + * @throws WP_Error When passed an invalid argument. */ public function __construct( $id, $config ) { if ( empty( $id ) && is_string( $id ) ) { - throw new Exception( 'Font Collection ID is required as a non-empty string.' ); + return new WP_Error( + 'font_collection_id_required', + __( 'Font Collection ID is required as a non-empty string.', 'gutenberg' ) + ); } if ( empty( $config ) ) { - throw new Exception( 'Font Collection config options is required as a non-empty array.' ); + return new WP_Error( + 'font_collection_config_required', + __( 'Font Collection config options is required as a non-empty array.', 'gutenberg' ) + ); } if ( empty( $config['data_json_file'] ) && is_string( $config['data_json_file'] ) ) { - throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); + return new WP_Error( + 'font_collection_data_json_file_required', + __( 'Font Collection config "data_json_file" option is required as a non-empty string.', 'gutenberg' ) + ); } $config['id'] = $id; diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index a342076ae7f26..ca84f8a33dbab 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -50,13 +50,13 @@ public static function register_font_collection( $id, $config ) { return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' ); } - try { - $new_collection = new WP_Font_Collection( $id, $config ); - self::$collections[ $id ] = $new_collection; + $new_collection = new WP_Font_Collection( $id, $config ); + if ( is_wp_error( $new_collection ) ) { return $new_collection; - } catch ( Exception $e ) { - return new WP_Error( 'font_collection_error', $e->getMessage() ); } + + self::$collections[ $id ] = $new_collection; + return $new_collection; } /** From f79212d86d3fd483334b4759aa0d2b1dca22d47d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 23 Aug 2023 16:19:36 -0500 Subject: [PATCH 26/50] Revert "Eliminates try/catch" commit This reverts commit 0e6c026c87933f64dae78049cc9bb53c7fa8dffb. --- .../font-library/class-wp-font-collection.php | 17 ++++------------- .../font-library/class-wp-font-library.php | 10 +++++----- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 05206086fa0e3..46897f0a99a90 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -36,29 +36,20 @@ class WP_Font_Collection { * * @param string $id Font collection id. * @param array $config Font collection config options. - * @throws WP_Error When passed an invalid argument. + * @throws Exception If the required parameters are missing. */ public function __construct( $id, $config ) { if ( empty( $id ) && is_string( $id ) ) { - return new WP_Error( - 'font_collection_id_required', - __( 'Font Collection ID is required as a non-empty string.', 'gutenberg' ) - ); + throw new Exception( 'Font Collection ID is required as a non-empty string.' ); } if ( empty( $config ) ) { - return new WP_Error( - 'font_collection_config_required', - __( 'Font Collection config options is required as a non-empty array.', 'gutenberg' ) - ); + throw new Exception( 'Font Collection config options is required as a non-empty array.' ); } if ( empty( $config['data_json_file'] ) && is_string( $config['data_json_file'] ) ) { - return new WP_Error( - 'font_collection_data_json_file_required', - __( 'Font Collection config "data_json_file" option is required as a non-empty string.', 'gutenberg' ) - ); + throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); } $config['id'] = $id; diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index ca84f8a33dbab..a342076ae7f26 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -50,13 +50,13 @@ public static function register_font_collection( $id, $config ) { return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' ); } - $new_collection = new WP_Font_Collection( $id, $config ); - if ( is_wp_error( $new_collection ) ) { + try { + $new_collection = new WP_Font_Collection( $id, $config ); + self::$collections[ $id ] = $new_collection; return $new_collection; + } catch ( Exception $e ) { + return new WP_Error( 'font_collection_error', $e->getMessage() ); } - - self::$collections[ $id ] = $new_collection; - return $new_collection; } /** From 4e952e3360a42c97b09cbd21c183827130abdfec Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 11:20:43 -0300 Subject: [PATCH 27/50] Remove wp_register_font_collection and replace it by a global function, remove try catch and raise the error if needed --- .../font-library/class-wp-font-collection.php | 14 ++++++-------- .../fonts/font-library/class-wp-font-library.php | 15 ++++++--------- .../fonts/font-library/font-library.php | 4 +++- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 46897f0a99a90..68c6960450c13 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -38,21 +38,19 @@ class WP_Font_Collection { * @param array $config Font collection config options. * @throws Exception If the required parameters are missing. */ - public function __construct( $id, $config ) { - - if ( empty( $id ) && is_string( $id ) ) { - throw new Exception( 'Font Collection ID is required as a non-empty string.' ); - } - + public function __construct( $config ) { if ( empty( $config ) ) { throw new Exception( 'Font Collection config options is required as a non-empty array.' ); } - if ( empty( $config['data_json_file'] ) && is_string( $config['data_json_file'] ) ) { + if ( empty( $config[ 'id' ] ) || !is_string( $config[ 'id' ] ) ) { + throw new Exception( 'Font Collection config ID is required as a non-empty string.' ); + } + + if ( empty( $config['data_json_file'] ) || !is_string( $config['data_json_file'] ) ) { throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); } - $config['id'] = $id; $this->config = $config; } diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index a342076ae7f26..712b589783b21 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -45,18 +45,15 @@ class WP_Font_Library { * @param array $config Font collection config options. * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. */ - public static function register_font_collection( $id, $config ) { - if ( isset( self::$collections[ $id ] ) ) { + 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.' ); } - try { - $new_collection = new WP_Font_Collection( $id, $config ); - self::$collections[ $id ] = $new_collection; - return $new_collection; - } catch ( Exception $e ) { - return new WP_Error( 'font_collection_error', $e->getMessage() ); - } + self::$collections[ $config['id'] ] = $new_collection; + return $new_collection; } /** diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index 1b4cc3bb6d5f5..6e367f36e6070 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -35,4 +35,6 @@ function gutenberg_init_font_library_routes() { add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' ); -add_filter( 'wp_register_font_collection', array( 'WP_Font_Library', 'register_font_collection' ), 10, 2 ); +function wp_register_font_collection ( $config ) { + return WP_Font_Library::register_font_collection( $config ); +} \ No newline at end of file From 196459a076d02876cf312f36fa65aed018b2da4b Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 12:33:09 -0300 Subject: [PATCH 28/50] adding function comment and guard agains re-declaration --- .../fonts/font-library/font-library.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index 6e367f36e6070..f07f7dda4e40d 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -35,6 +35,16 @@ function gutenberg_init_font_library_routes() { add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' ); -function wp_register_font_collection ( $config ) { - return WP_Font_Library::register_font_collection( $config ); -} \ No newline at end of file +/* + * Registers a new Font Collection in the Font Library. + * + * @since 6.4.0 + * + * @param array $config Font collection config options. + * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. + */ +if ( ! function_exists( 'wp_register_font_collection' ) ) { + function wp_register_font_collection( $config ) { + return WP_Font_Library::register_font_collection( $config ); + } +} From a76d943d7336a2dcfe2866a8b7117b557a5fdf30 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 12:33:19 -0300 Subject: [PATCH 29/50] php format --- .../fonts/font-library/class-wp-font-collection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 68c6960450c13..a9d7ee4d18a79 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -43,11 +43,11 @@ public function __construct( $config ) { throw new Exception( 'Font Collection config options is required as a non-empty array.' ); } - if ( empty( $config[ 'id' ] ) || !is_string( $config[ 'id' ] ) ) { + if ( empty( $config['id'] ) || ! is_string( $config['id'] ) ) { throw new Exception( 'Font Collection config ID is required as a non-empty string.' ); } - if ( empty( $config['data_json_file'] ) || !is_string( $config['data_json_file'] ) ) { + if ( empty( $config['data_json_file'] ) || ! is_string( $config['data_json_file'] ) ) { throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); } From c15b5ae5c6198d5a77473d86fef1493d2a69f920 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 13:27:37 -0300 Subject: [PATCH 30/50] fixing docblock coments --- .../font-library/class-wp-font-library.php | 2 -- .../fonts/font-library/font-library.php | 17 +++++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 712b589783b21..d6ee34bd8d27d 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -41,7 +41,6 @@ class WP_Font_Library { * * @since 6.4.0 * - * @param string $id Font collection id. * @param array $config Font collection config options. * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. */ @@ -72,7 +71,6 @@ public static function get_font_collections() { * * @since 6.4.0 * - * @param string $id Font collection id. * @return array List of font collections. */ public static function get_font_collection( $id ) { diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index f07f7dda4e40d..36908f41a378f 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -35,15 +35,16 @@ function gutenberg_init_font_library_routes() { add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' ); -/* - * Registers a new Font Collection in the Font Library. - * - * @since 6.4.0 - * - * @param array $config Font collection config options. - * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. - */ + if ( ! function_exists( 'wp_register_font_collection' ) ) { + /* + * Registers a new Font Collection in the Font Library. + * + * @since 6.4.0 + * + * @param array $config Font collection config options. + * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. + */ function wp_register_font_collection( $config ) { return WP_Font_Library::register_font_collection( $config ); } From 85261b34f5652cd55cdc7d35cd7034d0ff6e8901 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 13:28:24 -0300 Subject: [PATCH 31/50] removing param comment --- lib/experimental/fonts/font-library/class-wp-font-collection.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index a9d7ee4d18a79..a4bc56d84a1bc 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -34,7 +34,6 @@ class WP_Font_Collection { * * @since 6.4.0 * - * @param string $id Font collection id. * @param array $config Font collection config options. * @throws Exception If the required parameters are missing. */ From ad528296ee8d94ab3b9ed9ced7f3225c37b732c0 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 13:29:59 -0300 Subject: [PATCH 32/50] re-adding parameter comment removed by mistake --- lib/experimental/fonts/font-library/class-wp-font-library.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index d6ee34bd8d27d..64e1ea5fbb20b 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -71,6 +71,7 @@ public static function get_font_collections() { * * @since 6.4.0 * + * @param string $id Font collection id. * @return array List of font collections. */ public static function get_font_collection( $id ) { From 7161ba1421a57b55892a00a0f36f0ad5622d0d17 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 13:31:15 -0300 Subject: [PATCH 33/50] format php --- .../font-library/class-wp-font-collection.php | 2 +- .../fonts/font-library/class-wp-font-library.php | 2 +- .../fonts/font-library/font-library.php | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index a4bc56d84a1bc..96fcbbbcf1604 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -34,7 +34,7 @@ class WP_Font_Collection { * * @since 6.4.0 * - * @param array $config Font collection config options. + * @param array $config Font collection config options. * @throws Exception If the required parameters are missing. */ public function __construct( $config ) { diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 64e1ea5fbb20b..55c5108e731d6 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -41,7 +41,7 @@ class WP_Font_Library { * * @since 6.4.0 * - * @param array $config Font collection config options. + * @param array $config Font collection config options. * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. */ public static function register_font_collection( $config ) { diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index 36908f41a378f..d569ca32285c6 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -37,14 +37,14 @@ function gutenberg_init_font_library_routes() { if ( ! function_exists( 'wp_register_font_collection' ) ) { - /* - * Registers a new Font Collection in the Font Library. - * - * @since 6.4.0 - * - * @param array $config Font collection config options. - * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. - */ + /** + * Registers a new Font Collection in the Font Library. + * + * @since 6.4.0 + * + * @param array $config Font collection config options. + * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. + */ function wp_register_font_collection( $config ) { return WP_Font_Library::register_font_collection( $config ); } From 8402b910c175d9c8815e324eec77716162e09ffd Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 15:12:13 -0300 Subject: [PATCH 34/50] updating WP_Font_Collection __construct tests --- .../wpFontCollection/__construct-test.php | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php index f43048ba0fa01..983208e154fbb 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php @@ -16,35 +16,31 @@ public function test_should_initialize_data() { $property = new ReflectionProperty( WP_Font_Collection::class, 'config' ); $property->setAccessible( true ); - $id = 'my-collection'; $config = array( + 'id' => 'my-collection', 'name' => 'My Collection', 'description' => 'My collection description', 'data_json_file' => 'my-collection-data.json', ); - $font_collection = new WP_Font_Collection( $id, $config ); + $font_collection = new WP_Font_Collection( $config ); $actual = $property->getValue( $font_collection ); $property->setAccessible( false ); - $expected = $config; - $expected['id'] = $id; - - $this->assertSame( $expected, $actual ); + $this->assertSame( $config, $actual ); } /** * @dataProvider data_should_throw_exception * - * @param mixed $id Id of the font collection. * @param mixed $config Config of the font collection. * @param string $expected_exception_message Expected exception message. */ - public function test_should_throw_exception( $id, $config, $expected_exception_message ) { + public function test_should_throw_exception( $config, $expected_exception_message ) { $this->expectException( 'Exception' ); $this->expectExceptionMessage( $expected_exception_message ); - new WP_Font_Collection( $id, $config ); + new WP_Font_Collection( $config ); } /** @@ -55,46 +51,41 @@ public function test_should_throw_exception( $id, $config, $expected_exception_m public function data_should_throw_exception() { return array( 'no id' => array( - '', array( 'name' => 'My Collection', 'description' => 'My collection description', 'data_json_file' => 'my-collection-data.json', ), - 'Font Collection is missing the id.', + 'Font Collection config ID is required as a non-empty string.', ), 'no config' => array( - 'my-collection', '', - 'Font Collection is missing the config.', + 'Font Collection config options is required as a non-empty array.', ), 'empty array' => array( - 'my-collection', array(), - 'Font Collection is missing the config.', + 'Font Collection config options is required as a non-empty array.', ), 'boolean instead of config array' => array( - 'my-collection', false, - 'Font Collection is missing the config.', + 'Font Collection config options is required as a non-empty array.', ), 'null instead of config array' => array( - 'my-collection', null, - 'Font Collection is missing the config.', + 'Font Collection config options is required as a non-empty array.', ), 'missing data_json_file' => array( - 'my-collection', array( + 'id' => 'my-collection', 'name' => 'My Collection', 'description' => 'My collection description', ), - 'Font Collection is missing the data_json_file.', + 'Font Collection config "data_json_file" option is required as a non-empty string.', ), ); From e0947c991f684df031c13394971471811508aa00 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 15:14:28 -0300 Subject: [PATCH 35/50] updating WP_Font_Collection get_data tests --- .../fonts/font-library/wpFontCollection/getData-test.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php index 1b2b415ee892d..63452ef46b58f 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php @@ -15,12 +15,11 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase { /** * @dataProvider data_should_get_data * - * @param string $id Font collection id. * @param array $config Font collection config options. * @param array $expected_data Expected data. */ - public function test_should_get_data( $id, $config, $expected_data ) { - $collection = new WP_Font_Collection( $id, $config ); + public function test_should_get_data( $config, $expected_data ) { + $collection = new WP_Font_Collection( $config ); $this->assertSame( $expected_data, $collection->get_data() ); } @@ -35,16 +34,16 @@ public function data_should_get_data() { return array( 'with a data_json_file' => array( - 'id' => 'my-collection', 'config' => array( + 'id' => 'my-collection', 'name' => 'My Collection', 'description' => 'My collection description', 'data_json_file' => $mock_file, ), 'expected_data' => array( + 'id' => 'my-collection', 'name' => 'My Collection', 'description' => 'My collection description', - 'id' => 'my-collection', 'data' => '{"this is mock data":true}', ), ), From 98d52adaee94ead1e0f1d2f7d965ed600588e36d Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 15:52:56 -0300 Subject: [PATCH 36/50] updating tests --- .../font-library/class-wp-font-collection.php | 4 +++ .../wpFontCollection/__construct-test.php | 1 - .../wpFontLibrary/getFontCollection-test.php | 3 +- .../wpFontLibrary/getFontCollections-test.php | 6 ++-- .../registerFontCollection-test.php | 36 +++++++++++-------- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 96fcbbbcf1604..06948a01e65fa 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -46,6 +46,10 @@ public function __construct( $config ) { throw new Exception( 'Font Collection config ID is required as a non-empty string.' ); } + if ( empty( $config['name'] ) || ! is_string( $config['name'] ) ) { + throw new Exception( 'Font Collection config name is required as a non-empty string.' ); + } + if ( empty( $config['data_json_file'] ) || ! is_string( $config['data_json_file'] ) ) { throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); } diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php index 983208e154fbb..7d021bee02a9a 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php @@ -39,7 +39,6 @@ public function test_should_initialize_data() { public function test_should_throw_exception( $config, $expected_exception_message ) { $this->expectException( 'Exception' ); $this->expectExceptionMessage( $expected_exception_message ); - new WP_Font_Collection( $config ); } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection-test.php index 767fc55f62cd8..1d4573cbe060c 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection-test.php @@ -14,12 +14,13 @@ class Tests_Fonts_WpFontLibrary_GetFontCollection extends WP_UnitTestCase { public static function set_up_before_class() { $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.', 'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ), ); - apply_filters( 'wp_register_font_collection', 'my-font-collection', $my_font_collection_config ); + wp_register_font_collection( $my_font_collection_config ); } public function test_should_get_font_collection() { diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections-test.php index efdc7517c635b..1cf8029760dc1 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections-test.php @@ -14,20 +14,22 @@ class Tests_Fonts_WpFontLibrary_GetFontCollections extends WP_UnitTestCase { public static function set_up_before_class() { $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.', 'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ), ); - apply_filters( 'wp_register_font_collection', 'my-font-collection', $my_font_collection_config ); + wp_register_font_collection( $my_font_collection_config ); $another_font_collection_config = array( + 'id' => 'another-font-collection', 'name' => 'Another Font Collection', 'description' => 'Demo about how to a font collection to your WordPress Font Library.', 'data_json_file' => path_join( __DIR__, 'another-font-collection-data.json' ), ); - apply_filters( 'wp_register_font_collection', 'another-font-collection', $another_font_collection_config ); + wp_register_font_collection( $another_font_collection_config ); } public function test_should_get_font_collections() { diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php index 364e0ca044e9e..b074cb33cfad6 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php @@ -13,13 +13,13 @@ class Tests_Fonts_WpFontLibrary_RegisterFontCollection extends WP_UnitTestCase { public function test_should_register_font_collection() { - $id = 'my-collection'; $config = array( + 'id' => 'my-collection', 'name' => 'My Collection', 'description' => 'My Collection Description', 'data_json_file' => 'my-collection-data.json', ); - $collection = WP_Font_Library::register_font_collection( $id, $config ); + $collection = WP_Font_Library::register_font_collection( $config ); $this->assertInstanceOf( 'WP_Font_Collection', $collection ); } @@ -29,41 +29,49 @@ public function test_should_return_error_if_id_is_missing() { 'description' => 'My Collection Description', 'data_json_file' => 'my-collection-data.json', ); - $collection = WP_Font_Library::register_font_collection( '', $config ); - $this->assertInstanceOf( 'WP_Error', $collection ); + $this->expectException( 'Exception' ); + $this->expectExceptionMessage( 'Font Collection config ID is required as a non-empty string.' ); + $collection = WP_Font_Library::register_font_collection( $config ); } - public function test_should_return_error_if_config_is_missing() { - $id = 'my-other-collection'; - $collection = WP_Font_Library::register_font_collection( $id, '' ); - $this->assertInstanceOf( 'WP_Error', $collection ); + public function test_should_return_error_if_name_is_missing() { + $config = array( + 'id' => 'my-collection', + 'description' => 'My Collection Description', + 'data_json_file' => 'my-collection-data.json', + ); + $this->expectException( 'Exception' ); + $this->expectExceptionMessage( 'Font Collection config name is required as a non-empty string.' ); + $collection = WP_Font_Library::register_font_collection( $config ); } public function test_should_return_error_if_config_is_empty() { - $id = 'my-other-collection'; - $collection = WP_Font_Library::register_font_collection( $id, array() ); - $this->assertInstanceOf( 'WP_Error', $collection ); + $config = array(); + $this->expectException( 'Exception' ); + $this->expectExceptionMessage( 'Font Collection config options is required as a non-empty array.' ); + $collection = WP_Font_Library::register_font_collection( $config ); } public function test_should_return_error_if_id_is_repeated() { - $id1 = 'my-collection-1'; $config1 = array( + 'id' => 'my-collection-1', 'name' => 'My Collection 1', 'description' => 'My Collection 1 Description', 'data_json_file' => 'my-collection-1-data.json', ); $config2 = array( + 'id' => 'my-collection-1', 'name' => 'My Collection 2', 'description' => 'My Collection 2 Description', 'data_json_file' => 'my-collection-2-data.json', ); // Register first collection. - $collection1 = WP_Font_Library::register_font_collection( $id1, $config1 ); + $collection1 = WP_Font_Library::register_font_collection( $config1 ); $this->assertInstanceOf( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); // Try to register a second collection with same id. - $collection2 = WP_Font_Library::register_font_collection( $id1, $config2 ); + $collection2 = WP_Font_Library::register_font_collection( $config2 ); $this->assertInstanceOf( 'WP_Error', $collection2, 'Second collection with the same id should fail.' ); } } From a6ed14567ad002dafadbcacd3bb3192281ec56d3 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 15:55:04 -0300 Subject: [PATCH 37/50] php format --- .../font-library/wpFontCollection/__construct-test.php | 2 +- .../fonts/font-library/wpFontCollection/getData-test.php | 4 ++-- .../wpFontLibrary/registerFontCollection-test.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php index 7d021bee02a9a..0682425dd6282 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php @@ -80,7 +80,7 @@ public function data_should_throw_exception() { 'missing data_json_file' => array( array( - 'id' => 'my-collection', + 'id' => 'my-collection', 'name' => 'My Collection', 'description' => 'My collection description', ), diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php index 63452ef46b58f..55d12ac1c42fd 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php @@ -15,8 +15,8 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase { /** * @dataProvider data_should_get_data * - * @param array $config Font collection config options. - * @param array $expected_data Expected data. + * @param array $config Font collection config options. + * @param array $expected_data Expected data. */ public function test_should_get_data( $config, $expected_data ) { $collection = new WP_Font_Collection( $config ); diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php index b074cb33cfad6..2864be0aa5eff 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php @@ -24,7 +24,7 @@ public function test_should_register_font_collection() { } public function test_should_return_error_if_id_is_missing() { - $config = array( + $config = array( 'name' => 'My Collection', 'description' => 'My Collection Description', 'data_json_file' => 'my-collection-data.json', @@ -35,8 +35,8 @@ public function test_should_return_error_if_id_is_missing() { } public function test_should_return_error_if_name_is_missing() { - $config = array( - 'id' => 'my-collection', + $config = array( + 'id' => 'my-collection', 'description' => 'My Collection Description', 'data_json_file' => 'my-collection-data.json', ); From f8dc1a01d13287371e9ab7b98d6f4027dbeb5e54 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 15:56:06 -0300 Subject: [PATCH 38/50] array check Co-authored-by: Tonya Mork --- .../fonts/font-library/class-wp-font-collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 06948a01e65fa..407e34a5f327f 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -38,7 +38,7 @@ class WP_Font_Collection { * @throws Exception If the required parameters are missing. */ public function __construct( $config ) { - if ( empty( $config ) ) { + if ( empty( $config ) || ! is_array( $config ) ) { throw new Exception( 'Font Collection config options is required as a non-empty array.' ); } From 4cb5c770e715d3782716d67a55a7354935d1c20d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 24 Aug 2023 13:57:46 -0500 Subject: [PATCH 39/50] Documents config array structure --- .../fonts/font-library/class-wp-font-collection.php | 1 + .../fonts/font-library/class-wp-font-library.php | 3 ++- lib/experimental/fonts/font-library/font-library.php | 10 ++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 407e34a5f327f..1620467d36dfa 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -35,6 +35,7 @@ class WP_Font_Collection { * @since 6.4.0 * * @param array $config Font collection config options. + * See {@see wp_register_font_collection()} for the supported fields. * @throws Exception If the required parameters are missing. */ public function __construct( $config ) { diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 55c5108e731d6..193ee3b2e5b7d 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -42,7 +42,8 @@ class WP_Font_Library { * @since 6.4.0 * * @param array $config Font collection config options. - * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. + * See {@see wp_register_font_collection()} for the supported fields. + * @return WP_Font_Collection|WP_Error A font collection is it was registered successfully and a WP_Error otherwise. */ public static function register_font_collection( $config ) { $new_collection = new WP_Font_Collection( $config ); diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index d569ca32285c6..0df008a275094 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -42,8 +42,14 @@ function gutenberg_init_font_library_routes() { * * @since 6.4.0 * - * @param array $config Font collection config options. - * @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise. + * @param string[] $config { + * Font collection associative array of configuration options. + * + * @type string $id The font collection's unique ID. + * @type string $data_json_file The font collection's data JSON file. + * } + * @return WP_Font_Collection|WP_Error A font collection is it was registered + * successfully, else WP_Error. */ function wp_register_font_collection( $config ) { return WP_Font_Library::register_font_collection( $config ); From 4b7e5e3573f933db35b484a4397242f522f8d7cb Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 16:36:43 -0300 Subject: [PATCH 40/50] adding tests for /fonts/collections endpoint --- .../class-wp-rest-font-library-controller.php | 3 ++- .../class-wp-rest-font-library-controller.php | 24 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 2bf1c9dd67148..8cb977ca7e35d 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -122,7 +122,8 @@ public function get_font_collection( $request ) { public function get_font_collections() { $collections = array(); foreach ( WP_Font_Library::get_font_collections() as $collection ) { - $collections[] = $collection->get_config(); + $config = $collection->get_config(); + $collections[ $config['id' ] ] = $config; } return new WP_REST_Response( $collections ); diff --git a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php index a5d90a31028d4..5efa759cbfd3e 100644 --- a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php @@ -44,6 +44,29 @@ public function test_register_routes() { $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' ); } + /** + * @covers ::get_font_collections + */ + public function test_get_font_collections() { + $config = array ( + 'id' => 'my-font-collection', + 'name' => 'My Font Collection', + 'description' => 'Demo about how to a font collection to your WordPress Font Library.', + 'data_json_file' => 'my-font-collection-data.json', + ); + wp_register_font_collection ( $config ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + echo ( print_r( $data, true ) ); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + $this->assertCount( 1, $data, 'The response data is not an array with one element.' ); + $this->assertArrayHasKey( 'my-font-collection', $data, 'The response data does not have the key of the registered font collection id.' ); + } + /** * @covers ::uninstall_fonts */ @@ -68,7 +91,6 @@ public function test_uninstall_non_existing_fonts() { $this->assertSame( 500, $response->get_status(), 'The response status is not 500.' ); } - /** * @covers ::install_fonts * @covers ::uninstall_fonts From 56607c9641b25ba8dbf300bc1ff9f7c3d024e17c Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 16:43:08 -0300 Subject: [PATCH 41/50] adding /fonts/collections/ endpoint test --- .../class-wp-rest-font-library-controller.php | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php index 5efa759cbfd3e..ea8ced163c17e 100644 --- a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php @@ -21,11 +21,25 @@ class WP_REST_Font_Library_Controller_Test extends WP_UnitTestCase { * @param WP_UnitTest_Factory $factory Helper that lets us create fake data. */ public static function wpSetupBeforeClass( $factory ) { + // Create a user with administrator role. self::$admin_id = $factory->user->create( array( 'role' => 'administrator', ) ); + + // Mock font collection data file. + $mock_file = wp_tempnam( 'my-collection-data-' ); + file_put_contents( $mock_file, '{"this is mock data":true}' ); + + // Add a 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.', + 'data_json_file' => $mock_file, + ); + wp_register_font_collection ( $config ); } /** @@ -48,25 +62,27 @@ public function test_register_routes() { * @covers ::get_font_collections */ public function test_get_font_collections() { - $config = array ( - 'id' => 'my-font-collection', - 'name' => 'My Font Collection', - 'description' => 'Demo about how to a font collection to your WordPress Font Library.', - 'data_json_file' => 'my-font-collection-data.json', - ); - wp_register_font_collection ( $config ); - wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - - echo ( print_r( $data, true ) ); $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); $this->assertCount( 1, $data, 'The response data is not an array with one element.' ); $this->assertArrayHasKey( 'my-font-collection', $data, 'The response data does not have the key of the registered font collection id.' ); } + /** + * @covers ::get_font_collection + */ + public function test_get_font_collection() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/my-font-collection' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + $this->assertArrayHasKey( 'data', $data, 'The response data does not have the key with the file data.' ); + } + /** * @covers ::uninstall_fonts */ From c928ebe1459abe60ef777edb87a20a0638c47578 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 16:46:02 -0300 Subject: [PATCH 42/50] format --- .../font-library/class-wp-rest-font-library-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 8cb977ca7e35d..2763beca9dd6b 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -122,8 +122,8 @@ public function get_font_collection( $request ) { public function get_font_collections() { $collections = array(); foreach ( WP_Font_Library::get_font_collections() as $collection ) { - $config = $collection->get_config(); - $collections[ $config['id' ] ] = $config; + $config = $collection->get_config(); + $collections[ $config['id'] ] = $config; } return new WP_REST_Response( $collections ); From aeb42cfe804a2ff843a658b6be2c235a4f14a4a1 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 16:46:56 -0300 Subject: [PATCH 43/50] format --- .../class-wp-rest-font-library-controller.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php index ea8ced163c17e..235a027f8e99a 100644 --- a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php @@ -33,13 +33,13 @@ public static function wpSetupBeforeClass( $factory ) { file_put_contents( $mock_file, '{"this is mock data":true}' ); // Add a font collection. - $config = array ( + $config = array( 'id' => 'my-font-collection', 'name' => 'My Font Collection', 'description' => 'Demo about how to a font collection to your WordPress Font Library.', 'data_json_file' => $mock_file, ); - wp_register_font_collection ( $config ); + wp_register_font_collection( $config ); } /** @@ -63,9 +63,9 @@ public function test_register_routes() { */ public function test_get_font_collections() { wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections' ); $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); + $data = $response->get_data(); $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); $this->assertCount( 1, $data, 'The response data is not an array with one element.' ); $this->assertArrayHasKey( 'my-font-collection', $data, 'The response data does not have the key of the registered font collection id.' ); @@ -76,9 +76,9 @@ public function test_get_font_collections() { */ public function test_get_font_collection() { wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/my-font-collection' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/my-font-collection' ); $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); + $data = $response->get_data(); $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); $this->assertArrayHasKey( 'data', $data, 'The response data does not have the key with the file data.' ); } From 0a98de65adc0591a319693eae473c62447ba21b2 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 16:51:51 -0300 Subject: [PATCH 44/50] add test for missing collection --- .../class-wp-rest-font-library-controller.php | 1 + .../class-wp-rest-font-library-controller.php | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 2763beca9dd6b..576f7fbddec68 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -106,6 +106,7 @@ public function get_font_collection( $request ) { $collection = WP_Font_Library::get_font_collection( $id ); if ( is_wp_error( $collection ) ) { + $collection->add_data( array( 'status' => 404 ) ); return $collection; } diff --git a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php index 235a027f8e99a..8766d15c44bd6 100644 --- a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php @@ -83,6 +83,16 @@ public function test_get_font_collection() { $this->assertArrayHasKey( 'data', $data, 'The response data does not have the key with the file data.' ); } + /** + * @covers ::get_font_collection + */ + public function test_get_non_existing_font_collection() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/non-existing-collection-id' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertSame( 404, $response->get_status() ); + } + /** * @covers ::uninstall_fonts */ From 46f9fde0e32282c972f7cf40f8ed4eb8ce151642 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 24 Aug 2023 16:56:13 -0300 Subject: [PATCH 45/50] removing not needed variables --- .../wpFontLibrary/registerFontCollection-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php index 2864be0aa5eff..ccef1ca49568e 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php @@ -31,7 +31,7 @@ public function test_should_return_error_if_id_is_missing() { ); $this->expectException( 'Exception' ); $this->expectExceptionMessage( 'Font Collection config ID is required as a non-empty string.' ); - $collection = WP_Font_Library::register_font_collection( $config ); + WP_Font_Library::register_font_collection( $config ); } public function test_should_return_error_if_name_is_missing() { @@ -42,14 +42,14 @@ public function test_should_return_error_if_name_is_missing() { ); $this->expectException( 'Exception' ); $this->expectExceptionMessage( 'Font Collection config name is required as a non-empty string.' ); - $collection = WP_Font_Library::register_font_collection( $config ); + WP_Font_Library::register_font_collection( $config ); } public function test_should_return_error_if_config_is_empty() { $config = array(); $this->expectException( 'Exception' ); $this->expectExceptionMessage( 'Font Collection config options is required as a non-empty array.' ); - $collection = WP_Font_Library::register_font_collection( $config ); + WP_Font_Library::register_font_collection( $config ); } public function test_should_return_error_if_id_is_repeated() { From 785262b0c0cf267aa00525f7dec9d2e3dcd12e99 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 25 Aug 2023 00:28:19 -0300 Subject: [PATCH 46/50] Add more tests and split the test for WP_REST_Font_Library_Controller --- .../class-wp-rest-font-library-controller.php | 5 +- .../wpRestFontLibraryController/base.php | 35 +++++ .../getFontCollection.php | 49 ++++++ .../getFontCollections.php | 47 ++++++ .../installFonts.php} | 141 ++---------------- .../registerRoutes.php | 30 ++++ .../uninstallFonts.php | 101 +++++++++++++ 7 files changed, 278 insertions(+), 130 deletions(-) create mode 100644 phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php create mode 100644 phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php create mode 100644 phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php rename phpunit/tests/fonts/font-library/{class-wp-rest-font-library-controller.php => wpRestFontLibraryController/installFonts.php} (70%) create mode 100644 phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php create mode 100644 phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php index 576f7fbddec68..3096f11759691 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php @@ -123,11 +123,10 @@ public function get_font_collection( $request ) { public function get_font_collections() { $collections = array(); foreach ( WP_Font_Library::get_font_collections() as $collection ) { - $config = $collection->get_config(); - $collections[ $config['id'] ] = $config; + $collections[] = $collection->get_config(); } - return new WP_REST_Response( $collections ); + return new WP_REST_Response( $collections, 200 ); } /** diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php new file mode 100644 index 0000000000000..908440ac60670 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php @@ -0,0 +1,35 @@ +factory->user->create( + array( + 'role' => 'administrator', + ) + ); + wp_set_current_user( $admin_id ); + } + + /** + * Tear down each test method. + */ + public function tear_down() { + parent::tear_down(); + + // Reset $collections static property of WP_Font_Library class. + $reflection = new ReflectionClass( 'WP_Font_Library' ); + $property = $reflection->getProperty( 'collections' ); + $property->setAccessible( true ); + $property->setValue( array() ); + } + +} diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php new file mode 100644 index 0000000000000..c042432ea6158 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php @@ -0,0 +1,49 @@ + 'one-collection', + 'name' => 'One Font Collection', + 'description' => 'Demo about how to a font collection to your WordPress Font Library.', + 'data_json_file' => $mock_file, + ); + $hola = wp_register_font_collection( $config ); + } + + public function test_get_font_collection() { + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/one-collection' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + $this->assertArrayHasKey( 'data', $data, 'The response data does not have the key with the file data.' ); + } + + public function test_get_non_existing_collection_should_return_404() { + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/non-existing-collection-id' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertSame( 404, $response->get_status() ); + } + +} + diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php new file mode 100644 index 0000000000000..0050987099166 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php @@ -0,0 +1,47 @@ +dispatch( $request ); + $this->assertSame( 200, $response->get_status() ); + $this->assertSame( array(), $response->get_data() ); + } + + public function test_get_font_collections() { + // Mock font collection data file. + $mock_file = wp_tempnam( 'my-collection-data-' ); + file_put_contents( $mock_file, '{"this is mock data":true}' ); + + // Add a 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.', + 'data_json_file' => $mock_file, + ); + wp_register_font_collection( $config ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + $this->assertCount( 1, $data, 'The response data is not an array with one element.' ); + $this->assertArrayHasKey( 'id', $data[0], 'The response data does not have the key with the collection ID.' ); + $this->assertArrayHasKey( 'name', $data[0], 'The response data does not have the key with the collection name.' ); + } + +} + diff --git a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php similarity index 70% rename from phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php rename to phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index 8766d15c44bd6..a2b967c380e4f 100644 --- a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -1,134 +1,27 @@ user->create( - array( - 'role' => 'administrator', - ) - ); - - // Mock font collection data file. - $mock_file = wp_tempnam( 'my-collection-data-' ); - file_put_contents( $mock_file, '{"this is mock data":true}' ); - - // Add a 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.', - 'data_json_file' => $mock_file, - ); - wp_register_font_collection( $config ); - } - - /** - * @covers ::register_routes - */ - public function test_register_routes() { - $routes = rest_get_server()->get_routes(); - $this->assertArrayHasKey( '/wp/v2/fonts', $routes, 'Rest server has not the fonts path intialized.' ); - $this->assertCount( 2, $routes['/wp/v2/fonts'], 'Rest server has not the 2 fonts paths initialized.' ); - $this->assertCount( 1, $routes['/wp/v2/fonts/collections'], 'Rest server has not the collections path initialized.' ); - $this->assertCount( 1, $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'], 'Rest server has not the collection path initialized.' ); - - $this->assertArrayHasKey( 'POST', $routes['/wp/v2/fonts'][0]['methods'], 'Rest server has not the POST method for fonts intialized.' ); - $this->assertArrayHasKey( 'DELETE', $routes['/wp/v2/fonts'][1]['methods'], 'Rest server has not the DELETE method for fonts intialized.' ); - $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections'][0]['methods'], 'Rest server has not the GET method for collections intialized.' ); - $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' ); - } - - /** - * @covers ::get_font_collections - */ - public function test_get_font_collections() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); - $this->assertCount( 1, $data, 'The response data is not an array with one element.' ); - $this->assertArrayHasKey( 'my-font-collection', $data, 'The response data does not have the key of the registered font collection id.' ); - } - - /** - * @covers ::get_font_collection - */ - public function test_get_font_collection() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/my-font-collection' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); - $this->assertArrayHasKey( 'data', $data, 'The response data does not have the key with the file data.' ); - } +class Tests_Fonts_WPRESTFontLibraryController_InstallFonts extends WP_REST_Font_Library_Controller_UnitTestCase { /** - * @covers ::get_font_collection - */ - public function test_get_non_existing_font_collection() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/fonts/collections/non-existing-collection-id' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertSame( 404, $response->get_status() ); - } - - /** - * @covers ::uninstall_fonts - */ - public function test_uninstall_non_existing_fonts() { - wp_set_current_user( self::$admin_id ); - $uninstall_request = new WP_REST_Request( 'DELETE', '/wp/v2/fonts' ); - - $non_existing_font_data = array( - array( - 'slug' => 'non-existing-font', - 'name' => 'Non existing font', - ), - array( - 'slug' => 'another-not-installed-font', - 'name' => 'Another not installed font', - ), - ); - - $uninstall_request->set_param( 'fontFamilies', $non_existing_font_data ); - $response = rest_get_server()->dispatch( $uninstall_request ); - $response->get_data(); - $this->assertSame( 500, $response->get_status(), 'The response status is not 500.' ); - } - - /** - * @covers ::install_fonts - * @covers ::uninstall_fonts * - * @dataProvider data_install_and_uninstall_fonts + * @dataProvider data_install_fonts * * @param array $font_families Font families to install in theme.json format. * @param array $files Font files to install. * @param array $expected_response Expected response data. */ - public function test_install_and_uninstall_fonts( $font_families, $files, $expected_response ) { - wp_set_current_user( self::$admin_id ); + public function test_install_fonts( $font_families, $files, $expected_response ) { $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); $font_families_json = json_encode( $font_families ); $install_request->set_param( 'fontFamilies', $font_families_json ); @@ -158,16 +51,12 @@ public function test_install_and_uninstall_fonts( $font_families, $files, $expec $this->assertEquals( $expected_font, $installed_font, 'The endpoint answer is not as expected.' ); } - $uninstall_request = new WP_REST_Request( 'DELETE', '/wp/v2/fonts' ); - $uninstall_request->set_param( 'fontFamilies', $font_families ); - $response = rest_get_server()->dispatch( $uninstall_request ); - $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); } /** - * Data provider for test_install_and_uninstall_fonts + * Data provider for test_install_fonts */ - public function data_install_and_uninstall_fonts() { + public function data_install_fonts() { $temp_file_path1 = wp_tempnam( 'Piazzola1-' ); file_put_contents( $temp_file_path1, 'Mocking file content' ); @@ -397,16 +286,12 @@ public function data_install_and_uninstall_fonts() { /** * Tests failure when fonfaces has improper inputs * - * @covers ::install_fonts - * * @dataProvider data_install_with_improper_inputs * * @param array $font_families Font families to install in theme.json format. * @param array $files Font files to install. */ public function test_install_with_improper_inputs( $font_families, $files = array() ) { - wp_set_current_user( self::$admin_id ); - $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); $font_families_json = json_encode( $font_families ); $install_request->set_param( 'fontFamilies', $font_families_json ); @@ -530,4 +415,6 @@ public function data_install_with_improper_inputs() { ), ); } + } + diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php new file mode 100644 index 0000000000000..56d382d15d71e --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php @@ -0,0 +1,30 @@ +get_routes(); + $this->assertArrayHasKey( '/wp/v2/fonts', $routes, 'Rest server has not the fonts path intialized.' ); + $this->assertCount( 2, $routes['/wp/v2/fonts'], 'Rest server has not the 2 fonts paths initialized.' ); + $this->assertCount( 1, $routes['/wp/v2/fonts/collections'], 'Rest server has not the collections path initialized.' ); + $this->assertCount( 1, $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'], 'Rest server has not the collection path initialized.' ); + + $this->assertArrayHasKey( 'POST', $routes['/wp/v2/fonts'][0]['methods'], 'Rest server has not the POST method for fonts intialized.' ); + $this->assertArrayHasKey( 'DELETE', $routes['/wp/v2/fonts'][1]['methods'], 'Rest server has not the DELETE method for fonts intialized.' ); + $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections'][0]['methods'], 'Rest server has not the GET method for collections intialized.' ); + $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' ); + } + +} + diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php new file mode 100644 index 0000000000000..c4ef8b95ab2f8 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php @@ -0,0 +1,101 @@ + 'Piazzolla', + 'slug' => 'piazzolla', + 'name' => 'Piazzolla', + 'fontFace' => array( + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + 'src' => 'http://fonts.gstatic.com/s/piazzolla/v33/N0b72SlTPu5rIkWIZjVgI-TckS03oGpPETyEJ88Rbvi0_TzOzKcQhZqx3gX9BRy5m5M.ttf', + 'downloadFromUrl' => 'http://fonts.gstatic.com/s/piazzolla/v33/N0b72SlTPu5rIkWIZjVgI-TckS03oGpPETyEJ88Rbvi0_TzOzKcQhZqx3gX9BRy5m5M.ttf', + ), + ), + ), + array( + 'fontFamily' => 'Montserrat', + 'slug' => 'montserrat', + 'name' => 'Montserrat', + 'fontFace' => array( + array( + 'fontFamily' => 'Montserrat', + 'fontStyle' => 'normal', + 'fontWeight' => '100', + 'src' => 'http://fonts.gstatic.com/s/montserrat/v25/JTUHjIg1_i6t8kCHKm4532VJOt5-QNFgpCtr6Uw-Y3tcoqK5.ttf', + 'downloadFromUrl' => 'http://fonts.gstatic.com/s/montserrat/v25/JTUHjIg1_i6t8kCHKm4532VJOt5-QNFgpCtr6Uw-Y3tcoqK5.ttf', + ), + ), + ), + ); + + $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); + $font_families_json = json_encode( $mock_families ); + $install_request->set_param( 'fontFamilies', $font_families_json ); + $response = rest_get_server()->dispatch( $install_request ); + } + + public function test_uninstall() { + $font_families_to_uninstall = array( + array( + 'slug' => 'piazzolla', + ), + array( + 'slug' => 'montserrat', + ), + ); + + $uninstall_request = new WP_REST_Request( 'DELETE', '/wp/v2/fonts' ); + $uninstall_request->set_param( 'fontFamilies', $font_families_to_uninstall ); + $response = rest_get_server()->dispatch( $uninstall_request ); + echo ( print_r( $response->get_data(), true ) ); + $this->assertSame( 200, $response->get_status(), 'The response status is not 200.' ); + + } + + + public function test_uninstall_non_existing_fonts() { + $uninstall_request = new WP_REST_Request( 'DELETE', '/wp/v2/fonts' ); + + $non_existing_font_data = array( + array( + 'slug' => 'non-existing-font', + 'name' => 'Non existing font', + ), + array( + 'slug' => 'another-not-installed-font', + 'name' => 'Another not installed font', + ), + ); + + $uninstall_request->set_param( 'fontFamilies', $non_existing_font_data ); + $response = rest_get_server()->dispatch( $uninstall_request ); + $response->get_data(); + $this->assertSame( 500, $response->get_status(), 'The response status is not 500.' ); + } + +} + + From de12fe7943eceaa720e3372a93dcef5c66e58442 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 25 Aug 2023 00:34:08 -0300 Subject: [PATCH 47/50] lint --- .../wpRestFontLibraryController/getFontCollection.php | 2 +- .../font-library/wpRestFontLibraryController/uninstallFonts.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php index c042432ea6158..fa9f491c20d47 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php @@ -28,7 +28,7 @@ public static function wpSetupBeforeClass() { 'description' => 'Demo about how to a font collection to your WordPress Font Library.', 'data_json_file' => $mock_file, ); - $hola = wp_register_font_collection( $config ); + wp_register_font_collection( $config ); } public function test_get_font_collection() { diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php index c4ef8b95ab2f8..b89e488d25741 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php @@ -54,7 +54,7 @@ public function set_up() { $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); $font_families_json = json_encode( $mock_families ); $install_request->set_param( 'fontFamilies', $font_families_json ); - $response = rest_get_server()->dispatch( $install_request ); + rest_get_server()->dispatch( $install_request ); } public function test_uninstall() { From dc560b8284746b3715d71b303b9d46e8113b7762 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 25 Aug 2023 16:37:45 -0300 Subject: [PATCH 48/50] try to create dir for tests in CI --- phpunit/tests/fonts/font-library/wpFontFamily/base.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/base.php b/phpunit/tests/fonts/font-library/wpFontFamily/base.php index 9253885c6b3b2..a630954bb61bf 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamily/base.php +++ b/phpunit/tests/fonts/font-library/wpFontFamily/base.php @@ -30,6 +30,7 @@ public static function set_up_before_class() { $uploads_dir = wp_upload_dir(); static::$fonts_dir = $uploads_dir['basedir'] . '/fonts/'; + wp_mkdir_p( static::$fonts_dir ); } public function set_up() { From 803a1d19c223cc5edcffd9ed3021a3514c60824e Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 31 Aug 2023 13:24:07 -0500 Subject: [PATCH 49/50] Tests: Core prep * Renamed files to remove `-test` suffix (no longer needed). * Removed extra empty line before closing the class. * Used Core's `assertWPError()`. --- .../{__construct-test.php => __construct.php} | 0 .../wpFontCollection/{getData-test.php => getData.php} | 0 .../{getFontCollection-test.php => getFontCollection.php} | 0 .../{getFontCollections-test.php => getFontCollections.php} | 1 - ...sterFontCollection-test.php => registerFontCollection.php} | 4 ++-- .../fonts/font-library/wpRestFontLibraryController/base.php | 1 - .../wpRestFontLibraryController/getFontCollection.php | 1 - .../wpRestFontLibraryController/getFontCollections.php | 1 - .../font-library/wpRestFontLibraryController/installFonts.php | 1 - .../wpRestFontLibraryController/registerRoutes.php | 1 - .../wpRestFontLibraryController/uninstallFonts.php | 1 - 11 files changed, 2 insertions(+), 9 deletions(-) rename phpunit/tests/fonts/font-library/wpFontCollection/{__construct-test.php => __construct.php} (100%) rename phpunit/tests/fonts/font-library/wpFontCollection/{getData-test.php => getData.php} (100%) rename phpunit/tests/fonts/font-library/wpFontLibrary/{getFontCollection-test.php => getFontCollection.php} (100%) rename phpunit/tests/fonts/font-library/wpFontLibrary/{getFontCollections-test.php => getFontCollections.php} (99%) rename phpunit/tests/fonts/font-library/wpFontLibrary/{registerFontCollection-test.php => registerFontCollection.php} (92%) diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php similarity index 100% rename from phpunit/tests/fonts/font-library/wpFontCollection/__construct-test.php rename to phpunit/tests/fonts/font-library/wpFontCollection/__construct.php diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php b/phpunit/tests/fonts/font-library/wpFontCollection/getData.php similarity index 100% rename from phpunit/tests/fonts/font-library/wpFontCollection/getData-test.php rename to phpunit/tests/fonts/font-library/wpFontCollection/getData.php diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php similarity index 100% rename from phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection-test.php rename to phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php similarity index 99% rename from phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections-test.php rename to phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php index 1cf8029760dc1..4bda590d6e8fd 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php @@ -42,5 +42,4 @@ public function test_should_get_font_collections() { $this->assertArrayHasKey( 'another-font-collection', $font_collections, 'The array should have the key of the registered font collection id.' ); $this->assertInstanceOf( 'WP_Font_Collection', $font_collections['another-font-collection'], 'The value of the array $font_collections[id] should be an instance of WP_Font_Collection class.' ); } - } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php similarity index 92% rename from phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php rename to phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php index ccef1ca49568e..7cdb8ec264d70 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection-test.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php @@ -68,10 +68,10 @@ public function test_should_return_error_if_id_is_repeated() { // Register first collection. $collection1 = WP_Font_Library::register_font_collection( $config1 ); - $this->assertInstanceOf( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); + $this->assertWPError( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); // Try to register a second collection with same id. $collection2 = WP_Font_Library::register_font_collection( $config2 ); - $this->assertInstanceOf( 'WP_Error', $collection2, 'Second collection with the same id should fail.' ); + $this->assertWPError( $collection2, 'Second collection with the same id should fail.' ); } } diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php index 908440ac60670..f1e712bab85b4 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php @@ -31,5 +31,4 @@ public function tear_down() { $property->setAccessible( true ); $property->setValue( array() ); } - } diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php index fa9f491c20d47..2b605302e37ff 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php @@ -44,6 +44,5 @@ public function test_get_non_existing_collection_should_return_404() { $response = rest_get_server()->dispatch( $request ); $this->assertSame( 404, $response->get_status() ); } - } diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php index 0050987099166..bf527093e4df1 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php @@ -42,6 +42,5 @@ public function test_get_font_collections() { $this->assertArrayHasKey( 'id', $data[0], 'The response data does not have the key with the collection ID.' ); $this->assertArrayHasKey( 'name', $data[0], 'The response data does not have the key with the collection name.' ); } - } diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php index a2b967c380e4f..9ae5f8d49354f 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -415,6 +415,5 @@ public function data_install_with_improper_inputs() { ), ); } - } diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php index 56d382d15d71e..ee957300090b0 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php @@ -25,6 +25,5 @@ public function test_register_routes() { $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections'][0]['methods'], 'Rest server has not the GET method for collections intialized.' ); $this->assertArrayHasKey( 'GET', $routes['/wp/v2/fonts/collections/(?P[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' ); } - } diff --git a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php index b89e488d25741..9edcec970146d 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php @@ -95,7 +95,6 @@ public function test_uninstall_non_existing_fonts() { $response->get_data(); $this->assertSame( 500, $response->get_status(), 'The response status is not 500.' ); } - } From d3935e3e8b7145726136de83e5924a978ce17aa5 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 31 Aug 2023 13:48:04 -0500 Subject: [PATCH 50/50] Doh I changed the wrong assertion --- .../fonts/font-library/wpFontLibrary/registerFontCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php index 7cdb8ec264d70..61b5eab873d6c 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php @@ -68,7 +68,7 @@ public function test_should_return_error_if_id_is_repeated() { // Register first collection. $collection1 = WP_Font_Library::register_font_collection( $config1 ); - $this->assertWPError( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); + $this->assertInstanceOf( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); // Try to register a second collection with same id. $collection2 = WP_Font_Library::register_font_collection( $config2 );