Skip to content
Closed
Prev Previous commit
Next Next commit
template part unit tests
  • Loading branch information
tjcafferkey committed May 15, 2024
commit d3aa4adc5c38aaadefdb8025a96d2310ad98f3f7
28 changes: 27 additions & 1 deletion tests/phpunit/tests/block-templates/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ abstract class WP_Block_Templates_UnitTestCase extends WP_UnitTestCase {
protected static $template_part_post;
protected static $uncustomized_template_db_object;
protected static $customized_template_db_object;
protected static $uncustomized_template_part_db_object;
protected static $customized_template_part_db_object;


public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
Expand Down Expand Up @@ -78,7 +80,6 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {

// Setup uncustomized template db object.
self::$uncustomized_template_db_object = (object) array(
'post_type' => 'wp_template',
'post_status' => 'publish',
'tax_input' => array(
'wp_theme' => self::TEST_THEME,
Expand All @@ -100,6 +101,31 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'post_status' => 'publish',
'post_content' => '<!-- wp:heading {"level":1,"metadata":{"ignoredHookedBlocks":["tests/ignored"]}} --><h1>Template</h1><!-- /wp:heading -->',
);

// Setup uncustomized template part db object.
self::$uncustomized_template_part_db_object = (object) array(
'post_status' => 'publish',
'tax_input' => array(
'wp_theme' => self::TEST_THEME,
'wp_template_part_area' => WP_TEMPLATE_PART_AREA_HEADER,
),
'meta_input' => array(
'origin' => 'theme',
),
'post_content' => '<!-- wp:heading {"level":2,"metadata":{"ignoredHookedBlocks":["tests/ignored"]}} --><h2>Template Part</h2><!-- /wp:heading -->',
'post_type' => 'wp_template_part',
'post_name' => 'my_template_part',
'post_title' => 'My Template Part',
'post_excerpt' => 'Description of my template part',
);

// Setup customised template part db object.
self::$customized_template_part_db_object = (object) array(
'post_name' => 'my_template_part',
'post_title' => 'My Customized Template Part',
'post_status' => 'publish',
'post_content' => '<!-- wp:heading {"level":2,"metadata":{"ignoredHookedBlocks":["tests/ignored"]}} --><h2>Template Customized Part</h2><!-- /wp:heading -->',
);
}

public static function wpTearDownAfterClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,39 @@ public function test_should_build_template_from_customized_object() {
$this->assertSame( 'Description of my template', $template->description );
$this->assertSame( 'wp_template', $template->type );
}

/**
* @ticket 60759
*/
public function test_should_build_template_part_from_uncustomized_object() {
$template = _build_block_template_object_from_database_object( self::$uncustomized_template_part_db_object );

$this->assertNotWPError( $template );
$this->assertSame( get_stylesheet() . '//my_template_part', $template->id );
$this->assertSame( get_stylesheet(), $template->theme );
$this->assertSame( 'my_template_part', $template->slug );
$this->assertSame( 'publish', $template->status );
$this->assertSame( 'custom', $template->source );
$this->assertSame( 'My Template Part', $template->title );
$this->assertSame( 'Description of my template part', $template->description );
$this->assertSame( 'wp_template_part', $template->type );
}

/**
* @ticket 60759
*/
public function test_should_build_template_part_from_customized_object() {
self::$customized_template_part_db_object->ID = self::$template_part_post->ID;
$template = _build_block_template_object_from_database_object( self::$customized_template_part_db_object );

$this->assertNotWPError( $template );
$this->assertSame( get_stylesheet() . '//my_template_part', $template->id );
$this->assertSame( get_stylesheet(), $template->theme );
$this->assertSame( 'my_template_part', $template->slug );
$this->assertSame( 'publish', $template->status );
$this->assertSame( 'custom', $template->source );
$this->assertSame( 'My Customized Template Part', $template->title );
$this->assertSame( 'Description of my template part', $template->description );
$this->assertSame( 'wp_template_part', $template->type );
}
}