WPSpecialchars.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_WPSpecialchars extends WP_UnitTestCase {
  6. function test_wp_specialchars_basics() {
  7. $html = "&amp;&lt;hello world&gt;";
  8. $this->assertEquals( $html, _wp_specialchars( $html ) );
  9. $double = "&amp;amp;&amp;lt;hello world&amp;gt;";
  10. $this->assertEquals( $double, _wp_specialchars( $html, ENT_NOQUOTES, false, true ) );
  11. }
  12. function test_allowed_entity_names() {
  13. global $allowedentitynames;
  14. // Allowed entities should be unchanged
  15. foreach ( $allowedentitynames as $ent ) {
  16. $ent = '&' . $ent . ';';
  17. $this->assertEquals( $ent, _wp_specialchars( $ent ) );
  18. }
  19. }
  20. function test_not_allowed_entity_names() {
  21. $ents = array( 'iacut', 'aposs', 'pos', 'apo', 'apo?', 'apo.*', '.*apo.*', 'apos ', ' apos', ' apos ' );
  22. foreach ( $ents as $ent ) {
  23. $escaped = '&amp;' . $ent . ';';
  24. $ent = '&' . $ent . ';';
  25. $this->assertEquals( $escaped, _wp_specialchars( $ent ) );
  26. }
  27. }
  28. function test_optionally_escapes_quotes() {
  29. $source = "\"'hello!'\"";
  30. $this->assertEquals( '"&#039;hello!&#039;"', _wp_specialchars($source, 'single') );
  31. $this->assertEquals( "&quot;'hello!'&quot;", _wp_specialchars($source, 'double') );
  32. $this->assertEquals( '&quot;&#039;hello!&#039;&quot;', _wp_specialchars($source, true) );
  33. $this->assertEquals( $source, _wp_specialchars($source) );
  34. }
  35. }