mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement test coverage to ensure template part block works correctly…
… even without theme attribute.
- Loading branch information
1 parent
44d7eab
commit 14f6306
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
tests/phpunit/tests/blocks/renderBlockCoreTemplatePart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
/** | ||
* Tests for `render_block_core_template_part()`. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* | ||
* @group blocks | ||
*/ | ||
class Tests_Blocks_RenderBlockCoreTemplatePart extends WP_UnitTestCase { | ||
|
||
/** | ||
* Temporary storage for original block to render. | ||
* | ||
* @var array|null | ||
*/ | ||
private $original_block_to_render = null; | ||
|
||
/** | ||
* Flag to indicate whether to switch back to the default theme at tear down. | ||
* | ||
* @var bool | ||
*/ | ||
private $switch_to_default_theme_at_teardown = false; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
|
||
$this->original_block_to_render = WP_Block_Supports::$block_to_render; | ||
} | ||
|
||
public function tear_down() { | ||
WP_Block_Supports::$block_to_render = $this->original_block_to_render; | ||
$this->original_block_to_render = null; | ||
|
||
if ( $this->switch_to_default_theme_at_teardown ) { | ||
$this->switch_to_default_theme_at_teardown = false; | ||
switch_theme( WP_DEFAULT_THEME ); | ||
} | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Tests that the core template part block assumes the current theme if no theme attribute provided. | ||
* | ||
* @ticket 59583 | ||
*/ | ||
public function test_render_block_core_template_part_without_theme_attribute() { | ||
$this->maybe_switch_theme( 'block-theme' ); | ||
|
||
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/template-part' ); | ||
|
||
$content = render_block_core_template_part( array( 'slug' => 'small-header' ) ); | ||
|
||
$expected = '<header class="wp-block-template-part">' . "\n"; | ||
$expected .= '<p>Small Header Template Part</p>' . "\n"; | ||
$expected .= '</header>'; | ||
|
||
$this->assertSame( $expected, $content ); | ||
} | ||
|
||
/** | ||
* Tests that the core template part block returns the relevant part if current theme attribute provided. | ||
* | ||
* @ticket 59583 | ||
*/ | ||
public function test_render_block_core_template_part_with_current_theme_attribute() { | ||
$this->maybe_switch_theme( 'block-theme' ); | ||
|
||
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/template-part' ); | ||
|
||
$content = render_block_core_template_part( | ||
array( | ||
'slug' => 'small-header', | ||
'theme' => 'block-theme', | ||
) | ||
); | ||
|
||
$expected = '<header class="wp-block-template-part">' . "\n"; | ||
$expected .= '<p>Small Header Template Part</p>' . "\n"; | ||
$expected .= '</header>'; | ||
|
||
$this->assertSame( $expected, $content ); | ||
} | ||
|
||
/** | ||
* Tests that the core template part block returns nothing if theme attribute for a different theme provided. | ||
* | ||
* @ticket 59583 | ||
*/ | ||
public function test_render_block_core_template_part_with_another_theme_attribute() { | ||
$this->maybe_switch_theme( 'block-theme' ); | ||
|
||
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/template-part' ); | ||
|
||
$content = render_block_core_template_part( | ||
array( | ||
'slug' => 'small-header', | ||
'theme' => WP_DEFAULT_THEME, | ||
) | ||
); | ||
|
||
$expected = 'Template part has been deleted or is unavailable: small-header'; | ||
$this->assertSame( $expected, $content ); | ||
} | ||
|
||
/** | ||
* Switches the theme when not the default theme. | ||
* | ||
* @param string $theme Theme name to switch to. | ||
*/ | ||
private function maybe_switch_theme( $theme ) { | ||
if ( WP_DEFAULT_THEME === $theme ) { | ||
return; | ||
} | ||
|
||
switch_theme( $theme ); | ||
$this->switch_to_default_theme_at_teardown = true; | ||
} | ||
} |