Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
4cbe18b8 EM |
36 | /** |
37 | * Class CRM_Core_BAO_LocationTest | |
38 | */ | |
6a488035 | 39 | class CRM_Core_BAO_LocationTest extends CiviUnitTestCase { |
00be9182 | 40 | public function setUp() { |
6a488035 TO |
41 | parent::setUp(); |
42 | ||
92915c55 TO |
43 | $this->quickCleanup(array( |
44 | 'civicrm_contact', | |
45 | 'civicrm_address', | |
46 | 'civicrm_loc_block', | |
47 | 'civicrm_email', | |
48 | 'civicrm_phone', | |
af9b09df | 49 | 'civicrm_im', |
92915c55 | 50 | )); |
6a488035 TO |
51 | } |
52 | ||
53 | /** | |
54 | * Tears down the fixture, for example, closes a network connection. | |
55 | * This method is called after a test is executed. | |
6a488035 | 56 | */ |
00be9182 | 57 | public function tearDown() { |
6a488035 TO |
58 | $tablesToTruncate = array( |
59 | 'civicrm_contact', | |
60 | 'civicrm_openid', | |
61 | 'civicrm_loc_block', | |
62 | ); | |
63 | $this->quickCleanup($tablesToTruncate); | |
64 | } | |
65 | ||
00be9182 | 66 | public function testCreateWithMissingParams() { |
6a488035 TO |
67 | $contactId = Contact::createIndividual(); |
68 | $params = array( | |
69 | 'contact_id' => $contactId, | |
70 | 'street_address' => 'Saint Helier St', | |
71 | ); | |
72 | ||
73 | CRM_Core_BAO_Location::create($params); | |
74 | ||
75 | //Now check DB for Address | |
76 | $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address', | |
77 | 'Database check, Address created successfully.' | |
78 | ); | |
79 | ||
80 | //cleanup DB by deleting the contact | |
81 | Contact::delete($contactId); | |
82 | } | |
83 | ||
84 | /** | |
100fef9d | 85 | * Create() method |
6a488035 TO |
86 | * create various elements of location block |
87 | * without civicrm_loc_block entry | |
88 | */ | |
00be9182 | 89 | public function testCreateWithoutLocBlock() { |
6a488035 TO |
90 | $contactId = Contact::createIndividual(); |
91 | ||
92 | //create various element of location block | |
93 | //like address, phone, email, openid, im. | |
94 | $params = array( | |
95 | 'address' => array( | |
96 | '1' => array( | |
97 | 'street_address' => 'Saint Helier St', | |
98 | 'supplemental_address_1' => 'Hallmark Ct', | |
99 | 'supplemental_address_2' => 'Jersey Village', | |
100 | 'city' => 'Newark', | |
101 | 'postal_code' => '01903', | |
102 | 'country_id' => 1228, | |
103 | 'state_province_id' => 1029, | |
104 | 'geo_code_1' => '18.219023', | |
105 | 'geo_code_2' => '-105.00973', | |
106 | 'is_primary' => 1, | |
107 | 'location_type_id' => 1, | |
108 | ), | |
109 | ), | |
110 | 'email' => array( | |
111 | '1' => array( | |
112 | 'email' => 'john.smith@example.org', | |
113 | 'is_primary' => 1, | |
114 | 'location_type_id' => 1, | |
115 | ), | |
116 | ), | |
117 | 'phone' => array( | |
118 | '1' => array( | |
119 | 'phone_type_id' => 1, | |
120 | 'phone' => '303443689', | |
121 | 'is_primary' => 1, | |
122 | 'location_type_id' => 1, | |
123 | ), | |
124 | '2' => array( | |
125 | 'phone_type_id' => 2, | |
126 | 'phone' => '9833910234', | |
127 | 'location_type_id' => 1, | |
128 | ), | |
129 | ), | |
130 | 'openid' => array( | |
131 | '1' => array( | |
132 | 'openid' => 'http://civicrm.org/', | |
133 | 'location_type_id' => 1, | |
134 | 'is_primary' => 1, | |
135 | ), | |
136 | ), | |
137 | 'im' => array( | |
138 | '1' => array( | |
139 | 'name' => 'jane.doe', | |
140 | 'provider_id' => 1, | |
141 | 'location_type_id' => 1, | |
142 | 'is_primary' => 1, | |
143 | ), | |
144 | ), | |
145 | ); | |
146 | ||
147 | $params['contact_id'] = $contactId; | |
148 | ||
149 | $location = CRM_Core_BAO_Location::create($params); | |
150 | ||
151 | $locBlockId = CRM_Utils_Array::value('id', $location); | |
152 | ||
153 | //Now check DB for contact | |
154 | $searchParams = array( | |
155 | 'contact_id' => $contactId, | |
156 | 'location_type_id' => 1, | |
157 | 'is_primary' => 1, | |
158 | ); | |
159 | $compareParams = array( | |
160 | 'street_address' => 'Saint Helier St', | |
161 | 'supplemental_address_1' => 'Hallmark Ct', | |
162 | 'supplemental_address_2' => 'Jersey Village', | |
163 | 'city' => 'Newark', | |
164 | 'postal_code' => '01903', | |
165 | 'country_id' => 1228, | |
166 | 'state_province_id' => 1029, | |
167 | 'geo_code_1' => '18.219023', | |
168 | 'geo_code_2' => '-105.00973', | |
169 | ); | |
170 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); | |
171 | ||
172 | $compareParams = array('email' => 'john.smith@example.org'); | |
173 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); | |
174 | ||
175 | $compareParams = array('openid' => 'http://civicrm.org/'); | |
176 | $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams); | |
177 | ||
178 | $compareParams = array( | |
179 | 'name' => 'jane.doe', | |
180 | 'provider_id' => 1, | |
181 | ); | |
182 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); | |
183 | ||
184 | $searchParams = array( | |
185 | 'contact_id' => $contactId, | |
186 | 'location_type_id' => 1, | |
187 | 'is_primary' => 1, | |
188 | 'phone_type_id' => 1, | |
189 | ); | |
190 | $compareParams = array('phone' => '303443689'); | |
191 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); | |
192 | ||
193 | $searchParams = array( | |
194 | 'contact_id' => $contactId, | |
195 | 'location_type_id' => 1, | |
196 | 'phone_type_id' => 2, | |
197 | ); | |
198 | $compareParams = array('phone' => '9833910234'); | |
199 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); | |
200 | ||
201 | //delete the location block | |
202 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); | |
203 | ||
204 | //cleanup DB by deleting the contact | |
205 | Contact::delete($contactId); | |
206 | } | |
207 | ||
208 | /** | |
100fef9d | 209 | * Create() method |
6a488035 TO |
210 | * create various elements of location block |
211 | * with civicrm_loc_block | |
212 | */ | |
00be9182 | 213 | public function testCreateWithLocBlock() { |
6a488035 TO |
214 | $this->_contactId = Contact::createIndividual(); |
215 | //create test event record. | |
216 | $eventId = Event::create($this->_contactId); | |
217 | $params = array( | |
218 | 'address' => array( | |
219 | '1' => array( | |
220 | 'street_address' => 'Saint Helier St', | |
221 | 'supplemental_address_1' => 'Hallmark Ct', | |
222 | 'supplemental_address_2' => 'Jersey Village', | |
223 | 'city' => 'Newark', | |
224 | 'postal_code' => '01903', | |
225 | 'country_id' => 1228, | |
226 | 'state_province_id' => 1029, | |
227 | 'geo_code_1' => '18.219023', | |
228 | 'geo_code_2' => '-105.00973', | |
229 | 'is_primary' => 1, | |
230 | 'location_type_id' => 1, | |
231 | ), | |
232 | ), | |
233 | 'email' => array( | |
234 | '1' => array( | |
235 | 'email' => 'john.smith@example.org', | |
236 | 'is_primary' => 1, | |
237 | 'location_type_id' => 1, | |
238 | ), | |
239 | ), | |
240 | 'phone' => array( | |
241 | '1' => array( | |
242 | 'phone_type_id' => 1, | |
243 | 'phone' => '303443689', | |
244 | 'is_primary' => 1, | |
245 | 'location_type_id' => 1, | |
246 | ), | |
247 | '2' => array( | |
248 | 'phone_type_id' => 2, | |
249 | 'phone' => '9833910234', | |
250 | 'location_type_id' => 1, | |
251 | ), | |
252 | ), | |
253 | 'im' => array( | |
254 | '1' => array( | |
255 | 'name' => 'jane.doe', | |
256 | 'provider_id' => 1, | |
257 | 'location_type_id' => 1, | |
258 | 'is_primary' => 1, | |
259 | ), | |
260 | ), | |
261 | ); | |
262 | ||
263 | $params['entity_id'] = $eventId; | |
264 | $params['entity_table'] = 'civicrm_event'; | |
265 | ||
266 | //create location block. | |
267 | //with various element of location block | |
268 | //like address, phone, email, im. | |
269 | $location = CRM_Core_BAO_Location::create($params, NULL, TRUE); | |
270 | $locBlockId = CRM_Utils_Array::value('id', $location); | |
271 | ||
272 | //update event record with location block id | |
273 | $eventParams = array( | |
274 | 'id' => $eventId, | |
275 | 'loc_block_id' => $locBlockId, | |
276 | ); | |
277 | ||
278 | CRM_Event_BAO_Event::add($eventParams); | |
279 | ||
280 | //Now check DB for location block | |
281 | ||
282 | $this->assertDBCompareValue('CRM_Event_DAO_Event', | |
283 | $eventId, | |
284 | 'loc_block_id', | |
285 | 'id', | |
286 | $locBlockId, | |
287 | 'Checking database for the record.' | |
288 | ); | |
289 | $locElementIds = array(); | |
290 | $locParams = array('id' => $locBlockId); | |
291 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock', | |
292 | $locParams, | |
293 | $locElementIds | |
294 | ); | |
295 | ||
296 | //Now check DB for location elements. | |
6c6e6187 | 297 | $searchParams = array( |
92915c55 | 298 | 'id' => CRM_Utils_Array::value('address_id', $locElementIds), |
6a488035 TO |
299 | 'location_type_id' => 1, |
300 | 'is_primary' => 1, | |
301 | ); | |
302 | $compareParams = array( | |
303 | 'street_address' => 'Saint Helier St', | |
304 | 'supplemental_address_1' => 'Hallmark Ct', | |
305 | 'supplemental_address_2' => 'Jersey Village', | |
306 | 'city' => 'Newark', | |
307 | 'postal_code' => '01903', | |
308 | 'country_id' => 1228, | |
309 | 'state_province_id' => 1029, | |
310 | 'geo_code_1' => '18.219023', | |
311 | 'geo_code_2' => '-105.00973', | |
312 | ); | |
313 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); | |
314 | ||
6c6e6187 | 315 | $searchParams = array( |
92915c55 | 316 | 'id' => CRM_Utils_Array::value('email_id', $locElementIds), |
6a488035 TO |
317 | 'location_type_id' => 1, |
318 | 'is_primary' => 1, | |
319 | ); | |
320 | $compareParams = array('email' => 'john.smith@example.org'); | |
321 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); | |
322 | ||
6c6e6187 | 323 | $searchParams = array( |
92915c55 | 324 | 'id' => CRM_Utils_Array::value('phone_id', $locElementIds), |
6a488035 TO |
325 | 'location_type_id' => 1, |
326 | 'is_primary' => 1, | |
327 | 'phone_type_id' => 1, | |
328 | ); | |
329 | $compareParams = array('phone' => '303443689'); | |
330 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); | |
331 | ||
6c6e6187 | 332 | $searchParams = array( |
92915c55 | 333 | 'id' => CRM_Utils_Array::value('phone_2_id', $locElementIds), |
6a488035 TO |
334 | 'location_type_id' => 1, |
335 | 'phone_type_id' => 2, | |
336 | ); | |
337 | $compareParams = array('phone' => '9833910234'); | |
338 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); | |
339 | ||
6c6e6187 | 340 | $searchParams = array( |
92915c55 | 341 | 'id' => CRM_Utils_Array::value('im_id', $locElementIds), |
6a488035 TO |
342 | 'location_type_id' => 1, |
343 | 'is_primary' => 1, | |
344 | ); | |
345 | $compareParams = array( | |
346 | 'name' => 'jane.doe', | |
347 | 'provider_id' => 1, | |
348 | ); | |
349 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); | |
350 | ||
351 | //delete the location block | |
352 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); | |
353 | ||
354 | //cleanup DB by deleting the record. | |
355 | Event::delete($eventId); | |
356 | Contact::delete($this->_contactId); | |
357 | } | |
358 | ||
359 | /** | |
100fef9d | 360 | * DeleteLocBlock() method |
6a488035 TO |
361 | * delete the location block |
362 | * created with various elements. | |
6a488035 | 363 | */ |
00be9182 | 364 | public function testDeleteLocBlock() { |
6a488035 TO |
365 | $this->_contactId = Contact::createIndividual(); |
366 | //create test event record. | |
367 | $eventId = Event::create($this->_contactId); | |
368 | $params['location'][1] = array( | |
369 | 'location_type_id' => 1, | |
370 | 'is_primary' => 1, | |
371 | 'address' => array( | |
372 | 'street_address' => 'Saint Helier St', | |
373 | 'supplemental_address_1' => 'Hallmark Ct', | |
374 | 'supplemental_address_2' => 'Jersey Village', | |
375 | 'city' => 'Newark', | |
376 | 'postal_code' => '01903', | |
377 | 'country_id' => 1228, | |
378 | 'state_province_id' => 1029, | |
379 | 'geo_code_1' => '18.219023', | |
380 | 'geo_code_2' => '-105.00973', | |
381 | ), | |
382 | 'email' => array( | |
383 | '1' => array('email' => 'john.smith@example.org'), | |
384 | ), | |
385 | 'phone' => array( | |
386 | '1' => array( | |
387 | 'phone_type_id' => 1, | |
388 | 'phone' => '303443689', | |
389 | ), | |
390 | '2' => array( | |
391 | 'phone_type_id' => 2, | |
392 | 'phone' => '9833910234', | |
393 | ), | |
394 | ), | |
395 | 'im' => array( | |
396 | '1' => array( | |
397 | 'name' => 'jane.doe', | |
398 | 'provider_id' => 1, | |
399 | ), | |
400 | ), | |
401 | ); | |
402 | $params['entity_id'] = $eventId; | |
403 | $params['entity_table'] = 'civicrm_event'; | |
404 | ||
405 | //create location block. | |
406 | //with various elements | |
407 | //like address, phone, email, im. | |
408 | $location = CRM_Core_BAO_Location::create($params, NULL, TRUE); | |
409 | $locBlockId = CRM_Utils_Array::value('id', $location); | |
410 | //update event record with location block id | |
411 | $eventParams = array( | |
412 | 'id' => $eventId, | |
413 | 'loc_block_id' => $locBlockId, | |
414 | ); | |
415 | CRM_Event_BAO_Event::add($eventParams); | |
416 | ||
417 | //delete the location block | |
418 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); | |
419 | ||
420 | //Now check DB for location elements. | |
421 | //Now check DB for Address | |
422 | $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address', | |
423 | 'Database check, Address deleted successfully.' | |
424 | ); | |
425 | //Now check DB for Email | |
426 | $this->assertDBNull('CRM_Core_DAO_Email', 'john.smith@example.org', 'id', 'email', | |
427 | 'Database check, Email deleted successfully.' | |
428 | ); | |
429 | //Now check DB for Phone | |
430 | $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone', | |
431 | 'Database check, Phone deleted successfully.' | |
432 | ); | |
433 | //Now check DB for Mobile | |
434 | $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone', | |
435 | 'Database check, Mobile deleted successfully.' | |
436 | ); | |
437 | //Now check DB for IM | |
438 | $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name', | |
439 | 'Database check, IM deleted successfully.' | |
440 | ); | |
441 | ||
442 | //cleanup DB by deleting the record. | |
443 | Event::delete($eventId); | |
444 | Contact::delete($this->_contactId); | |
445 | ||
446 | //Now check DB for Event | |
447 | $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id', | |
448 | 'Database check, Event deleted successfully.' | |
449 | ); | |
450 | } | |
451 | ||
452 | /** | |
100fef9d | 453 | * GetValues() method |
6a488035 TO |
454 | * get the values of various location elements |
455 | */ | |
00be9182 | 456 | public function testLocBlockgetValues() { |
6a488035 TO |
457 | $contactId = Contact::createIndividual(); |
458 | ||
459 | //create various element of location block | |
460 | //like address, phone, email, openid, im. | |
461 | $params = array( | |
462 | 'address' => array( | |
463 | '1' => array( | |
464 | 'street_address' => 'Saint Helier St', | |
465 | 'supplemental_address_1' => 'Hallmark Ct', | |
466 | 'supplemental_address_2' => 'Jersey Village', | |
467 | 'city' => 'Newark', | |
468 | 'postal_code' => '01903', | |
469 | 'country_id' => 1228, | |
470 | 'state_province_id' => 1029, | |
471 | 'geo_code_1' => '18.219023', | |
472 | 'geo_code_2' => '-105.00973', | |
473 | 'is_primary' => 1, | |
474 | 'location_type_id' => 1, | |
475 | ), | |
476 | ), | |
477 | 'email' => array( | |
478 | '1' => array( | |
479 | 'email' => 'john.smith@example.org', | |
480 | 'is_primary' => 1, | |
481 | 'location_type_id' => 1, | |
482 | ), | |
483 | ), | |
484 | 'phone' => array( | |
485 | '1' => array( | |
486 | 'phone_type_id' => 1, | |
487 | 'phone' => '303443689', | |
488 | 'is_primary' => 1, | |
489 | 'location_type_id' => 1, | |
490 | ), | |
491 | '2' => array( | |
492 | 'phone_type_id' => 2, | |
493 | 'phone' => '9833910234', | |
494 | 'location_type_id' => 1, | |
495 | ), | |
496 | ), | |
497 | 'openid' => array( | |
498 | '1' => array( | |
499 | 'openid' => 'http://civicrm.org/', | |
500 | 'location_type_id' => 1, | |
501 | 'is_primary' => 1, | |
502 | ), | |
503 | ), | |
504 | 'im' => array( | |
505 | '1' => array( | |
506 | 'name' => 'jane.doe', | |
507 | 'provider_id' => 1, | |
508 | 'location_type_id' => 1, | |
509 | 'is_primary' => 1, | |
510 | ), | |
511 | ), | |
512 | ); | |
513 | ||
514 | $params['contact_id'] = $contactId; | |
515 | ||
516 | //create location elements. | |
517 | CRM_Core_BAO_Location::create($params); | |
518 | ||
519 | //get the values from DB | |
520 | $values = CRM_Core_BAO_Location::getValues($params); | |
521 | ||
522 | //Now check values of address | |
523 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']), | |
524 | CRM_Utils_Array::value('1', $values['address']) | |
525 | ); | |
526 | ||
527 | //Now check values of email | |
528 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']), | |
529 | CRM_Utils_Array::value('1', $values['email']) | |
530 | ); | |
531 | ||
532 | //Now check values of phone | |
533 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']), | |
534 | CRM_Utils_Array::value('1', $values['phone']) | |
535 | ); | |
536 | ||
537 | //Now check values of mobile | |
538 | $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']), | |
539 | CRM_Utils_Array::value('2', $values['phone']) | |
540 | ); | |
541 | ||
542 | //Now check values of openid | |
543 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']), | |
544 | CRM_Utils_Array::value('1', $values['openid']) | |
545 | ); | |
546 | ||
547 | //Now check values of im | |
548 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']), | |
549 | CRM_Utils_Array::value('1', $values['im']) | |
550 | ); | |
551 | ||
552 | //cleanup DB by deleting the contact | |
553 | Contact::delete($contactId); | |
554 | } | |
96025800 | 555 | |
6a488035 | 556 | } |