From 607ef9092bfaffdca1f4f7e6e13d63c1ba9a167e Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 31 Aug 2023 18:12:43 -0300 Subject: [PATCH] Font Library: Font Collection backend (#54098) Add extensibility capabilities to the Font Library. Provides a way to provide collections of typographic fonts by code. --------- Co-authored-by: Tonya Mork --- .../font-library/class-wp-font-collection.php | 95 +++++++++++++++++ .../font-library/class-wp-font-library.php | 55 ++++++++++ .../class-wp-rest-font-library-controller.php | 60 +++++++++++ .../fonts/font-library/font-library.php | 24 ++++- lib/load.php | 1 + .../wpFontCollection/__construct.php | 92 ++++++++++++++++ .../font-library/wpFontCollection/getData.php | 52 +++++++++ .../fonts/font-library/wpFontFamily/base.php | 1 + .../wpFontLibrary/getFontCollection.php | 36 +++++++ .../wpFontLibrary/getFontCollections.php | 45 ++++++++ .../wpFontLibrary/registerFontCollection.php | 77 ++++++++++++++ .../wpRestFontLibraryController/base.php | 34 ++++++ .../getFontCollection.php | 48 +++++++++ .../getFontCollections.php | 46 ++++++++ .../installFonts.php} | 87 +++------------ .../registerRoutes.php | 29 +++++ .../uninstallFonts.php | 100 ++++++++++++++++++ 17 files changed, 806 insertions(+), 76 deletions(-) create mode 100644 lib/experimental/fonts/font-library/class-wp-font-collection.php create mode 100644 phpunit/tests/fonts/font-library/wpFontCollection/__construct.php create mode 100644 phpunit/tests/fonts/font-library/wpFontCollection/getData.php create mode 100644 phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php create mode 100644 phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php create mode 100644 phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php 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} (82%) 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-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php new file mode 100644 index 0000000000000..1620467d36dfa --- /dev/null +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -0,0 +1,95 @@ +config = $config; + } + + /** + * Gets the font collection config. + * + * @since 6.4.0 + * + * @return array An array containing the font collection config. + */ + public function get_config() { + return $this->config; + } + + /** + * Gets the font collection data. + * + * @since 6.4.0 + * + * @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 ( ! 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' ) ); + } + + $collection_data = $this->get_config(); + $collection_data['data'] = $data; + unset( $collection_data['data_json_file'] ); + return $collection_data; + } +} 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..193ee3b2e5b7d 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,61 @@ class WP_Font_Library { 'woff2' => 'font/woff2', ); + /** + * Font collections. + * + * @since 6.4.0 + * + * @var array + */ + private static $collections = array(); + + /** + * Register a new font collection. + * + * @since 6.4.0 + * + * @param array $config Font collection config options. + * 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 ); + + if ( isset( self::$collections[ $config['id'] ] ) ) { + return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' ); + } + + self::$collections[ $config['id'] ] = $new_collection; + return $new_collection; + } + + /** + * Gets all the font collections available. + * + * @since 6.4.0 + * + * @return array List of font collections. + */ + 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 59e42d8716124..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 @@ -67,6 +67,66 @@ 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' ); + $collection = WP_Font_Library::get_font_collection( $id ); + + if ( is_wp_error( $collection ) ) { + $collection->add_data( array( 'status' => 404 ) ); + return $collection; + } + + return new WP_REST_Response( $collection->get_data() ); + } + + /** + * Gets the font collections available. + * + * @since 6.4.0 + * + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + public function get_font_collections() { + $collections = array(); + foreach ( WP_Font_Library::get_font_collections() as $collection ) { + $collections[] = $collection->get_config(); + } + + return new WP_REST_Response( $collections, 200 ); } /** diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index 96a33923917ec..0df008a275094 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,25 @@ 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' ); + +if ( ! function_exists( 'wp_register_font_collection' ) ) { + /** + * Registers a new Font Collection in the Font Library. + * + * @since 6.4.0 + * + * @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 ); + } +} diff --git a/lib/load.php b/lib/load.php index 6560ef566d444..ede4a8674c719 100644 --- a/lib/load.php +++ b/lib/load.php @@ -165,6 +165,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'; diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php new file mode 100644 index 0000000000000..0682425dd6282 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php @@ -0,0 +1,92 @@ +setAccessible( true ); + + $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( $config ); + + $actual = $property->getValue( $font_collection ); + $property->setAccessible( false ); + + $this->assertSame( $config, $actual ); + } + + /** + * @dataProvider data_should_throw_exception + * + * @param mixed $config Config of the font collection. + * @param string $expected_exception_message 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( $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 config ID is required as a non-empty string.', + ), + + 'no config' => array( + '', + 'Font Collection config options is required as a non-empty array.', + ), + + 'empty array' => array( + array(), + 'Font Collection config options is required as a non-empty array.', + ), + + 'boolean instead of config array' => array( + false, + 'Font Collection config options is required as a non-empty array.', + ), + + 'null instead of config array' => array( + null, + 'Font Collection config options is required as a non-empty array.', + ), + + 'missing data_json_file' => array( + array( + 'id' => 'my-collection', + 'name' => 'My Collection', + 'description' => 'My collection description', + ), + 'Font Collection config "data_json_file" option is required as a non-empty string.', + ), + + ); + } +} diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/getData.php b/phpunit/tests/fonts/font-library/wpFontCollection/getData.php new file mode 100644 index 0000000000000..55d12ac1c42fd --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpFontCollection/getData.php @@ -0,0 +1,52 @@ +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( + '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', + 'data' => '{"this is mock data":true}', + ), + ), + ); + } +} 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() { diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php new file mode 100644 index 0000000000000..1d4573cbe060c --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php @@ -0,0 +1,36 @@ + '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' ), + ); + + wp_register_font_collection( $my_font_collection_config ); + } + + public function test_should_get_font_collection() { + $font_collection = WP_Font_Library::get_font_collection( 'my-font-collection' ); + $this->assertInstanceOf( 'WP_Font_Collection', $font_collection ); + } + + 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/tests/fonts/font-library/wpFontLibrary/getFontCollections.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php new file mode 100644 index 0000000000000..4bda590d6e8fd --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php @@ -0,0 +1,45 @@ + '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' ), + ); + + 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' ), + ); + + wp_register_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.' ); + } +} diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php new file mode 100644 index 0000000000000..61b5eab873d6c --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php @@ -0,0 +1,77 @@ + 'my-collection', + 'name' => 'My Collection', + 'description' => 'My Collection Description', + 'data_json_file' => 'my-collection-data.json', + ); + $collection = WP_Font_Library::register_font_collection( $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_json_file' => 'my-collection-data.json', + ); + $this->expectException( 'Exception' ); + $this->expectExceptionMessage( 'Font Collection config ID is required as a non-empty string.' ); + WP_Font_Library::register_font_collection( $config ); + } + + 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.' ); + 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.' ); + WP_Font_Library::register_font_collection( $config ); + } + + public function test_should_return_error_if_id_is_repeated() { + $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( $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( $config2 ); + $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 new file mode 100644 index 0000000000000..f1e712bab85b4 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/base.php @@ -0,0 +1,34 @@ +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..2b605302e37ff --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollection.php @@ -0,0 +1,48 @@ + 'one-collection', + 'name' => 'One 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 ); + } + + 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..bf527093e4df1 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/getFontCollections.php @@ -0,0 +1,46 @@ +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 82% 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 f55d221acba96..9ae5f8d49354f 100644 --- a/phpunit/tests/fonts/font-library/class-wp-rest-font-library-controller.php +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php @@ -1,81 +1,27 @@ user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * @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->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.' ); - } - - /** - * @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.' ); - } - +class Tests_Fonts_WPRESTFontLibraryController_InstallFonts extends WP_REST_Font_Library_Controller_UnitTestCase { /** - * @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 ); @@ -105,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' ); @@ -344,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 ); @@ -478,3 +416,4 @@ 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..ee957300090b0 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/registerRoutes.php @@ -0,0 +1,29 @@ +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..9edcec970146d --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpRestFontLibraryController/uninstallFonts.php @@ -0,0 +1,100 @@ + '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 ); + 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.' ); + } +} + +