Skip to content
Closed
Prev Previous commit
Next Next commit
Unit tests for wp_template objects
  • Loading branch information
tjcafferkey committed May 15, 2024
commit 691efe76c85c819bb87c4290c8dd6b8c97e486ae
28 changes: 28 additions & 0 deletions tests/phpunit/tests/block-templates/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ abstract class WP_Block_Templates_UnitTestCase extends WP_UnitTestCase {

protected static $template_post;
protected static $template_part_post;
protected static $uncustomized_template_db_object;
protected static $customized_template_db_object;


public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
/*
Expand Down Expand Up @@ -72,6 +75,31 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {

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' );

// 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,
),
'meta_input' => array(
'origin' => 'theme',
),
'post_content' => '<!-- wp:heading {"level":1,"metadata":{"ignoredHookedBlocks":["tests/ignored"]}} --><h1>Template</h1><!-- /wp:heading -->',
'post_type' => 'wp_template',
'post_name' => 'my_template',
'post_title' => 'My Template',
'post_excerpt' => 'Description of my template',
);

// Setup customized template db object.
self::$customized_template_db_object = (object) array(
'post_name' => 'my_template',
'post_title' => 'My Customized Template',
'post_status' => 'publish',
'post_content' => '<!-- wp:heading {"level":1,"metadata":{"ignoredHookedBlocks":["tests/ignored"]}} --><h1>Template</h1><!-- /wp:heading -->',
);
}

public static function wpTearDownAfterClass() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

require_once __DIR__ . '/base.php';

/**
* @group block-templates
* @covers ::_build_block_template_object_from_database_object
*/
class Tests_Block_Templates_BuildBlockTemplateFromDatabaseObject extends WP_Block_Templates_UnitTestCase {

/**
* Tear down each test method.
*
* @since 6.6.0
*/
public function tear_down() {
$registry = WP_Block_Type_Registry::get_instance();

if ( $registry->is_registered( 'tests/my-block' ) ) {
$registry->unregister( 'tests/my-block' );
}

if ( $registry->is_registered( 'tests/ignored' ) ) {
$registry->unregister( 'tests/ignored' );
}

parent::tear_down();
}

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

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

/**
* @ticket 60759
*/
public function test_should_build_template_from_customized_object() {
self::$customized_template_db_object->ID = self::$template_post->ID;
$template = _build_block_template_object_from_database_object( self::$customized_template_db_object );

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