Merge pull request #8922 from totten/master-sql-import
[civicrm-core.git] / tests / phpunit / CRM / Core / DAOTest.php
CommitLineData
71e5aa5c
ARW
1<?php
2
aba1cd8b
EM
3/**
4 * Class CRM_Core_DAOTest
acb109b7 5 * @group headless
aba1cd8b 6 */
71e5aa5c 7class CRM_Core_DAOTest extends CiviUnitTestCase {
71e5aa5c 8
00be9182 9 public function testGetReferenceColumns() {
71e5aa5c
ARW
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;
15 }
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());
11626cf1 20 $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef));
71e5aa5c
ARW
21 }
22
00be9182 23 public function testGetReferencesToTable() {
71e5aa5c
ARW
24 $refs = CRM_Core_DAO::getReferencesToTable(CRM_Financial_DAO_FinancialType::getTableName());
25 $refsBySource = array();
26 foreach ($refs as $refSpec) {
27 $refsBySource[$refSpec->getReferenceTable()] = $refSpec;
28 }
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());
11626cf1 34 $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef));
71e5aa5c
ARW
35 }
36
00be9182 37 public function testFindReferences() {
71e5aa5c
ARW
38 $params = array(
39 'first_name' => 'Testy',
40 'last_name' => 'McScallion',
41 'contact_type' => 'Individual',
42 );
43
44 $contact = CRM_Contact_BAO_Contact::add($params);
45 $this->assertNotNull($contact->id);
46
47 $params = array(
48 'email' => 'spam@dev.null',
49 'contact_id' => $contact->id,
50 'is_primary' => 0,
51 'location_type_id' => 1,
52 );
53
54 $email = CRM_Core_BAO_Email::add($params);
55
56 $refs = $contact->findReferences();
57 $refsByTable = array();
58 foreach ($refs as $refObj) {
59 $refsByTable[$refObj->__table] = $refObj;
60 }
61
62 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
63 $refDao = $refsByTable['civicrm_email'];
64 $refDao->find(TRUE);
65 $this->assertEquals($contact->id, $refDao->contact_id);
66 }
c8950569 67
4cbe18b8
EM
68 /**
69 * @return array
70 */
00be9182 71 public function composeQueryExamples() {
0c627c6f
TO
72 $cases = array();
73 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
74
75 // CASE: No params
76 $cases[] = array(
77 'SELECT * FROM whatever',
78 array(),
79 'SELECT * FROM whatever',
80 );
81
82 // CASE: Integer param
83 $cases[] = array(
84 'SELECT * FROM whatever WHERE id = %1',
85 array(
86 1 => array(10, 'Integer'),
87 ),
88 'SELECT * FROM whatever WHERE id = 10',
89 );
90
91 // CASE: String param
92 $cases[] = array(
93 'SELECT * FROM whatever WHERE name = %1',
94 array(
95 1 => array('Alice', 'String'),
96 ),
97 'SELECT * FROM whatever WHERE name = \'Alice\'',
98 );
99
100 // CASE: Two params
101 $cases[] = array(
102 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
103 array(
104 1 => array('Alice', 'String'),
105 2 => array('Bob', 'String'),
106 ),
107 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
108 );
109
110 // CASE: Two params with special character (%1)
111 $cases[] = array(
112 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
113 array(
114 1 => array('Alice %2', 'String'),
115 2 => array('Bob', 'String'),
116 ),
117 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
118 );
119
120 // CASE: Two params with special character ($1)
121 $cases[] = array(
122 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
123 array(
124 1 => array('Alice $1', 'String'),
125 2 => array('Bob', 'String'),
126 ),
127 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
128 );
129
130 return $cases;
131 }
132
133 /**
134 * @dataProvider composeQueryExamples
1e1fdcf6
EM
135 * @param $inputSql
136 * @param $inputParams
137 * @param $expectSql
0c627c6f 138 */
00be9182 139 public function testComposeQuery($inputSql, $inputParams, $expectSql) {
0c627c6f
TO
140 $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
141 $this->assertEquals($expectSql, $actualSql);
142 }
e2508c90 143
0eea664b
TO
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
152 */
00be9182 153 public function testComposeQueryFailure() {
e2508c90
PJ
154 $cases[] = array(
155 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
156 array(
157 1 => array('Alice', 'String'),
158 2 => array('Bob', 'String'),
159 ),
160 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
161 );
162 list($inputSql, $inputParams, $expectSql) = $cases[0];
163 $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
164 $this->assertFalse(($expectSql == $actualSql));
165 }
6842bb53 166
4cbe18b8
EM
167 /**
168 * @return array
169 */
00be9182 170 public function sqlNameDataProvider() {
6842bb53
DL
171 return array(
172 array('this is a long string', 30, FALSE, 'this is a long string'),
92915c55
TO
173 array(
174 'this is an even longer string which is exactly 60 character',
175 60,
176 FALSE,
0eea664b 177 'this is an even longer string which is exactly 60 character',
92915c55
TO
178 ),
179 array(
180 'this is an even longer string which is exactly 60 character',
181 60,
182 TRUE,
0eea664b 183 'this is an even longer string which is exactly 60 character',
92915c55
TO
184 ),
185 array(
186 'this is an even longer string which is a bit more than 60 character',
187 60,
188 FALSE,
0eea664b 189 'this is an even longer string which is a bit more than 60 ch',
92915c55
TO
190 ),
191 array(
192 'this is an even longer string which is a bit more than 60 character',
193 60,
194 TRUE,
0eea664b 195 'this is an even longer string which is a bit more th_c1cbd519',
92915c55 196 ),
6842bb53
DL
197 );
198 }
199
200 /**
201 * @dataProvider sqlNameDataProvider
1e1fdcf6
EM
202 * @param $inputData
203 * @param $length
204 * @param $makeRandom
205 * @param $expectedResult
6842bb53 206 */
00be9182 207 public function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) {
6842bb53
DL
208 $this->assertEquals($expectedResult, CRM_Core_DAO::shortenSQLName($inputData, $length, $makeRandom));
209 }
210
00be9182 211 public function testFindById() {
2c0a1ab8
AN
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;
219 try {
220 $deleted_contact = CRM_Contact_BAO_Contact::findById($deleted_contact_id);
221 }
222 catch (Exception $e) {
223 $exception_thrown = TRUE;
224 }
225 $this->assertTrue($exception_thrown);
226 }
0eea664b 227
fd542660
PN
228 /**
229 * requireValidDBName() method (to check valid database name)
230 */
231 public function testRequireValidDBName() {
232 $databases = array(
233 'testdb' => TRUE,
234 'test_db' => TRUE,
235 'TEST_db' => TRUE,
236 '123testdb' => TRUE,
237 'test12db34' => TRUE,
238 'test_12_db34' => TRUE,
239 'test-db' => FALSE,
240 'test;db' => FALSE,
241 'test*&db' => FALSE,
242 'testdb;Delete test' => FALSE,
243 '123456' => FALSE,
244 'test#$%^&*' => FALSE,
245 );
246 $testDetails = array();
247 foreach ($databases as $database => $val) {
e5d3a11d 248 $this->assertEquals(CRM_Core_DAO::requireValidDBName($database), $val);
fd542660
PN
249 }
250 }
251
3fa9688a 252 /**
253 * Test the function designed to find myIsam tables.
254 */
255 public function testMyISAMCheck() {
256 $this->assertEquals(0, CRM_Core_DAO::isDBMyISAM());
257 CRM_Core_DAO::executeQuery('CREATE TABLE civicrm_my_isam (`id` int(10) unsigned NOT NULL) ENGINE = MyISAM');
258 $this->assertEquals(1, CRM_Core_DAO::isDBMyISAM());
259 CRM_Core_DAO::executeQuery('DROP TABLE civicrm_my_isam');
260 }
261
71e5aa5c 262}