Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 tests
  • Loading branch information
ntsekouras authored and hellofromtonya committed Sep 20, 2022
commit f1958f43406d653de05c3c60946604532a9a5c72
15 changes: 2 additions & 13 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ function _build_block_template_result_from_post( $post ) {
}

$theme = $terms[0]->name;
$template_file = _get_block_template_file( $post->post_type, $post->post_name );
$has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file;
$has_theme_file = wp_get_theme()->get_stylesheet() === $theme &&
null !== _get_block_template_file( $post->post_type, $post->post_name );

$origin = get_post_meta( $post->ID, 'origin', true );
$is_wp_suggestion = get_post_meta( $post->ID, 'is_wp_suggestion', true );
Expand Down Expand Up @@ -582,10 +582,6 @@ function _build_block_template_result_from_post( $post ) {
$template->is_custom = false;
}

if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
$template->post_types = $template_file['postTypes'];
}

if ( 'wp_template_part' === $post->post_type ) {
$type_terms = get_the_terms( $post, 'wp_template_part_area' );
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
Expand Down Expand Up @@ -684,13 +680,6 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
continue;
}

if ( $post_type &&
isset( $template->post_types ) &&
! in_array( $post_type, $template->post_types, true )
) {
continue;
}

$query_result[] = $template;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ public function register_routes() {
)
);

// Get fallback template content.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/lookup',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_template_fallback' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'slug' => array(
'description' => __( 'The slug of the template to get the fallback for' ),
'type' => 'string',
),
'is_custom' => array(
'description' => __( ' Indicates if a template is custom or part of the template hierarchy' ),
'type' => 'boolean',
),
'template_prefix' => array(
'description' => __( 'The template prefix for the created template. This is used to extract the main template type ex. in `taxonomy-books` we extract the `taxonomy`' ),
'type' => 'string',
),
),
),
)
);

// Lists/updates a single template based on the given id.
register_rest_route(
$this->namespace,
Expand Down Expand Up @@ -116,33 +143,6 @@ public function register_routes() {
'schema' => array( $this, 'get_public_item_schema' ),
)
);

// Get fallback template content.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/lookup',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_template_fallback' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'slug' => array(
'description' => __( 'The slug of the template to get the fallback for' ),
'type' => 'string',
),
'is_custom' => array(
'description' => __( ' Indicates if a template is custom or part of the template hierarchy' ),
'type' => 'boolean',
),
'template_prefix' => array(
'description' => __( 'The template prefix for the created template. This is used to extract the main template type ex. in `taxonomy-books` we extract the `taxonomy`' ),
'type' => 'string',
),
),
),
)
);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/tests/rest-api/rest-post-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 12, $properties );
$this->assertCount( 13, $properties );
$this->assertArrayHasKey( 'capabilities', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'hierarchical', $properties );
Expand All @@ -174,6 +174,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'rest_base', $properties );
$this->assertArrayHasKey( 'rest_namespace', $properties );
$this->assertArrayHasKey( 'visibility', $properties );
$this->assertArrayHasKey( 'icon', $properties );
}

