CiviTest - Configure autoloader for test classes
[civicrm-core.git] / tests / phpunit / CRM / Core / DAOTest.php
CommitLineData
71e5aa5c
ARW
1<?php
2
3require_once 'CiviTest/CiviUnitTestCase.php';
aba1cd8b
EM
4
5/**
6 * Class CRM_Core_DAOTest
7 */
71e5aa5c 8class CRM_Core_DAOTest extends CiviUnitTestCase {
71e5aa5c 9
00be9182 10 public function testGetReferenceColumns() {
71e5aa5c
ARW
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());
11626cf1 21 $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef));
71e5aa5c
ARW
22 }
23
00be9182 24 public function testGetReferencesToTable() {
71e5aa5c
ARW
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());
11626cf1 35 $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef));
71e5aa5c
ARW
36 }
37
00be9182 38 public function testFindReferences() {
71e5aa5c
ARW
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 }
c8950569 68
4cbe18b8
EM
69 /**
70 * @return array
71 */
00be9182 72 public function composeQueryExamples() {
0c627c6f
TO
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
1e1fdcf6
EM
136 * @param $inputSql
137 * @param $inputParams
138 * @param $expectSql
0c627c6f 139 */
00be9182 140 public function testComposeQuery($inputSql, $inputParams, $expectSql) {
0c627c6f
TO
141 $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
142 $this->assertEquals($expectSql, $actualSql);
143 }
e2508c90
PJ
144
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
00be9182 152 public function testComposeQueryFailure() {
e2508c90
PJ
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 }
6842bb53 165
4cbe18b8
EM
166 /**
167 * @return array
168 */
00be9182 169 public function sqlNameDataProvider() {
6842bb53
DL
170 return array(
171 array('this is a long string', 30, FALSE, 'this is a long string'),
92915c55
TO
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 ),
6842bb53
DL
196 );
197 }
198
199 /**
200 * @dataProvider sqlNameDataProvider
1e1fdcf6
EM
201 * @param $inputData
202 * @param $length
203 * @param $makeRandom
204 * @param $expectedResult
6842bb53 205 */
00be9182 206 public function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
6842bb53
DL
207 $this->assertEquals($expectedResult, CRM_Core_DAO::shortenSQLName($inputData, $length, $makeRandom));
208 }
209
00be9182 210 public function testFindById() {
2c0a1ab8
AN
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 }
71e5aa5c 226}