From 73b24fcc3475318dd663c8204b3d74ba20e8da9a Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 15 Jan 2024 14:36:03 -0600 Subject: [PATCH 1/6] Prevent creating font families with duplicate slugs --- ...class-wp-rest-font-families-controller.php | 31 +++++++++++++++ .../wpRestFontFamiliesController.php | 38 ++++++++++++++----- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index 0c4b3d8c6c0c77..5eed8d7642507b 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -192,6 +192,37 @@ public function sanitize_font_family_settings( $value ) { return $settings; } + /** + * Creates a single font family. + * + * @since 6.5.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 create_item( $request ) { + $settings = $request->get_param( 'font_family_settings' ); + + // Check that the font family slug is unique. + $existing_font_family = get_posts( + array( + 'post_type' => $this->post_type, + 'posts_per_page' => 1, + 'name' => $settings['slug'], + ) + ); + if ( ! empty( $existing_font_family ) ) { + return new WP_Error( + 'rest_duplicate_font_family', + /* translators: %s: Font family slug. */ + sprintf( __( 'A font family with slug "%s" already exists.', 'gutenberg' ), $settings['slug'] ), + array( 'status' => WP_Http::CONFLICT ) + ); + } + + return parent::create_item( $request ); + } + /** * Deletes a single font family. * diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index d00d653c0f7274..7b36254c9b7374 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -249,19 +249,20 @@ public function test_get_item_no_permission() { * @covers WP_REST_Font_Faces_Controller::create_item */ public function test_create_item() { - wp_set_current_user( self::$admin_id ); + $settings = array_merge( self::$default_settings, array( 'slug' => 'test_create_item' ) ); + wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); $request->set_param( 'theme_json_version', 2 ); - $request->set_param( 'font_family_settings', wp_json_encode( self::$default_settings ) ); + $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertSame( 201, $response->get_status() ); $this->check_font_family_data( $data, $data['id'], $response->get_links() ); - $settings = $data['font_family_settings']; - $this->assertSame( self::$default_settings, $settings ); + $reponse_settings = $data['font_family_settings']; + $this->assertSame( $settings, $reponse_settings ); $this->assertEmpty( $data['font_faces'] ); wp_delete_post( $data['id'], true ); @@ -271,9 +272,10 @@ public function test_create_item() { * @covers WP_REST_Font_Faces_Controller::validate_create_font_face_request */ public function test_create_item_default_theme_json_version() { + $settings = array_merge( self::$default_settings, array( 'slug' => 'test_create_item_theme_json' ) ); wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); - $request->set_param( 'font_family_settings', wp_json_encode( self::$default_settings ) ); + $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -331,7 +333,7 @@ public function test_create_item_with_default_preview( $settings ) { public function data_create_item_with_default_preview() { $default_settings = array( 'name' => 'Open Sans', - 'slug' => 'open-sans', + 'slug' => 'create_item_with_default_preview', 'fontFamily' => '"Open Sans", sans-serif', ); return array( @@ -404,17 +406,35 @@ public function test_create_item_invalid_settings_json() { $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); $expected_message = 'font_family_settings parameter must be a valid JSON string.'; - $actual_message = $response->as_error()->get_all_error_data()[0]['params']['font_family_settings']; - $this->assertSame( $expected_message, $actual_message ); + $message = $response->as_error()->get_all_error_data()[0]['params']['font_family_settings']; + $this->assertSame( $expected_message, $message ); + } + + /** + * @covers WP_REST_Font_Family_Controller::validate_font_family_settings + */ + public function test_create_item_with_duplicate_slug() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); + $request->set_param( 'theme_json_version', 2 ); + $request->set_param( 'font_family_settings', wp_json_encode( array_merge( self::$default_settings, array( 'slug' => 'helvetica' ) ) ) ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_duplicate_font_family', $response, 409 ); + $expected_message = 'A font family with slug "helvetica" already exists.'; + $message = $response->as_error()->get_error_messages()[0]; + $this->assertSame( $expected_message, $message ); } /** * @covers WP_REST_Font_Faces_Controller::create_item */ public function test_create_item_no_permission() { + $settings = array_merge( self::$default_settings, array( 'slug' => 'test_create_item_no_permissions' ) ); wp_set_current_user( 0 ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); - $request->set_param( 'font_family_settings', wp_json_encode( self::$default_settings ) ); + $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_create', $response, 401 ); From c2a510df8d6d2f4f5befdb105e0374e9c593eb15 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 15 Jan 2024 14:55:33 -0600 Subject: [PATCH 2/6] Query font families by slug --- ...class-wp-rest-font-families-controller.php | 3 ++- .../wpRestFontFamiliesController.php | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index 5eed8d7642507b..66e26f098505f5 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -216,7 +216,7 @@ public function create_item( $request ) { 'rest_duplicate_font_family', /* translators: %s: Font family slug. */ sprintf( __( 'A font family with slug "%s" already exists.', 'gutenberg' ), $settings['slug'] ), - array( 'status' => WP_Http::CONFLICT ) + array( 'status' => 400 ) ); } @@ -380,6 +380,7 @@ public function get_collection_params() { 'page' => $params['page'], 'per_page' => $params['per_page'], 'search' => $params['search'], + 'slug' => $params['slug'], ); } diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 7b36254c9b7374..2ff0dc821e6bf9 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -145,6 +145,23 @@ public function test_get_items() { $this->check_font_family_data( $data[1], self::$font_family_id2, $data[1]['_links'] ); } + /** + * @covers WP_REST_Font_Faces_Controller::get_items + */ + public function test_get_items_by_slug() { + $font_family = get_post( self::$font_family_id2 ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/font-families' ); + $request->set_param( 'slug', $font_family->post_name ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertSame( 200, $response->get_status() ); + $this->assertCount( 1, $data ); + $this->assertSame( $font_family->ID, $data[0]['id'] ); + } + /** * @covers WP_REST_Font_Faces_Controller::get_items */ @@ -421,7 +438,7 @@ public function test_create_item_with_duplicate_slug() { $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_duplicate_font_family', $response, 409 ); + $this->assertErrorResponse( 'rest_duplicate_font_family', $response, 400 ); $expected_message = 'A font family with slug "helvetica" already exists.'; $message = $response->as_error()->get_error_messages()[0]; $this->assertSame( $expected_message, $message ); From aac5477eadfad787db8e4530d542307e98bbb141 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 15 Jan 2024 15:31:37 -0600 Subject: [PATCH 3/6] Disallow updating the font family slug, since it's used as a unique identifier --- ...class-wp-rest-font-families-controller.php | 11 +++- .../wpRestFontFamiliesController.php | 53 +++++++++++++------ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index 66e26f098505f5..bf36d8b37b8988 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -140,9 +140,18 @@ public function validate_font_family_settings( $value, $request ) { $schema = $this->get_item_schema()['properties']['font_family_settings']; $required = $schema['required']; - // Allow setting individual properties if we are updating an existing font family. if ( isset( $request['id'] ) ) { + // Allow sending individual properties if we are updating an existing font family. unset( $schema['required'] ); + + // But don't allow updating the slug, since it is used as a unique identifier. + if ( isset( $settings['slug'] ) ) { + return new WP_Error( + 'rest_invalid_param', + __( 'font_family_settings[slug] cannot be updated.', 'gutenberg' ), + array( 'status' => 400 ) + ); + } } // Check that the font face settings match the theme.json schema. diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 2ff0dc821e6bf9..125f710e73ea2c 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -428,7 +428,7 @@ public function test_create_item_invalid_settings_json() { } /** - * @covers WP_REST_Font_Family_Controller::validate_font_family_settings + * @covers WP_REST_Font_Family_Controller::create_item */ public function test_create_item_with_duplicate_slug() { wp_set_current_user( self::$admin_id ); @@ -478,9 +478,8 @@ public function test_create_item_no_permission() { public function test_update_item() { wp_set_current_user( self::$admin_id ); - $updated_settings = array( + $settings = array( 'name' => 'Open Sans', - 'slug' => 'open-sans', 'fontFamily' => '"Open Sans, "Noto Sans", sans-serif', 'preview' => 'https://s.w.org/images/fonts/16.9/previews/open-sans/open-sans-400-normal.svg', ); @@ -489,14 +488,21 @@ public function test_update_item() { $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . $font_family_id ); $request->set_param( 'font_family_settings', - wp_json_encode( $updated_settings ) + wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertSame( 200, $response->get_status() ); $this->check_font_family_data( $data, $font_family_id, $response->get_links() ); - $this->assertSame( $updated_settings, $data['font_family_settings'] ); + + $expected_settings = array( + 'name' => $settings['name'], + 'slug' => 'open-sans', + 'fontFamily' => $settings['fontFamily'], + 'preview' => $settings['preview'], + ); + $this->assertSame( $expected_settings, $data['font_family_settings'] ); wp_delete_post( $font_family_id, true ); } @@ -526,7 +532,6 @@ public function test_update_item_individual_settings( $settings ) { public function data_update_item_individual_settings() { return array( array( array( 'name' => 'Opened Sans' ) ), - array( array( 'slug' => 'opened-sans' ) ), array( array( 'fontFamily' => '"Opened Sans", sans-serif' ) ), array( array( 'preview' => 'https://s.w.org/images/fonts/16.7/previews/opened-sans/opened-sans-400-normal.svg' ) ), // Empty preview is allowed. @@ -564,6 +569,7 @@ public function data_update_item_santize_font_family() { /** * @dataProvider data_update_item_invalid_settings + * * @covers WP_REST_Font_Faces_Controller::update_item */ public function test_update_item_empty_settings( $settings ) { @@ -585,12 +591,6 @@ public function data_update_item_invalid_settings() { 'Wrong name type' => array( array( 'name' => 1234 ), ), - 'Empty slug' => array( - array( 'slug' => '' ), - ), - 'Wrong slug type' => array( - array( 'slug' => 1234 ), - ), 'Empty fontFamily' => array( array( 'fontFamily' => '' ), ), @@ -603,14 +603,31 @@ public function data_update_item_invalid_settings() { /** * @covers WP_REST_Font_Faces_Controller::update_item */ - public function test_update_item_invalid_font_family_id() { + public function test_update_item_update_slug_not_allowed() { wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); + $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id1 ); $request->set_param( 'font_family_settings', - wp_json_encode( self::$default_settings ) + wp_json_encode( array( 'slug' => 'new-slug' ) ) ); $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $expected_message = 'font_family_settings[slug] cannot be updated.'; + $message = $response->as_error()->get_all_error_data()[0]['params']['font_family_settings']; + $this->assertSame( $expected_message, $message ); + } + + /** + * @covers WP_REST_Font_Faces_Controller::update_item + */ + public function test_update_item_invalid_font_family_id() { + $settings = array_diff_key( self::$default_settings, array( 'slug' => '' ) ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); + $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); + $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); } @@ -618,15 +635,17 @@ public function test_update_item_invalid_font_family_id() { * @covers WP_REST_Font_Faces_Controller::update_item */ public function test_update_item_no_permission() { + $settings = array_diff_key( self::$default_settings, array( 'slug' => '' ) ); + wp_set_current_user( 0 ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id1 ); - $request->set_param( 'font_family_settings', wp_json_encode( self::$default_settings ) ); + $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id1 ); - $request->set_param( 'font_family_settings', wp_json_encode( self::$default_settings ) ); + $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 ); } From 33b901059f5ef4ad76ec78b58b86bd14b43602b9 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 15 Jan 2024 15:46:18 -0600 Subject: [PATCH 4/6] Fix indenting --- .../class-wp-rest-font-families-controller.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index bf36d8b37b8988..87c9f1d3588aa6 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -213,13 +213,13 @@ public function create_item( $request ) { $settings = $request->get_param( 'font_family_settings' ); // Check that the font family slug is unique. - $existing_font_family = get_posts( - array( - 'post_type' => $this->post_type, - 'posts_per_page' => 1, - 'name' => $settings['slug'], - ) - ); + $existing_font_family = get_posts( + array( + 'post_type' => $this->post_type, + 'posts_per_page' => 1, + 'name' => $settings['slug'], + ) + ); if ( ! empty( $existing_font_family ) ) { return new WP_Error( 'rest_duplicate_font_family', @@ -229,7 +229,7 @@ public function create_item( $request ) { ); } - return parent::create_item( $request ); + return parent::create_item( $request ); } /** From cdadbfa18b07d7e0d6f4cdb38bdb697508938066 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 15 Jan 2024 18:32:40 -0600 Subject: [PATCH 5/6] Removes duplicate font information from post content. --- .../class-wp-rest-font-faces-controller.php | 39 +++++++----- ...class-wp-rest-font-families-controller.php | 61 +++++++++++-------- .../wpRestFontFacesController.php | 2 +- .../wpRestFontFamiliesController.php | 23 +++++-- 4 files changed, 80 insertions(+), 45 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php index 4a5d1914095482..7f4fe653a28548 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-faces-controller.php @@ -381,20 +381,7 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V $data['id'] = $item->ID; $data['theme_json_version'] = 2; $data['parent'] = $item->post_parent; - - $settings = json_decode( $item->post_content, true ); - $properties = $this->get_item_schema()['properties']['font_face_settings']['properties']; - - // Provide required, empty settings if the post_content is not valid JSON. - if ( null === $settings ) { - $settings = array( - 'fontFamily' => '', - 'src' => array(), - ); - } - - // Only return the properties defined in the schema. - $data['font_face_settings'] = array_intersect_key( $settings, $properties ); + $data['font_face_settings'] = $this->get_settings_from_post( $item ); $response = rest_ensure_response( $data ); $links = $this->prepare_links( $item ); @@ -748,4 +735,28 @@ protected function relative_fonts_path( $path ) { return $new_path; } + + /** + * Gets the font face's settings from the post. + * + * @since 6.5.0 + * + * @param WP_Post $post Font face post object. + * @return array Font face settings array. + */ + protected function get_settings_from_post( $post ) { + $settings = json_decode( $post->post_content, true ); + $properties = $this->get_item_schema()['properties']['font_face_settings']['properties']; + + // Provide required, empty settings if needed. + if ( null === $settings ) { + $settings = array( + 'src' => array(), + ); + } + $settings['fontFamily'] = $post->post_title ?? ''; + + // Only return the properties defined in the schema. + return array_intersect_key( $settings, $properties ); + } } diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index 87c9f1d3588aa6..89bc88020e3330 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -277,25 +277,10 @@ public function delete_item( $request ) { public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- required by parent class $data = array(); - $data['id'] = $item->ID; - $data['theme_json_version'] = 2; - $data['font_faces'] = $this->get_font_face_ids( $item->ID ); - - $settings = json_decode( $item->post_content, true ); - $properties = $this->get_item_schema()['properties']['font_family_settings']['properties']; - - // Provide empty settings if the post_content is not valid JSON. - if ( null === $settings ) { - $settings = array( - 'name' => '', - 'slug' => '', - 'fontFamily' => '', - 'preview' => '', - ); - } - - // Only return the properties defined in the schema. - $data['font_family_settings'] = array_intersect_key( $settings, $properties ); + $data['id'] = $item->ID; + $data['theme_json_version'] = 2; + $data['font_faces'] = $this->get_font_face_ids( $item->ID ); + $data['font_family_settings'] = $this->get_settings_from_post( $item ); $response = rest_ensure_response( $data ); $links = $this->prepare_links( $item ); @@ -499,9 +484,10 @@ protected function prepare_font_face_links( $font_family_id ) { */ protected function prepare_item_for_database( $request ) { $prepared_post = new stdClass(); - // Settings have already been decoded by sanitize_font_family_settings(). + // Settings have already been decoded by ::sanitize_font_family_settings(). $settings = $request->get_param( 'font_family_settings' ); + // This is an update and we merge with the existing font family. if ( isset( $request['id'] ) ) { $existing_post = $this->get_post( $request['id'] ); if ( is_wp_error( $existing_post ) ) { @@ -509,16 +495,41 @@ protected function prepare_item_for_database( $request ) { } $prepared_post->ID = $existing_post->ID; - $existing_settings = json_decode( $existing_post->post_content, true ); + $existing_settings = $this->get_settings_from_post( $existing_post ); $settings = array_merge( $existing_settings, $settings ); } - $prepared_post->post_type = $this->post_type; - $prepared_post->post_status = 'publish'; - $prepared_post->post_title = $settings['name']; - $prepared_post->post_name = sanitize_title( $settings['slug'] ); + $prepared_post->post_type = $this->post_type; + $prepared_post->post_status = 'publish'; + $prepared_post->post_title = $settings['name']; + $prepared_post->post_name = sanitize_title( $settings['slug'] ); + + // Remove duplicate information from settings. + unset( $settings['name'] ); + unset( $settings['slug'] ); + $prepared_post->post_content = wp_json_encode( $settings ); return $prepared_post; } + + /** + * Gets the font family's settings from the post. + * + * @since 6.5.0 + * + * @param WP_Post $post Font family post object. + * @return array Font family settings array. + */ + protected function get_settings_from_post( $post ) { + $settings_json = json_decode( $post->post_content, true ); + + // Default to empty strings if the settings are missing. + return array( + 'name' => $post->post_title ?? '', + 'slug' => $post->post_name ?? '', + 'fontFamily' => $settings_json['fontFamily'] ?? '', + 'preview' => $settings_json['preview'] ?? '', + ); + } } diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index 9b5b596e41ac63..d248394e611dbf 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -200,8 +200,8 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { ); $empty_settings = array( - 'fontFamily' => '', 'src' => array(), + 'fontFamily' => '', ); wp_set_current_user( self::$admin_id ); diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 125f710e73ea2c..c538ed0dc64dfb 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -88,7 +88,12 @@ public static function create_font_family_post( $settings = array() ) { 'post_status' => 'publish', 'post_title' => $settings['name'], 'post_name' => $settings['slug'], - 'post_content' => wp_json_encode( $settings ), + 'post_content' => wp_json_encode( + array( + 'fontFamily' => $settings['fontFamily'], + 'preview' => $settings['preview'], + ) + ), ) ) ); @@ -221,7 +226,8 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { $empty_settings = array( 'name' => '', - 'slug' => '', + // Slug will default to the post id. + 'slug' => (string) $font_family_id, 'fontFamily' => '', 'preview' => '', ); @@ -484,7 +490,7 @@ public function test_update_item() { 'preview' => 'https://s.w.org/images/fonts/16.9/previews/open-sans/open-sans-400-normal.svg', ); - $font_family_id = self::create_font_family_post(); + $font_family_id = self::create_font_family_post( array( 'slug' => 'open-sans-update' ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . $font_family_id ); $request->set_param( 'font_family_settings', @@ -498,7 +504,7 @@ public function test_update_item() { $expected_settings = array( 'name' => $settings['name'], - 'slug' => 'open-sans', + 'slug' => 'open-sans-update', 'fontFamily' => $settings['fontFamily'], 'preview' => $settings['preview'], ); @@ -768,7 +774,14 @@ protected function check_font_family_data( $data, $post_id, $links ) { $this->assertSame( $font_face_ids, $data['font_faces'] ); $this->assertArrayHasKey( 'font_family_settings', $data ); - $this->assertSame( $post->post_content, wp_json_encode( $data['font_family_settings'] ) ); + $settings = $data['font_family_settings']; + $expected_settings = array( + 'name' => $post->post_title, + 'slug' => $post->post_name, + 'fontFamily' => $settings['fontFamily'], + 'preview' => $settings['preview'], + ); + $this->assertSame( $expected_settings, $settings ); $this->assertNotEmpty( $links ); $this->assertSame( rest_url( 'wp/v2/font-families/' . $post->ID ), $links['self'][0]['href'] ); From 41a16da25b56bf5d53d61ef9d1b5f64509e06f66 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Tue, 16 Jan 2024 10:14:50 -0600 Subject: [PATCH 6/6] Use sensible font family slugs in tests --- .../font-library/wpRestFontFamiliesController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index c538ed0dc64dfb..1f7e86ab92fe59 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -272,7 +272,7 @@ public function test_get_item_no_permission() { * @covers WP_REST_Font_Faces_Controller::create_item */ public function test_create_item() { - $settings = array_merge( self::$default_settings, array( 'slug' => 'test_create_item' ) ); + $settings = array_merge( self::$default_settings, array( 'slug' => 'open-sans-2' ) ); wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); @@ -295,7 +295,7 @@ public function test_create_item() { * @covers WP_REST_Font_Faces_Controller::validate_create_font_face_request */ public function test_create_item_default_theme_json_version() { - $settings = array_merge( self::$default_settings, array( 'slug' => 'test_create_item_theme_json' ) ); + $settings = array_merge( self::$default_settings, array( 'slug' => 'open-sans-2' ) ); wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); @@ -356,7 +356,7 @@ public function test_create_item_with_default_preview( $settings ) { public function data_create_item_with_default_preview() { $default_settings = array( 'name' => 'Open Sans', - 'slug' => 'create_item_with_default_preview', + 'slug' => 'open-sans-2', 'fontFamily' => '"Open Sans", sans-serif', ); return array( @@ -454,7 +454,7 @@ public function test_create_item_with_duplicate_slug() { * @covers WP_REST_Font_Faces_Controller::create_item */ public function test_create_item_no_permission() { - $settings = array_merge( self::$default_settings, array( 'slug' => 'test_create_item_no_permissions' ) ); + $settings = array_merge( self::$default_settings, array( 'slug' => 'open-sans-2' ) ); wp_set_current_user( 0 ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); @@ -490,7 +490,7 @@ public function test_update_item() { 'preview' => 'https://s.w.org/images/fonts/16.9/previews/open-sans/open-sans-400-normal.svg', ); - $font_family_id = self::create_font_family_post( array( 'slug' => 'open-sans-update' ) ); + $font_family_id = self::create_font_family_post( array( 'slug' => 'open-sans-2' ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . $font_family_id ); $request->set_param( 'font_family_settings', @@ -504,7 +504,7 @@ public function test_update_item() { $expected_settings = array( 'name' => $settings['name'], - 'slug' => 'open-sans-update', + 'slug' => 'open-sans-2', 'fontFamily' => $settings['fontFamily'], 'preview' => $settings['preview'], );