Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation Block: Fix erroneous escaping of ampersands (etc) #59561

Merged
merged 9 commits into from
Mar 5, 2024
76 changes: 76 additions & 0 deletions phpunit/blocks/block-navigation-block-hooks-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Navigation block block hooks tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Navigation block.
*
* @group blocks
*/
class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase {
/**
* Original markup.
*
* @var string
*/
protected static $original_markup;

/**
* Post object.
*
* @var object
*/
protected static $navigation_post;

/**
* Setup method.
*/
public static function wpSetUpBeforeClass() {
self::$original_markup = '<!-- wp:navigation-link {"label":"News & About","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';

self::$navigation_post = self::factory()->post->create_and_get(
array(
'post_type' => 'wp_navigation',
'post_title' => 'Navigation Menu',
'post_content' => self::$original_markup,
)
);
}

/**
* Tear down each test method.
*/
public function tear_down() {
$registry = WP_Block_Type_Registry::get_instance();

if ( $registry->is_registered( 'tests/my-block' ) ) {
$registry->unregister( 'tests/my-block' );
}

parent::tear_down();
}

/**
* @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta
*/
public function test_block_core_navigation_update_ignore_hooked_blocks_meta() {
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/navigation' => 'last_child',
),
)
);

$post = get_post( self::$navigation_post );

gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta( $post );
$this->assertSame( self::$original_markup . '<!-- wp:tests/my-block /-->', $post->post_content );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently failing, but apparently not because of #59516:

1) Block_Navigation_Block_Hooks_Test::test_block_core_navigation_update_ignore_hooked_blocks_meta
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'<!-- wp:navigation-link {"label":"News & About","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /--><!-- wp:tests/my-block /-->'
+'<!-- wp:navigation-link {"label":"News \u0026amp; About","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->'

(Note that & is being escaped to \u0026amp;, which seems correct. Most importantly, the leading slash is retained.)

$this->assertSame( array( 'tests/my-block' ), get_post_meta( $post, '_wp_ignored_hooked_blocks' ) );
ockham marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading