Commit | Line | Data |
---|---|---|
71e5aa5c ARW |
1 | <?php |
2 | ||
3 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
aba1cd8b EM |
4 | |
5 | /** | |
6 | * Class CRM_Core_DAOTest | |
7 | */ | |
71e5aa5c | 8 | class CRM_Core_DAOTest extends CiviUnitTestCase { |
aba1cd8b EM |
9 | /** |
10 | * @return array | |
11 | */ | |
71e5aa5c ARW |
12 | function get_info() { |
13 | return array( | |
14 | 'name' => 'DAO', | |
15 | 'description' => 'Test core DAO functions', | |
16 | 'group' => 'Core', | |
17 | ); | |
18 | } | |
19 | ||
20 | function testGetReferenceColumns() { | |
21 | // choose CRM_Core_DAO_Email as an arbitrary example | |
22 | $emailRefs = CRM_Core_DAO_Email::getReferenceColumns(); | |
23 | $refsByTarget = array(); | |
24 | foreach ($emailRefs as $refSpec) { | |
25 | $refsByTarget[$refSpec->getTargetTable()] = $refSpec; | |
26 | } | |
27 | $this->assertTrue(array_key_exists('civicrm_contact', $refsByTarget)); | |
28 | $contactRef = $refsByTarget['civicrm_contact']; | |
29 | $this->assertEquals('contact_id', $contactRef->getReferenceKey()); | |
30 | $this->assertEquals('id', $contactRef->getTargetKey()); | |
11626cf1 | 31 | $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef)); |
71e5aa5c ARW |
32 | } |
33 | ||
34 | function testGetReferencesToTable() { | |
35 | $refs = CRM_Core_DAO::getReferencesToTable(CRM_Financial_DAO_FinancialType::getTableName()); | |
36 | $refsBySource = array(); | |
37 | foreach ($refs as $refSpec) { | |
38 | $refsBySource[$refSpec->getReferenceTable()] = $refSpec; | |
39 | } | |
40 | $this->assertTrue(array_key_exists('civicrm_entity_financial_account', $refsBySource)); | |
41 | $genericRef = $refsBySource['civicrm_entity_financial_account']; | |
42 | $this->assertEquals('entity_id', $genericRef->getReferenceKey()); | |
43 | $this->assertEquals('entity_table', $genericRef->getTypeColumn()); | |
44 | $this->assertEquals('id', $genericRef->getTargetKey()); | |
11626cf1 | 45 | $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef)); |
71e5aa5c ARW |
46 | } |
47 | ||
48 | function testFindReferences() { | |
49 | $params = array( | |
50 | 'first_name' => 'Testy', | |
51 | 'last_name' => 'McScallion', | |
52 | 'contact_type' => 'Individual', | |
53 | ); | |
54 | ||
55 | $contact = CRM_Contact_BAO_Contact::add($params); | |
56 | $this->assertNotNull($contact->id); | |
57 | ||
58 | $params = array( | |
59 | 'email' => 'spam@dev.null', | |
60 | 'contact_id' => $contact->id, | |
61 | 'is_primary' => 0, | |
62 | 'location_type_id' => 1, | |
63 | ); | |
64 | ||
65 | $email = CRM_Core_BAO_Email::add($params); | |
66 | ||
67 | $refs = $contact->findReferences(); | |
68 | $refsByTable = array(); | |
69 | foreach ($refs as $refObj) { | |
70 | $refsByTable[$refObj->__table] = $refObj; | |
71 | } | |
72 | ||
73 | $this->assertTrue(array_key_exists('civicrm_email', $refsByTable)); | |
74 | $refDao = $refsByTable['civicrm_email']; | |
75 | $refDao->find(TRUE); | |
76 | $this->assertEquals($contact->id, $refDao->contact_id); | |
77 | } | |
c8950569 | 78 | |
4cbe18b8 EM |
79 | /** |
80 | * @return array | |
81 | */ | |
0c627c6f TO |
82 | function composeQueryExamples() { |
83 | $cases = array(); | |
84 | // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL'); | |
85 | ||
86 | // CASE: No params | |
87 | $cases[] = array( | |
88 | 'SELECT * FROM whatever', | |
89 | array(), | |
90 | 'SELECT * FROM whatever', | |
91 | ); | |
92 | ||
93 | // CASE: Integer param | |
94 | $cases[] = array( | |
95 | 'SELECT * FROM whatever WHERE id = %1', | |
96 | array( | |
97 | 1 => array(10, 'Integer'), | |
98 | ), | |
99 | 'SELECT * FROM whatever WHERE id = 10', | |
100 | ); | |
101 | ||
102 | // CASE: String param | |
103 | $cases[] = array( | |
104 | 'SELECT * FROM whatever WHERE name = %1', | |
105 | array( | |
106 | 1 => array('Alice', 'String'), | |
107 | ), | |
108 | 'SELECT * FROM whatever WHERE name = \'Alice\'', | |
109 | ); | |
110 | ||
111 | // CASE: Two params | |
112 | $cases[] = array( | |
113 | 'SELECT * FROM whatever WHERE name = %1 AND title = %2', | |
114 | array( | |
115 | 1 => array('Alice', 'String'), | |
116 | 2 => array('Bob', 'String'), | |
117 | ), | |
118 | 'SELECT * FROM whatever WHERE name = \'Alice\' 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 %2', 'String'), | |
126 | 2 => array('Bob', 'String'), | |
127 | ), | |
128 | 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'', | |
129 | ); | |
130 | ||
131 | // CASE: Two params with special character ($1) | |
132 | $cases[] = array( | |
133 | 'SELECT * FROM whatever WHERE name = %1 AND title = %2', | |
134 | array( | |
135 | 1 => array('Alice $1', 'String'), | |
136 | 2 => array('Bob', 'String'), | |
137 | ), | |
138 | 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'', | |
139 | ); | |
140 | ||
141 | return $cases; | |
142 | } | |
143 | ||
144 | /** | |
145 | * @dataProvider composeQueryExamples | |
146 | */ | |
147 | function testComposeQuery($inputSql, $inputParams, $expectSql) { | |
148 | $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams); | |
149 | $this->assertEquals($expectSql, $actualSql); | |
150 | } | |
e2508c90 PJ |
151 | |
152 | // CASE: Two params where the %2 is already present in the query | |
153 | // NOTE: This case should rightly FAIL, as using strstr in the replace mechanism will turn | |
154 | // the query into: SELECT * FROM whatever WHERE name = 'Alice' AND title = 'Bob' AND year LIKE ''Bob'012' | |
155 | // So, to avoid such ERROR, the query should be framed like: | |
156 | // 'SELECT * FROM whatever WHERE name = %1 AND title = %3 AND year LIKE '%2012' | |
157 | // $params[3] = array('Bob', 'String'); | |
158 | // i.e. the place holder should be unique and should not contain in any other operational use in query | |
159 | function testComposeQueryFailure() { | |
160 | $cases[] = array( | |
161 | 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ', | |
162 | array( | |
163 | 1 => array('Alice', 'String'), | |
164 | 2 => array('Bob', 'String'), | |
165 | ), | |
166 | 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ', | |
167 | ); | |
168 | list($inputSql, $inputParams, $expectSql) = $cases[0]; | |
169 | $actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams); | |
170 | $this->assertFalse(($expectSql == $actualSql)); | |
171 | } | |
6842bb53 | 172 | |
4cbe18b8 EM |
173 | /** |
174 | * @return array | |
175 | */ | |
6842bb53 DL |
176 | function sqlNameDataProvider() { |
177 | return array( | |
178 | array('this is a long string', 30, FALSE, 'this is a long string'), | |
179 | ||
180 | 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'), | |
181 | 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'), | |
182 | ||
183 | 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'), | |
a8dd306e | 184 | 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'), |
6842bb53 DL |
185 | ); |
186 | } | |
187 | ||
188 | /** | |
189 | * @dataProvider sqlNameDataProvider | |
190 | */ | |
191 | function testShortenSQLName($inputData, $length, $makeRandom, $expectedResult) { | |
192 | $this->assertEquals($expectedResult, CRM_Core_DAO::shortenSQLName($inputData, $length, $makeRandom)); | |
193 | } | |
194 | ||
71e5aa5c | 195 | } |