@@ -58,6 +58,9 @@ public function tear_down() {
5858 unregister_block_type ( $ this ->test_block_name );
5959 $ this ->test_block_name = null ;
6060
61+ // Resets script modules enqueued while rendering fit text.
62+ $ GLOBALS ['wp_script_modules ' ] = null ;
63+
6164 parent ::tear_down ();
6265 }
6366
@@ -1135,6 +1138,80 @@ public function data_generate_replace_inline_font_styles_with_fluid_values_fixtu
11351138 );
11361139 }
11371140
1141+ /**
1142+ * Tests that the `has-fit-text` class name is added to the block wrapper when the
1143+ * `fitText` attribute is set.
1144+ *
1145+ * @ticket 65973
1146+ *
1147+ * @covers ::wp_render_typography_support
1148+ *
1149+ * @dataProvider data_should_add_fit_text_class_name
1150+ *
1151+ * @param non-falsy-string $block_content HTML block content.
1152+ * @param non-falsy-string $expected_class Expected value of the class attribute on the block wrapper.
1153+ */
1154+ public function test_should_add_fit_text_class_name ( string $ block_content , string $ expected_class ) {
1155+ $ block = array (
1156+ 'blockName ' => 'core/site-title ' ,
1157+ 'attrs ' => array (
1158+ 'fitText ' => true ,
1159+ ),
1160+ );
1161+
1162+ $ actual = wp_render_typography_support ( $ block_content , $ block );
1163+
1164+ $ processor = new WP_HTML_Tag_Processor ( $ actual );
1165+ $ processor ->next_tag ();
1166+
1167+ $ this ->assertSame ( $ expected_class , $ processor ->get_attribute ( 'class ' ), 'The block wrapper does not have the expected class names. ' );
1168+ $ this ->assertSame ( 1 , substr_count ( $ actual , 'has-fit-text ' ), 'The class name should be added exactly once, to the outermost tag only. ' );
1169+ }
1170+
1171+ /**
1172+ * Data provider for test_should_add_fit_text_class_name().
1173+ *
1174+ * @return array<non-falsy-string, array{ block_content: non-falsy-string, expected_class: non-falsy-string }>
1175+ */
1176+ public function data_should_add_fit_text_class_name (): array {
1177+ return array (
1178+ 'wrapper without a class attribute ' => array (
1179+ 'block_content ' => '<h1>Site Title</h1> ' ,
1180+ 'expected_class ' => 'has-fit-text ' ,
1181+ ),
1182+ 'wrapper with existing class names ' => array (
1183+ 'block_content ' => '<h1 class="wp-block-site-title"><a href="https://example.com">Site Title</a></h1> ' ,
1184+ 'expected_class ' => 'wp-block-site-title has-fit-text ' ,
1185+ ),
1186+ 'wrapper with an already serialized class name ' => array (
1187+ 'block_content ' => '<p class="has-fit-text">A paragraph</p> ' ,
1188+ 'expected_class ' => 'has-fit-text ' ,
1189+ ),
1190+ 'wrapper with inner blocks ' => array (
1191+ 'block_content ' => '<div class="wp-block-group"><p>A paragraph inside a group</p></div> ' ,
1192+ 'expected_class ' => 'wp-block-group has-fit-text ' ,
1193+ ),
1194+ );
1195+ }
1196+
1197+ /**
1198+ * Tests that the `has-fit-text` class name is not added when the block does not
1199+ * opt in to the fit text support.
1200+ *
1201+ * @ticket 65973
1202+ *
1203+ * @covers ::wp_render_typography_support
1204+ */
1205+ public function test_should_not_add_fit_text_class_name_without_fit_text_attribute () {
1206+ $ block_content = '<h1 class="wp-block-site-title">Site Title</h1> ' ;
1207+ $ block = array (
1208+ 'blockName ' => 'core/site-title ' ,
1209+ 'attrs ' => array (),
1210+ );
1211+
1212+ $ this ->assertSame ( $ block_content , wp_render_typography_support ( $ block_content , $ block ) );
1213+ }
1214+
11381215 /**
11391216 * Tests that valid font size values are parsed.
11401217 *
0 commit comments