Skip to content

Commit 4312099

Browse files
committed
Refactor wp_load_classic_theme_block_styles_on_demand() tests to use data provider
1 parent 1c5165d commit 4312099

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

tests/phpunit/tests/template.php

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -922,64 +922,70 @@ public function test_wp_load_classic_theme_block_styles_on_demand_in_block_theme
922922
}
923923

924924
/**
925-
* Tests that wp_load_classic_theme_block_styles_on_demand() does not add hooks for classic themes when output buffering is blocked.
925+
* Data provider.
926926
*
927-
* @ticket 64099
928-
* @covers ::wp_load_classic_theme_block_styles_on_demand
927+
* @return array<string, array{theme: string, set_up: Closure|null, expected_on_demand: bool, expected_buffer_started: bool}>
929928
*/
930-
public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_theme_but_output_buffering_blocked(): void {
931-
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_false' );
932-
switch_theme( 'default' );
933-
934-
wp_load_classic_theme_block_styles_on_demand();
935-
936-
$this->assertFalse( has_filter( 'should_load_separate_core_block_assets' ), 'Expect should_load_separate_core_block_assets filter NOT to be added for block themes.' );
937-
$this->assertFalse( has_filter( 'should_load_block_assets_on_demand', '__return_true' ), 'Expect should_load_block_assets_on_demand filter NOT to be added for block themes.' );
938-
$this->assertFalse( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expect wp_template_enhancement_output_buffer_started action NOT to be added for block themes.' );
929+
public function data_wp_load_classic_theme_block_styles_on_demand(): array {
930+
return array(
931+
'block_theme' => array(
932+
'theme' => 'block-theme',
933+
'set_up' => static function () {},
934+
'expected_on_demand' => false,
935+
'expected_buffer_started' => false,
936+
),
937+
'classic_theme_with_output_buffer_blocked' => array(
938+
'theme' => 'default',
939+
'set_up' => static function () {
940+
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_false' );
941+
},
942+
'expected_on_demand' => false,
943+
'expected_buffer_started' => false,
944+
),
945+
'classic_theme_with_block_styles_support' => array(
946+
'theme' => 'default',
947+
'set_up' => static function () {
948+
add_theme_support( 'wp-block-styles' );
949+
},
950+
'expected_on_demand' => true,
951+
'expected_buffer_started' => true,
952+
),
953+
'classic_theme_without_block_styles_support' => array(
954+
'theme' => 'default',
955+
'set_up' => static function () {
956+
remove_theme_support( 'wp-block-styles' );
957+
},
958+
'expected_on_demand' => false,
959+
'expected_buffer_started' => true,
960+
),
961+
);
939962
}
940963

941964
/**
942-
* Tests that wp_load_classic_theme_block_styles_on_demand() adds the expected hooks for classic theme which supports block styles.
965+
* Tests that wp_load_classic_theme_block_styles_on_demand() adds the expected hooks (or not).
943966
*
944967
* @ticket 64099
945968
* @ticket 64150
946-
* @covers ::wp_load_classic_theme_block_styles_on_demand
947-
*/
948-
public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_theme_with_block_styles_support(): void {
949-
switch_theme( 'default' );
950-
add_theme_support( 'wp-block-styles' );
951-
952-
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() to return false initially.' );
953-
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true' );
954-
$this->assertFalse( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action to be added for classic themes.' );
955-
956-
wp_load_classic_theme_block_styles_on_demand();
957-
958-
$this->assertTrue( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() filters to return true because the theme does support wp-block-styles' );
959-
$this->assertTrue( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true because the theme does support wp-block-styles' );
960-
$this->assertNotFalse( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action to be added for classic themes.' );
961-
}
962-
963-
/**
964-
* Tests that wp_load_classic_theme_block_styles_on_demand() adds the expected hooks for classic theme which does not support block styles.
965969
*
966-
* @ticket 64099
967-
* @ticket 64150
968970
* @covers ::wp_load_classic_theme_block_styles_on_demand
971+
*
972+
* @dataProvider data_wp_load_classic_theme_block_styles_on_demand
969973
*/
970-
public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_theme_without_block_styles_support(): void {
971-
switch_theme( 'default' );
972-
remove_theme_support( 'wp-block-styles' );
973-
974+
public function test_wp_load_classic_theme_block_styles_on_demand( string $theme, ?Closure $set_up, bool $expected_on_demand, bool $expected_buffer_started ) {
974975
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() to return false initially.' );
975976
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true' );
976977
$this->assertFalse( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action to be added for classic themes.' );
977978

979+
switch_theme( $theme );
980+
if ( $set_up ) {
981+
$set_up();
982+
}
983+
978984
wp_load_classic_theme_block_styles_on_demand();
979985

980-
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() filters to return false because the theme does not support wp-block-styles' );
981-
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return false because the theme does not support wp-block-styles' );
982-
$this->assertNotFalse( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action to be added for classic themes.' );
986+
$this->assertSame( $expected_on_demand, wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() return value.' );
987+
$this->assertSame( $expected_on_demand, wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() return value.' );
988+
$this->assertSame( $expected_buffer_started, (bool) has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action to be added for classic themes.' );
983989
}
984990

985991
/**

0 commit comments

Comments
 (0)