123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- /**
- * @group formatting
- */
- class Tests_Formatting_WPSpecialchars extends WP_UnitTestCase {
- function test_wp_specialchars_basics() {
- $html = '&<hello world>';
- $this->assertSame( $html, _wp_specialchars( $html ) );
- $double = '&amp;&lt;hello world&gt;';
- $this->assertSame( $double, _wp_specialchars( $html, ENT_NOQUOTES, false, true ) );
- }
- function test_allowed_entity_names() {
- global $allowedentitynames;
- // Allowed entities should be unchanged.
- foreach ( $allowedentitynames as $ent ) {
- if ( 'apos' === $ent ) {
- // But for some reason, PHP doesn't allow '
- continue;
- }
- $ent = '&' . $ent . ';';
- $this->assertSame( $ent, _wp_specialchars( $ent ) );
- }
- }
- function test_not_allowed_entity_names() {
- $ents = array( 'iacut', 'aposs', 'pos', 'apo', 'apo?', 'apo.*', '.*apo.*', 'apos ', ' apos', ' apos ' );
- foreach ( $ents as $ent ) {
- $escaped = '&' . $ent . ';';
- $ent = '&' . $ent . ';';
- $this->assertSame( $escaped, _wp_specialchars( $ent ) );
- }
- }
- function test_optionally_escapes_quotes() {
- $source = "\"'hello!'\"";
- $this->assertSame( '"'hello!'"', _wp_specialchars( $source, 'single' ) );
- $this->assertSame( ""'hello!'"", _wp_specialchars( $source, 'double' ) );
- $this->assertSame( '"'hello!'"', _wp_specialchars( $source, true ) );
- $this->assertSame( $source, _wp_specialchars( $source ) );
- }
- /**
- * Check some of the double-encoding features for entity references.
- *
- * @ticket 17780
- * @dataProvider data_double_encoding
- */
- function test_double_encoding( $input, $output ) {
- return $this->assertSame( $output, _wp_specialchars( $input, ENT_NOQUOTES, false, true ) );
- }
- function data_double_encoding() {
- return array(
- array(
- 'This & that, this & that, — " " Ú " " " " " $ ×',
- 'This & that, this &amp; that, &#8212; &quot; &QUOT; &Uacute; &nbsp; &#34; &#034; &#0034; &#x00022; &#x22; &dollar; &times;',
- ),
- array(
- '&& && && &;',
- '&& &&amp; &amp;&amp; &amp;;',
- ),
- array(
- '&garbage; &***; &aaaa; &0000; &####; &;;',
- '&garbage; &***; &aaaa; &0000; &####; &;;',
- ),
- );
- }
- /**
- * Check some of the double-encoding features for entity references.
- *
- * @ticket 17780
- * @dataProvider data_no_double_encoding
- */
- function test_no_double_encoding( $input, $output ) {
- return $this->assertSame( $output, _wp_specialchars( $input, ENT_NOQUOTES, false, false ) );
- }
- function data_no_double_encoding() {
- return array(
- array(
- 'This & that, this & that, — " " Ú " " " " " $ ×',
- 'This & that, this & that, — " &QUOT; Ú " " " " " &dollar; ×',
- ),
- array(
- '&& && && &;',
- '&& && && &;',
- ),
- array(
- '&garbage; &***; &aaaa; &0000; &####; &;;',
- '&garbage; &***; &aaaa; &0000; &####; &;;',
- ),
- );
- }
- }
|