mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-08 23:08:42 +00:00
0dd866cc15
- Added %Core.EscapeNonASCIICharacters to workaround %Core.Encoding misbehavior - Add "All Tests" to test runner title and reorder subfile names - Specific file is now called with ?f= - Link to UTF-8 docs, even though they're not done - 1000th unit test passed! W00t! (that's a third as many as SimpleTest has for itself.) git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@658 48356398-32a2-884e-a903-53898d9a118a
133 lines
4.2 KiB
PHP
133 lines
4.2 KiB
PHP
<?php
|
|
|
|
require_once 'HTMLPurifier/Encoder.php';
|
|
|
|
class HTMLPurifier_EncoderTest extends UnitTestCase
|
|
{
|
|
|
|
var $_entity_lookup;
|
|
|
|
function setUp() {
|
|
$this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
|
|
}
|
|
|
|
function assertCleanUTF8($string, $expect = null) {
|
|
if ($expect === null) $expect = $string;
|
|
$this->assertIdentical(HTMLPurifier_Encoder::cleanUTF8($string), $expect, 'iconv: %s');
|
|
$this->assertIdentical(HTMLPurifier_Encoder::cleanUTF8($string, true), $expect, 'PHP: %s');
|
|
}
|
|
|
|
function test_cleanUTF8() {
|
|
$this->assertCleanUTF8('Normal string.');
|
|
$this->assertCleanUTF8("Test\tAllowed\nControl\rCharacters");
|
|
$this->assertCleanUTF8("null byte: \0", 'null byte: ');
|
|
$this->assertCleanUTF8("\1\2\3\4\5\6\7", '');
|
|
$this->assertCleanUTF8("\x7F", ''); // one byte invalid SGML char
|
|
$this->assertCleanUTF8("\xC2\x80", ''); // two byte invalid SGML
|
|
$this->assertCleanUTF8("\xF3\xBF\xBF\xBF"); // valid four byte
|
|
$this->assertCleanUTF8("\xDF\xFF", ''); // malformed UTF8
|
|
}
|
|
|
|
function test_convertToUTF8() {
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
// UTF-8 means that we don't touch it
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertToUTF8("\xF6", $config, $context),
|
|
"\xF6" // this is invalid
|
|
);
|
|
$this->assertNoErrors();
|
|
|
|
$config->set('Core', 'Encoding', 'ISO-8859-1');
|
|
|
|
// Now it gets converted
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertToUTF8("\xF6", $config, $context),
|
|
"\xC3\xB6"
|
|
);
|
|
|
|
$config->set('Test', 'ForceNoIconv', true);
|
|
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertToUTF8("\xF6", $config, $context),
|
|
"\xC3\xB6"
|
|
);
|
|
|
|
}
|
|
|
|
function test_convertFromUTF8() {
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
// zhong-wen
|
|
$chinese = "\xE4\xB8\xAD\xE6\x96\x87 (Chinese)";
|
|
|
|
// UTF-8 means that we don't touch it
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $config, $context),
|
|
"\xC3\xB6"
|
|
);
|
|
|
|
$config->set('Core', 'Encoding', 'ISO-8859-1');
|
|
|
|
// Now it gets converted
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $config, $context),
|
|
"\xF6"
|
|
);
|
|
|
|
if (function_exists('iconv')) {
|
|
// iconv has it's own way
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertFromUTF8($chinese, $config, $context),
|
|
" (Chinese)"
|
|
);
|
|
}
|
|
|
|
// Plain PHP implementation has slightly different behavior
|
|
$config->set('Test', 'ForceNoIconv', true);
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $config, $context),
|
|
"\xF6"
|
|
);
|
|
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertFromUTF8($chinese, $config, $context),
|
|
"?? (Chinese)"
|
|
);
|
|
|
|
// Preserve the characters!
|
|
|
|
$config->set('Core', 'EscapeNonASCIICharacters', true);
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertFromUTF8($chinese, $config, $context),
|
|
"中文 (Chinese)"
|
|
);
|
|
|
|
}
|
|
|
|
function test_convertToASCIIDumbLossless() {
|
|
|
|
// Uppercase thorn letter
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertToASCIIDumbLossless("\xC3\x9Eorn"),
|
|
"Þorn"
|
|
);
|
|
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertToASCIIDumbLossless("an"),
|
|
"an"
|
|
);
|
|
|
|
// test up to four bytes
|
|
$this->assertIdentical(
|
|
HTMLPurifier_Encoder::convertToASCIIDumbLossless("\xF3\xA0\x80\xA0"),
|
|
"󠀠"
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|