Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font Library: load collection JSON data from a URL in the collection config #54067

Merged
merged 71 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
79d6af7
Adding Font Collection class
matiasbenedetto Aug 18, 2023
fd1c967
php formatting and linting
matiasbenedetto Aug 18, 2023
0cfac6c
Adding tests for collections routes
matiasbenedetto Aug 18, 2023
6bb2f8b
Adding tests for WP_Font_Collection constructor
matiasbenedetto Aug 18, 2023
4a67d0b
adding tests for WP_Font_Collection get_data()
matiasbenedetto Aug 18, 2023
b26cb63
adding tests for WP_Font_Library register_font_collection()
matiasbenedetto Aug 18, 2023
0cfddb2
get font collection tests
matiasbenedetto Aug 21, 2023
7ba6f61
adding 'wp_' prefix to the 'register_font_collection' filter name
matiasbenedetto Aug 21, 2023
454f9a5
fix callback name
matiasbenedetto Aug 21, 2023
932064f
making class property private
matiasbenedetto Aug 21, 2023
b184398
registering filter from font-library.php file
matiasbenedetto Aug 21, 2023
6a2dfa0
Merge branch 'trunk' into try/font-collections
matiasbenedetto Aug 22, 2023
36ab188
removing superfluous comment
matiasbenedetto Aug 22, 2023
ac938c9
moving files to according changes in trunk
matiasbenedetto Aug 22, 2023
a9c7b6a
config without a json file should fail
matiasbenedetto Aug 22, 2023
b2deb14
fix property name in tests
matiasbenedetto Aug 22, 2023
4765538
name fix
matiasbenedetto Aug 23, 2023
3fb8b0a
comment update
matiasbenedetto Aug 23, 2023
3fadd9d
Adds WP_Error to return type
hellofromtonya Aug 23, 2023
bdaacf8
Improves contructor error handling.
hellofromtonya Aug 23, 2023
85fe753
FontCollection::get_data(): Improves error handling
hellofromtonya Aug 23, 2023
4ae9af6
Removes empty space for wpcs
hellofromtonya Aug 23, 2023
de053da
adding filter in a simpler way
matiasbenedetto Aug 23, 2023
21dfbe3
micro-optimization
matiasbenedetto Aug 23, 2023
fb913af
reuse WP_Error response instead of creating a new one
matiasbenedetto Aug 23, 2023
0e6c026
Eliminates try/catch
hellofromtonya Aug 23, 2023
f79212d
Revert "Eliminates try/catch" commit
hellofromtonya Aug 23, 2023
4e952e3
Remove wp_register_font_collection and replace it by a global functio…
matiasbenedetto Aug 24, 2023
196459a
adding function comment and guard agains re-declaration
matiasbenedetto Aug 24, 2023
a76d943
php format
matiasbenedetto Aug 24, 2023
c15b5ae
fixing docblock coments
matiasbenedetto Aug 24, 2023
85261b3
removing param comment
matiasbenedetto Aug 24, 2023
ad52829
re-adding parameter comment removed by mistake
matiasbenedetto Aug 24, 2023
7161ba1
format php
matiasbenedetto Aug 24, 2023
8402b91
updating WP_Font_Collection __construct tests
matiasbenedetto Aug 24, 2023
e0947c9
updating WP_Font_Collection get_data tests
matiasbenedetto Aug 24, 2023
98d52ad
updating tests
matiasbenedetto Aug 24, 2023
a6ed145
php format
matiasbenedetto Aug 24, 2023
f8dc1a0
array check
matiasbenedetto Aug 24, 2023
4cb5c77
Documents config array structure
hellofromtonya Aug 24, 2023
4b7e5e3
adding tests for /fonts/collections endpoint
matiasbenedetto Aug 24, 2023
56607c9
adding /fonts/collections/<id> endpoint test
matiasbenedetto Aug 24, 2023
c928ebe
format
matiasbenedetto Aug 24, 2023
aeb42cf
format
matiasbenedetto Aug 24, 2023
0a98de6
add test for missing collection
matiasbenedetto Aug 24, 2023
46f9fde
removing not needed variables
matiasbenedetto Aug 24, 2023
785262b
Add more tests and split the test for WP_REST_Font_Library_Controller
matiasbenedetto Aug 25, 2023
de12fe7
lint
matiasbenedetto Aug 25, 2023
dc560b8
try to create dir for tests in CI
matiasbenedetto Aug 25, 2023
60997e9
Merge branch 'trunk' into try/font-collections
matiasbenedetto Aug 25, 2023
f87f538
renane config property to src and handle URLs apart from file paths
matiasbenedetto Aug 29, 2023
f55f7a7
Add the ability to load collection JSON data from a URL
matiasbenedetto Aug 30, 2023
0df9a0e
remove unwanted comment
matiasbenedetto Aug 30, 2023
7cc1dd3
merge with trunk
matiasbenedetto Sep 5, 2023
31a6315
fix missing merge
matiasbenedetto Sep 5, 2023
7d8e6e3
fix merge with trunk
matiasbenedetto Sep 5, 2023
4eadf1a
removing test file after merge with trunk
matiasbenedetto Sep 5, 2023
08f2e48
removing duplicated test files after merge with trunk
matiasbenedetto Sep 5, 2023
ebb5b7b
Merge branch 'trunk' into try/font-collections-url
matiasbenedetto Sep 20, 2023
61568e0
decode data to make availanle in the client as json
matiasbenedetto Sep 20, 2023
678aaef
update google fonts collection source
matiasbenedetto Sep 20, 2023
a097eca
format php'
matiasbenedetto Sep 20, 2023
0022826
removing the default font collection (google fonts) json file from repo
matiasbenedetto Sep 20, 2023
bdd756f
fixing logic to get data from url
matiasbenedetto Sep 20, 2023
d51a3ff
add tests to try the font collection fetching from url
matiasbenedetto Sep 20, 2023
44acc0c
update test
matiasbenedetto Sep 20, 2023
08fc24b
format comments
matiasbenedetto Sep 20, 2023
a047eb7
replacing existing url in test with a mock url
matiasbenedetto Sep 20, 2023
241f2a6
early return
matiasbenedetto Sep 21, 2023
baa8114
remove not needed filter
matiasbenedetto Sep 21, 2023
e4d7565
rewording error message
matiasbenedetto Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get font collection tests
  • Loading branch information
