mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
30b2a950fe
MINOR Added unit tests for Convert JSON functions (from r100423) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@105567 467b73ca-7a2a-4603-9d3b-597d59a354a9
81 lines
2.6 KiB
PHP
81 lines
2.6 KiB
PHP
<?php
|
|
/**
|
|
* Test various functions on the {@link Convert} class.
|
|
* @package sapphire
|
|
* @subpackage tests
|
|
*/
|
|
class ConvertTest extends SapphireTest {
|
|
|
|
/**
|
|
* Tests {@link Convert::raw2att()}
|
|
*/
|
|
function testRaw2Att() {
|
|
$val1 = '<input type="text">';
|
|
$this->assertEquals('<input type="text">', Convert::raw2att($val1), 'Special characters are escaped');
|
|
|
|
$val2 = 'This is some normal text.';
|
|
$this->assertEquals('This is some normal text.', Convert::raw2att($val2), 'Normal text is not escaped');
|
|
}
|
|
|
|
/**
|
|
* Tests {@link Convert::raw2htmlatt()}
|
|
*/
|
|
function testRaw2HtmlAtt() {
|
|
$val1 = '<input type="text">';
|
|
$this->assertEquals('ltinputtypequottextquotgt', Convert::raw2htmlatt($val1), 'Special characters are escaped');
|
|
|
|
$val2 = 'This is some normal text.';
|
|
$this->assertEquals('Thisissomenormaltext', Convert::raw2htmlatt($val2), 'Normal text is not escaped');
|
|
}
|
|
|
|
/**
|
|
* Tests {@link Convert::raw2xml()}
|
|
*/
|
|
function testRaw2Xml() {
|
|
$val1 = '<input type="text">';
|
|
$this->assertEquals('<input type="text">', Convert::raw2xml($val1), 'Special characters are escaped');
|
|
|
|
$val2 = 'This is some normal text.';
|
|
$this->assertEquals('This is some normal text.', Convert::raw2xml($val2), 'Normal text is not escaped');
|
|
}
|
|
|
|
/**
|
|
* Tests {@link Convert::xml2raw()}
|
|
*/
|
|
function testXml2Raw() {
|
|
$val1 = '<input type="text">';
|
|
$this->assertEquals('<input type="text">', Convert::xml2raw($val1), 'Special characters are escaped');
|
|
|
|
$val2 = 'This is some normal text.';
|
|
$this->assertEquals('This is some normal text.', Convert::xml2raw($val2), 'Normal text is not escaped');
|
|
}
|
|
|
|
function testArray2JSON() {
|
|
$val = array(
|
|
'Joe' => 'Bloggs',
|
|
'Tom' => 'Jones',
|
|
'My' => array(
|
|
'Complicated' => 'Structure'
|
|
)
|
|
);
|
|
$encoded = Convert::array2json($val);
|
|
$this->assertEquals('{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}', $encoded, 'Array is encoded in JSON');
|
|
}
|
|
|
|
function testJSON2Array() {
|
|
$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');
|
|
}
|
|
|
|
function testJSON2Obj() {
|
|
$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);
|
|
}
|
|
|
|
} |