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..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 ) { @@ -137,7 +138,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' ), 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..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 @@ -340,4 +340,141 @@ public function data_install_and_uninstall_fonts() { ), ); } + + /** + * Tests failure when fonfaces has improper inputs + * + * @covers ::install_fonts + * + * @dataProvider data_install_with_improper_inputs + * + * @param array $font_families Font families to install in theme.json format. + * @param array $files Font files to install. + */ + public function test_install_with_improper_inputs( $font_families, $files = array() ) { + wp_set_current_user( self::$admin_id ); + + $install_request = new WP_REST_Request( 'POST', '/wp/v2/fonts' ); + $font_families_json = json_encode( $font_families ); + $install_request->set_param( 'fontFamilies', $font_families_json ); + $install_request->set_file_params( $files ); + + $response = rest_get_server()->dispatch( $install_request ); + $this->assertSame( 400, $response->get_status() ); + } + + /** + * 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 property' => array( + 'font_families' => array( + array( + 'fontFamily' => 'Piazzolla', + 'name' => 'Piazzolla', + 'slug' => 'piazzolla', + 'fontFace' => 'This is not an array', + ), + ), + ), + + 'with empty font face property' => 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 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', + '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', + ), + ), + ), + ), + ), + ); + } }