wordpress-develop/tests/phpunit/tests/block-template.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

88 lines
2.7 KiB
PHP

<?php
/**
* Block_Template_Test class
*
* @package WordPress
*/
/**
* Tests for the block template loading algorithm.
*/
class Block_Template_Test extends WP_UnitTestCase {
private static $post;
private static $template_canvas_path = ABSPATH . WPINC . '/template-canvas.php';
public static function wpSetUpBeforeClass() {
// Set up custom template post.
$args = array(
'post_type' => 'wp_template',
'post_name' => 'wp-custom-template-my-block-template',
'post_title' => 'My Custom Block Template',
'post_content' => 'Content',
'post_excerpt' => 'Description of my block template',
'tax_input' => array(
'wp_theme' => array(
get_stylesheet(),
),
),
);
self::$post = self::factory()->post->create_and_get( $args );
wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
}
public static function wpTearDownAfterClass() {
wp_delete_post( self::$post->ID );
}
public function tear_down() {
global $_wp_current_template_content;
unset( $_wp_current_template_content );
}
/**
* Regression: https://github.com/WordPress/gutenberg/issues/31399.
*/
function test_custom_page_php_template_takes_precedence_over_all_other_templates() {
$custom_page_template = 'templates/full-width.php';
$custom_page_template_path = get_stylesheet_directory() . '/' . $custom_page_template;
$type = 'page';
$templates = array(
$custom_page_template,
'page-slug.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( $custom_page_template_path, $type, $templates );
$this->assertSame( $custom_page_template_path, $resolved_template_path );
}
/**
* Covers: https://github.com/WordPress/gutenberg/pull/30438.
*/
function test_custom_page_block_template_takes_precedence_over_all_other_templates() {
global $_wp_current_template_content;
$custom_page_block_template = 'wp-custom-template-my-block-template';
$page_template_path = get_stylesheet_directory() . '/' . 'page.php';
$type = 'page';
$templates = array(
$custom_page_block_template,
'page-slug.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( $page_template_path, $type, $templates );
$this->assertSame( self::$template_canvas_path, $resolved_template_path );
$this->assertSame( self::$post->post_content, $_wp_current_template_content );
}
/**
* Regression: https://github.com/WordPress/gutenberg/issues/31652.
*/
function test_template_remains_unchanged_if_templates_array_is_empty() {
$resolved_template_path = locate_block_template( '', 'search', array() );
$this->assertSame( '', $resolved_template_path );
}
}