mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Block Editor: Introduce block templates for classic themes.
With this patch, users will be able to create custom block based templates and assign them to specific pages/posts. Themes can also opt-out of this feature Props bernhard-reiter, carlomanf. Fixes #53176. git-svn-id: https://develop.svn.wordpress.org/trunk@51003 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
97
tests/phpunit/tests/block-template-utils.php
Normal file
97
tests/phpunit/tests/block-template-utils.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Block_Template_Utils_Test class
|
||||
*
|
||||
* @package WordPress
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for the Block Template Loader abstraction layer.
|
||||
*/
|
||||
class Block_Template_Utils_Test extends WP_UnitTestCase {
|
||||
private static $post;
|
||||
|
||||
public static function wpSetUpBeforeClass() {
|
||||
// Set up template post.
|
||||
$args = array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'my_template',
|
||||
'post_title' => 'My Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
get_stylesheet(),
|
||||
),
|
||||
),
|
||||
);
|
||||
self::$post = self::factory()->post->create_and_get( $args );
|
||||
wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$post->ID );
|
||||
}
|
||||
|
||||
function test_build_template_result_from_post() {
|
||||
$template = _build_template_result_from_post(
|
||||
self::$post,
|
||||
'wp_template'
|
||||
);
|
||||
|
||||
$this->assertNotWPError( $template );
|
||||
$this->assertEquals( get_stylesheet() . '//my_template', $template->id );
|
||||
$this->assertEquals( get_stylesheet(), $template->theme );
|
||||
$this->assertEquals( 'my_template', $template->slug );
|
||||
$this->assertEquals( 'publish', $template->status );
|
||||
$this->assertEquals( 'custom', $template->source );
|
||||
$this->assertEquals( 'My Template', $template->title );
|
||||
$this->assertEquals( 'Description of my template', $template->description );
|
||||
$this->assertEquals( 'wp_template', $template->type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Should retrieve the template from the CPT.
|
||||
*/
|
||||
function test_get_block_template_from_post() {
|
||||
$id = get_stylesheet() . '//' . 'my_template';
|
||||
$template = get_block_template( $id, 'wp_template' );
|
||||
$this->assertEquals( $id, $template->id );
|
||||
$this->assertEquals( get_stylesheet(), $template->theme );
|
||||
$this->assertEquals( 'my_template', $template->slug );
|
||||
$this->assertEquals( 'publish', $template->status );
|
||||
$this->assertEquals( 'custom', $template->source );
|
||||
$this->assertEquals( 'wp_template', $template->type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Should retrieve block templates.
|
||||
*/
|
||||
function test_get_block_templates() {
|
||||
function get_template_ids( $templates ) {
|
||||
return array_map(
|
||||
function( $template ) {
|
||||
return $template->id;
|
||||
},
|
||||
$templates
|
||||
);
|
||||
}
|
||||
|
||||
// All results.
|
||||
$templates = get_block_templates( array(), 'wp_template' );
|
||||
$template_ids = get_template_ids( $templates );
|
||||
|
||||
// Avoid testing the entire array because the theme might add/remove templates.
|
||||
$this->assertContains( get_stylesheet() . '//' . 'my_template', $template_ids );
|
||||
|
||||
// Filter by slug.
|
||||
$templates = get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' );
|
||||
$template_ids = get_template_ids( $templates );
|
||||
$this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
|
||||
|
||||
// Filter by CPT ID.
|
||||
$templates = get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' );
|
||||
$template_ids = get_template_ids( $templates );
|
||||
$this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
|
||||
}
|
||||
}
|
||||
87
tests/phpunit/tests/block-template.php
Normal file
87
tests/phpunit/tests/block-template.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* Block_Template_Test class
|
||||
*
|
||||
* @package WordPress
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for the block template loading algorithm.
|
||||
*/
|
||||
class Block_Template_Test extends WP_UnitTestCase {
|
||||
private static $post;
|
||||
|
||||
private static $template_canvas_path = ABSPATH . WPINC . '/template-canvas.php';
|
||||
|
||||
public static function wpSetUpBeforeClass() {
|
||||
// Set up custom template post.
|
||||
$args = array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'wp-custom-template-my-block-template',
|
||||
'post_title' => 'My Custom Block Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my block template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
get_stylesheet(),
|
||||
),
|
||||
),
|
||||
);
|
||||
self::$post = self::factory()->post->create_and_get( $args );
|
||||
wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$post->ID );
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
global $_wp_current_template_content;
|
||||
unset( $_wp_current_template_content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression: https://github.com/WordPress/gutenberg/issues/31399.
|
||||
*/
|
||||
function test_custom_page_php_template_takes_precedence_over_all_other_templates() {
|
||||
$custom_page_template = 'templates/full-width.php';
|
||||
$custom_page_template_path = get_stylesheet_directory() . '/' . $custom_page_template;
|
||||
$type = 'page';
|
||||
$templates = array(
|
||||
$custom_page_template,
|
||||
'page-slug.php',
|
||||
'page-1.php',
|
||||
'page.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( $custom_page_template_path, $type, $templates );
|
||||
$this->assertEquals( $custom_page_template_path, $resolved_template_path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Covers: https://github.com/WordPress/gutenberg/pull/30438.
|
||||
*/
|
||||
function test_custom_page_block_template_takes_precedence_over_all_other_templates() {
|
||||
global $_wp_current_template_content;
|
||||
|
||||
$custom_page_block_template = 'wp-custom-template-my-block-template';
|
||||
$page_template_path = get_stylesheet_directory() . '/' . 'page.php';
|
||||
$type = 'page';
|
||||
$templates = array(
|
||||
$custom_page_block_template,
|
||||
'page-slug.php',
|
||||
'page-1.php',
|
||||
'page.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( $page_template_path, $type, $templates );
|
||||
$this->assertEquals( self::$template_canvas_path, $resolved_template_path );
|
||||
$this->assertEquals( self::$post->post_content, $_wp_current_template_content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression: https://github.com/WordPress/gutenberg/issues/31652.
|
||||
*/
|
||||
function test_template_remains_unchanged_if_templates_array_is_empty() {
|
||||
$resolved_template_path = locate_block_template( '', 'search', array() );
|
||||
$this->assertEquals( '', $resolved_template_path );
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,12 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
|
||||
'/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)',
|
||||
'/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)',
|
||||
'/wp/v2/settings',
|
||||
'/wp/v2/templates',
|
||||
'/wp/v2/templates/(?P<id>[\/\w-]+)',
|
||||
'/wp/v2/templates/(?P<id>[\d]+)/autosaves',
|
||||
'/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/themes',
|
||||
'/wp/v2/themes/(?P<stylesheet>[\w-]+)',
|
||||
'/wp/v2/plugins',
|
||||
|
||||
197
tests/phpunit/tests/rest-api/rest-templates-controller.php
Normal file
197
tests/phpunit/tests/rest-api/rest-templates-controller.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering the templates endpoint..
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
class WP_REST_Template_Controller_Test extends WP_Test_REST_Controller_Testcase {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
private static $post;
|
||||
|
||||
/**
|
||||
* Create fake data before our tests run.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
|
||||
*/
|
||||
public static function wpSetupBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
// Set up template post.
|
||||
$args = array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'my_template',
|
||||
'post_title' => 'My Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template.',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
get_stylesheet(),
|
||||
),
|
||||
),
|
||||
);
|
||||
self::$post = self::factory()->post->create_and_get( $args );
|
||||
wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$post->ID );
|
||||
}
|
||||
|
||||
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
$this->assertArrayHasKey( '/wp/v2/templates', $routes );
|
||||
$this->assertArrayHasKey( '/wp/v2/templates/(?P<id>[\/\w-]+)', $routes );
|
||||
}
|
||||
|
||||
public function test_context_param() {
|
||||
// TODO: Implement test_context_param() method.
|
||||
}
|
||||
|
||||
public function test_get_items() {
|
||||
function find_and_normalize_template_by_id( $templates, $id ) {
|
||||
foreach ( $templates as $template ) {
|
||||
if ( $template['id'] === $id ) {
|
||||
unset( $template['content'] );
|
||||
unset( $template['_links'] );
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/templates' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 );
|
||||
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/templates' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'default//my_template',
|
||||
'theme' => 'default',
|
||||
'slug' => 'my_template',
|
||||
'title' => array(
|
||||
'raw' => 'My Template',
|
||||
'rendered' => 'My Template',
|
||||
),
|
||||
'description' => 'Description of my template.',
|
||||
'status' => 'publish',
|
||||
'source' => 'custom',
|
||||
'type' => 'wp_template',
|
||||
'wp_id' => self::$post->ID,
|
||||
'has_theme_file' => false,
|
||||
),
|
||||
find_and_normalize_template_by_id( $data, 'default//my_template' )
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/default//my_template' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
unset( $data['content'] );
|
||||
unset( $data['_links'] );
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'default//my_template',
|
||||
'theme' => 'default',
|
||||
'slug' => 'my_template',
|
||||
'title' => array(
|
||||
'raw' => 'My Template',
|
||||
'rendered' => 'My Template',
|
||||
),
|
||||
'description' => 'Description of my template.',
|
||||
'status' => 'publish',
|
||||
'source' => 'custom',
|
||||
'type' => 'wp_template',
|
||||
'wp_id' => self::$post->ID,
|
||||
'has_theme_file' => false,
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
public function test_create_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'slug' => 'my_custom_template',
|
||||
'title' => 'My Template',
|
||||
'description' => 'Just a description',
|
||||
'content' => 'Content',
|
||||
)
|
||||
);
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
unset( $data['_links'] );
|
||||
unset( $data['wp_id'] );
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'default//my_custom_template',
|
||||
'theme' => 'default',
|
||||
'slug' => 'my_custom_template',
|
||||
'title' => array(
|
||||
'raw' => 'My Template',
|
||||
'rendered' => 'My Template',
|
||||
),
|
||||
'description' => 'Just a description',
|
||||
'status' => 'publish',
|
||||
'source' => 'custom',
|
||||
'type' => 'wp_template',
|
||||
'content' => array(
|
||||
'raw' => 'Content',
|
||||
),
|
||||
'has_theme_file' => false,
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
public function test_update_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/templates/default//my_template' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'title' => 'My new Index Title',
|
||||
)
|
||||
);
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'My new Index Title', $data['title']['raw'] );
|
||||
$this->assertEquals( 'custom', $data['source'] );
|
||||
}
|
||||
|
||||
public function test_delete_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/justrandom//template' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_template_not_found', $response, 404 );
|
||||
}
|
||||
|
||||
public function test_prepare_item() {
|
||||
// TODO: Implement test_prepare_item() method.
|
||||
}
|
||||
|
||||
public function test_get_item_schema() {
|
||||
// TODO: Implement test_get_item_schema() method.
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Theme
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @group themes
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Theme
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @group themes
|
||||
|
||||
Reference in New Issue
Block a user