4 * Class CRM_Core_DAOTest
6 class CRM_Core_DAOTest
extends CiviUnitTestCase
{
8 public function testGetReferenceColumns() {
9 // choose CRM_Core_DAO_Email as an arbitrary example
10 $emailRefs = CRM_Core_DAO_Email
::getReferenceColumns();
11 $refsByTarget = array();
12 foreach ($emailRefs as $refSpec) {
13 $refsByTarget[$refSpec->getTargetTable()] = $refSpec;
15 $this->assertTrue(array_key_exists('civicrm_contact', $refsByTarget));
16 $contactRef = $refsByTarget['civicrm_contact'];
17 $this->assertEquals('contact_id', $contactRef->getReferenceKey());
18 $this->assertEquals('id', $contactRef->getTargetKey());
19 $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef));
22 public function testGetReferencesToTable() {
23 $refs = CRM_Core_DAO
::getReferencesToTable(CRM_Financial_DAO_FinancialType
::getTableName());
24 $refsBySource = array();
25 foreach ($refs as $refSpec) {
26 $refsBySource[$refSpec->getReferenceTable()] = $refSpec;
28 $this->assertTrue(array_key_exists('civicrm_entity_financial_account', $refsBySource));
29 $genericRef = $refsBySource['civicrm_entity_financial_account'];
30 $this->assertEquals('entity_id', $genericRef->getReferenceKey());
31 $this->assertEquals('entity_table', $genericRef->getTypeColumn());
32 $this->assertEquals('id', $genericRef->getTargetKey());
33 $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef));
36 public function testFindReferences() {
38 'first_name' => 'Testy',
39 'last_name' => 'McScallion',
40 'contact_type' => 'Individual',
43 $contact = CRM_Contact_BAO_Contact
::add($params);
44 $this->assertNotNull($contact->id
);
47 'email' => 'spam@dev.null',
48 'contact_id' => $contact->id
,
50 'location_type_id' => 1,
53 $email = CRM_Core_BAO_Email
::add($params);
55 $refs = $contact->findReferences();
56 $refsByTable = array();
57 foreach ($refs as $refObj) {
58 $refsByTable[$refObj->__table
] = $refObj;
61 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
62 $refDao = $refsByTable['civicrm_email'];
64 $this->assertEquals($contact->id
, $refDao->contact_id
);
70 public function composeQueryExamples() {
72 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
76 'SELECT * FROM whatever',
78 'SELECT * FROM whatever',
81 // CASE: Integer param
83 'SELECT * FROM whatever WHERE id = %1',
85 1 => array(10, 'Integer'),
87 'SELECT * FROM whatever WHERE id = 10',
92 'SELECT * FROM whatever WHERE name = %1',
94 1 => array('Alice', 'String'),
96 'SELECT * FROM whatever WHERE name = \'Alice\'',
101 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
103 1 => array('Alice', 'String'),
104 2 => array('Bob', 'String'),
106 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
109 // CASE: Two params with special character (%1)
111 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
113 1 => array('Alice %2', 'String'),
114 2 => array('Bob', 'String'),
116 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
119 // CASE: Two params with special character ($1)
121 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
123 1 => array('Alice $1', 'String'),
124 2 => array('Bob', 'String'),
126 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
133 * @dataProvider composeQueryExamples
135 * @param $inputParams
138 public function testComposeQuery($inputSql, $inputParams, $expectSql) {
139 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
140 $this->assertEquals($expectSql, $actualSql);
144 * CASE: Two params where the %2 is already present in the query
145 * NOTE: This case should rightly FAIL, as using strstr in the replace mechanism will turn
146 * the query into: SELECT * FROM whatever WHERE name = 'Alice' AND title = 'Bob' AND year LIKE ''Bob'012'
147 * So, to avoid such ERROR, the query should be framed like:
148 * 'SELECT * FROM whatever WHERE name = %1 AND title = %3 AND year LIKE '%2012'
149 * $params[3] = array('Bob', 'String');
150 * i.e. the place holder should be unique and should not contain in any other operational use in query
152 public function testComposeQueryFailure() {
154 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
156 1 => array('Alice', 'String'),
157 2 => array('Bob', 'String'),
159 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
161 list($inputSql, $inputParams, $expectSql) = $cases[0];
162 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
163 $this->assertFalse(($expectSql == $actualSql));
169 public function sqlNameDataProvider() {
171 array('this is a long string', 30, FALSE, 'this is a long string'),
173 'this is an even longer string which is exactly 60 character',
176 'this is an even longer string which is exactly 60 character',
179 'this is an even longer string which is exactly 60 character',
182 'this is an even longer string which is exactly 60 character',
185 'this is an even longer string which is a bit more than 60 character',
188 'this is an even longer string which is a bit more than 60 ch',
191 'this is an even longer string which is a bit more than 60 character',
194 'this is an even longer string which is a bit more th_c1cbd519',
200 * @dataProvider sqlNameDataProvider
204 * @param $expectedResult
206 public function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
207 $this->assertEquals($expectedResult, CRM_Core_DAO
::shortenSQLName($inputData, $length, $makeRandom));
210 public function testFindById() {
211 $params = $this->sampleContact('Individual', 4);
212 $existing_contact = CRM_Contact_BAO_Contact
::add($params);
213 $contact = CRM_Contact_BAO_Contact
::findById($existing_contact->id
);
214 $this->assertEquals($existing_contact->id
, $contact->id
);
215 $deleted_contact_id = $existing_contact->id
;
216 CRM_Contact_BAO_Contact
::deleteContact($contact->id
, FALSE, TRUE);
217 $exception_thrown = FALSE;
219 $deleted_contact = CRM_Contact_BAO_Contact
::findById($deleted_contact_id);
221 catch (Exception
$e) {
222 $exception_thrown = TRUE;
224 $this->assertTrue($exception_thrown);