2009-05-20 05:09:50 +02:00
|
|
|
<?php
|
2013-05-26 01:01:46 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
/**
|
|
|
|
* Test various functions on the {@link Convert} class.
|
2013-05-26 01:01:46 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-05-20 05:09:50 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ConvertTest extends SapphireTest {
|
|
|
|
|
2013-05-26 01:01:46 +02:00
|
|
|
protected $usesDatabase = false;
|
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
/**
|
|
|
|
* Tests {@link Convert::raw2att()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRaw2Att() {
|
2009-05-20 05:09:50 +02:00
|
|
|
$val1 = '<input type="text">';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('<input type="text">', Convert::raw2att($val1),
|
|
|
|
'Special characters are escaped');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
$val2 = 'This is some normal text.';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('This is some normal text.', Convert::raw2att($val2),
|
|
|
|
'Normal text is not escaped');
|
2009-05-20 05:09:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
/**
|
|
|
|
* Tests {@link Convert::raw2htmlatt()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRaw2HtmlAtt() {
|
2009-05-20 05:09:50 +02:00
|
|
|
$val1 = '<input type="text">';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('<input type="text">', Convert::raw2htmlatt($val1),
|
|
|
|
'Special characters are escaped');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
$val2 = 'This is some normal text.';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('This is some normal text.', Convert::raw2htmlatt($val2),
|
|
|
|
'Normal text is not escaped');
|
2009-05-20 05:09:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHtml2raw() {
|
2014-08-15 08:53:05 +02:00
|
|
|
$val1 = 'This has a <strong>strong tag</strong>.';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('This has a *strong tag*.', Convert::xml2raw($val1),
|
|
|
|
'Strong tags are replaced with asterisks');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$val1 = 'This has a <b class="test" style="font-weight: bold">b tag with attributes</b>.';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('This has a *b tag with attributes*.', Convert::xml2raw($val1),
|
|
|
|
'B tags with attributes are replaced with asterisks');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$val2 = 'This has a <strong class="test" style="font-weight: bold">strong tag with attributes</STRONG>.';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('This has a *strong tag with attributes*.', Convert::xml2raw($val2),
|
|
|
|
'Strong tags with attributes are replaced with asterisks');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-14 19:55:52 +01:00
|
|
|
$val3 = '<script type="text/javascript">Some really nasty javascript here</script>';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('', Convert::xml2raw($val3),
|
|
|
|
'Script tags are completely removed');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-14 19:55:52 +01:00
|
|
|
$val4 = '<style type="text/css">Some really nasty CSS here</style>';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('', Convert::xml2raw($val4),
|
|
|
|
'Style tags are completely removed');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-14 19:55:52 +01:00
|
|
|
$val5 = '<script type="text/javascript">Some really nasty
|
|
|
|
multiline javascript here</script>';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('', Convert::xml2raw($val5),
|
|
|
|
'Multiline script tags are completely removed');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-14 19:55:52 +01:00
|
|
|
$val6 = '<style type="text/css">Some really nasty
|
|
|
|
multiline CSS here</style>';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('', Convert::xml2raw($val6),
|
|
|
|
'Multiline style tags are completely removed');
|
2010-10-15 05:51:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
/**
|
|
|
|
* Tests {@link Convert::raw2xml()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRaw2Xml() {
|
2009-05-20 05:09:50 +02:00
|
|
|
$val1 = '<input type="text">';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('<input type="text">', Convert::raw2xml($val1),
|
|
|
|
'Special characters are escaped');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
$val2 = 'This is some normal text.';
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('This is some normal text.', Convert::raw2xml($val2),
|
|
|
|
'Normal text is not escaped');
|
2010-12-16 23:55:17 +01:00
|
|
|
|
|
|
|
$val3 = "This is test\nNow on a new line.";
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals("This is test\nNow on a new line.", Convert::raw2xml($val3),
|
|
|
|
'Newlines are retained. They should not be replaced with <br /> as it is not XML valid');
|
2010-12-16 23:55:17 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-26 01:01:46 +02:00
|
|
|
/**
|
|
|
|
* Tests {@link Convert::raw2htmlid()}
|
|
|
|
*/
|
|
|
|
public function testRaw2HtmlID() {
|
2010-12-16 23:55:17 +01:00
|
|
|
$val1 = 'test test 123';
|
2013-05-26 01:01:46 +02:00
|
|
|
$this->assertEquals('test_test_123', Convert::raw2htmlid($val1));
|
|
|
|
|
|
|
|
$val1 = 'test[test][123]';
|
|
|
|
$this->assertEquals('test_test_123', Convert::raw2htmlid($val1));
|
|
|
|
|
|
|
|
$val1 = '[test[[test]][123]]';
|
|
|
|
$this->assertEquals('test_test_123', Convert::raw2htmlid($val1));
|
2009-05-20 05:09:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
/**
|
|
|
|
* Tests {@link Convert::xml2raw()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testXml2Raw() {
|
2009-05-20 05:09:50 +02:00
|
|
|
$val1 = '<input type="text">';
|
|
|
|
$this->assertEquals('<input type="text">', Convert::xml2raw($val1), 'Special characters are escaped');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
$val2 = 'This is some normal text.';
|
|
|
|
$this->assertEquals('This is some normal text.', Convert::xml2raw($val2), 'Normal text is not escaped');
|
|
|
|
}
|
2010-05-25 05:52:38 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArray2JSON() {
|
2010-05-25 05:52:38 +02:00
|
|
|
$val = array(
|
|
|
|
'Joe' => 'Bloggs',
|
|
|
|
'Tom' => 'Jones',
|
|
|
|
'My' => array(
|
|
|
|
'Complicated' => 'Structure'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$encoded = Convert::array2json($val);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}', $encoded,
|
|
|
|
'Array is encoded in JSON');
|
2010-05-25 05:52:38 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testJSON2Array() {
|
2010-05-25 05:52:38 +02:00
|
|
|
$val = '{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}';
|
|
|
|
$decoded = Convert::json2array($val);
|
|
|
|
$this->assertEquals(3, count($decoded), '3 items in the decoded array');
|
|
|
|
$this->assertContains('Bloggs', $decoded, 'Contains "Bloggs" value in decoded array');
|
|
|
|
$this->assertContains('Jones', $decoded, 'Contains "Jones" value in decoded array');
|
2012-03-30 05:18:14 +02:00
|
|
|
$this->assertContains('Structure', $decoded['My']['Complicated']);
|
2010-05-25 05:52:38 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testJSON2Obj() {
|
2010-05-25 05:52:38 +02:00
|
|
|
$val = '{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}';
|
|
|
|
$obj = Convert::json2obj($val);
|
|
|
|
$this->assertEquals('Bloggs', $obj->Joe);
|
|
|
|
$this->assertEquals('Jones', $obj->Tom);
|
|
|
|
$this->assertEquals('Structure', $obj->My->Complicated);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 04:18:51 +01:00
|
|
|
/**
|
|
|
|
* @todo test toASCII()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRaw2URL() {
|
2013-03-21 19:48:54 +01:00
|
|
|
$orig = Config::inst()->get('URLSegmentFilter', 'default_allow_multibyte');
|
|
|
|
Config::inst()->update('URLSegmentFilter', 'default_allow_multibyte', false);
|
2010-12-20 04:18:51 +01:00
|
|
|
$this->assertEquals('foo', Convert::raw2url('foo'));
|
|
|
|
$this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar'));
|
|
|
|
$this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar!'));
|
2011-11-14 12:26:51 +01:00
|
|
|
$this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('URLSegmentFilter', 'default_allow_multibyte', $orig);
|
2010-12-20 04:18:51 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
/**
|
|
|
|
* Helper function for comparing characters with significant whitespaces
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $expected
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $actual
|
2012-10-16 06:10:54 +02:00
|
|
|
*/
|
|
|
|
protected function assertEqualsQuoted($expected, $actual) {
|
|
|
|
$message = sprintf(
|
2014-08-15 08:53:05 +02:00
|
|
|
"Expected \"%s\" but given \"%s\"",
|
|
|
|
addcslashes($expected, "\r\n"),
|
2012-10-16 06:10:54 +02:00
|
|
|
addcslashes($actual, "\r\n")
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $actual, $message);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
public function testNL2OS() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
foreach(array("\r\n", "\r", "\n") as $nl) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
// Base case: no action
|
|
|
|
$this->assertEqualsQuoted(
|
|
|
|
"Base case",
|
|
|
|
Convert::nl2os("Base case", $nl)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
// Mixed formats
|
|
|
|
$this->assertEqualsQuoted(
|
|
|
|
"Test{$nl}Text{$nl}Is{$nl}{$nl}Here{$nl}.",
|
|
|
|
Convert::nl2os("Test\rText\r\nIs\n\rHere\r\n.", $nl)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
// Test that multiple runs are non-destructive
|
|
|
|
$expected = "Test{$nl}Text{$nl}Is{$nl}{$nl}Here{$nl}.";
|
|
|
|
$this->assertEqualsQuoted(
|
|
|
|
$expected,
|
|
|
|
Convert::nl2os($expected, $nl)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
// Check repeated sequence behaves correctly
|
|
|
|
$expected = "{$nl}{$nl}{$nl}{$nl}{$nl}{$nl}{$nl}{$nl}";
|
|
|
|
$input = "\r\r\n\r\r\n\n\n\n\r";
|
|
|
|
$this->assertEqualsQuoted(
|
|
|
|
$expected,
|
|
|
|
Convert::nl2os($input, $nl)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-02 06:19:14 +02:00
|
|
|
public function testRaw2JS() {
|
|
|
|
// Test attempt to break out of string
|
|
|
|
$this->assertEquals(
|
|
|
|
'\\"; window.location=\\"http://www.google.com',
|
|
|
|
Convert::raw2js('"; window.location="http://www.google.com')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'\\\'; window.location=\\\'http://www.google.com',
|
|
|
|
Convert::raw2js('\'; window.location=\'http://www.google.com')
|
|
|
|
);
|
|
|
|
// Test attempt to close script tag
|
|
|
|
$this->assertEquals(
|
|
|
|
'\\"; \\x3c/script\\x3e\\x3ch1\\x3eHa \\x26amp; Ha\\x3c/h1\\x3e\\x3cscript\\x3e',
|
|
|
|
Convert::raw2js('"; </script><h1>Ha & Ha</h1><script>')
|
|
|
|
);
|
|
|
|
// Test newlines are properly escaped
|
|
|
|
$this->assertEquals(
|
|
|
|
'New\\nLine\\rReturn', Convert::raw2js("New\nLine\rReturn")
|
|
|
|
);
|
|
|
|
// Check escape of slashes
|
|
|
|
$this->assertEquals(
|
|
|
|
'\\\\\\"\\x3eClick here',
|
|
|
|
Convert::raw2js('\\">Click here')
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-02 06:19:14 +02:00
|
|
|
public function testRaw2JSON() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-02 06:19:14 +02:00
|
|
|
// Test object
|
|
|
|
$input = new stdClass();
|
|
|
|
$input->Title = 'My Object';
|
|
|
|
$input->Content = '<p>Data</p>';
|
|
|
|
$this->assertEquals(
|
|
|
|
'{"Title":"My Object","Content":"<p>Data<\/p>"}',
|
|
|
|
Convert::raw2json($input)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-02 06:19:14 +02:00
|
|
|
// Array
|
|
|
|
$array = array('One' => 'Apple', 'Two' => 'Banana');
|
|
|
|
$this->assertEquals(
|
|
|
|
'{"One":"Apple","Two":"Banana"}',
|
|
|
|
Convert::raw2json($array)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-02 06:19:14 +02:00
|
|
|
// String value with already encoded data. Result should be quoted.
|
|
|
|
$value = '{"Left": "Value"}';
|
|
|
|
$this->assertEquals(
|
|
|
|
'"{\\"Left\\": \\"Value\\"}"',
|
|
|
|
Convert::raw2json($value)
|
|
|
|
);
|
|
|
|
}
|
2012-03-30 05:18:14 +02:00
|
|
|
}
|