Skip to content

Commit fc08fdb

Browse files
committed
Fix Unit Tests: version-gate navigation block markup expectations
Cherry-pick of two commits, applied in order: - 1062062 ("Fix navigation block rendering unit test (#76685)") - 29d7db9a4ec9c0fbb7f74879465bcb700a4b12de ("Fix navigation block unit test and e2e test (#76692)", second attempt at the same fix) Both are 100% test-only, no production code touched. Excluded the test/e2e/specs/editor/plugins/block-hooks.spec.js hunk from the second commit — End-to-End Tests already pass on this branch, and this change isn't needed to fix the failing PHPUnit tests, so it's out of scope here. Root cause: WordPress core changed two aspects of navigation block markup as of WP 7.0 — the leading space before the 'wp-block-navigation-item' class on <li> wrappers, and a trailing semicolon in inline 'style' attributes on submenu <ul> wrappers. Both commits use is_wp_version_compatible('7.0') to assert the old markup against older cores and the new markup against 7.0+, so the suite stays green across the WordPress versions Gutenberg supports regardless of which core wp-env happens to pull. Also fixed a real bug carried over from both upstream commits (still present verbatim on wp/7.0 today): in class-wp-navigation-block-renderer-test.php, test_gutenberg_default_block_is_enclosed_in_li_tags()'s is_wp_version_compatible('7.0') branch set $expected but never called $this->assertEquals() — only the 'else' (pre-7.0) branch did. On WP >= 7.0 the test would silently pass without asserting anything at all. Moved the assertEquals call after the if/else so it runs in both cases.
1 parent e7f072d commit fc08fdb

2 files changed

Lines changed: 53 additions & 19 deletions

File tree

‎phpunit/blocks/class-wp-navigation-block-renderer-test.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ public function test_gutenberg_default_block_is_enclosed_in_li_tags() {
3131
// Invoke the private method.
3232
$result = $method->invoke( $reflection, $navigation_link_block );
3333

34-
$expected = '<li class=" wp-block-navigation-item wp-block-navigation-link"><a class="wp-block-navigation-item__content" href="/hello-world"><span class="wp-block-navigation-item__label">Sample Page</span></a></li>';
34+
if ( is_wp_version_compatible( '7.0' ) ) {
35+
$expected = '<li class="wp-block-navigation-item wp-block-navigation-link"><a class="wp-block-navigation-item__content" href="/hello-world"><span class="wp-block-navigation-item__label">Sample Page</span></a></li>';
36+
} else {
37+
// Block markup for WP 6.9 (space before wp-block-navigation-item class)
38+
// TODO: Remove the second expected markup after WP 6.9 support is dropped and the old markup is no longer generated.
39+
$expected = '<li class=" wp-block-navigation-item wp-block-navigation-link"><a class="wp-block-navigation-item__content" href="/hello-world"><span class="wp-block-navigation-item__label">Sample Page</span></a></li>';
40+
}
3541
$this->assertEquals( $expected, $result );
3642
}
3743

‎phpunit/blocks/render-block-navigation-submenu-test.php‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,29 @@ public function test_should_apply_custom_colors_inherited_from_parent_block_via_
122122

123123
$navigation_submenu_block = new WP_Block( $block, $context );
124124

125-
$this->assertStringContainsString(
126-
'<ul style="color:' . $context['customOverlayTextColor'] . ';background-color:' . $context['customOverlayBackgroundColor'] . ';" class="wp-block-navigation__submenu-container has-text-color has-background">',
127-
gutenberg_render_block_core_navigation_submenu(
128-
$navigation_submenu_block->attributes,
129-
array(),
130-
$navigation_submenu_block
131-
),
132-
'Submenu block colors inherited from context not applied correctly'
133-
);
125+
if ( is_wp_version_compatible( '7.0' ) ) {
126+
$this->assertStringContainsString(
127+
'<ul style="color:' . $context['customOverlayTextColor'] . ';background-color:' . $context['customOverlayBackgroundColor'] . '" class="wp-block-navigation__submenu-container has-text-color has-background">',
128+
gutenberg_render_block_core_navigation_submenu(
129+
$navigation_submenu_block->attributes,
130+
array(),
131+
$navigation_submenu_block
132+
),
133+
'Submenu block colors inherited from context not applied correctly'
134+
);
135+
} else {
136+
// Block markup for WP 6.9 (semicolon in style attribute)
137+
// TODO: Remove the second expected markup after WP 6.9 support is dropped and the old markup is no longer generated.
138+
$this->assertStringContainsString(
139+
'<ul style="color:' . $context['customOverlayTextColor'] . ';background-color:' . $context['customOverlayBackgroundColor'] . ';" class="wp-block-navigation__submenu-container has-text-color has-background">',
140+
gutenberg_render_block_core_navigation_submenu(
141+
$navigation_submenu_block->attributes,
142+
array(),
143+
$navigation_submenu_block
144+
),
145+
'Submenu block colors inherited from context not applied correctly'
146+
);
147+
}
134148
}
135149

136150
/**
@@ -158,15 +172,29 @@ public function test_should_apply_mix_of_preset_and_custom_colors_inherited_from
158172

159173
$navigation_submenu_block = new WP_Block( $block, $context );
160174

161-
$this->assertStringContainsString(
162-
'<ul style="background-color:' . $context['customOverlayBackgroundColor'] . ';" class="wp-block-navigation__submenu-container has-text-color has-' . $context['overlayTextColor'] . '-color has-background">',
163-
gutenberg_render_block_core_navigation_submenu(
164-
$navigation_submenu_block->attributes,
165-
array(),
166-
$navigation_submenu_block
167-
),
168-
'Submenu block colors inherited from context not applied correctly'
169-
);
175+
if ( is_wp_version_compatible( '7.0' ) ) {
176+
$this->assertStringContainsString(
177+
'<ul style="background-color:' . $context['customOverlayBackgroundColor'] . '" class="wp-block-navigation__submenu-container has-text-color has-' . $context['overlayTextColor'] . '-color has-background">',
178+
gutenberg_render_block_core_navigation_submenu(
179+
$navigation_submenu_block->attributes,
180+
array(),
181+
$navigation_submenu_block
182+
),
183+
'Submenu block colors inherited from context not applied correctly'
184+
);
185+
} else {
186+
// Block markup for WP 6.9 (semicolon in style attribute)
187+
// TODO: Remove the second expected markup after WP 6.9 support is dropped and the old markup is no longer generated.
188+
$this->assertStringContainsString(
189+
'<ul style="background-color:' . $context['customOverlayBackgroundColor'] . ';" class="wp-block-navigation__submenu-container has-text-color has-' . $context['overlayTextColor'] . '-color has-background">',
190+
gutenberg_render_block_core_navigation_submenu(
191+
$navigation_submenu_block->attributes,
192+
array(),
193+
$navigation_submenu_block
194+
),
195+
'Submenu block colors inherited from context not applied correctly'
196+
);
197+
}
170198
}
171199

172200
/**

0 commit comments

Comments
 (0)