*/
class CRM_Core_DAOTest extends CiviUnitTestCase {
+ const ABORTED_SQL = "_aborted_sql_";
+
public function testGetReferenceColumns() {
// choose CRM_Core_DAO_Email as an arbitrary example
$emailRefs = CRM_Core_DAO_Email::getReferenceColumns();
$cases = array();
// $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
+ $cases[0] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'String')), 'UPDATE civicrm_foo SET bar = \'\'');
+ $cases[1] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('the text', 'String')), 'UPDATE civicrm_foo SET bar = \'the text\'');
+ $cases[2] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'String')), self::ABORTED_SQL);
+ $cases[3] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'String')), 'UPDATE civicrm_foo SET bar = NULL');
+
+ $cases[3] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Float')), self::ABORTED_SQL);
+ $cases[4] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('1.23', 'Float')), 'UPDATE civicrm_foo SET bar = 1.23');
+ $cases[5] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Float')), self::ABORTED_SQL);
+ $cases[6] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Float')), self::ABORTED_SQL);
+
+ $cases[11] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Money')), self::ABORTED_SQL);
+ $cases[12] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('1.23', 'Money')), 'UPDATE civicrm_foo SET bar = 1.23');
+ $cases[13] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Money')), self::ABORTED_SQL);
+ $cases[14] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Money')), self::ABORTED_SQL);
+
+ $cases[15] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Int')), self::ABORTED_SQL);
+ $cases[16] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('123', 'Int')), 'UPDATE civicrm_foo SET bar = 123');
+ $cases[17] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Int')), self::ABORTED_SQL);
+ $cases[18] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Int')), self::ABORTED_SQL);
+
+ $cases[19] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Timestamp')), 'UPDATE civicrm_foo SET bar = null');
+ $cases[20] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('20150102030405', 'Timestamp')), 'UPDATE civicrm_foo SET bar = 20150102030405');
+ $cases[21] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Timestamp')), 'UPDATE civicrm_foo SET bar = null');
+ $cases[22] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Timestamp')), self::ABORTED_SQL);
+
// CASE: No params
- $cases[] = array(
+ $cases[1000] = array(
'SELECT * FROM whatever',
array(),
'SELECT * FROM whatever',
);
// CASE: Integer param
- $cases[] = array(
+ $cases[1001] = array(
'SELECT * FROM whatever WHERE id = %1',
array(
1 => array(10, 'Integer'),
);
// CASE: String param
- $cases[] = array(
+ $cases[1002] = array(
'SELECT * FROM whatever WHERE name = %1',
array(
1 => array('Alice', 'String'),
);
// CASE: Two params
- $cases[] = array(
+ $cases[1003] = array(
'SELECT * FROM whatever WHERE name = %1 AND title = %2',
array(
1 => array('Alice', 'String'),
);
// CASE: Two params with special character (%1)
- $cases[] = array(
+ $cases[1004] = array(
'SELECT * FROM whatever WHERE name = %1 AND title = %2',
array(
1 => array('Alice %2', 'String'),
);
// CASE: Two params with special character ($1)
- $cases[] = array(
+ $cases[1005] = array(
'SELECT * FROM whatever WHERE name = %1 AND title = %2',
array(
1 => array('Alice $1', 'String'),
* @param $expectSql
*/
public function testComposeQuery($inputSql, $inputParams, $expectSql) {
- $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
+ $scope = CRM_Core_TemporaryErrorScope::useException();
+ try {
+ $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
+ }
+ catch (Exception $e) {
+ $actualSql = self::ABORTED_SQL;
+ }
$this->assertEquals($expectSql, $actualSql);
}
list($inputSql, $inputParams, $expectSql) = $cases[0];
$actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
$this->assertFalse(($expectSql == $actualSql));
+ unset($scope);
}
/**