matiasbenedetto committed Aug 21, 2023
commit 0cfddb268ad804c6a9d79ec9e5da734a145c04f6
17 changes: 16 additions & 1 deletion lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function register_font_collection( $id, $config ) {
}

/**
* Gets the font collections available.
* Gets all the font collections available.
*
* @since 6.4.0
*
Expand All @@ -84,6 +84,21 @@ public static function get_font_collections() {
return self::$collections;
}

/**
* Gets a font collection.
*
* @since 6.4.0
*
* @param string $id Font collection id.
* @return array List of font collections.
*/
public static function get_font_collection( $id ) {
if ( array_key_exists( $id, self::$collections ) ) {
return self::$collections[ $id ];
}
return new WP_Error( 'font_collection_not_found', 'Font collection not found.' );
}

/**
* Gets the upload directory for fonts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collection( $request ) {
$id = $request->get_param( 'id' );

Check warning on line 105 in lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space
if ( ! array_key_exists( $id, WP_Font_Library::get_font_collections() ) ) {
$collection = WP_Font_Library::get_font_collection( $id );

if ( is_wp_error( $collection ) ) {
return new WP_Error(
'font_collection_not_found',
__( 'Font collection not found.', 'gutenberg' ),
array( 'status' => 404 )
);
}

$collection = WP_Font_Library::get_font_collections()[ $id ];

Check failure on line 115 in lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Whitespace found at end of line
return new WP_REST_Response( $collection->get_data() );
}

Expand All @@ -120,7 +121,7 @@
*
* @since 6.4.0
*
* @param WP_REST_Request $request Full details about the request.

Check failure on line 124 in lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Superfluous parameter comment
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collections() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Test WP_Font_Library::get_font_collections().
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*
* @covers WP_Font_Library::get_font_collection
*/
class Tests_Fonts_WpFontLibrary_GetFontCollection extends WP_UnitTestCase {

public static function set_up_before_class() {
$my_font_collection_config = array(
'name' => 'My Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ),
);

apply_filters( 'register_font_collection', 'my-font-collection', $my_font_collection_config );
}

public function test_should_get_font_collection() {
$font_collection = WP_Font_Library::get_font_collection( 'my-font-collection' );
$this->assertInstanceOf( 'WP_Font_Collection', $font_collection );
}

public function test_should_get_no_font_collection_if_the_id_is_not_registered() {
$font_collection = WP_Font_Library::get_font_collection( 'not-registered-font-collection' );
$this->assertWPError( $font_collection );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Test WP_Font_Library::get_font_collections().
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*
* @covers WP_Font_Library::get_font_collections
*/
class Tests_Fonts_WpFontLibrary_GetFontCollections extends WP_UnitTestCase {

public static function set_up_before_class() {
$my_font_collection_config = array(
'name' => 'My Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'data_json_file' => path_join( __DIR__, 'my-font-collection-data.json' ),
);

apply_filters( 'register_font_collection', 'my-font-collection', $my_font_collection_config );

$another_font_collection_config = array(
'name' => 'Another Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'data_json_file' => path_join( __DIR__, 'another-font-collection-data.json' ),
);

apply_filters( 'register_font_collection', 'another-font-collection', $another_font_collection_config );
}

public function test_should_get_font_collections() {
$font_collections = WP_Font_Library::get_font_collections();
$this->assertNotEmpty( $font_collections, 'Sould return an array of font collections.' );
$this->assertCount( 2, $font_collections, 'Should return an array with one font collection.' );

$this->assertArrayHasKey( 'my-font-collection', $font_collections, 'The array should have the key of the registered font collection id.' );
$this->assertInstanceOf( 'WP_Font_Collection', $font_collections['my-font-collection'], 'The value of the array $font_collections[id] should be an instance of WP_Font_Collection class.' );
$this->assertArrayHasKey( 'another-font-collection', $font_collections, 'The array should have the key of the registered font collection id.' );
$this->assertInstanceOf( 'WP_Font_Collection', $font_collections['another-font-collection'], 'The value of the array $font_collections[id] should be an instance of WP_Font_Collection class.' );
}

}
Loading