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