Skip to content

Commit cde7c42

Browse files
committed
Merge branch 'main' of github.com:wptrainingteam/assert-equal-html-examples
2 parents 65d32dd + 33658fa commit cde7c42

2 files changed

Lines changed: 90 additions & 2 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Tests demonstrating assertEqualHTML's HTML character reference normalization.
4+
*
5+
* Any given character has multiple valid representations in HTML: a literal
6+
* character, a named reference (&not;), a decimal reference (&#172;), a
7+
* padded decimal (&#0172;), a hex reference (&#xAC;), a padded hex
8+
* (&#x0000AC;), or even a named reference without a semicolon (&not).
9+
* assertEqualHTML treats all of them as equivalent. assertSame does not.
10+
*
11+
* @package AssertEqualHTMLExamples
12+
*/
13+
14+
class CharacterReferenceEquivalenceTest extends WP_UnitTestCase {
15+
16+
/**
17+
* Demonstrates why assertSame is fragile for HTML character references.
18+
*
19+
* The NOT SIGN (¬, U+00AC) can be encoded as &not; or &#172; or &#xAC;,
20+
* among others. To assertSame, those strings look completely different even
21+
* though every browser renders them identically.
22+
*
23+
* This test is marked skipped so the suite stays green.
24+
* Comment out markTestSkipped() to see it fail.
25+
*/
26+
public function test_assertsame_fails_for_character_references(): void {
27+
$this->markTestSkipped(
28+
'Intentionally skipped. Remove markTestSkipped() to see how assertSame ' .
29+
'fails when the same character is encoded differently.'
30+
);
31+
32+
$expected = '<meta name="&not;">';
33+
$actual = '<meta name="&#172;">';
34+
35+
// ❌ This will fail even though both encode the same character (¬).
36+
$this->assertSame( $expected, $actual );
37+
}
38+
39+
/**
40+
* assertEqualHTML normalizes all representations of a character to the same
41+
* value before comparing, so every encoding of ¬ (U+00AC) is equivalent.
42+
*
43+
* Representations covered:
44+
* - Literal UTF-8 character
45+
* - Named reference: &not;
46+
* - Decimal reference: &#172;
47+
* - Padded decimal reference: &#0172;
48+
* - Hex reference: &#xAC;
49+
* - Padded hex reference: &#x0000AC;
50+
* - Named reference without semicolon: &not
51+
*/
52+
public function test_all_character_reference_forms_are_equivalent(): void {
53+
$expected = <<<HTML
54+
<meta
55+
not-literal="¬"
56+
not-named="¬"
57+
not-decimal="¬"
58+
not-decimal-padded="¬"
59+
not-hex="¬"
60+
not-hex-padded="¬"
61+
also-not="¬"
62+
>
63+
HTML;
64+
65+
$actual = <<<HTML
66+
<meta
67+
not-literal="¬"
68+
not-named="&not;"
69+
not-decimal="&#172;"
70+
not-decimal-padded="&#0172;"
71+
not-hex="&#xAC;"
72+
not-hex-padded="&#x0000AC;"
73+
also-not="&not"
74+
>
75+
HTML;
76+
77+
// ✅ Passes because assertEqualHTML decodes all character references
78+
// before comparing — literal, named, decimal, hex, with or without
79+
// a trailing semicolon.
80+
$this->assertEqualHTML( $expected, $actual );
81+
}
82+
}

tests/RenderCardBlockTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class RenderCardBlockTest extends WP_UnitTestCase {
2323
* actual output. Since the render callback concatenates $content directly
2424
* with no surrounding whitespace, the expected string must do the same:
2525
* `...><p>...</p></div>` with no gaps.
26+
*
27+
* NOWDOC syntax (<<<'HTML') keeps the expected markup readable without
28+
* variable interpolation. Indented closing markers (PHP 7.3+) let the
29+
* heredoc align naturally with the surrounding code.
2630
*/
2731
public function test_card_block_renders_with_background_color(): void {
2832
$attributes = array(
@@ -38,10 +42,12 @@ public function test_card_block_renders_with_background_color(): void {
3842
// Attributes may be on separate lines for readability — that's fine.
3943
// But there must be no whitespace between > and <p>, since the actual
4044
// output has none (whitespace-only text nodes are compared as-is).
41-
$expected = '<div
45+
$expected = <<<'HTML'
46+
<div
4247
class="is-style-outlined my-custom-class wp-block-my-plugin-card"
4348
style="background-color: #f5f5f5;"
44-
><p class="wp-block-paragraph">Hello</p></div>';
49+
><p class="wp-block-paragraph">Hello</p></div>
50+
HTML;
4551

4652
$this->assertEqualHTML( $expected, $output );
4753
}

0 commit comments

Comments
 (0)