Skip to content
Prev Previous commit
Next Next commit
adding tests to specify when the endpoit should respond with 400 (bad…
… request) code
  • Loading branch information
matiasbenedetto committed Aug 14, 2023
commit 7bfeb774f5d87e97408c757b0bbb065c80ddf358
109 changes: 109 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,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',
),
),
),
),
),

);
}


}