9fae687224bf05397c0a614507b5812f36f216c7
[civicrm-core.git] / tests / phpunit / CRM / Core / DAOTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_DAOTest
5 */
6 class CRM_Core_DAOTest extends CiviUnitTestCase {
7
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;
14 }
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));
20 }
21
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;
27 }
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));
34 }
35
36 public function testFindReferences() {
37 $params = array(
38 'first_name' => 'Testy',
39 'last_name' => 'McScallion',
40 'contact_type' => 'Individual',
41 );
42
43 $contact = CRM_Contact_BAO_Contact::add($params);
44 $this->assertNotNull($contact->id);
45
46 $params = array(
47 'email' => 'spam@dev.null',
48 'contact_id' => $contact->id,
49 'is_primary' => 0,
50 'location_type_id' => 1,
51 );
52
53 $email = CRM_Core_BAO_Email::add($params);
54
55 $refs = $contact->findReferences();
56 $refsByTable = array();
57 foreach ($refs as $refObj) {
58 $refsByTable[$refObj->__table] = $refObj;
59 }
60
61 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
62 $refDao = $refsByTable['civicrm_email'];
63 $refDao->find(TRUE);
64 $this->assertEquals($contact->id, $refDao->contact_id);
65 }
66
67 /**
68 * @return array
69 */
70 public function composeQueryExamples() {
71 $cases = array();
72 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
73
74 // CASE: No params
75 $cases[] = array(
76 'SELECT * FROM whatever',
77 array(),
78 'SELECT * FROM whatever',
79 );
80
81 // CASE: Integer param
82 $cases[] = array(
83 'SELECT * FROM whatever WHERE id = %1',
84 array(
85 1 => array(10, 'Integer'),
86 ),
87 'SELECT * FROM whatever WHERE id = 10',
88 );
89
90 // CASE: String param
91 $cases[] = array(
92 'SELECT * FROM whatever WHERE name = %1',
93 array(
94 1 => array('Alice', 'String'),
95 ),
96 'SELECT * FROM whatever WHERE name = \'Alice\'',
97 );
98
99 // CASE: Two params
100 $cases[] = array(
101 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
102 array(
103 1 => array('Alice', 'String'),
104 2 => array('Bob', 'String'),
105 ),
106 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
107 );
108
109 // CASE: Two params with special character (%1)
110 $cases[] = array(
111 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
112 array(
113 1 => array('Alice %2', 'String'),
114 2 => array('Bob', 'String'),
115 ),
116 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
117 );
118
119 // CASE: Two params with special character ($1)
120 $cases[] = array(
121 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
122 array(
123 1 => array('Alice $1', 'String'),
124 2 => array('Bob', 'String'),
125 ),
126 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
127 );
128
129 return $cases;
130 }
131
132 /**
133 * @dataProvider composeQueryExamples
134 * @param $inputSql
135 * @param $inputParams
136 * @param $expectSql
137 */
138 public function testComposeQuery($inputSql, $inputParams, $expectSql) {
139 $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
140 $this->assertEquals($expectSql, $actualSql);
141 }
142
143 /**
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
151 */
152 public function testComposeQueryFailure() {
153 $cases[] = array(
154 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
155 array(
156 1 => array('Alice', 'String'),
157 2 => array('Bob', 'String'),
158 ),
159 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
160 );
161 list($inputSql, $inputParams, $expectSql) = $cases[0];
162 $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
163 $this->assertFalse(($expectSql == $actualSql));
164 }
165
166 /**
167 * @return array
168 */
169 public function sqlNameDataProvider() {
170 return array(
171 array('this is a long string', 30, FALSE, 'this is a long string'),
172 array(
173 'this is an even longer string which is exactly 60 character',
174 60,
175 FALSE,
176 'this is an even longer string which is exactly 60 character',
177 ),
178 array(
179 'this is an even longer string which is exactly 60 character',
180 60,
181 TRUE,
182 'this is an even longer string which is exactly 60 character',
183 ),
184 array(
185 'this is an even longer string which is a bit more than 60 character',
186 60,
187 FALSE,
188 'this is an even longer string which is a bit more than 60 ch',
189 ),
190 array(
191 'this is an even longer string which is a bit more than 60 character',
192 60,
193 TRUE,
194 'this is an even longer string which is a bit more th_c1cbd519',
195 ),
196 );
197 }
198
199 /**
200 * @dataProvider sqlNameDataProvider
201 * @param $inputData
202 * @param $length
203 * @param $makeRandom
204 * @param $expectedResult
205 */
206 public function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
207 $this->assertEquals($expectedResult, CRM_Core_DAO::shortenSQLName($inputData, $length, $makeRandom));
208 }
209
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;
218 try {
219 $deleted_contact = CRM_Contact_BAO_Contact::findById($deleted_contact_id);
220 }
221 catch (Exception $e) {
222 $exception_thrown = TRUE;
223 }
224 $this->assertTrue($exception_thrown);
225 }
226
227 }