Skip to content

Commit

Permalink
Add PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed May 1, 2024
1 parent 8818200 commit 60f8453
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
109 changes: 109 additions & 0 deletions phpunit/blocks/get-block-templates-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* @group blocks
* @group block-templates
*
* @covers ::get_block_templates
*/
class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase {

const TEST_THEME = 'block-theme';

/**
* Theme root directory.
*
* @var string
*/
private $theme_root;

/**
* Original theme directory.
*
* @var string
*/
private $orig_theme_dir;

public function set_up() {
parent::set_up();
$this->theme_root = realpath( __DIR__ . '/../data/themedir1' );
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];

// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );

add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
switch_theme( self::TEST_THEME );
}

public function tear_down() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
parent::tear_down();
}

public function filter_set_theme_root() {
return $this->theme_root;
}

/**
* Gets the template IDs from the given array.
*
* @param object[] $templates Array of template objects to parse.
* @return string[] The template IDs.
*/
private function get_template_ids( $templates ) {
return array_map(
static function ( $template ) {
return $template->id;
},
$templates
);
}

/**
* @dataProvider data_get_block_templates_should_respect_posttypes_property
* @ticket 55881
*
* @param string $post_type Post type for query.
* @param array $expected Expected template IDs.
*/
public function test_get_block_templates_should_respect_posttypes_property( $post_type, $expected ) {
$templates = gutenberg_get_block_templates( array( 'post_type' => $post_type ) );

$this->assertSameSets(
$expected,
$this->get_template_ids( $templates )
);
}

/**
* Data provider.
*
* @return array
*/
public function data_get_block_templates_should_respect_posttypes_property() {
// @code-merge: This code will go into Core's tests/phpunit/tests/blocks/getBlockTemplates.php.
return array(
'post' => array(
'post_type' => 'post',
'expected' => array(
'block-theme//custom-hero-template',
'block-theme//custom-single-post-template',
),
),
'page' => array(
'post_type' => 'page',
'expected' => array(
'block-theme//custom-hero-template',
'block-theme//page-home',
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Custom Hero template</p>
<!-- /wp:paragraph -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Custom Single Post template</p>
<!-- /wp:paragraph -->
3 changes: 3 additions & 0 deletions phpunit/data/themedir1/block-theme/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Index Template</p>
<!-- /wp:paragraph -->
3 changes: 3 additions & 0 deletions phpunit/data/themedir1/block-theme/templates/page-home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Page (Home) Template</p>
<!-- /wp:paragraph -->

0 comments on commit 60f8453

Please sign in to comment.