Skip to content

Commit

Permalink
Fix typo and add tests for fonts install endpoint (#53644)
Browse files Browse the repository at this point in the history
* Fix typo

Co-authored-by: Anton Vlasenko <[email protected]>

* fixing get validations error logic

* adding tests to specify when the endpoit should respond with 400 (bad request) code

* deleting unused variable

* validate if all the referenced files exists in the request

* adding comments and format

* Formatting and Core parity improvements.

* Fix phpcs alignment in tests

* Fix spacing whoopsie in tests

---------

Co-authored-by: Anton Vlasenko <[email protected]>
Co-authored-by: Tonya Mork <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2023
1 parent e8cf0c4 commit f3fa02b
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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' ),
Expand Down
137 changes: 137 additions & 0 deletions phpunit/fonts-library/class-wp-rest-fonts-library-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
),
),
),
);
}
}

0 comments on commit f3fa02b

Please sign in to comment.