3 require_once 'CiviTest/CiviUnitTestCase.php';
6 * Class CRM_Core_DAOTest
8 class CRM_Core_DAOTest
extends CiviUnitTestCase
{
15 'description' => 'Test core DAO functions',
20 function testGetReferenceColumns() {
21 // choose CRM_Core_DAO_Email as an arbitrary example
22 $emailRefs = CRM_Core_DAO_Email
::getReferenceColumns();
23 $refsByTarget = array();
24 foreach ($emailRefs as $refSpec) {
25 $refsByTarget[$refSpec->getTargetTable()] = $refSpec;
27 $this->assertTrue(array_key_exists('civicrm_contact', $refsByTarget));
28 $contactRef = $refsByTarget['civicrm_contact'];
29 $this->assertEquals('contact_id', $contactRef->getReferenceKey());
30 $this->assertEquals('id', $contactRef->getTargetKey());
31 $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef));
34 function testGetReferencesToTable() {
35 $refs = CRM_Core_DAO
::getReferencesToTable(CRM_Financial_DAO_FinancialType
::getTableName());
36 $refsBySource = array();
37 foreach ($refs as $refSpec) {
38 $refsBySource[$refSpec->getReferenceTable()] = $refSpec;
40 $this->assertTrue(array_key_exists('civicrm_entity_financial_account', $refsBySource));
41 $genericRef = $refsBySource['civicrm_entity_financial_account'];
42 $this->assertEquals('entity_id', $genericRef->getReferenceKey());
43 $this->assertEquals('entity_table', $genericRef->getTypeColumn());
44 $this->assertEquals('id', $genericRef->getTargetKey());
45 $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef));
48 function testFindReferences() {
50 'first_name' => 'Testy',
51 'last_name' => 'McScallion',
52 'contact_type' => 'Individual',
55 $contact = CRM_Contact_BAO_Contact
::add($params);
56 $this->assertNotNull($contact->id
);
59 'email' => 'spam@dev.null',
60 'contact_id' => $contact->id
,
62 'location_type_id' => 1,
65 $email = CRM_Core_BAO_Email
::add($params);
67 $refs = $contact->findReferences();
68 $refsByTable = array();
69 foreach ($refs as $refObj) {
70 $refsByTable[$refObj->__table
] = $refObj;
73 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
74 $refDao = $refsByTable['civicrm_email'];
76 $this->assertEquals($contact->id
, $refDao->contact_id
);
82 function composeQueryExamples() {
84 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
88 'SELECT * FROM whatever',
90 'SELECT * FROM whatever',
93 // CASE: Integer param
95 'SELECT * FROM whatever WHERE id = %1',
97 1 => array(10, 'Integer'),
99 'SELECT * FROM whatever WHERE id = 10',
102 // CASE: String param
104 'SELECT * FROM whatever WHERE name = %1',
106 1 => array('Alice', 'String'),
108 'SELECT * FROM whatever WHERE name = \'Alice\'',
113 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
115 1 => array('Alice', 'String'),
116 2 => array('Bob', 'String'),
118 'SELECT * FROM whatever WHERE name = \'Alice\' 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 %2', 'String'),
126 2 => array('Bob', 'String'),
128 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
131 // CASE: Two params with special character ($1)
133 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
135 1 => array('Alice $1', 'String'),
136 2 => array('Bob', 'String'),
138 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
145 * @dataProvider composeQueryExamples
147 function testComposeQuery($inputSql, $inputParams, $expectSql) {
148 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
149 $this->assertEquals($expectSql, $actualSql);
152 // CASE: Two params where the %2 is already present in the query
153 // NOTE: This case should rightly FAIL, as using strstr in the replace mechanism will turn
154 // the query into: SELECT * FROM whatever WHERE name = 'Alice' AND title = 'Bob' AND year LIKE ''Bob'012'
155 // So, to avoid such ERROR, the query should be framed like:
156 // 'SELECT * FROM whatever WHERE name = %1 AND title = %3 AND year LIKE '%2012'
157 // $params[3] = array('Bob', 'String');
158 // i.e. the place holder should be unique and should not contain in any other operational use in query
159 function testComposeQueryFailure() {
161 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
163 1 => array('Alice', 'String'),
164 2 => array('Bob', 'String'),
166 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
168 list($inputSql, $inputParams, $expectSql) = $cases[0];
169 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
170 $this->assertFalse(($expectSql == $actualSql));
176 function sqlNameDataProvider() {
178 array('this is a long string', 30, FALSE, 'this is a long string'),
180 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'),
181 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'),
183 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'),
184 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'),
189 * @dataProvider sqlNameDataProvider
191 function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
192 $this->assertEquals($expectedResult, CRM_Core_DAO
::shortenSQLName($inputData, $length, $makeRandom));