Skip to content

Commit

Permalink
rename the route
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Jul 26, 2022
1 parent 3a035bd commit 13563e2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public function register_routes() {
// Get fallback template content.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/fallback_content',
'/' . $this->rest_base . '/lookup',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_template_fallback_content' ),
'callback' => array( $this, 'get_template_fallback' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'slug' => array(
Expand All @@ -39,13 +39,13 @@ public function register_routes() {
}

/**
* Returns the fallback template content for a given slug.
* Returns the fallback template for a given slug.
*
* @param WP_REST_Request $request The request instance.
*
* @return WP_REST_Response|WP_Error
*/
public function get_template_fallback_content( $request ) {
public function get_template_fallback( $request ) {
if ( empty( $request['slug'] ) ) {
return new WP_Error(
'rest_invalid_param',
Expand All @@ -55,7 +55,7 @@ public function get_template_fallback_content( $request ) {
}
$template_hierarchy = gutenberg_get_template_hierarchy( $request['slug'] );
$fallback_template = resolve_block_template( $request['slug'], $template_hierarchy, '' );
return rest_ensure_response( $fallback_template->content );
return rest_ensure_response( $fallback_template );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ export default function NewTemplate( { postType } ) {
let templateContent = template.content;
// Try to find fallback content from existing templates.
if ( ! templateContent ) {
templateContent = await apiFetch( {
path: addQueryArgs( '/wp/v2/templates/fallback_content', {
const fallbackTemplate = await apiFetch( {
path: addQueryArgs( '/wp/v2/templates/lookup', {
slug,
} ),
} );
templateContent = fallbackTemplate.content;
}
const newTemplate = await saveEntityRecord(
'postType',
Expand Down
13 changes: 6 additions & 7 deletions phpunit/class-gutenberg-rest-templates-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class Gutenberg_REST_Templates_Controller_Test extends WP_Test_REST_Controller_Testcase {
/**
* @var int
* @group something
*/
protected static $admin_id;

Expand All @@ -28,32 +27,32 @@ public static function wpSetupBeforeClass( $factory ) {
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey(
'/wp/v2/templates/fallback_content',
'/wp/v2/templates/lookup',
$routes,
'Get template fallback content route does not exist'
);
}

public function test_get_template_fallback_content() {
public function test_get_template_fallback() {
$base_path = gutenberg_dir_path() . 'test/emptytheme/block-templates/';
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/fallback_content' );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
// Should match `category.html`.
$request->set_param( 'slug', 'category-fruits' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = $response->get_data()->content;
$expected = file_get_contents( $base_path . 'category.html' );
$this->assertEquals( $expected, $data );
// Should fallback to `index.html` .
$request->set_param( 'slug', 'tag-status' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = $response->get_data()->content;
$expected = file_get_contents( $base_path . 'index.html' );
$this->assertEquals( $expected, $data );
// Should fallback to `singular.html` .
$request->set_param( 'slug', 'page-hello' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = $response->get_data()->content;
$expected = file_get_contents( $base_path . 'singular.html' );
$this->assertEquals( $expected, $data );
}
Expand Down

0 comments on commit 13563e2

Please sign in to comment.