Merge pull request #4806 from civicrm/4.5
[civicrm-core.git] / tests / phpunit / CRM / Core / DAOTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4
5 /**
6 * Class CRM_Core_DAOTest
7 */
8 class CRM_Core_DAOTest extends CiviUnitTestCase {
9
10 function testGetReferenceColumns() {
11 // choose CRM_Core_DAO_Email as an arbitrary example
12 $emailRefs = CRM_Core_DAO_Email::getReferenceColumns();
13 $refsByTarget = array();
14 foreach ($emailRefs as $refSpec) {
15 $refsByTarget[$refSpec->getTargetTable()] = $refSpec;
16 }
17 $this->assertTrue(array_key_exists('civicrm_contact', $refsByTarget));
18 $contactRef = $refsByTarget['civicrm_contact'];
19 $this->assertEquals('contact_id', $contactRef->getReferenceKey());
20 $this->assertEquals('id', $contactRef->getTargetKey());
21 $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef));
22 }
23
24 function testGetReferencesToTable() {
25 $refs = CRM_Core_DAO::getReferencesToTable(CRM_Financial_DAO_FinancialType::getTableName());
26 $refsBySource = array();
27 foreach ($refs as $refSpec) {
28 $refsBySource[$refSpec->getReferenceTable()] = $refSpec;
29 }
30 $this->assertTrue(array_key_exists('civicrm_entity_financial_account', $refsBySource));
31 $genericRef = $refsBySource['civicrm_entity_financial_account'];
32 $this->assertEquals('entity_id', $genericRef->getReferenceKey());
33 $this->assertEquals('entity_table', $genericRef->getTypeColumn());
34 $this->assertEquals('id', $genericRef->getTargetKey());
35 $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef));
36 }
37
38 function testFindReferences() {
39 $params = array(
40 'first_name' => 'Testy',
41 'last_name' => 'McScallion',
42 'contact_type' => 'Individual',
43 );
44
45 $contact = CRM_Contact_BAO_Contact::add($params);
46 $this->assertNotNull($contact->id);
47
48 $params = array(
49 'email' => 'spam@dev.null',
50 'contact_id' => $contact->id,
51 'is_primary' => 0,
52 'location_type_id' => 1,
53 );
54
55 $email = CRM_Core_BAO_Email::add($params);
56
57 $refs = $contact->findReferences();
58 $refsByTable = array();
59 foreach ($refs as $refObj) {
60 $refsByTable[$refObj->__table] = $refObj;
61 }
62
63 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
64 $refDao = $refsByTable['civicrm_email'];
65 $refDao->find(TRUE);
66 $this->assertEquals($contact->id, $refDao->contact_id);
67 }
68
69 /**
70 * @return array
71 */
72 function composeQueryExamples() {
73 $cases = array();
74 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
75
76 // CASE: No params
77 $cases[] = array(
78 'SELECT * FROM whatever',
79 array(),
80 'SELECT * FROM whatever',
81 );
82
83 // CASE: Integer param
84 $cases[] = array(
85 'SELECT * FROM whatever WHERE id = %1',
86 array(
87 1 => array(10, 'Integer'),
88 ),
89 'SELECT * FROM whatever WHERE id = 10',
90 );
91
92 // CASE: String param
93 $cases[] = array(
94 'SELECT * FROM whatever WHERE name = %1',
95 array(
96 1 => array('Alice', 'String'),
97 ),
98 'SELECT * FROM whatever WHERE name = \'Alice\'',
99 );
100
101 // CASE: Two params
102 $cases[] = array(
103 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
104 array(
105 1 => array('Alice', 'String'),
106 2 => array('Bob', 'String'),
107 ),
108 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
109 );
110
111 // CASE: Two params with special character (%1)
112 $cases[] = array(
113 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
114 array(
115 1 => array('Alice %2', 'String'),
116 2 => array('Bob', 'String'),
117 ),
118 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
119 );
120
121 // CASE: Two params with special character ($1)
122 $cases[] = array(
123 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
124 array(
125 1 => array('Alice $1', 'String'),
126 2 => array('Bob', 'String'),
127 ),
128 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
129 );
130
131 return $cases;
132 }
133
134 /**
135 * @dataProvider composeQueryExamples
136 */
137 function testComposeQuery($inputSql, $inputParams, $expectSql) {
138 $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
139 $this->assertEquals($expectSql, $actualSql);
140 }
141
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() {
150 $cases[] = array(
151 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
152 array(
153 1 => array('Alice', 'String'),
154 2 => array('Bob', 'String'),
155 ),
156 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
157 );
158 list($inputSql, $inputParams, $expectSql) = $cases[0];
159 $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
160 $this->assertFalse(($expectSql == $actualSql));
161 }
162
163 /**
164 * @return array
165 */
166 function sqlNameDataProvider() {
167 return array(
168 array('this is a long string', 30, FALSE, 'this is a long string'),
169
170 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'),
171 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'),
172
173 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'),
174 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'),
175 );
176 }
177
178 /**
179 * @dataProvider sqlNameDataProvider
180 */
181 function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
182 $this->assertEquals($expectedResult, CRM_Core_DAO::shortenSQLName($inputData, $length, $makeRandom));
183 }
184
185 function testFindById() {
186 $params = $this->sampleContact('Individual', 4);
187 $existing_contact = CRM_Contact_BAO_Contact::add($params);
188 $contact = CRM_Contact_BAO_Contact::findById($existing_contact->id);
189 $this->assertEquals($existing_contact->id, $contact->id);
190 $deleted_contact_id = $existing_contact->id;
191 CRM_Contact_BAO_Contact::deleteContact($contact->id, FALSE, TRUE);
192 $exception_thrown = FALSE;
193 try {
194 $deleted_contact = CRM_Contact_BAO_Contact::findById($deleted_contact_id);
195 }
196 catch (Exception $e) {
197 $exception_thrown = TRUE;
198 }
199 $this->assertTrue($exception_thrown);
200 }
201 }