From 6134d72afb8031d67457c3639a98a7457893bc9e Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 24 Jan 2024 10:14:13 -0300 Subject: [PATCH] Remove WP_Font_Family class that is no longer used. --- .../font-library/class-wp-font-family.php | 621 ------------------ lib/load.php | 1 - .../font-library/wpFontFamily/__construct.php | 62 -- .../fonts/font-library/wpFontFamily/base.php | 75 --- .../font-library/wpFontFamily/getData.php | 94 --- .../wpFontFamily/getDataAsJson.php | 67 -- .../font-library/wpFontFamily/getFontPost.php | 42 -- .../wpFontFamily/hasFontFaces.php | 78 --- .../font-library/wpFontFamily/install.php | 562 ---------------- .../font-library/wpFontFamily/uninstall.php | 194 ------ 10 files changed, 1796 deletions(-) delete mode 100644 lib/experimental/fonts/font-library/class-wp-font-family.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/__construct.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/base.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/getData.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/getDataAsJson.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/getFontPost.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/hasFontFaces.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/install.php delete mode 100644 phpunit/tests/fonts/font-library/wpFontFamily/uninstall.php diff --git a/lib/experimental/fonts/font-library/class-wp-font-family.php b/lib/experimental/fonts/font-library/class-wp-font-family.php deleted file mode 100644 index f64aebc0c8efa..0000000000000 --- a/lib/experimental/fonts/font-library/class-wp-font-family.php +++ /dev/null @@ -1,621 +0,0 @@ -data = $font_data; - } - - /** - * Gets the font family data. - * - * @since 6.5.0 - * - * @return array An array in fontFamily theme.json format. - */ - public function get_data() { - return $this->data; - } - - /** - * Gets the font family data. - * - * @since 6.5.0 - * - * @return string fontFamily in theme.json format as stringified JSON. - */ - public function get_data_as_json() { - return wp_json_encode( $this->get_data() ); - } - - /** - * Checks whether the font family has font faces defined. - * - * @since 6.5.0 - * - * @return bool True if the font family has font faces defined, false otherwise. - */ - public function has_font_faces() { - return ! empty( $this->data['fontFace'] ) && is_array( $this->data['fontFace'] ); - } - - /** - * Removes font family assets. - * - * @since 6.5.0 - * - * @return bool True if assets were removed, false otherwise. - */ - private function remove_font_family_assets() { - if ( $this->has_font_faces() ) { - foreach ( $this->data['fontFace'] as $font_face ) { - $were_assets_removed = $this->delete_font_face_assets( $font_face ); - if ( false === $were_assets_removed ) { - return false; - } - } - } - return true; - } - - /** - * Removes a font family from the database and deletes its assets. - * - * @since 6.5.0 - * - * @return bool|WP_Error True if the font family was uninstalled, WP_Error otherwise. - */ - public function uninstall() { - $post = $this->get_data_from_post(); - if ( null === $post ) { - return new WP_Error( - 'font_family_not_found', - __( 'The font family could not be found.', 'gutenberg' ) - ); - } - - if ( - ! $this->remove_font_family_assets() || - ! wp_delete_post( $post->ID, true ) - ) { - return new WP_Error( - 'font_family_not_deleted', - __( 'The font family could not be deleted.', 'gutenberg' ) - ); - } - - return true; - } - - /** - * Deletes a specified font asset file from the fonts directory. - * - * @since 6.5.0 - * - * @param string $src The path of the font asset file to delete. - * @return bool Whether the file was deleted. - */ - private static function delete_asset( $src ) { - $filename = basename( $src ); - $file_path = path_join( wp_get_font_dir()['path'], $filename ); - - wp_delete_file( $file_path ); - - return ! file_exists( $file_path ); - } - - /** - * Deletes all font face asset files associated with a given font face. - * - * @since 6.5.0 - * - * @param array $font_face The font face array containing the 'src' attribute - * with the file path(s) to be deleted. - * @return bool True if delete was successful, otherwise false. - */ - private static function delete_font_face_assets( $font_face ) { - $sources = (array) $font_face['src']; - foreach ( $sources as $src ) { - $was_asset_removed = self::delete_asset( $src ); - if ( ! $was_asset_removed ) { - // Bail if any of the assets could not be removed. - return false; - } - } - return true; - } - - /** - * Gets the overrides for the 'wp_handle_upload' function. - * - * @since 6.5.0 - * - * @param string $filename The filename to be used for the uploaded file. - * @return array The overrides for the 'wp_handle_upload' function. - */ - private function get_upload_overrides( $filename ) { - return array( - // Arbitrary string to avoid the is_uploaded_file() check applied - // when using 'wp_handle_upload'. - 'action' => 'wp_handle_font_upload', - // Not testing a form submission. - 'test_form' => false, - // Seems mime type for files that are not images cannot be tested. - // See wp_check_filetype_and_ext(). - 'test_type' => true, - 'mimes' => WP_Font_Library::get_expected_font_mime_types_per_php_version(), - 'unique_filename_callback' => static function () use ( $filename ) { - // Keep the original filename. - return $filename; - }, - ); - } - - /** - * Downloads a font asset from a specified source URL and saves it to - * the font directory. - * - * @since 6.5.0 - * - * @param string $url The source URL of the font asset to be downloaded. - * @param string $filename The filename to save the downloaded font asset as. - * @return string|bool The relative path to the downloaded font asset. - * False if the download failed. - */ - private function download_asset( $url, $filename ) { - // Include file with download_url() if function doesn't exist. - if ( ! function_exists( 'download_url' ) ) { - require_once ABSPATH . 'wp-admin/includes/file.php'; - } - - // Downloads the font asset or returns false. - $temp_file = download_url( $url ); - if ( is_wp_error( $temp_file ) ) { - return false; - } - - $overrides = $this->get_upload_overrides( $filename ); - - $file = array( - 'tmp_name' => $temp_file, - 'name' => $filename, - ); - - $handled_file = wp_handle_upload( $file, $overrides ); - - // Cleans the temp file. - @unlink( $temp_file ); - - if ( ! isset( $handled_file['url'] ) ) { - return false; - } - - // Returns the relative path to the downloaded font asset to be used as - // font face src. - return $handled_file['url']; - } - - /** - * Moves an uploaded font face asset from temp folder to the fonts directory. - * - * This is used when uploading local fonts. - * - * @since 6.5.0 - * - * @param array $font_face Font face to download. - * @param array $file Uploaded file. - * @return array New font face with all assets downloaded and referenced in - * the font face definition. - */ - private function move_font_face_asset( $font_face, $file ) { - $new_font_face = $font_face; - $filename = WP_Font_Family_Utils::get_filename_from_font_face( - $this->data['slug'], - $font_face, - $file['name'] - ); - - // Remove the uploaded font asset reference from the font face definition - // because it is no longer needed. - unset( $new_font_face['uploadedFile'] ); - - // Move the uploaded font asset from the temp folder to the fonts directory. - if ( ! function_exists( 'wp_handle_upload' ) ) { - require_once ABSPATH . 'wp-admin/includes/file.php'; - } - - $overrides = $this->get_upload_overrides( $filename ); - - $handled_file = wp_handle_upload( $file, $overrides ); - - if ( isset( $handled_file['url'] ) ) { - // If the file was successfully moved, update the font face definition - // to reference the new file location. - $new_font_face['src'] = $handled_file['url']; - } - - return $new_font_face; - } - - /** - * Sanitizes the font family data using WP_Theme_JSON. - * - * @since 6.5.0 - * - * @return array A sanitized font family definition. - */ - private function sanitize() { - // Creates the structure of theme.json array with the new fonts. - $fonts_json = array( - 'version' => '2', - 'settings' => array( - 'typography' => array( - 'fontFamilies' => array( - 'custom' => array( - $this->data, - ), - ), - ), - ), - ); - - // Creates a new WP_Theme_JSON object with the new fonts to - // leverage sanitization and validation. - $fonts_json = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $fonts_json ); - $theme_json = new WP_Theme_JSON_Gutenberg( $fonts_json ); - $theme_data = $theme_json->get_data(); - $sanitized_font = ! empty( $theme_data['settings']['typography']['fontFamilies'] ) - ? $theme_data['settings']['typography']['fontFamilies'][0] - : array(); - - $sanitized_font['slug'] = _wp_to_kebab_case( $sanitized_font['slug'] ); - $sanitized_font['fontFamily'] = WP_Font_Family_Utils::format_font_family( $sanitized_font['fontFamily'] ); - $this->data = $sanitized_font; - return $this->data; - } - - /** - * Downloads font face assets. - * - * Downloads the font face asset(s) associated with a font face. It works with - * both single source URLs and arrays of multiple source URLs. - * - * @since 6.5.0 - * - * @param array $font_face The font face array containing the 'src' attribute - * with the source URL(s) of the assets. - * @return array The modified font face array with the new source URL(s) to - * the downloaded assets. - */ - private function download_font_face_assets( $font_face ) { - $new_font_face = $font_face; - $sources = (array) $font_face['downloadFromUrl']; - $new_font_face['src'] = array(); - $index = 0; - - foreach ( $sources as $src ) { - $suffix = $index++ > 0 ? $index : ''; - $filename = WP_Font_Family_Utils::get_filename_from_font_face( - $this->data['slug'], - $font_face, - $src, - $suffix - ); - $new_src = $this->download_asset( $src, $filename ); - if ( $new_src ) { - $new_font_face['src'][] = $new_src; - } - } - - if ( count( $new_font_face['src'] ) === 1 ) { - $new_font_face['src'] = $new_font_face['src'][0]; - } - - // Remove the download url reference from the font face definition - // because it is no longer needed. - unset( $new_font_face['downloadFromUrl'] ); - - return $new_font_face; - } - - - /** - * Downloads font face assets if the font family is a Google font, - * or moves them if it is a local font. - * - * @since 6.5.0 - * - * @param array $files An array of files to be installed. - * @return bool True if the font faces were downloaded or moved successfully, false otherwise. - */ - private function download_or_move_font_faces( $files ) { - if ( ! $this->has_font_faces() ) { - return true; - } - - $new_font_faces = array(); - foreach ( $this->data['fontFace'] as $font_face ) { - // If the fonts are not meant to be downloaded or uploaded - // (for example to install fonts that use a remote url). - $new_font_face = $font_face; - - $font_face_is_repeated = false; - - // If the font face has the same fontStyle and fontWeight as an existing, continue. - foreach ( $new_font_faces as $font_to_compare ) { - if ( $new_font_face['fontStyle'] === $font_to_compare['fontStyle'] && - $new_font_face['fontWeight'] === $font_to_compare['fontWeight'] ) { - $font_face_is_repeated = true; - } - } - - if ( $font_face_is_repeated ) { - continue; - } - - // If the font face requires the use of the filesystem, create the fonts dir if it doesn't exist. - if ( ! empty( $font_face['downloadFromUrl'] ) && ! empty( $font_face['uploadedFile'] ) ) { - wp_mkdir_p( wp_get_font_dir()['path'] ); - } - - // If installing google fonts, download the font face assets. - if ( ! empty( $font_face['downloadFromUrl'] ) ) { - $new_font_face = $this->download_font_face_assets( $new_font_face ); - } - - // If installing local fonts, move the font face assets from - // the temp folder to the wp fonts directory. - if ( ! empty( $font_face['uploadedFile'] ) && ! empty( $files ) ) { - $new_font_face = $this->move_font_face_asset( - $new_font_face, - $files[ $new_font_face['uploadedFile'] ] - ); - } - - /* - * If the font face assets were successfully downloaded, add the font face - * to the new font. Font faces with failed downloads are not added to the - * new font. - */ - if ( ! empty( $new_font_face['src'] ) ) { - $new_font_faces[] = $new_font_face; - } - } - - if ( ! empty( $new_font_faces ) ) { - $this->data['fontFace'] = $new_font_faces; - return true; - } - - return false; - } - - /** - * Gets the post for a font family. - * - * @since 6.5.0 - * - * @return WP_Post|null The post for this font family object or - * null if the post does not exist. - */ - public function get_font_post() { - $args = array( - 'post_type' => 'wp_font_family', - 'post_name' => $this->data['slug'], - 'name' => $this->data['slug'], - 'posts_per_page' => 1, - ); - - $posts_query = new WP_Query( $args ); - - if ( $posts_query->have_posts() ) { - return $posts_query->posts[0]; - } - - return null; - } - - /** - * Gets the data for this object from the database and - * sets it to the data property. - * - * @since 6.5.0 - * - * @return WP_Post|null The post for this font family object or - * null if the post does not exist. - */ - private function get_data_from_post() { - $post = $this->get_font_post(); - if ( $post ) { - $this->data = json_decode( $post->post_content, true ); - return $post; - } - - return null; - } - - /** - * Creates a post for a font family. - * - * @since 6.5.0 - * - * @return int|WP_Error Post ID if the post was created, WP_Error otherwise. - */ - private function create_font_post() { - $post = array( - 'post_title' => $this->data['name'], - 'post_name' => $this->data['slug'], - 'post_type' => 'wp_font_family', - 'post_content' => $this->get_data_as_json(), - 'post_status' => 'publish', - ); - - $post_id = wp_insert_post( $post ); - if ( 0 === $post_id || is_wp_error( $post_id ) ) { - return new WP_Error( - 'font_post_creation_failed', - __( 'Font post creation failed', 'gutenberg' ) - ); - } - - return $post_id; - } - - /** - * Gets the font faces that are in both the existing and incoming font families. - * - * @since 6.5.0 - * - * @param array $existing The existing font faces. - * @param array $incoming The incoming font faces. - * @return array The font faces that are in both the existing and incoming font families. - */ - private function get_intersecting_font_faces( $existing, $incoming ) { - $intersecting = array(); - foreach ( $existing as $existing_face ) { - foreach ( $incoming as $incoming_face ) { - if ( $incoming_face['fontStyle'] === $existing_face['fontStyle'] && - $incoming_face['fontWeight'] === $existing_face['fontWeight'] && - $incoming_face['src'] !== $existing_face['src'] ) { - $intersecting[] = $existing_face; - } - } - } - return $intersecting; - } - - /** - * Updates a post for a font family. - * - * @since 6.5.0 - * - * @param WP_Post $post The post to update. - * @return int|WP_Error Post ID if the update was successful, WP_Error otherwise. - */ - private function update_font_post( $post ) { - $post_font_data = json_decode( $post->post_content, true ); - $new_data = WP_Font_Family_Utils::merge_fonts_data( $post_font_data, $this->data ); - if ( isset( $post_font_data['fontFace'] ) && ! empty( $post_font_data['fontFace'] ) ) { - $intersecting = $this->get_intersecting_font_faces( $post_font_data['fontFace'], $new_data['fontFace'] ); - } - - if ( isset( $intersecting ) && ! empty( $intersecting ) ) { - $serialized_font_faces = array_map( 'serialize', $new_data['fontFace'] ); - $serialized_intersecting = array_map( 'serialize', $intersecting ); - - $diff = array_diff( $serialized_font_faces, $serialized_intersecting ); - - $new_data['fontFace'] = array_values( array_map( 'unserialize', $diff ) ); - - foreach ( $intersecting as $intersect ) { - $this->delete_font_face_assets( $intersect ); - } - } - $this->data = $new_data; - - $post = array( - 'ID' => $post->ID, - 'post_content' => $this->get_data_as_json(), - ); - - $post_id = wp_update_post( $post ); - - if ( 0 === $post_id || is_wp_error( $post_id ) ) { - return new WP_Error( - 'font_post_update_failed', - __( 'Font post update failed', 'gutenberg' ) - ); - } - - return $post_id; - } - - /** - * Creates a post for a font in the Font Library if it doesn't exist, - * or updates it if it does. - * - * @since 6.5.0 - * - * @return int|WP_Error Post id if the post was created or updated successfully, - * WP_Error otherwise. - */ - private function create_or_update_font_post() { - $this->sanitize(); - - $post = $this->get_font_post(); - if ( $post ) { - return $this->update_font_post( $post ); - } - - return $this->create_font_post(); - } - - /** - * Installs the font family into the library. - * - * @since 6.5.0 - * - * @param array $files Optional. An array of files to be installed. Default null. - * @return array|WP_Error An array of font family data on success, WP_Error otherwise. - */ - public function install( $files = null ) { - add_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) ); - add_filter( 'upload_dir', 'wp_get_font_dir' ); - $were_assets_written = $this->download_or_move_font_faces( $files ); - remove_filter( 'upload_dir', 'wp_get_font_dir' ); - remove_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) ); - - if ( ! $were_assets_written ) { - return new WP_Error( - 'font_face_download_failed', - __( 'The font face assets could not be written.', 'gutenberg' ) - ); - } - - $post_id = $this->create_or_update_font_post(); - - if ( is_wp_error( $post_id ) ) { - return $post_id; - } - - return $this->get_data(); - } -} diff --git a/lib/load.php b/lib/load.php index 2dd33962e355b..b78fc0c3dda6e 100644 --- a/lib/load.php +++ b/lib/load.php @@ -143,7 +143,6 @@ function gutenberg_is_experiment_enabled( $name ) { 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'; require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-families-controller.php'; require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php'; require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-collections-controller.php'; diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/__construct.php b/phpunit/tests/fonts/font-library/wpFontFamily/__construct.php deleted file mode 100644 index cee0628dad310..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/__construct.php +++ /dev/null @@ -1,62 +0,0 @@ -setAccessible( true ); - - $font_data = array( - 'fontFamily' => 'Piazzolla', - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - ); - $font_family = new WP_Font_Family( $font_data ); - - $actual = $property->getValue( $font_family ); - $property->setAccessible( false ); - - $this->assertSame( $font_data, $actual ); - } - - /** - * @dataProvider data_should_do_it_wrong - * - * @param mixed $font_data Data to test. - */ - public function test_should_do_it_wrong( $font_data ) { - $this->expectException( 'Exception' ); - $this->expectExceptionMessage( 'Font family data is missing the slug.' ); - - new WP_Font_Family( $font_data ); - } - - /** - * Data provider. - * - * @return array - */ - public function data_should_do_it_wrong() { - return array( - 'no slug' => array( - array( - 'fontFamily' => 'Piazzolla', - 'name' => 'Piazzolla', - ), - ), - 'empty array' => array( array() ), - 'boolean instead of array' => array( false ), - 'null instead of array' => array( null ), - ); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/base.php b/phpunit/tests/fonts/font-library/wpFontFamily/base.php deleted file mode 100644 index 3f6ff153fa12f..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/base.php +++ /dev/null @@ -1,75 +0,0 @@ - array(), - 'files_data' => array(), - 'font_filename' => '', - ); - - public static function set_up_before_class() { - parent::set_up_before_class(); - - static::$fonts_dir = wp_get_font_dir()['path']; - wp_mkdir_p( static::$fonts_dir ); - } - - public function set_up() { - parent::set_up(); - - $merriweather_tmp_name = wp_tempnam( 'Merriweather-' ); - copy( __DIR__ . '/../../../data/fonts/Merriweather.ttf', $merriweather_tmp_name ); - $this->merriweather = array( - 'font_data' => array( - 'name' => 'Merriweather', - 'slug' => 'merriweather', - 'fontFamily' => 'Merriweather', - 'fontFace' => array( - array( - 'fontFamily' => 'Merriweather', - 'fontStyle' => 'normal', - 'fontWeight' => '400', - 'uploadedFile' => 'files0', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'merriweather.ttf', - 'type' => 'font/ttf', - 'tmp_name' => $merriweather_tmp_name, - 'error' => 0, - 'size' => 123, - ), - ), - 'font_filename' => path_join( static::$fonts_dir, 'merriweather_normal_400.ttf' ), - ); - } - - public function tear_down() { - // Clean up the /fonts directory. - foreach ( $this->files_in_dir( static::$fonts_dir ) as $file ) { - @unlink( $file ); - } - - parent::tear_down(); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/getData.php b/phpunit/tests/fonts/font-library/wpFontFamily/getData.php deleted file mode 100644 index 2de88ad3aae37..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/getData.php +++ /dev/null @@ -1,94 +0,0 @@ -assertSame( $font_data, $font->get_data() ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_get_data() { - return array( - 'with one google font face to be downloaded' => array( - array( - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - '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', - ), - ), - ), - ), - 'with one google font face to not be downloaded' => array( - array( - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://fonts.gstatic.com/s/piazzolla/v33/N0b72SlTPu5rIkWIZjVgI-TckS03oGpPETyEJ88Rbvi0_TzOzKcQhZqx3gX9BRy5m5M.ttf', - ), - ), - ), - ), - 'without font faces' => array( - array( - 'name' => 'Arial', - 'slug' => 'arial', - 'fontFamily' => 'Arial', - 'fontFace' => array(), - ), - ), - 'with local files' => array( - array( - 'name' => 'Inter', - 'slug' => 'inter', - 'fontFamily' => 'Inter', - 'fontFace' => array( - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'normal', - 'fontWeight' => '400', - 'uploadedFile' => 'files0', - ), - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'normal', - 'fontWeight' => '500', - 'uploadedFile' => 'files1', - ), - ), - ), - ), - ); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/getDataAsJson.php b/phpunit/tests/fonts/font-library/wpFontFamily/getDataAsJson.php deleted file mode 100644 index b18b2d0b4f4b3..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/getDataAsJson.php +++ /dev/null @@ -1,67 +0,0 @@ -assertSame( $expected, $font->get_data_as_json() ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_get_data_as_json() { - return array( - 'piazzolla' => array( - 'font_data' => array( - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'name' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'src' => 'https://example.com/fonts/piazzolla_italic_400.ttf', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - ), - ), - ), - 'expected' => '{"slug":"piazzolla","fontFamily":"Piazzolla","name":"Piazzolla","fontFace":[{"fontFamily":"Piazzolla","src":"https:\/\/example.com\/fonts\/piazzolla_italic_400.ttf","fontStyle":"italic","fontWeight":"400"}]}', - ), - 'piazzolla2' => array( - 'font_data' => array( - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'name' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'src' => 'https://example.com/fonts/piazzolla_italic_400.ttf', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - ), - ), - ), - 'expected' => '{"slug":"piazzolla","fontFamily":"Piazzolla","name":"Piazzolla","fontFace":[{"fontFamily":"Piazzolla","src":"https:\/\/example.com\/fonts\/piazzolla_italic_400.ttf","fontStyle":"italic","fontWeight":"400"}]}', - ), - ); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/getFontPost.php b/phpunit/tests/fonts/font-library/wpFontFamily/getFontPost.php deleted file mode 100644 index eb42cc3ee0898..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/getFontPost.php +++ /dev/null @@ -1,42 +0,0 @@ - $this->merriweather['font_data']['name'], - 'post_name' => $this->merriweather['font_data']['slug'], - 'post_type' => 'wp_font_family', - 'post_content' => '', - 'post_status' => 'publish', - ); - $post_id = wp_insert_post( $post ); - $font = new WP_Font_Family( $this->merriweather['font_data'] ); - - // Test. - $actual = $font->get_font_post(); - $this->assertInstanceOf( WP_Post::class, $actual, 'Font post should exist' ); - $this->assertSame( $post_id, $actual->ID, 'Font post ID should match' ); - } - - public function test_should_return_null_when_post_does_not_exist() { - $font = new WP_Font_Family( $this->merriweather['font_data'] ); - - $this->assertNull( $font->get_font_post() ); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/hasFontFaces.php b/phpunit/tests/fonts/font-library/wpFontFamily/hasFontFaces.php deleted file mode 100644 index 0c153d62aa79d..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/hasFontFaces.php +++ /dev/null @@ -1,78 +0,0 @@ - 'piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - ), - ), - ); - $font = new WP_Font_Family( $font_data ); - $this->assertTrue( $font->has_font_faces() ); - } - - /** - * @dataProvider data_should_return_false_when_check_fails - * - * @param array $font_data Font family data in theme.json format. - */ - public function test_should_return_false_when_check_fails( $font_data ) { - $font = new WP_Font_Family( $font_data ); - $this->assertFalse( $font->has_font_faces() ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_return_false_when_check_fails() { - return array( - 'wrong fontFace key' => array( - array( - 'slug' => 'piazzolla', - 'fontFaces' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - ), - ), - ), - ), - 'without font faces' => array( - array( - 'slug' => 'piazzolla', - ), - ), - 'empty array' => array( - array( - 'slug' => 'piazzolla', - 'fontFace' => array(), - ), - ), - 'null' => array( - array( - 'slug' => 'piazzolla', - 'fontFace' => null, - ), - ), - ); - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/install.php b/phpunit/tests/fonts/font-library/wpFontFamily/install.php deleted file mode 100644 index 79cb6ac3ba2ba..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/install.php +++ /dev/null @@ -1,562 +0,0 @@ -install(); - $this->assertEmpty( $this->files_in_dir( static::$fonts_dir ), 'Font directory should be empty' ); - $this->assertInstanceOf( WP_Post::class, $font->get_font_post(), 'Font post should exist after install' ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_not_download_when_no_fontface() { - return array( - 'wrong fontFace key' => array( - array( - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFaces' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - ), - ), - ), - ), - 'without font faces' => array( - array( - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - ), - ), - 'empty array' => array( - array( - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array(), - ), - ), - 'null' => array( - array( - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => null, - ), - ), - ); - } - - /** - * @dataProvider data_should_download_fontfaces - * - * @param array $font_data Font family data in theme.json format. - * @param array $expected Expected font filename(s). - */ - public function test_should_download_fontfaces_and_create_post( $font_data, array $expected ) { - // Pre-checks to ensure starting conditions. - foreach ( $expected as $font_file ) { - $font_file = path_join( static::$fonts_dir, $font_file ); - $this->assertFileDoesNotExist( $font_file, "Font file [{$font_file}] should not exist in the fonts/ directory after installing" ); - } - $font = new WP_Font_Family( $font_data ); - - // Test. - $font->install(); - foreach ( $expected as $font_file ) { - $font_file = path_join( static::$fonts_dir, $font_file ); - $this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" ); - } - $this->assertInstanceOf( WP_Post::class, $font->get_font_post(), 'Font post should exist after install' ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_download_fontfaces() { - return array( - '1 font face to download' => array( - 'font_data' => array( - 'name' => 'Piazzolla', - 'slug' => 'piazzolla', - 'fontFamily' => 'Piazzolla', - 'fontFace' => array( - array( - 'fontFamily' => 'Piazzolla', - 'fontStyle' => 'italic', - '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', - ), - ), - ), - 'expected' => array( 'piazzolla_italic_400.ttf' ), - ), - '2 font faces to download' => array( - 'font_data' => array( - 'name' => 'Lato', - 'slug' => 'lato', - 'fontFamily' => 'Lato', - 'fontFace' => array( - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '400', - 'src' => 'http://fonts.gstatic.com/s/lato/v24/S6uyw4BMUTPHvxk6XweuBCY.ttf', - 'downloadFromUrl' => 'http://fonts.gstatic.com/s/lato/v24/S6uyw4BMUTPHvxk6XweuBCY.ttf', - ), - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'italic', - 'fontWeight' => '400', - 'src' => 'http://fonts.gstatic.com/s/lato/v24/S6u8w4BMUTPHjxswWyWrFCbw7A.ttf', - 'downloadFromUrl' => 'http://fonts.gstatic.com/s/lato/v24/S6u8w4BMUTPHjxswWyWrFCbw7A.ttf', - ), - ), - ), - 'expected' => array( 'lato_normal_400.ttf', 'lato_italic_400.ttf' ), - ), - ); - } - - /** - * @dataProvider data_should_move_local_fontfaces - * - * @param array $font_data Font family data in theme.json format. - * @param array $files_data Files data in $_FILES format. - * @param array $expected Expected font filename(s). - */ - public function test_should_move_local_fontfaces( $font_data, array $files_data, array $expected ) { - // Set up the temporary files. - foreach ( $files_data as $file ) { - if ( 'font/ttf' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/Merriweather.ttf', $file['tmp_name'] ); - } elseif ( 'font/woff' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/cooper-hewitt.woff', $file['tmp_name'] ); - } elseif ( 'font/woff2' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/DMSans.woff2', $file['tmp_name'] ); - } elseif ( 'application/vnd.ms-opentype' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/gilbert-color.otf', $file['tmp_name'] ); - } - } - - $font = new WP_Font_Family( $font_data ); - $font->install( $files_data ); - - foreach ( $expected as $font_file ) { - $font_file = path_join( static::$fonts_dir, $font_file ); - $this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" ); - } - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_move_local_fontfaces() { - return array( - // ttf font type. - '1 local font' => array( - 'font_data' => array( - 'name' => 'Inter', - 'slug' => 'inter', - 'fontFamily' => 'Inter', - 'fontFace' => array( - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '900', - 'uploadedFile' => 'files0', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'inter1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'expected' => array( 'inter_italic_900.ttf' ), - ), - '2 local fonts' => array( - 'font_data' => array( - 'name' => 'Lato', - 'slug' => 'lato', - 'fontFamily' => 'Lato', - 'fontFace' => array( - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '400', - 'uploadedFile' => 'files1', - ), - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '500', - 'uploadedFile' => 'files2', - ), - ), - ), - 'files_data' => array( - 'files1' => array( - 'name' => 'lato1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Lato-' ), - 'error' => 0, - 'size' => 123, - ), - 'files2' => array( - 'name' => 'lato2.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Lato-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'expected' => array( 'lato_normal_400.ttf', 'lato_normal_500.ttf' ), - ), - // woff font type. - 'woff local font' => array( - 'font_data' => array( - 'name' => 'Cooper Hewitt', - 'slug' => 'cooper-hewitt', - 'fontFamily' => 'Cooper Hewitt', - 'fontFace' => array( - array( - 'fontFamily' => 'Cooper Hewitt', - 'fontStyle' => 'italic', - 'fontWeight' => '900', - 'uploadedFile' => 'files0', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'cooper-hewitt.woff', - 'type' => 'font/woff', - 'tmp_name' => wp_tempnam( 'Cooper-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'expected' => array( 'cooper-hewitt_italic_900.woff' ), - ), - // woff2 font type. - 'woff2 local font' => array( - 'font_data' => array( - 'name' => 'DM Sans', - 'slug' => 'dm-sans', - 'fontFamily' => 'DM Sans', - 'fontFace' => array( - array( - 'fontFamily' => 'DM Sans', - 'fontStyle' => 'regular', - 'fontWeight' => '500', - 'uploadedFile' => 'files0', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'DMSans.woff2', - 'type' => 'font/woff2', - 'tmp_name' => wp_tempnam( 'DMSans-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'expected' => array( 'dm-sans_regular_500.woff2' ), - ), - // otf font type. - 'otf local font' => array( - 'font_data' => array( - 'name' => 'Gilbert Color', - 'slug' => 'gilbert-color', - 'fontFamily' => 'Gilbert Color', - 'fontFace' => array( - array( - 'fontFamily' => 'Gilbert Color', - 'fontStyle' => 'regular', - 'fontWeight' => '500', - 'uploadedFile' => 'files0', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'gilbert-color.otf', - 'type' => 'application/vnd.ms-opentype', - 'tmp_name' => wp_tempnam( 'Gilbert-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'expected' => array( 'gilbert-color_regular_500.otf' ), - ), - ); - } - - /** - * @dataProvider data_should_not_install_duplicate_fontfaces - * - * @param array $font_data Font family data in theme.json format. - * @param array $files_data Files data in $_FILES format. - * @param array $expected Expected font filename(s). - */ - public function test_should_not_install_duplicate_fontfaces( $font_data, array $files_data, array $expected ) { - // Set up the temporary files. - foreach ( $files_data as $file ) { - copy( __DIR__ . '/../../../data/fonts/Merriweather.ttf', $file['tmp_name'] ); - } - - $font = new WP_Font_Family( $font_data ); - $font->install( $files_data ); - - $this->assertCount( count( $expected ), $this->files_in_dir( static::$fonts_dir ), 'Font directory should contain the same number of files as expected' ); - - foreach ( $expected as $font_file ) { - $font_file = path_join( static::$fonts_dir, $font_file ); - $this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" ); - } - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_not_install_duplicate_fontfaces() { - return array( - 'single unique font face' => array( - 'font_data' => array( - 'name' => 'Inter', - 'slug' => 'inter', - 'fontFamily' => 'Inter', - 'fontFace' => array( - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '900', - 'uploadedFile' => 'files0', - ), - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '900', - 'uploadedFile' => 'files1', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'inter1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - 'files1' => array( - 'name' => 'inter1.woff', - 'type' => 'font/woff', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'expected' => array( 'inter_italic_900.ttf' ), - ), - 'multiple unique font faces' => array( - 'font_data' => array( - 'name' => 'Lato', - 'slug' => 'lato', - 'fontFamily' => 'Lato', - 'fontFace' => array( - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '400', - 'uploadedFile' => 'files0', - ), - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '500', - 'uploadedFile' => 'files1', - ), - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '500', - 'uploadedFile' => 'files2', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'lato1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Lato-' ), - 'error' => 0, - 'size' => 123, - ), - 'files1' => array( - 'name' => 'lato2.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Lato-' ), - 'error' => 0, - 'size' => 123, - ), - 'files2' => array( - 'name' => 'lato2.woff', - 'type' => 'font/woff', - 'tmp_name' => wp_tempnam( 'Lato-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'expected' => array( 'lato_normal_400.ttf', 'lato_normal_500.ttf' ), - ), - ); - } - - public function test_should_overwrite_fontface_with_different_extension() { - $font_data_initial = array( - 'name' => 'Inter', - 'slug' => 'inter', - 'fontFamily' => 'Inter', - 'fontFace' => array( - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'uploadedFile' => 'files0', - ), - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '900', - 'uploadedFile' => 'files1', - ), - ), - ); - $files_data_initial = array( - 'files0' => array( - 'name' => 'inter1.ttf', - 'type' => 'font/woff', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - 'files1' => array( - 'name' => 'inter1.woff', - 'type' => 'font/woff', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - ); - $font_data_overwrite = array( - 'name' => 'Inter', - 'slug' => 'inter', - 'fontFamily' => 'Inter', - 'fontFace' => array( - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '500', - 'uploadedFile' => 'files0', - ), - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '900', - 'uploadedFile' => 'files1', - ), - ), - ); - $files_data_overwrite = array( - 'files0' => array( - 'name' => 'inter1.woff', - 'type' => 'font/woff', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - 'files1' => array( - 'name' => 'inter1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - ); - - $expected = array( 'inter_italic_500.woff', 'inter_italic_900.ttf' ); - - // Set up the temporary files. - foreach ( $files_data_initial as $file ) { - if ( 'font/ttf' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/Merriweather.ttf', $file['tmp_name'] ); - } elseif ( 'font/woff' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/cooper-hewitt.woff', $file['tmp_name'] ); - } - } - foreach ( $files_data_overwrite as $file ) { - if ( 'font/ttf' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/Merriweather.ttf', $file['tmp_name'] ); - } elseif ( 'font/woff' === $file['type'] ) { - copy( __DIR__ . '/../../../data/fonts/cooper-hewitt.woff', $file['tmp_name'] ); - } - } - - $font = new WP_Font_Family( $font_data_initial ); - $font->install( $files_data_initial ); - - $font = new WP_Font_Family( $font_data_overwrite ); - $font->install( $files_data_overwrite ); - - $this->assertCount( count( $expected ), $this->files_in_dir( static::$fonts_dir ), 'Font directory should contain the same number of files as expected' ); - - foreach ( $expected as $font_file ) { - $font_file = path_join( static::$fonts_dir, $font_file ); - $this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" ); - } - } -} diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/uninstall.php b/phpunit/tests/fonts/font-library/wpFontFamily/uninstall.php deleted file mode 100644 index c878dd00fdb5c..0000000000000 --- a/phpunit/tests/fonts/font-library/wpFontFamily/uninstall.php +++ /dev/null @@ -1,194 +0,0 @@ -merriweather['font_data'] ); - - // Test. - $actual = $font->uninstall(); - $this->assertWPError( $actual, 'WP_Error should have been returned' ); - $this->assertSame( - array( 'font_family_not_found' => array( 'The font family could not be found.' ) ), - $actual->errors, - 'WP_Error should have "fonts_must_have_same_slug" error' - ); - } - - /** - * @dataProvider data_should_return_error_when_not_able_to_uninstall - * - * @param string $failure_to_mock The filter name to mock the failure. - */ - public function test_should_return_error_when_not_able_to_uninstall( $failure_to_mock ) { - // Set up the font. - add_filter( $failure_to_mock, '__return_empty_string' ); - $font = new WP_Font_Family( $this->merriweather['font_data'] ); - $font->install( $this->merriweather['files_data'] ); - - // Test. - $actual = $font->uninstall(); - $this->assertWPError( $actual, 'WP_Error should be returned' ); - $this->assertSame( - array( 'font_family_not_deleted' => array( 'The font family could not be deleted.' ) ), - $actual->errors, - 'WP_Error should have "font_family_not_deleted" error' - ); - } - - /** - * Data provider. - * - * @return string[][] - */ - public function data_should_return_error_when_not_able_to_uninstall() { - return array( - 'When delete file fails' => array( 'wp_delete_file' ), - 'when delete post fails' => array( 'pre_delete_post' ), - ); - } - - /** - * @dataProvider data_should_uninstall - * - * @param array $font_data Font family data in theme.json format. - * @param array $files_data Files data in $_FILES format. - */ - public function test_should_uninstall( $font_data, array $files_data ) { - // Set up. - foreach ( $files_data as $file ) { - copy( __DIR__ . '/../../../data/fonts/Merriweather.ttf', $file['tmp_name'] ); - } - $font = new WP_Font_Family( $font_data ); - $font->install( $files_data ); - - // Pre-checks to ensure the starting point is as expected. - $this->assertInstanceOf( WP_Post::class, $font->get_font_post(), 'Font post should exist' ); - $this->assertNotEmpty( $this->files_in_dir( static::$fonts_dir ), 'Fonts should be installed' ); - - // Uninstall. - $this->assertTrue( $font->uninstall() ); - - // Test the post and font file(s) were uninstalled. - $this->assertNull( $font->get_font_post(), 'Font post should be deleted after uninstall' ); - $this->assertEmpty( $this->files_in_dir( static::$fonts_dir ), 'Fonts should be uninstalled' ); - } - - /** - * @dataProvider data_should_uninstall - * - * @param array $font_data Font family data in theme.json format. - * @param array $files_data Files data in $_FILES format. - * @param array $files_to_uninstall Files to uninstall. - */ - public function test_should_uninstall_only_its_font_family( $font_data, array $files_data, array $files_to_uninstall ) { - // Set up a different font family instance. This font family should not be uninstalled. - $merriweather = new WP_Font_Family( $this->merriweather['font_data'] ); - $merriweather->install( $this->merriweather['files_data'] ); - - // Set up the font family to be uninstalled. - foreach ( $files_data as $file ) { - copy( __DIR__ . '/../../../data/fonts/Merriweather.ttf', $file['tmp_name'] ); - } - $font = new WP_Font_Family( $font_data ); - $font->install( $files_data ); - - $this->assertTrue( $font->uninstall() ); - - // Check that the files were uninstalled. - foreach ( $files_to_uninstall as $font_file ) { - $font_file = path_join( static::$fonts_dir, $font_file ); - $this->assertFileDoesNotExist( $font_file, "Font file [{$font_file}] should not exists in the uploads/fonts/ directory after uninstalling" ); - } - // Check that the Merriweather file was not uninstalled. - $this->assertFileExists( $this->merriweather['font_filename'], 'The other font family [Merriweather] should not have been uninstalled.' ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_should_uninstall() { - return array( - '1 local font' => array( - 'font_data' => array( - 'name' => 'Inter', - 'slug' => 'inter', - 'fontFamily' => 'Inter', - 'fontFace' => array( - array( - 'fontFamily' => 'Inter', - 'fontStyle' => 'italic', - 'fontWeight' => '900', - 'uploadedFile' => 'files0', - ), - ), - ), - 'files_data' => array( - 'files0' => array( - 'name' => 'inter1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Inter-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'files_to_uninstall' => array( 'inter_italic_900.ttf' ), - ), - '2 local fonts' => array( - 'font_data' => array( - 'name' => 'Lato', - 'slug' => 'lato', - 'fontFamily' => 'Lato', - 'fontFace' => array( - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '400', - 'uploadedFile' => 'files1', - ), - array( - 'fontFamily' => 'Lato', - 'fontStyle' => 'normal', - 'fontWeight' => '500', - 'uploadedFile' => 'files2', - ), - ), - ), - 'files_data' => array( - 'files1' => array( - 'name' => 'lato1.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Lato-' ), - 'error' => 0, - 'size' => 123, - ), - 'files2' => array( - 'name' => 'lato2.ttf', - 'type' => 'font/ttf', - 'tmp_name' => wp_tempnam( 'Lato-' ), - 'error' => 0, - 'size' => 123, - ), - ), - 'files_to_uninstall' => array( 'lato_normal_400.ttf', 'lato_normal_500.ttf' ), - ), - ); - } -}