mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
d8e9af8af8
Database abstraction broken up into controller, connector, query builder, and schema manager, each independently configurable via YAML / Injector Creation of new DBQueryGenerator for database specific generation of SQL Support for parameterised queries, move of code base to use these over escaped conditions Refactor of SQLQuery into separate query classes for each of INSERT UPDATE DELETE and SELECT Support for PDO Installation process upgraded to use new ORM SS_DatabaseException created to handle database errors, maintaining details of raw sql and parameter details for user code designed interested in that data. Renamed DB static methods to conform correctly to naming conventions (e.g. DB::getConn -> DB::get_conn) 3.2 upgrade docs Performance Optimisation and simplification of code to use more concise API API Ability for database adapters to register extensions to ConfigureFromEnv.php
203 lines
6.6 KiB
PHP
203 lines
6.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Test various functions on the {@link Convert} class.
|
|
*
|
|
* @package framework
|
|
* @subpackage tests
|
|
*/
|
|
class ConvertTest extends SapphireTest {
|
|
|
|
protected $usesDatabase = false;
|
|
|
|
/**
|
|
* Tests {@link Convert::raw2att()}
|
|
*/
|
|
public 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()}
|
|
*/
|
|
public function testRaw2HtmlAtt() {
|
|
$val1 = '<input type="text">';
|
|
$this->assertEquals('<input type="text">', Convert::raw2htmlatt($val1),
|
|
'Special characters are escaped');
|
|
|
|
$val2 = 'This is some normal text.';
|
|
$this->assertEquals('This is some normal text.', Convert::raw2htmlatt($val2),
|
|
'Normal text is not escaped');
|
|
}
|
|
|
|
public function testHtml2raw() {
|
|
$val1 = 'This has a <strong>strong tag</strong>.';
|
|
$this->assertEquals('This has a *strong tag*.', Convert::xml2raw($val1),
|
|
'Strong tags are replaced with asterisks');
|
|
|
|
$val1 = 'This has a <b class="test" style="font-weight: bold">b tag with attributes</b>.';
|
|
$this->assertEquals('This has a *b tag with attributes*.', Convert::xml2raw($val1),
|
|
'B tags with attributes are replaced with asterisks');
|
|
|
|
$val2 = 'This has a <strong class="test" style="font-weight: bold">strong tag with attributes</STRONG>.';
|
|
$this->assertEquals('This has a *strong tag with attributes*.', Convert::xml2raw($val2),
|
|
'Strong tags with attributes are replaced with asterisks');
|
|
|
|
$val3 = '<script type="text/javascript">Some really nasty javascript here</script>';
|
|
$this->assertEquals('', Convert::xml2raw($val3),
|
|
'Script tags are completely removed');
|
|
|
|
$val4 = '<style type="text/css">Some really nasty CSS here</style>';
|
|
$this->assertEquals('', Convert::xml2raw($val4),
|
|
'Style tags are completely removed');
|
|
|
|
$val5 = '<script type="text/javascript">Some really nasty
|
|
multiline javascript here</script>';
|
|
$this->assertEquals('', Convert::xml2raw($val5),
|
|
'Multiline script tags are completely removed');
|
|
|
|
$val6 = '<style type="text/css">Some really nasty
|
|
multiline CSS here</style>';
|
|
$this->assertEquals('', Convert::xml2raw($val6),
|
|
'Multiline style tags are completely removed');
|
|
}
|
|
|
|
/**
|
|
* Tests {@link Convert::raw2xml()}
|
|
*/
|
|
public 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');
|
|
|
|
$val3 = "This is test\nNow on a new line.";
|
|
$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');
|
|
}
|
|
|
|
/**
|
|
* Tests {@link Convert::raw2htmlid()}
|
|
*/
|
|
public function testRaw2HtmlID() {
|
|
$val1 = 'test test 123';
|
|
$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));
|
|
}
|
|
|
|
/**
|
|
* Tests {@link Convert::xml2raw()}
|
|
*/
|
|
public 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');
|
|
}
|
|
|
|
public 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');
|
|
}
|
|
|
|
public 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');
|
|
$this->assertContains('Structure', $decoded['My']['Complicated']);
|
|
}
|
|
|
|
public 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);
|
|
}
|
|
|
|
/**
|
|
* @todo test toASCII()
|
|
*/
|
|
public function testRaw2URL() {
|
|
$orig = Config::inst()->get('URLSegmentFilter', 'default_allow_multibyte');
|
|
Config::inst()->update('URLSegmentFilter', 'default_allow_multibyte', false);
|
|
$this->assertEquals('foo', Convert::raw2url('foo'));
|
|
$this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar'));
|
|
$this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar!'));
|
|
$this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
|
|
Config::inst()->update('URLSegmentFilter', 'default_allow_multibyte', $orig);
|
|
}
|
|
|
|
/**
|
|
* Helper function for comparing characters with significant whitespaces
|
|
* @param string $expected
|
|
* @param string $actual
|
|
*/
|
|
protected function assertEqualsQuoted($expected, $actual) {
|
|
$message = sprintf(
|
|
"Expected \"%s\" but given \"%s\"",
|
|
addcslashes($expected, "\r\n"),
|
|
addcslashes($actual, "\r\n")
|
|
);
|
|
$this->assertEquals($expected, $actual, $message);
|
|
}
|
|
|
|
public function testNL2OS() {
|
|
|
|
foreach(array("\r\n", "\r", "\n") as $nl) {
|
|
|
|
// Base case: no action
|
|
$this->assertEqualsQuoted(
|
|
"Base case",
|
|
Convert::nl2os("Base case", $nl)
|
|
);
|
|
|
|
// Mixed formats
|
|
$this->assertEqualsQuoted(
|
|
"Test{$nl}Text{$nl}Is{$nl}{$nl}Here{$nl}.",
|
|
Convert::nl2os("Test\rText\r\nIs\n\rHere\r\n.", $nl)
|
|
);
|
|
|
|
// Test that multiple runs are non-destructive
|
|
$expected = "Test{$nl}Text{$nl}Is{$nl}{$nl}Here{$nl}.";
|
|
$this->assertEqualsQuoted(
|
|
$expected,
|
|
Convert::nl2os($expected, $nl)
|
|
);
|
|
|
|
// 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)
|
|
);
|
|
}
|
|
}
|
|
}
|