4 * Class CRM_Core_DAOTest
7 class CRM_Core_DAOTest
extends CiviUnitTestCase
{
9 public function testGetReferenceColumns() {
10 // choose CRM_Core_DAO_Email as an arbitrary example
11 $emailRefs = CRM_Core_DAO_Email
::getReferenceColumns();
12 $refsByTarget = array();
13 foreach ($emailRefs as $refSpec) {
14 $refsByTarget[$refSpec->getTargetTable()] = $refSpec;
16 $this->assertTrue(array_key_exists('civicrm_contact', $refsByTarget));
17 $contactRef = $refsByTarget['civicrm_contact'];
18 $this->assertEquals('contact_id', $contactRef->getReferenceKey());
19 $this->assertEquals('id', $contactRef->getTargetKey());
20 $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef));
23 public function testGetReferencesToTable() {
24 $refs = CRM_Core_DAO
::getReferencesToTable(CRM_Financial_DAO_FinancialType
::getTableName());
25 $refsBySource = array();
26 foreach ($refs as $refSpec) {
27 $refsBySource[$refSpec->getReferenceTable()] = $refSpec;
29 $this->assertTrue(array_key_exists('civicrm_entity_financial_account', $refsBySource));
30 $genericRef = $refsBySource['civicrm_entity_financial_account'];
31 $this->assertEquals('entity_id', $genericRef->getReferenceKey());
32 $this->assertEquals('entity_table', $genericRef->getTypeColumn());
33 $this->assertEquals('id', $genericRef->getTargetKey());
34 $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef));
37 public function testFindReferences() {
39 'first_name' => 'Testy',
40 'last_name' => 'McScallion',
41 'contact_type' => 'Individual',
44 $contact = CRM_Contact_BAO_Contact
::add($params);
45 $this->assertNotNull($contact->id
);
48 'email' => 'spam@dev.null',
49 'contact_id' => $contact->id
,
51 'location_type_id' => 1,
54 $email = CRM_Core_BAO_Email
::add($params);
56 $refs = $contact->findReferences();
57 $refsByTable = array();
58 foreach ($refs as $refObj) {
59 $refsByTable[$refObj->__table
] = $refObj;
62 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
63 $refDao = $refsByTable['civicrm_email'];
65 $this->assertEquals($contact->id
, $refDao->contact_id
);
71 public function composeQueryExamples() {
73 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
77 'SELECT * FROM whatever',
79 'SELECT * FROM whatever',
82 // CASE: Integer param
84 'SELECT * FROM whatever WHERE id = %1',
86 1 => array(10, 'Integer'),
88 'SELECT * FROM whatever WHERE id = 10',
93 'SELECT * FROM whatever WHERE name = %1',
95 1 => array('Alice', 'String'),
97 'SELECT * FROM whatever WHERE name = \'Alice\'',
102 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
104 1 => array('Alice', 'String'),
105 2 => array('Bob', 'String'),
107 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
110 // CASE: Two params with special character (%1)
112 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
114 1 => array('Alice %2', 'String'),
115 2 => array('Bob', 'String'),
117 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
120 // CASE: Two params with special character ($1)
122 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
124 1 => array('Alice $1', 'String'),
125 2 => array('Bob', 'String'),
127 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
134 * @dataProvider composeQueryExamples
136 * @param $inputParams
139 public function testComposeQuery($inputSql, $inputParams, $expectSql) {
140 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
141 $this->assertEquals($expectSql, $actualSql);
145 * CASE: Two params where the %2 is already present in the query
146 * NOTE: This case should rightly FAIL, as using strstr in the replace mechanism will turn
147 * the query into: SELECT * FROM whatever WHERE name = 'Alice' AND title = 'Bob' AND year LIKE ''Bob'012'
148 * So, to avoid such ERROR, the query should be framed like:
149 * 'SELECT * FROM whatever WHERE name = %1 AND title = %3 AND year LIKE '%2012'
150 * $params[3] = array('Bob', 'String');
151 * i.e. the place holder should be unique and should not contain in any other operational use in query
153 public function testComposeQueryFailure() {
155 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
157 1 => array('Alice', 'String'),
158 2 => array('Bob', 'String'),
160 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
162 list($inputSql, $inputParams, $expectSql) = $cases[0];
163 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
164 $this->assertFalse(($expectSql == $actualSql));
170 public function sqlNameDataProvider() {
172 array('this is a long string', 30, FALSE, 'this is a long string'),
174 'this is an even longer string which is exactly 60 character',
177 'this is an even longer string which is exactly 60 character',
180 'this is an even longer string which is exactly 60 character',
183 'this is an even longer string which is exactly 60 character',
186 'this is an even longer string which is a bit more than 60 character',
189 'this is an even longer string which is a bit more than 60 ch',
192 'this is an even longer string which is a bit more than 60 character',
195 'this is an even longer string which is a bit more th_c1cbd519',
201 * @dataProvider sqlNameDataProvider
205 * @param $expectedResult
207 public function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
208 $this->assertEquals($expectedResult, CRM_Core_DAO
::shortenSQLName($inputData, $length, $makeRandom));
211 public function testFindById() {
212 $params = $this->sampleContact('Individual', 4);
213 $existing_contact = CRM_Contact_BAO_Contact
::add($params);
214 $contact = CRM_Contact_BAO_Contact
::findById($existing_contact->id
);
215 $this->assertEquals($existing_contact->id
, $contact->id
);
216 $deleted_contact_id = $existing_contact->id
;
217 CRM_Contact_BAO_Contact
::deleteContact($contact->id
, FALSE, TRUE);
218 $exception_thrown = FALSE;
220 $deleted_contact = CRM_Contact_BAO_Contact
::findById($deleted_contact_id);
222 catch (Exception
$e) {
223 $exception_thrown = TRUE;
225 $this->assertTrue($exception_thrown);