public function test_get_additional_field_registration() {
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ public function test_expected_routes_in_schema() {
'/wp/v2/template-parts/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/template-parts/(?P<parent>[\d]+)/revisions',
'/wp/v2/template-parts/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/template-parts/lookup',
'/wp/v2/templates',
'/wp/v2/templates/(?P<id>[\d]+)/autosaves',
'/wp/v2/templates/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w-]+)',
'/wp/v2/templates/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/templates/(?P<parent>[\d]+)/revisions',
'/wp/v2/templates/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/templates/lookup',
'/wp/v2/themes',
'/wp/v2/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
'/wp/v2/plugins',
Expand Down
48 changes: 17 additions & 31 deletions tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,35 +688,21 @@ protected function find_and_normalize_template_by_id( $templates, $id ) {
* @ticket 56467
* @covers WP_REST_Templates_Controller::get_template_fallback
*/
// TODO: check how to make this test work..
// https://github.com/WordPress/gutenberg/pull/42520/files#diff-5dcd7f0ba0e053098e93f6907913da73787fb7487e05d37e3249fb1c0ec434d6
// 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/lookup' );
// // Should match `category.html`.
// $request->set_param( 'slug', 'category-fruits' );
// $request->set_param( 'is_custom', false );
// $request->set_param( 'template_prefix', 'category' );
// $response = rest_get_server()->dispatch( $request );
// $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' );
// $request->set_param( 'is_custom', false );
// $request->set_param( 'template_prefix', 'tag' );
// $response = rest_get_server()->dispatch( $request );
// $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' );
// $request->set_param( 'is_custom', false );
// $request->set_param( 'template_prefix', 'page' );
// $response = rest_get_server()->dispatch( $request );
// $data = $response->get_data()->content;
// $expected = file_get_contents( $base_path . 'singular.html' );
// $this->assertEquals( $expected, $data );
// }
public function test_get_template_fallback() {
wp_set_current_user( self::$admin_id );
switch_theme( 'block-theme' );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
// Should fallback to `index.html` .
$request->set_param( 'slug', 'tag-status' );
$request->set_param( 'is_custom', false );
$request->set_param( 'template_prefix', 'tag' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 'index', $response->get_data()->slug );
// Should fallback to `page.html` .
$request->set_param( 'slug', 'page-hello' );
$request->set_param( 'is_custom', false );
$request->set_param( 'template_prefix', 'page' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 'page', $response->get_data()->slug );
}
}
83 changes: 83 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -5140,6 +5140,43 @@ mockedApiResponse.Schema = {
]
}
},
"/wp/v2/templates/lookup": {
"namespace": "wp/v2",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"slug": {
"description": "The slug of the template to get the fallback for",
"type": "string",
"required": false
},
"is_custom": {
"description": " Indicates if a template is custom or part of the template hierarchy",
"type": "boolean",
"required": false
},
"template_prefix": {
"description": "The template prefix for the created template. This is used to extract the main template type ex. in `taxonomy-books` we extract the `taxonomy`",
"type": "string",
"required": false
}
}
}
],
"_links": {
"self": [
{
"href": "http://example.org/index.php?rest_route=/wp/v2/templates/lookup"
}
]
}
},
"/wp/v2/templates/(?P<id>([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w-]+)": {
"namespace": "wp/v2",
"methods": [
Expand Down Expand Up @@ -5792,6 +5829,43 @@ mockedApiResponse.Schema = {
]
}
},
"/wp/v2/template-parts/lookup": {
"namespace": "wp/v2",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"slug": {
"description": "The slug of the template to get the fallback for",
"type": "string",
"required": false
},
"is_custom": {
"description": " Indicates if a template is custom or part of the template hierarchy",
"type": "boolean",
"required": false
},
"template_prefix": {
"description": "The template prefix for the created template. This is used to extract the main template type ex. in `taxonomy-books` we extract the `taxonomy`",
"type": "string",
"required": false
}
}
}
],
"_links": {
"self": [
{
"href": "http://example.org/index.php?rest_route=/wp/v2/template-parts/lookup"
}
]
}
},
"/wp/v2/template-parts/(?P<id>([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w-]+)": {
"namespace": "wp/v2",
"methods": [
Expand Down Expand Up @@ -11591,6 +11665,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Posts",
"slug": "post",
"icon": "dashicons-admin-post",
"taxonomies": [
"category",
"post_tag"
Expand Down Expand Up @@ -11622,6 +11697,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": true,
"name": "Pages",
"slug": "page",
"icon": "dashicons-admin-page",
"taxonomies": [],
"rest_base": "pages",
"rest_namespace": "wp/v2",
Expand Down Expand Up @@ -11650,6 +11726,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Media",
"slug": "attachment",
"icon": "dashicons-admin-media",
"taxonomies": [],
"rest_base": "media",
"rest_namespace": "wp/v2",
Expand Down Expand Up @@ -11678,6 +11755,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Navigation Menu Items",
"slug": "nav_menu_item",
"icon": null,
"taxonomies": [
"nav_menu"
],
Expand Down Expand Up @@ -11708,6 +11786,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Reusable blocks",
"slug": "wp_block",
"icon": null,
"taxonomies": [],
"rest_base": "blocks",
"rest_namespace": "wp/v2",
Expand Down Expand Up @@ -11736,6 +11815,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Templates",
"slug": "wp_template",
"icon": null,
"taxonomies": [],
"rest_base": "templates",
"rest_namespace": "wp/v2",
Expand Down Expand Up @@ -11764,6 +11844,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Template Parts",
"slug": "wp_template_part",
"icon": null,
"taxonomies": [],
"rest_base": "template-parts",
"rest_namespace": "wp/v2",
Expand Down Expand Up @@ -11792,6 +11873,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Navigation Menus",
"slug": "wp_navigation",
"icon": null,
"taxonomies": [],
"rest_base": "navigation",
"rest_namespace": "wp/v2",
Expand Down Expand Up @@ -11822,6 +11904,7 @@ mockedApiResponse.TypeModel = {
"hierarchical": false,
"name": "Posts",
"slug": "post",
"icon": "dashicons-admin-post",
"taxonomies": [
"category",
"post_tag"
Expand Down