Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Sep 9, 2022
1 parent 3692d4d commit a3b7df7
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 72 deletions.
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 All @@ -578,10 +578,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 @@ -680,13 +676,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 @@ -11573,6 +11647,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Posts",
"slug": "post",
"icon": "dashicons-admin-post",
"taxonomies": [
"category",
"post_tag"
Expand Down Expand Up @@ -11604,6 +11679,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 @@ -11632,6 +11708,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 @@ -11660,6 +11737,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Navigation Menu Items",
"slug": "nav_menu_item",
"icon": null,
"taxonomies": [
"nav_menu"
],
Expand Down Expand Up @@ -11690,6 +11768,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 @@ -11718,6 +11797,7 @@ mockedApiResponse.TypesCollection = {
"hierarchical": false,
"name": "Templates",
"slug": "wp_template",
"icon": null,
"taxonomies": [],
"rest_base": "templates",
"rest_namespace": "wp/v2",
Expand Down Expand Up @@ -11746,6 +11826,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 @@ -11774,6 +11855,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 @@ -11804,6 +11886,7 @@ mockedApiResponse.TypeModel = {
"hierarchical": false,
"name": "Posts",
"slug": "post",
"icon": "dashicons-admin-post",
"taxonomies": [
"category",
"post_tag"
Expand Down

0 comments on commit a3b7df7

Please sign in to comment.