Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Significance: patch
Type: changed
Comment: Update Verbum block test expectations for WordPress 7.0+ block output changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
*/
Expand All @@ -43,7 +47,14 @@ public function test_comment_text_block_sanitization_sanity_check() {
$comment_content = '<!-- wp:paragraph --><p>test</p><!-- /wp:paragraph --><!-- wp:list --><ul><!-- wp:list-item --><li>1</li><!-- /wp:list-item --><!-- wp:list-item --><li>2</li><!-- /wp:list-item --><!-- wp:list-item --><li>3</li><!-- /wp:list-item --></ul><!-- /wp:list --><!-- wp:quote --><blockquote class="wp-block-quote"><!-- wp:paragraph --><p>something</p><!-- /wp:paragraph --><cite>someone</cite></blockquote><!-- /wp:quote -->';
$filtered_content = preg_replace( '/\R+/', '', Verbum_Block_Utils::render_verbum_blocks( $comment_content ) );

$expected_content = '<p>test</p><ul><li>1</li><li>2</li><li>3</li></ul><blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>something</p><cite>someone</cite></blockquote>';
// 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 = '<p>test</p><ul><li>1</li><li>2</li><li>3</li></ul><blockquote class="wp-block-quote"><p>something</p><cite>someone</cite></blockquote>';
$expected_content_wp6 = '<p>test</p><ul><li>1</li><li>2</li><li>3</li></ul><blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>something</p><cite>someone</cite></blockquote>';

$expected_content = $this->get_version_specific_expected_html( $expected_content_wp7, $expected_content_wp6 );

$this->assertSame( $expected_content, $filtered_content );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Trait for handling WordPress version-specific test expectations.
*
* WordPress trunk (7.0+) changed block output in Gutenberg PR #71207,
* removing layout classes like 'is-layout-flow' and 'wp-block-quote-is-layout-flow'.
*
* This trait provides helper methods to handle version-specific expectations in tests.
*
* @todo Remove this trait once WP 7.0 is the minimum supported version.
*
* @package automattic/jetpack-mu-wpcom
*/

trait Jetpack_Mu_Wpcom_WP_Version_Test_Helpers {

/**
* Determine if we're testing against WordPress 7.0+ (trunk).
*
* WordPress 7.0+ removed layout classes from block output (Gutenberg PR #71207).
*
* @return bool True if WordPress 7.0+, false otherwise.
*/
protected function is_wp_7_or_higher() {
global $wp_version;

if ( empty( $wp_version ) ) {
return false;
}

return version_compare( $wp_version, '7.0-dev', '>=' );
}

/**
* 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;
}
}
3 changes: 3 additions & 0 deletions projects/plugins/jetpack/changelog/fix-block-tests-wp-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Significance: patch
Type: other
Comment: Normalize block fixture test output for WordPress version compatibility.
Original file line number Diff line number Diff line change
Expand Up @@ -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" .
Expand All @@ -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",
Expand All @@ -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;
}
}
Loading