3 require_once 'CiviTest/CiviUnitTestCase.php';
4 class CRM_Core_DAOTest
extends CiviUnitTestCase
{
8 'description' => 'Test core DAO functions',
13 function testGetReferenceColumns() {
14 // choose CRM_Core_DAO_Email as an arbitrary example
15 $emailRefs = CRM_Core_DAO_Email
::getReferenceColumns();
16 $refsByTarget = array();
17 foreach ($emailRefs as $refSpec) {
18 $refsByTarget[$refSpec->getTargetTable()] = $refSpec;
20 $this->assertTrue(array_key_exists('civicrm_contact', $refsByTarget));
21 $contactRef = $refsByTarget['civicrm_contact'];
22 $this->assertEquals('contact_id', $contactRef->getReferenceKey());
23 $this->assertEquals('id', $contactRef->getTargetKey());
24 $this->assertEquals(FALSE, $contactRef->isGeneric());
27 function testGetReferencesToTable() {
28 $refs = CRM_Core_DAO
::getReferencesToTable(CRM_Financial_DAO_FinancialType
::getTableName());
29 $refsBySource = array();
30 foreach ($refs as $refSpec) {
31 $refsBySource[$refSpec->getReferenceTable()] = $refSpec;
33 $this->assertTrue(array_key_exists('civicrm_entity_financial_account', $refsBySource));
34 $genericRef = $refsBySource['civicrm_entity_financial_account'];
35 $this->assertEquals('entity_id', $genericRef->getReferenceKey());
36 $this->assertEquals('entity_table', $genericRef->getTypeColumn());
37 $this->assertEquals('id', $genericRef->getTargetKey());
38 $this->assertEquals(TRUE, $genericRef->isGeneric());
41 function testFindReferences() {
43 'first_name' => 'Testy',
44 'last_name' => 'McScallion',
45 'contact_type' => 'Individual',
48 $contact = CRM_Contact_BAO_Contact
::add($params);
49 $this->assertNotNull($contact->id
);
52 'email' => 'spam@dev.null',
53 'contact_id' => $contact->id
,
55 'location_type_id' => 1,
58 $email = CRM_Core_BAO_Email
::add($params);
60 $refs = $contact->findReferences();
61 $refsByTable = array();
62 foreach ($refs as $refObj) {
63 $refsByTable[$refObj->__table
] = $refObj;
66 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
67 $refDao = $refsByTable['civicrm_email'];
69 $this->assertEquals($contact->id
, $refDao->contact_id
);
72 function composeQueryExamples() {
74 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
78 'SELECT * FROM whatever',
80 'SELECT * FROM whatever',
83 // CASE: Integer param
85 'SELECT * FROM whatever WHERE id = %1',
87 1 => array(10, 'Integer'),
89 'SELECT * FROM whatever WHERE id = 10',
94 'SELECT * FROM whatever WHERE name = %1',
96 1 => array('Alice', 'String'),
98 'SELECT * FROM whatever WHERE name = \'Alice\'',
103 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
105 1 => array('Alice', 'String'),
106 2 => array('Bob', 'String'),
108 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
111 // CASE: Two params with special character (%1)
113 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
115 1 => array('Alice %2', 'String'),
116 2 => array('Bob', 'String'),
118 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
121 // CASE: Two params with special character ($1)
123 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
125 1 => array('Alice $1', 'String'),
126 2 => array('Bob', 'String'),
128 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
135 * @dataProvider composeQueryExamples
137 function testComposeQuery($inputSql, $inputParams, $expectSql) {
138 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
139 $this->assertEquals($expectSql, $actualSql);
142 // CASE: Two params where the %2 is already present in the query
143 // NOTE: This case should rightly FAIL, as using strstr in the replace mechanism will turn
144 // the query into: SELECT * FROM whatever WHERE name = 'Alice' AND title = 'Bob' AND year LIKE ''Bob'012'
145 // So, to avoid such ERROR, the query should be framed like:
146 // 'SELECT * FROM whatever WHERE name = %1 AND title = %3 AND year LIKE '%2012'
147 // $params[3] = array('Bob', 'String');
148 // i.e. the place holder should be unique and should not contain in any other operational use in query
149 function testComposeQueryFailure() {
151 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
153 1 => array('Alice', 'String'),
154 2 => array('Bob', 'String'),
156 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
158 list($inputSql, $inputParams, $expectSql) = $cases[0];
159 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
160 $this->assertFalse(($expectSql == $actualSql));
163 function sqlNameDataProvider() {
165 array('this is a long string', 30, FALSE, 'this is a long string'),
167 array('this is an even longer string which is exactly 60 character', 60, FALSE, 'this is an even longer string which is exactly 60 character'),
168 array('this is an even longer string which is exactly 60 character', 60, TRUE , 'this is an even longer string which is exactly 60 character'),
170 array('this is an even longer string which is a bit more than 60 character', 60, FALSE, 'this is an even longer string which is a bit more than 60 ch'),
171 array('this is an even longer string which is a bit more than 60 character', 60, TRUE , 'this is an even longer string which is a bit more th_c1cbd519'),
176 * @dataProvider sqlNameDataProvider
178 function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
179 $this->assertEquals($expectedResult, CRM_Core_DAO
::shortenSQLName($inputData, $length, $makeRandom));