From 7e34261dd291324c029745ac73849da6ee49fd88 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 11 Aug 2023 16:28:32 -0300 Subject: [PATCH 1/9] Fix typo Co-authored-by: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> --- .../fonts-library/class-wp-rest-fonts-library-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php b/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php index c49560ea32c160..51c4cfb900f4e2 100644 --- a/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php +++ b/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php @@ -137,7 +137,7 @@ private function get_validation_errors( $font_families, $files ) { ); } - if ( isset( $font_face['download_from_url'] ) && isset( $font_face['uplodaded_file'] ) ) { + if ( isset( $font_face['download_from_url'] ) && isset( $font_face['uploaded_file'] ) ) { $error_messages[] = sprintf( // translators: 1: font family index, 2: font face index. __( 'Font family [%1$s] Font face [%2$s] should have only one of the download_from_url or uploaded_file properties defined and not both.', 'gutenberg' ), From 00f8846f12362c85b5a8e7283e97b94cf0386da1 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 14 Aug 2023 13:09:33 -0300 Subject: [PATCH 2/9] fixing get validations error logic --- .../fonts-library/class-wp-rest-fonts-library-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php b/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php index 51c4cfb900f4e2..bd094c6230226c 100644 --- a/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php +++ b/lib/experimental/fonts-library/class-wp-rest-fonts-library-controller.php @@ -114,6 +114,7 @@ private function get_validation_errors( $font_families, $files ) { __( 'Font family [%s] should have fontFace property defined as an array.', 'gutenberg' ), $family_index ); + continue; } if ( count( $font_family['fontFace'] ) < 1 ) { From 7bfeb774f5d87e97408c757b0bbb065c80ddf358 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 14 Aug 2023 13:10:26 -0300 Subject: [PATCH 3/9] adding tests to specify when the endpoit should respond with 400 (bad request) code --- ...-wp-rest-fonts-library-controller-test.php | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php index ddf61aeb41ea6c..a49191aeea59be 100644 --- a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php +++ b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php @@ -340,4 +340,113 @@ public function data_install_and_uninstall_fonts() { ), ); } + + /** + * Tests failure when fonfaces has improper inputs + * + * @covers ::install_fonts + * + * @dataProvider data_install_with_improper_inputs + */ + 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 ); + $response = rest_get_server()->dispatch( $install_request ); + $data = $response->get_data(); + + $this->assertEquals( 400, $response->get_status(), 'Response status is not 400 when font face has both donwload_from_url and uploaded_file properties.' ); + } + + /** + * Data provider for test_install_with_improper_inputs + */ + public function data_install_with_improper_inputs() { + $temp_file_path1 = wp_tempnam( 'Piazzola1-' ); + file_put_contents( $temp_file_path1, 'Mocking file content' ); + + return array( + 'not a font families array' => array( + 'font_families' => 'This is not an array', + ), + + 'empty array' => array( + 'font_families' => array(), + ), + + 'without slug' => array( + 'font_families' => array( + array( + 'fontFamily' => 'Piazzolla', + 'name' => 'Piazzolla', + ), + ), + ), + + 'with improper font face propety' => array( + 'font_families' => array( + array( + 'fontFamily' => 'Piazzolla', + 'name' => 'Piazzolla', + 'slug' => 'piazzolla', + 'fontFace' => 'This is not an array', + ), + ), + ), + + 'with empty font face propety' => array( + 'font_families' => array( + array( + 'fontFamily' => 'Piazzolla', + 'name' => 'Piazzolla', + 'slug' => 'piazzolla', + 'fontFace' => array(), + ), + ), + ), + + 'fontface referencing uploaded file without uploaded files' => array( + 'font_families' => array( + array( + 'fontFamily' => 'Piazzolla', + 'name' => 'Piazzolla', + 'slug' => 'piazzolla', + 'fontFace' => array( + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + 'uploaded_file' => 'files0', + ), + ), + ), + ), + 'files' => array(), + ), + + 'fontface with incompatible properties' => array( + 'font_families' => array( + array( + 'fontFamily' => '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', + 'download_from_url' => 'http://fonts.gstatic.com/s/piazzolla/v33/N0b72SlTPu5rIkWIZjVgI-TckS03oGpPETyEJ88Rbvi0_TzOzKcQhZqx3gX9BRy5m5M.ttf', + 'uploaded_file' => 'files0', + ), + ), + ), + ), + ), + + ); + } + + } From d0e605812fdbceed634cb5c3329e901bf142c900 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 14 Aug 2023 13:19:40 -0300 Subject: [PATCH 4/9] deleting unused variable --- .../class-wp-rest-fonts-library-controller-test.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php index a49191aeea59be..2a040eb3597e7f 100644 --- a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php +++ b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php @@ -354,8 +354,6 @@ public function test_install_with_improper_inputs( $font_families, $files = arra $font_families_json = json_encode( $font_families ); $install_request->set_param( 'fontFamilies', $font_families_json ); $response = rest_get_server()->dispatch( $install_request ); - $data = $response->get_data(); - $this->assertEquals( 400, $response->get_status(), 'Response status is not 400 when font face has both donwload_from_url and uploaded_file properties.' ); } From 26157f96d7bcb8c1a1fd645629c77885282b2116 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 14 Aug 2023 13:24:37 -0300 Subject: [PATCH 5/9] validate if all the referenced files exists in the request --- ...-wp-rest-fonts-library-controller-test.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php index 2a040eb3597e7f..b21e70499e3cdd 100644 --- a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php +++ b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php @@ -353,6 +353,7 @@ public function test_install_with_improper_inputs( $font_families, $files = arra $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 ); + $install_request->set_file_params( $files ); $response = rest_get_server()->dispatch( $install_request ); $this->assertEquals( 400, $response->get_status(), 'Response status is not 400 when font face has both donwload_from_url and uploaded_file properties.' ); } @@ -423,7 +424,34 @@ public function data_install_with_improper_inputs() { 'files' => array(), ), - 'fontface with incompatible properties' => array( + 'fontface referencing uploaded file without uploaded files' => array( + 'font_families' => array( + array( + 'fontFamily' => 'Piazzolla', + 'name' => 'Piazzolla', + 'slug' => 'piazzolla', + 'fontFace' => array( + array( + 'fontFamily' => 'Piazzolla', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + 'uploaded_file' => 'files666', + ), + ), + ), + ), + 'files' => array( + 'files0' => array( + 'name' => 'piazzola1.ttf', + 'type' => 'font/ttf', + 'tmp_name' => $temp_file_path1, + 'error' => 0, + 'size' => 123, + ), + ), + ), + + 'fontface with incompatible properties (download_from_url and uploaded_file together)' => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', From a7755725c0cf25fe2f28462e3af63fb871bded71 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 14 Aug 2023 13:27:11 -0300 Subject: [PATCH 6/9] adding comments and format --- .../class-wp-rest-fonts-library-controller-test.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php index b21e70499e3cdd..a8d6cd0e72900e 100644 --- a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php +++ b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php @@ -347,6 +347,9 @@ public function data_install_and_uninstall_fonts() { * @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 ); @@ -366,15 +369,15 @@ public function data_install_with_improper_inputs() { file_put_contents( $temp_file_path1, 'Mocking file content' ); return array( - 'not a font families array' => array( + 'not a font families array' => array( 'font_families' => 'This is not an array', ), - 'empty array' => array( + 'empty array' => array( 'font_families' => array(), ), - 'without slug' => array( + 'without slug' => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', @@ -383,7 +386,7 @@ public function data_install_with_improper_inputs() { ), ), - 'with improper font face propety' => array( + 'with improper font face propety' => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', @@ -394,7 +397,7 @@ public function data_install_with_improper_inputs() { ), ), - 'with empty font face propety' => array( + 'with empty font face propety' => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', From 5ca7a920b9eb72772fe3b97cc04a178215e1e8f7 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 14 Aug 2023 11:54:31 -0500 Subject: [PATCH 7/9] Formatting and Core parity improvements. --- .../class-wp-rest-fonts-library-controller-test.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php index a8d6cd0e72900e..daf9deda9c6adb 100644 --- a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php +++ b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php @@ -348,8 +348,8 @@ public function data_install_and_uninstall_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. + * @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 ); @@ -358,7 +358,7 @@ public function test_install_with_improper_inputs( $font_families, $files = arra $install_request->set_param( 'fontFamilies', $font_families_json ); $install_request->set_file_params( $files ); $response = rest_get_server()->dispatch( $install_request ); - $this->assertEquals( 400, $response->get_status(), 'Response status is not 400 when font face has both donwload_from_url and uploaded_file properties.' ); + $this->assertSame( 400, $response->get_status(), 'Response status is not 400 when font face has both donwload_from_url and uploaded_file properties.' ); } /** @@ -386,7 +386,7 @@ public function data_install_with_improper_inputs() { ), ), - 'with improper font face propety' => array( + 'with improper font face property' => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', @@ -476,6 +476,4 @@ public function data_install_with_improper_inputs() { ); } - - } From 451ec8d8f00e9bff34819213aa2d44fb353fec64 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 14 Aug 2023 12:08:40 -0500 Subject: [PATCH 8/9] Fix phpcs alignment in tests --- .../class-wp-rest-fonts-library-controller-test.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php index daf9deda9c6adb..2c7c0ab16f00cf 100644 --- a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php +++ b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php @@ -369,15 +369,18 @@ public function data_install_with_improper_inputs() { file_put_contents( $temp_file_path1, 'Mocking file content' ); return array( - 'not a font families array' => array( + 'not a font families array' + => array( 'font_families' => 'This is not an array', ), - 'empty array' => array( + 'empty array' + => array( 'font_families' => array(), ), - 'without slug' => array( + 'without slug' + => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', @@ -397,7 +400,7 @@ public function data_install_with_improper_inputs() { ), ), - 'with empty font face propety' => array( + 'with empty font face property' => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', From fc5c00790e9321e1c4a94ecdd5eed3dc9fc6bf35 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 14 Aug 2023 12:31:23 -0500 Subject: [PATCH 9/9] Fix spacing whoopsie in tests --- ...class-wp-rest-fonts-library-controller-test.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php index 2c7c0ab16f00cf..f263194e099bc6 100644 --- a/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php +++ b/phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php @@ -353,12 +353,14 @@ public function data_install_and_uninstall_fonts() { */ 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 ); $install_request->set_file_params( $files ); + $response = rest_get_server()->dispatch( $install_request ); - $this->assertSame( 400, $response->get_status(), 'Response status is not 400 when font face has both donwload_from_url and uploaded_file properties.' ); + $this->assertSame( 400, $response->get_status() ); } /** @@ -369,18 +371,15 @@ public function data_install_with_improper_inputs() { file_put_contents( $temp_file_path1, 'Mocking file content' ); return array( - 'not a font families array' - => array( + 'not a font families array' => array( 'font_families' => 'This is not an array', ), - 'empty array' - => array( + 'empty array' => array( 'font_families' => array(), ), - 'without slug' - => array( + 'without slug' => array( 'font_families' => array( array( 'fontFamily' => 'Piazzolla', @@ -476,7 +475,6 @@ public function data_install_with_improper_inputs() { ), ), ), - ); } }