diff --git a/projects/packages/jetpack-mu-wpcom/changelog/fix-verbum-block-test-wp-trunk b/projects/packages/jetpack-mu-wpcom/changelog/fix-verbum-block-test-wp-trunk new file mode 100644 index 000000000000..0cdb2aab3e43 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/fix-verbum-block-test-wp-trunk @@ -0,0 +1,3 @@ +Significance: patch +Type: changed +Comment: Update Verbum block test expectations for WordPress 7.0+ block output changes. diff --git a/projects/packages/jetpack-mu-wpcom/tests/php/features/verbum-comments/Verbum_Block_Utils_Test.php b/projects/packages/jetpack-mu-wpcom/tests/php/features/verbum-comments/Verbum_Block_Utils_Test.php index 7e052d482cd1..12b15194a014 100644 --- a/projects/packages/jetpack-mu-wpcom/tests/php/features/verbum-comments/Verbum_Block_Utils_Test.php +++ b/projects/packages/jetpack-mu-wpcom/tests/php/features/verbum-comments/Verbum_Block_Utils_Test.php @@ -8,7 +8,9 @@ use Automattic\Jetpack\Jetpack_Mu_Wpcom; use Brain\Monkey\Functions; use PHPUnit\Framework\Attributes\CoversClass; + require_once Jetpack_Mu_Wpcom::PKG_DIR . 'src/features/verbum-comments/assets/class-verbum-block-utils.php'; +require_once __DIR__ . '/../../trait-wp-version-test-helpers.php'; /** * Test class for Verbum_Block_Utils. @@ -17,6 +19,8 @@ */ #[CoversClass( Verbum_Block_Utils::class )] class Verbum_Block_Utils_Test extends \WorDBless\BaseTestCase { + use Jetpack_Mu_Wpcom_WP_Version_Test_Helpers; + /** * Ensure string comments are not modified when 'render_verbum_blocks' is applied */ @@ -43,7 +47,14 @@ public function test_comment_text_block_sanitization_sanity_check() { $comment_content = '

test

something

someone
'; $filtered_content = preg_replace( '/\R+/', '', Verbum_Block_Utils::render_verbum_blocks( $comment_content ) ); - $expected_content = '

test

something

someone
'; + // WordPress trunk (7.0+) removed layout classes from block output (Gutenberg PR #71207). + // We need to handle both old (with classes) and new (without classes) formats. + // @todo Simplify to a single expected value once WP 7.0 is the minimum supported version. + $expected_content_wp7 = '

test

something

someone
'; + $expected_content_wp6 = '

test

something

someone
'; + + $expected_content = $this->get_version_specific_expected_html( $expected_content_wp7, $expected_content_wp6 ); + $this->assertSame( $expected_content, $filtered_content ); } diff --git a/projects/packages/jetpack-mu-wpcom/tests/php/trait-wp-version-test-helpers.php b/projects/packages/jetpack-mu-wpcom/tests/php/trait-wp-version-test-helpers.php new file mode 100644 index 000000000000..2beddaee3c55 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/tests/php/trait-wp-version-test-helpers.php @@ -0,0 +1,48 @@ +=' ); + } + + /** + * Get version-appropriate expected HTML for block output. + * + * @param string $html_without_layout_classes HTML without layout classes (WP 7.0+ format). + * @param string $html_with_layout_classes HTML with layout classes (WP 6.9 and earlier format). + * @return string The appropriate HTML for the current WordPress version. + */ + protected function get_version_specific_expected_html( $html_without_layout_classes, $html_with_layout_classes ) { + if ( $this->is_wp_7_or_higher() ) { + return $html_without_layout_classes; + } + + return $html_with_layout_classes; + } +} diff --git a/projects/plugins/jetpack/changelog/fix-block-tests-wp-version b/projects/plugins/jetpack/changelog/fix-block-tests-wp-version new file mode 100644 index 000000000000..40443d5bd314 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-block-tests-wp-version @@ -0,0 +1,3 @@ +Significance: patch +Type: other +Comment: Normalize block fixture test output for WordPress version compatibility. diff --git a/projects/plugins/jetpack/tests/php/extensions/blocks/class-block-fixture-testcase.php b/projects/plugins/jetpack/tests/php/extensions/blocks/class-block-fixture-testcase.php index f172e6101cd0..164c879b3f8d 100644 --- a/projects/plugins/jetpack/tests/php/extensions/blocks/class-block-fixture-testcase.php +++ b/projects/plugins/jetpack/tests/php/extensions/blocks/class-block-fixture-testcase.php @@ -39,11 +39,14 @@ public function generate_server_side_rendering_based_on_serialized_fixtures( $parsed_blocks = parse_blocks( $block_markup ); $rendered_output = trim( render_block( $parsed_blocks[0] ) ); + // Normalize output for version-agnostic comparison + $normalized_output = $this->normalize_block_output( $rendered_output ); + $target_markup_filename = str_replace( '.serialized.html', $target_extension, $file ); // Create a server rendered fixture if one does not exist. if ( ! file_exists( $target_markup_filename ) ) { - file_put_contents( $target_markup_filename, $rendered_output ); + file_put_contents( $target_markup_filename, $normalized_output ); $fail_messages[] = sprintf( "No server rendered fixture could be found for the %s block's %s fixture\n" . @@ -56,8 +59,8 @@ public function generate_server_side_rendering_based_on_serialized_fixtures( $server_rendered_fixture = file_get_contents( $target_markup_filename ); $this->assertEquals( - $rendered_output, - trim( $server_rendered_fixture ), + $normalized_output, + $this->normalize_block_output( trim( $server_rendered_fixture ) ), sprintf( 'The results of render_block for %s called with serialized markup from %s do not match ' . "the server-rendered fixture: %s\n", @@ -76,4 +79,35 @@ public function generate_server_side_rendering_based_on_serialized_fixtures( ); } } + + /** + * Normalize block HTML output for version-agnostic comparison. + * + * Strips classes that vary between WordPress versions so fixtures work + * on both WP latest and WP trunk: + * - `is-layout-*` classes (removed in WP trunk, Gutenberg PR #71207) + * - `wp-block-*-is-layout-*` classes (same change) + * - `wp-block-paragraph` class (added in WP trunk, core r61605) + * - Leftover empty `class=""` attributes after stripping + * + * @todo Remove this method once WP 7.0 is the minimum supported version. + * + * @param string $html The HTML content to normalize. + * @return string The normalized HTML. + */ + private function normalize_block_output( $html ) { + // Remove wp-block-*-is-layout-* classes (must come before is-layout-* to avoid partial matches). + $html = preg_replace( '/\s*wp-block-[a-z-]+-is-layout-[a-z-]+/', '', $html ); + + // Remove is-layout-* classes. + $html = preg_replace( '/\s*is-layout-[a-z-]+/', '', $html ); + + // Remove wp-block-paragraph class. + $html = preg_replace( '/\s*wp-block-paragraph/', '', $html ); + + // Remove leftover empty class attributes. + $html = preg_replace( '/\s*class=""/', '', $html ); + + return $html; + } }