Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6ca902d
google fonts collection data provisional url
matiasbenedetto Jan 16, 2024
9e3c3f8
rename controller methods
matiasbenedetto Jan 16, 2024
78c6941
fix get_items parameters
matiasbenedetto Jan 16, 2024
399cf88
fix endpoint return
matiasbenedetto Jan 16, 2024
b600531
rafactor font collection class
matiasbenedetto Jan 17, 2024
adaebd6
fix tests for the refactored class
matiasbenedetto Jan 17, 2024
2e57945
refactor font collections rest controller
matiasbenedetto Jan 17, 2024
cc7067f
update font collection tests
matiasbenedetto Jan 17, 2024
8fe9713
update the frontend to use the new endpoint data schema
matiasbenedetto Jan 17, 2024
9f4cfe2
format php
matiasbenedetto Jan 17, 2024
8dcdcf2
Merge branch 'try/font-library-refactor' into try/font-collection-new…
matiasbenedetto Jan 17, 2024
823a580
adding linter line ignore rul
matiasbenedetto Jan 17, 2024
6dff6f1
replacing throwing an exception by calling doing_it_wrong
matiasbenedetto Jan 18, 2024
778d27b
add translation marks
matiasbenedetto Jan 18, 2024
3c011c8
user ternary operator
matiasbenedetto Jan 18, 2024
e698680
correct translation formatting and comments
matiasbenedetto Jan 18, 2024
3a5b423
renaming function
matiasbenedetto Jan 19, 2024
6f9463b
renaming tests
matiasbenedetto Jan 19, 2024
9d1d78b
improve url matching
matiasbenedetto Jan 19, 2024
ed27754
return error without rest_ensure_response
matiasbenedetto Jan 19, 2024
ae2f519
Merge branch 'try/font-collection-new-schema' of github.com:WordPress…
matiasbenedetto Jan 19, 2024
21b21d5
fix contradictory if condition
matiasbenedetto Jan 19, 2024
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
update font collection tests
  • Loading branch information
matiasbenedetto committed Jan 17, 2024
commit cc7067f40ca8164a379510e5c357db8fb67c65a2
117 changes: 0 additions & 117 deletions phpunit/tests/fonts/font-library/wpFontCollection/getConfigAndData.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function test_should_return_error_if_name_is_missing() {
public function test_should_return_error_if_config_is_empty() {
$config = array();
$this->expectException( 'Exception' );
$this->expectExceptionMessage( 'Font Collection config options is required as a non-empty array.' );
$this->expectExceptionMessage( 'Font Collection config options are required as a non-empty array.' );
WP_Font_Library::register_font_collection( $config );
}

Expand Down
162 changes: 162 additions & 0 deletions phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
/**
* Unit tests covering WP_REST_Font_Collections_Controller functionality.
*
* @package WordPress
* @subpackage REST_API
* @since 6.5.0
*
* @group restapi
*
* @coversDefaultClass WP_REST_Font_Collections_Controller
*/
class WP_REST_Font_Collections_Controller_Test extends WP_Test_REST_Controller_Testcase {
protected static $admin_id;
protected static $editor_id;
protected static $mock_file;


public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
self::$editor_id = $factory->user->create(
array(
'role' => 'editor',
)
);
$mock_file = wp_tempnam( 'my-collection-data-' );
file_put_contents( $mock_file, '{"font_families": [ "mock" ], "categories": [ "mock" ] }' );

wp_register_font_collection( array(
'name' => 'My Collection',
'slug' => 'mock-col-slug',
'src' => $mock_file,
) );
}

public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
self::delete_user( self::$editor_id );
wp_unregister_font_collection( 'mock-col-slug' );
}


/**
* @covers WP_REST_Font_Collections_Controller::register_routes
*/
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertCount( 1, $routes['/wp/v2/font-collections'], 'Rest server has not the collections path initialized.' );
$this->assertCount( 1, $routes['/wp/v2/font-collections/(?P<slug>[\/\w-]+)'], 'Rest server has not the collection path initialized.' );

$this->assertArrayHasKey( 'GET', $routes['/wp/v2/font-collections'][0]['methods'], 'Rest server has not the GET method for collections intialized.' );
$this->assertArrayHasKey( 'GET', $routes['/wp/v2/font-collections/(?P<slug>[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' );
}

/**
* @covers WP_REST_Font_Collections_Controller::get_items
*/
public function test_get_items() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections' );
$response = rest_get_server()->dispatch( $request );
$content = $response->get_data();
$this->assertIsArray( $content );
$this->assertEquals( 200, $response->get_status() );
}

/**
* @covers WP_REST_Font_Collections_Controller::get_item
*/
public function test_get_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/mock-col-slug' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status(), 'Response code is not 200' );

$response_data = $response->get_data();
$this->assertArrayHasKey( 'name', $response_data, 'Response data does not have the name key.' );
$this->assertArrayHasKey( 'slug', $response_data, 'Response data does not have the slug key.' );
$this->assertArrayHasKey( 'description', $response_data, 'Response data does not have the description key.' );
$this->assertArrayHasKey( 'font_families', $response_data, 'Response data does not have the font_families key.' );
$this->assertArrayHasKey( 'categories', $response_data, 'Response data does not have the categories key.' );

$this->assertIsString( $response_data['name'], 'name is not a string.' );
$this->assertIsString( $response_data['slug'], 'slug is not a string.' );
$this->assertIsString( $response_data['description'], 'description is not a string.' );

$this->assertIsArray( $response_data['font_families'], 'font_families is not an array.' );
$this->assertIsArray( $response_data['categories'], 'categories is not an array.' );
}

/**
* @covers WP_REST_Font_Collections_Controller::get_item
*/
public function test_get_item_invalid_slug() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/non-existing-collection' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'font_collection_not_found', $response, 404 );
}

/**
* @covers WP_REST_Font_Collections_Controller::get_item
*/
public function test_get_item_invalid_id_permission() {
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/mock-col-slug' );

wp_set_current_user( 0 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 401, 'Response code should be 401 for non-authenticated users.' );

wp_set_current_user( self::$editor_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 403, 'Response code should be 403 for users without the right permissions.' );
}

/**
* @doesNotPerformAssertions
*/
public function test_context_param() {
// Controller does not use get_context_param().
}

/**
* @doesNotPerformAssertions
*/
public function test_create_item() {
// Controller does not use test_create_item().
}

/**
* @doesNotPerformAssertions
*/
public function test_update_item() {
// Controller does not use test_update_item().
}

/**
* @doesNotPerformAssertions
*/
public function test_delete_item() {
// Controller does not use test_delete_item().
}

/**
* @doesNotPerformAssertions
*/
public function test_prepare_item() {
// Controller does not use test_prepare_item().
}

/**
* @doesNotPerformAssertions
*/
public function test_get_item_schema() {
// Controller does not use test_get_item_schema().
}

}

This file was deleted.

Loading