Skip to content
Closed
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
53 changes: 53 additions & 0 deletions tests/phpunit/tests/formatting/formatToEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Tests for the `format_to_edit()` function.
*
* @group formatting
*
* @covers ::format_to_edit
*/
class Tests_Formatting_FormatToEdit extends WP_UnitTestCase {

/**
* Tests that format_to_edit() escapes text for textarea by default ($rich_text = false).
*
* @ticket 65819
*/
public function test_format_to_edit_escapes_when_rich_text_is_false() {
$content = '<b>"hello" & \'world\'</b>';
$expected = '&lt;b&gt;&quot;hello&quot; &amp; &#039;world&#039;&lt;/b&gt;';

$this->assertSame( $expected, format_to_edit( $content ) );
$this->assertSame( $expected, format_to_edit( $content, false ) );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This one looks like a small coverage gap: esc_textarea() differs from esc_html() by double-encoding existing entities, and the class still passes if the function switches to esc_html(). Could you add a case like $this->assertSame( 'AT&amp;amp;T', format_to_edit( 'AT&amp;T' ) );?

}

/**
* Tests that format_to_edit() does not escape text when $rich_text is true.
*
* @ticket 65819
*/
public function test_format_to_edit_does_not_escape_when_rich_text_is_true() {
$content = '<b>"hello" & \'world\'</b>';

$this->assertSame( $content, format_to_edit( $content, true ) );
}

/**
* Tests that format_to_edit filter modifies content before escaping.
*
* @ticket 65819
*/
public function test_format_to_edit_filter() {
add_filter(
'format_to_edit',
static function ( $text ) {
return $text . ' & <span>extra</span>';
}
);

$expected = 'original &amp; &lt;span&gt;extra&lt;/span&gt;';

$this->assertSame( $expected, format_to_edit( 'original' ) );
}
}
Loading