Skip to content

Commit

Permalink
Add tests to _build_block_template_result_from_post
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Sep 7, 2022
1 parent 551eeaf commit 916828f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>My Page Template</p>
<!-- /wp:paragraph -->
5 changes: 5 additions & 0 deletions tests/phpunit/data/themedir1/block-theme/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
{
"name": "page-home",
"title": "Homepage template"
},
{
"name": "my-page",
"title": "My Page template",
"postTypes": [ "page" ]
}
],
"templateParts": [
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class Tests_Block_Template_Utils extends WP_UnitTestCase {
private static $post;
private static $template_part_post;
private static $template_with_post_type_post;
private static $test_theme = 'block-theme';

public static function wpSetUpBeforeClass() {
Expand Down Expand Up @@ -69,6 +70,22 @@ public static function wpSetUpBeforeClass() {
self::$template_part_post = self::factory()->post->create_and_get( $template_part_args );
wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
wp_set_post_terms( self::$template_part_post->ID, self::$test_theme, 'wp_theme' );

// Set up template with specific post type post.
$template_with_post_type_args = array(
'post_type' => 'wp_template',
'post_name' => 'my_page',
'post_title' => 'My Page template',
'post_content' => 'Content',
'post_excerpt' => 'Description of my page template',
'tax_input' => array(
'wp_theme' => array(
self::$test_theme,
),
),
);
self::$template_with_post_type_post = self::factory()->post->create_and_get( $template_with_post_type_args );
wp_set_post_terms( self::$template_with_post_type_post->ID, self::$test_theme, 'wp_theme' );
}

public function set_up() {
Expand Down Expand Up @@ -111,6 +128,22 @@ public function test_build_block_template_result_from_post() {
$this->assertSame( 'Description of my template part', $template_part->description );
$this->assertSame( 'wp_template_part', $template_part->type );
$this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );

// Test template with specific post type.
$template_with_post_type = _build_block_template_result_from_post(
self::$template_with_post_type_post,
'wp_template'
);
$this->assertNotWPError( $template_with_post_type );
$this->assertSame( get_stylesheet() . '//my_page', $template_with_post_type->id );
$this->assertSame( get_stylesheet(), $template_with_post_type->theme );
$this->assertSame( 'my_page', $template_with_post_type->slug );
$this->assertSame( 'publish', $template_with_post_type->status );
$this->assertSame( 'custom', $template_with_post_type->source );
$this->assertSame( 'My Page template', $template_with_post_type->title );
$this->assertSame( 'Description of my page template', $template_with_post_type->description );
$this->assertSame( 'wp_template', $template_with_post_type->type );
$this->assertEquals( array( 'page' ), $template_with_post_type->post_types );
}

function test_build_block_template_result_from_file() {
Expand Down

0 comments on commit 916828f

Please sign in to comment.