mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-05 23:27:04 +00:00
Editor: Ensure font collection metadata can be properly localized.
Updates `wp_register_font_collection()` and `WP_Font_Collection` so that only font families can be loaded from a file or URL. All metadata, such as name, description, and list of font categories, needs to be passed directly in PHP so that it can be properly localized. Props swissspidy, mmaattiiaass, grantmkin, youknowriad. Fixes #60509. git-svn-id: https://develop.svn.wordpress.org/trunk@57686 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -40,11 +40,17 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
$mock_file = wp_tempnam( 'my-collection-data-' );
|
||||
file_put_contents( $mock_file, wp_json_encode( $config ) );
|
||||
|
||||
$collection = new WP_Font_Collection( $slug, $mock_file );
|
||||
$collection = new WP_Font_Collection(
|
||||
$slug,
|
||||
array_merge(
|
||||
$config,
|
||||
array( 'font_families' => $mock_file )
|
||||
)
|
||||
);
|
||||
$data = $collection->get_data();
|
||||
|
||||
$this->assertSame( $slug, $collection->slug, 'The slug should match.' );
|
||||
$this->assertSame( $expected_data, $data, 'The collection data should match.' );
|
||||
$this->assertEqualSetsWithIndex( $expected_data, $data, 'The collection data should match.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,13 +64,21 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
add_filter( 'pre_http_request', array( $this, 'mock_request' ), 10, 3 );
|
||||
|
||||
self::$mock_collection_data = $config;
|
||||
$collection = new WP_Font_Collection( $slug, 'https://example.com/fonts/mock-font-collection.json' );
|
||||
$collection = new WP_Font_Collection(
|
||||
$slug,
|
||||
array_merge(
|
||||
$config,
|
||||
array(
|
||||
'font_families' => 'https://example.com/fonts/mock-font-collection.json',
|
||||
)
|
||||
)
|
||||
);
|
||||
$data = $collection->get_data();
|
||||
|
||||
remove_filter( 'pre_http_request', array( $this, 'mock_request' ) );
|
||||
|
||||
$this->assertSame( $slug, $collection->slug, 'The slug should match.' );
|
||||
$this->assertSame( $expected_data, $data, 'The collection data should match.' );
|
||||
$this->assertEqualSetsWithIndex( $expected_data, $data, 'The collection data should match.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +88,6 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
*/
|
||||
public function data_create_font_collection() {
|
||||
return array(
|
||||
|
||||
'font collection with required data' => array(
|
||||
'slug' => 'my-collection',
|
||||
'config' => array(
|
||||
@@ -185,7 +198,6 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@@ -202,8 +214,8 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
|
||||
$this->assertWPError( $data, 'Error is not returned when property is missing or invalid.' );
|
||||
$this->assertSame(
|
||||
$data->get_error_code(),
|
||||
'font_collection_missing_property',
|
||||
$data->get_error_code(),
|
||||
'Incorrect error code when property is missing or invalid.'
|
||||
);
|
||||
}
|
||||
@@ -243,13 +255,19 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
public function test_should_error_with_invalid_json_file_path() {
|
||||
$this->setExpectedIncorrectUsage( 'WP_Font_Collection::load_from_json' );
|
||||
|
||||
$collection = new WP_Font_Collection( 'my-collection', 'non-existing.json' );
|
||||
$collection = new WP_Font_Collection(
|
||||
'my-collection',
|
||||
array(
|
||||
'name' => 'My collection',
|
||||
'font_families' => 'non-existing.json',
|
||||
)
|
||||
);
|
||||
$data = $collection->get_data();
|
||||
|
||||
$this->assertWPError( $data, 'Error is not returned when invalid file path is provided.' );
|
||||
$this->assertSame(
|
||||
$data->get_error_code(),
|
||||
'font_collection_json_missing',
|
||||
$data->get_error_code(),
|
||||
'Incorrect error code when invalid file path is provided.'
|
||||
);
|
||||
}
|
||||
@@ -258,15 +276,21 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
$mock_file = wp_tempnam( 'my-collection-data-' );
|
||||
file_put_contents( $mock_file, 'invalid-json' );
|
||||
|
||||
$collection = new WP_Font_Collection( 'my-collection', $mock_file );
|
||||
$collection = new WP_Font_Collection(
|
||||
'my-collection',
|
||||
array(
|
||||
'name' => 'Invalid collection',
|
||||
'font_families' => $mock_file,
|
||||
)
|
||||
);
|
||||
|
||||
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Testing error response returned by `load_from_json`, not the underlying error from `wp_json_file_decode`.
|
||||
$data = @$collection->get_data();
|
||||
|
||||
$this->assertWPError( $data, 'Error is not returned with invalid json file contents.' );
|
||||
$this->assertSame(
|
||||
$data->get_error_code(),
|
||||
'font_collection_decode_error',
|
||||
$data->get_error_code(),
|
||||
'Incorrect error code with invalid json file contents.'
|
||||
);
|
||||
}
|
||||
@@ -274,13 +298,19 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
public function test_should_error_with_invalid_url() {
|
||||
$this->setExpectedIncorrectUsage( 'WP_Font_Collection::load_from_json' );
|
||||
|
||||
$collection = new WP_Font_Collection( 'my-collection', 'not-a-url' );
|
||||
$collection = new WP_Font_Collection(
|
||||
'my-collection',
|
||||
array(
|
||||
'name' => 'Invalid collection',
|
||||
'font_families' => 'not-a-url',
|
||||
)
|
||||
);
|
||||
$data = $collection->get_data();
|
||||
|
||||
$this->assertWPError( $data, 'Error is not returned when invalid url is provided.' );
|
||||
$this->assertSame(
|
||||
$data->get_error_code(),
|
||||
'font_collection_json_missing',
|
||||
$data->get_error_code(),
|
||||
'Incorrect error code when invalid url is provided.'
|
||||
);
|
||||
}
|
||||
@@ -288,31 +318,43 @@ class Tests_Fonts_WpFontCollection_GetData extends WP_UnitTestCase {
|
||||
public function test_should_error_with_unsuccessful_response_status() {
|
||||
add_filter( 'pre_http_request', array( $this, 'mock_request_unsuccessful_response' ), 10, 3 );
|
||||
|
||||
$collection = new WP_Font_Collection( 'my-collection', 'https://example.com/fonts/missing-collection.json' );
|
||||
$collection = new WP_Font_Collection(
|
||||
'my-collection',
|
||||
array(
|
||||
'name' => 'Missing collection',
|
||||
'font_families' => 'https://example.com/fonts/missing-collection.json',
|
||||
)
|
||||
);
|
||||
$data = $collection->get_data();
|
||||
|
||||
remove_filter( 'pre_http_request', array( $this, 'mock_request_unsuccessful_response' ) );
|
||||
|
||||
$this->assertWPError( $data, 'Error is not returned when response is unsuccessful.' );
|
||||
$this->assertSame(
|
||||
$data->get_error_code(),
|
||||
'font_collection_request_error',
|
||||
'Incorrect error code when response is unsuccussful.'
|
||||
$data->get_error_code(),
|
||||
'Incorrect error code when response is unsuccessful.'
|
||||
);
|
||||
}
|
||||
|
||||
public function test_should_error_with_invalid_json_from_url() {
|
||||
add_filter( 'pre_http_request', array( $this, 'mock_request_invalid_json' ), 10, 3 );
|
||||
|
||||
$collection = new WP_Font_Collection( 'my-collection', 'https://example.com/fonts/invalid-collection.json' );
|
||||
$collection = new WP_Font_Collection(
|
||||
'my-collection',
|
||||
array(
|
||||
'name' => 'Invalid collection',
|
||||
'font_families' => 'https://example.com/fonts/invalid-collection.json',
|
||||
)
|
||||
);
|
||||
$data = $collection->get_data();
|
||||
|
||||
remove_filter( 'pre_http_request', array( $this, 'mock_request_invalid_json' ) );
|
||||
|
||||
$this->assertWPError( $data, 'Error is not returned when response is invalid json.' );
|
||||
$this->assertSame(
|
||||
$data->get_error_code(),
|
||||
'font_collection_decode_error',
|
||||
$data->get_error_code(),
|
||||
'Incorrect error code when response is invalid json.'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,7 +38,13 @@ class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller
|
||||
$mock_file = wp_tempnam( 'my-collection-data-' );
|
||||
file_put_contents( $mock_file, '{"name": "Mock Collection", "font_families": [ "mock" ], "categories": [ "mock" ] }' );
|
||||
|
||||
wp_register_font_collection( 'mock-col-slug', $mock_file );
|
||||
wp_register_font_collection(
|
||||
'mock-col-slug',
|
||||
array(
|
||||
'name' => 'My collection',
|
||||
'font_families' => $mock_file,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
@@ -78,7 +84,13 @@ class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller
|
||||
$this->setExpectedIncorrectUsage( 'WP_Font_Collection::load_from_json' );
|
||||
|
||||
wp_set_current_user( self::$admin_id );
|
||||
wp_register_font_collection( 'invalid-collection', 'invalid-collection-file' );
|
||||
wp_register_font_collection(
|
||||
'invalid-collection',
|
||||
array(
|
||||
'name' => 'My collection',
|
||||
'font_families' => 'invalid-collection-file',
|
||||
)
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
@@ -132,14 +144,20 @@ class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller
|
||||
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$slug = 'invalid-collection';
|
||||
wp_register_font_collection( $slug, 'invalid-collection-file' );
|
||||
wp_register_font_collection(
|
||||
$slug,
|
||||
array(
|
||||
'name' => 'My collection',
|
||||
'font_families' => 'invalid-collection-file',
|
||||
)
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/' . $slug );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
wp_unregister_font_collection( $slug );
|
||||
|
||||
$this->assertErrorResponse( 'font_collection_json_missing', $response, 500, 'When the collection json file is invalid, the response should return an error for "font_collection_json_missing" with 500 status.' );
|
||||
$this->assertErrorResponse( 'font_collection_json_missing', $response, 500 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,11 +168,11 @@ class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_cannot_read', $response, 401, 'The response status should be 401 for non-authenticated users.' );
|
||||
$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
|
||||
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_cannot_read', $response, 403, 'The response status should be 403 for users without the right permissions.' );
|
||||
$this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user