2008-09-11 08:22:33 +02:00
|
|
|
<?php
|
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Tests for DBField objects.
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*
|
|
|
|
*/
|
2008-09-11 08:22:33 +02:00
|
|
|
class DBFieldTest extends SapphireTest {
|
|
|
|
|
2010-10-13 03:43:58 +02:00
|
|
|
/**
|
|
|
|
* Test the nullValue() method on DBField.
|
|
|
|
*/
|
|
|
|
function testNullValue() {
|
|
|
|
/* Float and Double use 0 for "null" value representation */
|
|
|
|
$this->assertEquals(0, singleton('Float')->nullValue());
|
|
|
|
$this->assertEquals(0, singleton('Double')->nullValue());
|
|
|
|
}
|
|
|
|
|
2008-09-11 08:22:33 +02:00
|
|
|
/**
|
|
|
|
* Test the prepValueForDB() method on DBField.
|
|
|
|
*/
|
|
|
|
function testPrepValueForDB() {
|
2012-03-20 09:20:35 +01:00
|
|
|
$db = DB::getConn();
|
|
|
|
|
2010-10-13 03:43:58 +02:00
|
|
|
/* Float behaviour, asserting we have 0 */
|
|
|
|
$this->assertEquals('0', singleton('Float')->prepValueForDB(0));
|
|
|
|
$this->assertEquals('0', singleton('Float')->prepValueForDB(null));
|
|
|
|
$this->assertEquals('0', singleton('Float')->prepValueForDB(false));
|
|
|
|
$this->assertEquals('0', singleton('Float')->prepValueForDB(''));
|
|
|
|
$this->assertEquals('0', singleton('Float')->prepValueForDB('0'));
|
|
|
|
|
|
|
|
/* Double behaviour, asserting we have 0 */
|
|
|
|
$this->assertEquals('0', singleton('Double')->prepValueForDB(0));
|
|
|
|
$this->assertEquals('0', singleton('Double')->prepValueForDB(null));
|
|
|
|
$this->assertEquals('0', singleton('Double')->prepValueForDB(false));
|
|
|
|
$this->assertEquals('0', singleton('Double')->prepValueForDB(''));
|
|
|
|
$this->assertEquals('0', singleton('Double')->prepValueForDB('0'));
|
|
|
|
|
2008-09-11 08:22:33 +02:00
|
|
|
/* Integer behaviour, asserting we have 0 */
|
|
|
|
$this->assertEquals('0', singleton('Int')->prepValueForDB(0));
|
|
|
|
$this->assertEquals('0', singleton('Int')->prepValueForDB(null));
|
|
|
|
$this->assertEquals('0', singleton('Int')->prepValueForDB(false));
|
|
|
|
$this->assertEquals('0', singleton('Int')->prepValueForDB(''));
|
|
|
|
$this->assertEquals('0', singleton('Int')->prepValueForDB('0'));
|
|
|
|
|
|
|
|
/* Integer behaviour, asserting we have 1 */
|
|
|
|
$this->assertEquals('1', singleton('Int')->prepValueForDB(true));
|
|
|
|
$this->assertEquals('1', singleton('Int')->prepValueForDB(1));
|
|
|
|
$this->assertEquals('1', singleton('Int')->prepValueForDB('1'));
|
|
|
|
|
|
|
|
/* Decimal behaviour, asserting we have 0 */
|
|
|
|
$this->assertEquals('0', singleton('Decimal')->prepValueForDB(0));
|
|
|
|
$this->assertEquals('0', singleton('Decimal')->prepValueForDB(null));
|
|
|
|
$this->assertEquals('0', singleton('Decimal')->prepValueForDB(false));
|
|
|
|
$this->assertEquals('0', singleton('Decimal')->prepValueForDB(''));
|
|
|
|
$this->assertEquals('0', singleton('Decimal')->prepValueForDB('0'));
|
|
|
|
|
|
|
|
/* Decimal behaviour, asserting we have 1 */
|
|
|
|
$this->assertEquals('1', singleton('Decimal')->prepValueForDB(true));
|
|
|
|
$this->assertEquals('1', singleton('Decimal')->prepValueForDB(1));
|
|
|
|
$this->assertEquals('1', singleton('Decimal')->prepValueForDB('1'));
|
|
|
|
|
|
|
|
/* Boolean behaviour, asserting we have 0 */
|
2008-11-24 20:28:46 +01:00
|
|
|
$this->assertEquals("'0'", singleton('Boolean')->prepValueForDB(0));
|
|
|
|
$this->assertEquals("'0'", singleton('Boolean')->prepValueForDB(null));
|
|
|
|
$this->assertEquals("'0'", singleton('Boolean')->prepValueForDB(false));
|
|
|
|
$this->assertEquals("'0'", singleton('Boolean')->prepValueForDB(''));
|
|
|
|
$this->assertEquals("'0'", singleton('Boolean')->prepValueForDB('0'));
|
2008-09-11 08:22:33 +02:00
|
|
|
|
|
|
|
/* Boolean behaviour, asserting we have 1 */
|
2008-11-24 20:28:46 +01:00
|
|
|
$this->assertEquals("'1'", singleton('Boolean')->prepValueForDB(true));
|
|
|
|
$this->assertEquals("'1'", singleton('Boolean')->prepValueForDB(1));
|
|
|
|
$this->assertEquals("'1'", singleton('Boolean')->prepValueForDB('1'));
|
2008-09-11 08:22:33 +02:00
|
|
|
|
|
|
|
/* Varchar behaviour */
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertEquals($db->prepStringForDB("0"), singleton('Varchar')->prepValueForDB(0));
|
2008-09-11 08:22:33 +02:00
|
|
|
$this->assertEquals("'0'", singleton('Varchar')->prepValueForDB(0));
|
|
|
|
$this->assertEquals("null", singleton('Varchar')->prepValueForDB(null));
|
|
|
|
$this->assertEquals("null", singleton('Varchar')->prepValueForDB(false));
|
|
|
|
$this->assertEquals("null", singleton('Varchar')->prepValueForDB(''));
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertEquals($db->prepStringForDB("0"), singleton('Varchar')->prepValueForDB('0'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("1"), singleton('Varchar')->prepValueForDB(1));
|
|
|
|
$this->assertEquals($db->prepStringForDB("1"), singleton('Varchar')->prepValueForDB(true));
|
|
|
|
$this->assertEquals($db->prepStringForDB("1"), singleton('Varchar')->prepValueForDB('1'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("00000"), singleton('Varchar')->prepValueForDB('00000'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("0"), singleton('Varchar')->prepValueForDB(0000));
|
|
|
|
$this->assertEquals($db->prepStringForDB("test"), singleton('Varchar')->prepValueForDB('test'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("123"), singleton('Varchar')->prepValueForDB(123));
|
2008-09-11 08:22:33 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/* AllowEmpty Varchar behaviour */
|
|
|
|
$varcharField = new Varchar("testfield", 50, array("nullifyEmpty"=>false));
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertSame($db->prepStringForDB("0"), $varcharField->prepValueForDB(0));
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->assertSame("null", $varcharField->prepValueForDB(null));
|
|
|
|
$this->assertSame("null", $varcharField->prepValueForDB(false));
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertSame($db->prepStringForDB(""), $varcharField->prepValueForDB(''));
|
|
|
|
$this->assertSame($db->prepStringForDB("0"), $varcharField->prepValueForDB('0'));
|
|
|
|
$this->assertSame($db->prepStringForDB("1"), $varcharField->prepValueForDB(1));
|
|
|
|
$this->assertSame($db->prepStringForDB("1"), $varcharField->prepValueForDB(true));
|
|
|
|
$this->assertSame($db->prepStringForDB("1"), $varcharField->prepValueForDB('1'));
|
|
|
|
$this->assertSame($db->prepStringForDB("00000"), $varcharField->prepValueForDB('00000'));
|
|
|
|
$this->assertSame($db->prepStringForDB("0"), $varcharField->prepValueForDB(0000));
|
|
|
|
$this->assertSame($db->prepStringForDB("test"), $varcharField->prepValueForDB('test'));
|
|
|
|
$this->assertSame($db->prepStringForDB("123"), $varcharField->prepValueForDB(123));
|
2009-10-23 03:29:55 +02:00
|
|
|
unset($varcharField);
|
|
|
|
|
2008-09-11 08:22:33 +02:00
|
|
|
/* Text behaviour */
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertEquals($db->prepStringForDB("0"), singleton('Text')->prepValueForDB(0));
|
2008-09-11 08:22:33 +02:00
|
|
|
$this->assertEquals("null", singleton('Text')->prepValueForDB(null));
|
|
|
|
$this->assertEquals("null", singleton('Text')->prepValueForDB(false));
|
|
|
|
$this->assertEquals("null", singleton('Text')->prepValueForDB(''));
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertEquals($db->prepStringForDB("0"), singleton('Text')->prepValueForDB('0'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("1"), singleton('Text')->prepValueForDB(1));
|
|
|
|
$this->assertEquals($db->prepStringForDB("1"), singleton('Text')->prepValueForDB(true));
|
|
|
|
$this->assertEquals($db->prepStringForDB("1"), singleton('Text')->prepValueForDB('1'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("00000"), singleton('Text')->prepValueForDB('00000'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("0"), singleton('Text')->prepValueForDB(0000));
|
|
|
|
$this->assertEquals($db->prepStringForDB("test"), singleton('Text')->prepValueForDB('test'));
|
|
|
|
$this->assertEquals($db->prepStringForDB("123"), singleton('Text')->prepValueForDB(123));
|
2009-10-23 03:29:55 +02:00
|
|
|
|
|
|
|
/* AllowEmpty Text behaviour */
|
|
|
|
$textField = new Text("testfield", array("nullifyEmpty"=>false));
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertSame($db->prepStringForDB("0"), $textField->prepValueForDB(0));
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->assertSame("null", $textField->prepValueForDB(null));
|
|
|
|
$this->assertSame("null", $textField->prepValueForDB(false));
|
2012-03-20 09:20:35 +01:00
|
|
|
$this->assertSame($db->prepStringForDB(""), $textField->prepValueForDB(''));
|
|
|
|
$this->assertSame($db->prepStringForDB("0"), $textField->prepValueForDB('0'));
|
|
|
|
$this->assertSame($db->prepStringForDB("1"), $textField->prepValueForDB(1));
|
|
|
|
$this->assertSame($db->prepStringForDB("1"), $textField->prepValueForDB(true));
|
|
|
|
$this->assertSame($db->prepStringForDB("1"), $textField->prepValueForDB('1'));
|
|
|
|
$this->assertSame($db->prepStringForDB("00000"), $textField->prepValueForDB('00000'));
|
|
|
|
$this->assertSame($db->prepStringForDB("0"), $textField->prepValueForDB(0000));
|
|
|
|
$this->assertSame($db->prepStringForDB("test"), $textField->prepValueForDB('test'));
|
|
|
|
$this->assertSame($db->prepStringForDB("123"), $textField->prepValueForDB(123));
|
2009-10-23 03:29:55 +02:00
|
|
|
unset($textField);
|
2009-08-10 05:56:59 +02:00
|
|
|
|
|
|
|
/* Time behaviour */
|
|
|
|
$time = singleton('Time');
|
|
|
|
$time->setValue('00:01am');
|
|
|
|
$this->assertEquals("00:01:00", $time->getValue());
|
|
|
|
$time->setValue('00:59am');
|
|
|
|
$this->assertEquals("00:59:00", $time->getValue());
|
|
|
|
$time->setValue('11:59am');
|
|
|
|
$this->assertEquals("11:59:00", $time->getValue());
|
|
|
|
$time->setValue('12:00pm');
|
|
|
|
$this->assertEquals("12:00:00", $time->getValue());
|
|
|
|
$time->setValue('12:59am');
|
|
|
|
$this->assertEquals("12:59:00", $time->getValue());
|
|
|
|
$time->setValue('1:00pm');
|
|
|
|
$this->assertEquals("13:00:00", $time->getValue());
|
|
|
|
$time->setValue('11:59pm');
|
|
|
|
$this->assertEquals("23:59:00", $time->getValue());
|
|
|
|
$time->setValue('00:00am');
|
|
|
|
$this->assertEquals("00:00:00", $time->getValue());
|
|
|
|
$time->setValue('00:00:00');
|
|
|
|
$this->assertEquals("00:00:00", $time->getValue());
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
function testHasValue() {
|
|
|
|
$varcharField = new Varchar("testfield");
|
|
|
|
$this->assertTrue($varcharField->getNullifyEmpty());
|
|
|
|
$varcharField->setValue('abc');
|
|
|
|
$this->assertTrue($varcharField->hasValue());
|
|
|
|
$varcharField->setValue('');
|
|
|
|
$this->assertFalse($varcharField->hasValue());
|
|
|
|
$varcharField->setValue(null);
|
|
|
|
$this->assertFalse($varcharField->hasValue());
|
|
|
|
|
|
|
|
$varcharField = new Varchar("testfield", 50, array('nullifyEmpty'=>false));
|
|
|
|
$this->assertFalse($varcharField->getNullifyEmpty());
|
|
|
|
$varcharField->setValue('abc');
|
|
|
|
$this->assertTrue($varcharField->hasValue());
|
|
|
|
$varcharField->setValue('');
|
|
|
|
$this->assertTrue($varcharField->hasValue());
|
|
|
|
$varcharField->setValue(null);
|
|
|
|
$this->assertFalse($varcharField->hasValue());
|
|
|
|
|
|
|
|
$textField = new Text("testfield");
|
|
|
|
$this->assertTrue($textField->getNullifyEmpty());
|
|
|
|
$textField->setValue('abc');
|
|
|
|
$this->assertTrue($textField->hasValue());
|
|
|
|
$textField->setValue('');
|
|
|
|
$this->assertFalse($textField->hasValue());
|
|
|
|
$textField->setValue(null);
|
|
|
|
$this->assertFalse($textField->hasValue());
|
|
|
|
|
|
|
|
$textField = new Text("testfield", array('nullifyEmpty'=>false));
|
|
|
|
$this->assertFalse($textField->getNullifyEmpty());
|
|
|
|
$textField->setValue('abc');
|
|
|
|
$this->assertTrue($textField->hasValue());
|
|
|
|
$textField->setValue('');
|
|
|
|
$this->assertTrue($textField->hasValue());
|
|
|
|
$textField->setValue(null);
|
|
|
|
$this->assertFalse($textField->hasValue());
|
|
|
|
}
|
2011-12-17 00:27:14 +01:00
|
|
|
|
|
|
|
function testStringFieldsWithMultibyteData() {
|
|
|
|
$plainFields = array('Varchar', 'Text');
|
|
|
|
$htmlFields = array('HTMLVarchar', 'HTMLText');
|
|
|
|
$allFields = array_merge($plainFields, $htmlFields);
|
|
|
|
|
|
|
|
$value = 'üåäöÜÅÄÖ';
|
|
|
|
foreach ($allFields as $stringField) {
|
2012-03-27 06:57:42 +02:00
|
|
|
$stringField = DBField::create_field($stringField, $value);
|
2011-12-17 00:27:14 +01:00
|
|
|
for ($i = 1; $i < mb_strlen($value); $i++) {
|
|
|
|
$expected = mb_substr($value, 0, $i) . '...';
|
|
|
|
$this->assertEquals($expected, $stringField->LimitCharacters($i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = '<p>üåäö&ÜÅÄÖ</p>';
|
|
|
|
foreach ($htmlFields as $stringField) {
|
2012-03-27 06:57:42 +02:00
|
|
|
$stringField = DBField::create_field($stringField, $value);
|
2011-12-17 00:27:14 +01:00
|
|
|
$this->assertEquals('üåäö&ÜÅÄ...', $stringField->LimitCharacters(8));
|
|
|
|
}
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('ÅÄÖ', DBField::create_field('Text', 'åäö')->UpperCase());
|
|
|
|
$this->assertEquals('åäö', DBField::create_field('Text', 'ÅÄÖ')->LowerCase());
|
2011-12-17 00:27:14 +01:00
|
|
|
}
|
|
|
|
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|