CRM_Contact_BAO_QueryTest - Update assertion on ORDER BY to use backtick
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / ContactTest.php
CommitLineData
6a488035 1<?php
0eea664b 2
6a488035
TO
3require_once 'CiviTest/Contact.php';
4require_once 'CiviTest/Custom.php';
aba1cd8b
EM
5
6/**
7 * Class CRM_Contact_BAO_ContactTest
acb109b7 8 * @group headless
aba1cd8b 9 */
6a488035 10class CRM_Contact_BAO_ContactTest extends CiviUnitTestCase {
22f80e87
EM
11
12 /**
13 * Set up function.
14 */
00be9182 15 public function setUp() {
6a488035
TO
16 parent::setUp();
17 }
18
19 /**
22f80e87
EM
20 * Test case for add( ).
21 *
6a488035
TO
22 * test with empty params.
23 */
00be9182 24 public function testAddWithEmptyParams() {
6c6e6187 25 $params = array();
6a488035
TO
26 $contact = CRM_Contact_BAO_Contact::add($params);
27
22f80e87 28 // Now check Contact object.
6a488035
TO
29 $this->assertNull($contact);
30 }
31
32 /**
22f80e87
EM
33 * Test case for add( ).
34 *
35 * Test with names (create and update modes)
6a488035 36 */
00be9182 37 public function testAddWithNames() {
6a488035 38 $firstName = 'Shane';
92915c55
TO
39 $lastName = 'Whatson';
40 $params = array(
6a488035
TO
41 'first_name' => $firstName,
42 'last_name' => $lastName,
43 'contact_type' => 'Individual',
44 );
45
46 $contact = CRM_Contact_BAO_Contact::add($params);
47
22f80e87 48 // Now check $contact is object of contact DAO.
6a488035
TO
49 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
50 $this->assertEquals($firstName, $contact->first_name, 'Check for first name creation.');
51 $this->assertEquals($lastName, $contact->last_name, 'Check for last name creation.');
52
53 $contactId = $contact->id;
54
22f80e87 55 // Update and change first name and last name, using add( ).
6a488035
TO
56 $firstName = 'Jane';
57 $params = array(
58 'first_name' => $firstName,
59 'contact_type' => 'Individual',
60 'contact_id' => $contactId,
61 );
62
63 $contact = CRM_Contact_BAO_Contact::add($params);
64
22f80e87 65 // Now check $contact is object of contact DAO.
6a488035
TO
66 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
67 $this->assertEquals($firstName, $contact->first_name, 'Check for updated first name.');
68
69 $contactId = $contact->id;
70 Contact::delete($contactId);
71 }
72
73 /**
22f80e87
EM
74 * Test case for add.
75 *
76 * Test with all contact params
77 * (create and update modes)
6a488035 78 */
00be9182 79 public function testAddWithAll() {
22f80e87 80 // Take the common contact params.
6a488035
TO
81 $params = $this->contactParams();
82
83 unset($params['location']);
84 $prefComm = $params['preferred_communication_method'];
6a488035
TO
85 $contact = CRM_Contact_BAO_Contact::add($params);
86 $contactId = $contact->id;
87
6a488035
TO
88 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
89
6a488035
TO
90 $this->assertEquals($params['first_name'], $contact->first_name, 'Check for first name creation.');
91 $this->assertEquals($params['last_name'], $contact->last_name, 'Check for last name creation.');
92 $this->assertEquals($params['middle_name'], $contact->middle_name, 'Check for middle name creation.');
93 $this->assertEquals($params['contact_type'], $contact->contact_type, 'Check for contact type creation.');
94 $this->assertEquals('1', $contact->do_not_email, 'Check for do_not_email creation.');
95 $this->assertEquals('1', $contact->do_not_phone, 'Check for do_not_phone creation.');
96 $this->assertEquals('1', $contact->do_not_mail, 'Check for do_not_mail creation.');
97 $this->assertEquals('1', $contact->do_not_trade, 'Check for do_not_trade creation.');
98 $this->assertEquals('1', $contact->is_opt_out, 'Check for is_opt_out creation.');
99 $this->assertEquals($params['external_identifier'], $contact->external_identifier, 'Check for external_identifier creation.');
100 $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name, 'Check for sort_name creation.');
101 $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format,
102 'Check for preferred_mail_format creation.'
103 );
104 $this->assertEquals($params['contact_source'], $contact->source, 'Check for contact_source creation.');
105 $this->assertEquals($params['prefix_id'], $contact->prefix_id, 'Check for prefix_id creation.');
106 $this->assertEquals($params['suffix_id'], $contact->suffix_id, 'Check for suffix_id creation.');
107 $this->assertEquals($params['job_title'], $contact->job_title, 'Check for job_title creation.');
108 $this->assertEquals($params['gender_id'], $contact->gender_id, 'Check for gender_id creation.');
109 $this->assertEquals('1', $contact->is_deceased, 'Check for is_deceased creation.');
110 $this->assertEquals(CRM_Utils_Date::processDate($params['birth_date']),
111 $contact->birth_date, 'Check for birth_date creation.'
112 );
113 $this->assertEquals(CRM_Utils_Date::processDate($params['deceased_date']),
114 $contact->deceased_date, 'Check for deceased_date creation.'
115 );
116 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
117 $contact->preferred_communication_method
118 );
119 $checkPrefComm = array();
120 foreach ($dbPrefComm as $key => $value) {
121 if ($value) {
122 $checkPrefComm[$value] = 1;
123 }
124 }
125 $this->assertAttributesEquals($checkPrefComm, $prefComm);
126
6a488035
TO
127 $updateParams = array(
128 'contact_type' => 'Individual',
129 'first_name' => 'Jane',
130 'middle_name' => 'abc',
131 'last_name' => 'Doe',
132 'prefix_id' => 2,
133 'suffix_id' => 3,
134 'nick_name' => 'Nick Name Second',
135 'job_title' => 'software Developer',
136 'gender_id' => 1,
137 'is_deceased' => 1,
138 'website' => array(
139 1 => array(
140 'website_type_id' => 1,
141 'url' => 'http://docs.civicrm.org',
142 ),
143 ),
144 'contact_source' => 'test update contact',
145 'external_identifier' => 111111111,
146 'preferred_mail_format' => 'Both',
147 'is_opt_out' => 0,
148 'deceased_date' => '1981-03-03',
149 'birth_date' => '1951-04-04',
150 'privacy' => array(
151 'do_not_phone' => 0,
152 'do_not_email' => 0,
153 'do_not_mail' => 0,
154 'do_not_trade' => 0,
155 ),
156 'preferred_communication_method' => array(
157 '1' => 0,
158 '2' => 1,
159 '3' => 0,
160 '4' => 1,
161 '5' => 0,
162 ),
163 );
164
165 $prefComm = $updateParams['preferred_communication_method'];
166 $updateParams['contact_id'] = $contactId;
6a488035
TO
167 $contact = CRM_Contact_BAO_Contact::create($updateParams);
168 $contactId = $contact->id;
169
6a488035
TO
170 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
171
6a488035
TO
172 $this->assertEquals($updateParams['first_name'], $contact->first_name, 'Check for first name creation.');
173 $this->assertEquals($updateParams['last_name'], $contact->last_name, 'Check for last name creation.');
174 $this->assertEquals($updateParams['middle_name'], $contact->middle_name, 'Check for middle name creation.');
175 $this->assertEquals($updateParams['contact_type'], $contact->contact_type, 'Check for contact type creation.');
176 $this->assertEquals('0', $contact->do_not_email, 'Check for do_not_email creation.');
177 $this->assertEquals('0', $contact->do_not_phone, 'Check for do_not_phone creation.');
178 $this->assertEquals('0', $contact->do_not_mail, 'Check for do_not_mail creation.');
179 $this->assertEquals('0', $contact->do_not_trade, 'Check for do_not_trade creation.');
180 $this->assertEquals('0', $contact->is_opt_out, 'Check for is_opt_out creation.');
181 $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier,
182 'Check for external_identifier creation.'
183 );
184 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
185 $contact->sort_name, 'Check for sort_name creation.'
186 );
187 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format,
188 'Check for preferred_mail_format creation.'
189 );
190 $this->assertEquals($updateParams['contact_source'], $contact->source, 'Check for contact_source creation.');
191 $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id, 'Check for prefix_id creation.');
192 $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id, 'Check for suffix_id creation.');
193 $this->assertEquals($updateParams['job_title'], $contact->job_title, 'Check for job_title creation.');
194 $this->assertEquals($updateParams['gender_id'], $contact->gender_id, 'Check for gender_id creation.');
195 $this->assertEquals('1', $contact->is_deceased, 'Check for is_deceased creation.');
196 $this->assertEquals(CRM_Utils_Date::processDate($updateParams['birth_date']),
197 date('YmdHis', strtotime($contact->birth_date)), 'Check for birth_date creation.'
198 );
199 $this->assertEquals(CRM_Utils_Date::processDate($updateParams['deceased_date']),
200 date('YmdHis', strtotime($contact->deceased_date)), 'Check for deceased_date creation.'
201 );
202 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
203 $contact->preferred_communication_method
204 );
205 $checkPrefComm = array();
206 foreach ($dbPrefComm as $key => $value) {
207 if ($value) {
208 $checkPrefComm[$value] = 1;
209 }
210 }
211 $this->assertAttributesEquals($checkPrefComm, $prefComm);
212
6a488035
TO
213 Contact::delete($contactId);
214 }
215
216 /**
22f80e87 217 * Test case for add( ) with All contact types.
6a488035 218 */
00be9182 219 public function testAddWithAllContactTypes() {
6a488035 220 $firstName = 'Bill';
92915c55
TO
221 $lastName = 'Adams';
222 $params = array(
6a488035
TO
223 'first_name' => $firstName,
224 'last_name' => $lastName,
225 'contact_type' => 'Individual',
226 );
227
228 $contact = CRM_Contact_BAO_Contact::add($params);
229 $this->assertEquals($firstName, $contact->first_name, 'Check for first name creation.');
230 $this->assertEquals($lastName, $contact->last_name, 'Check for last name creation.');
231
232 $contactId = $contact->id;
233
234 //update and change first name and last name, using create()
235 $firstName = 'Joan';
236 $params = array(
237 'first_name' => $firstName,
238 'contact_type' => 'Individual',
239 'contact_id' => $contactId,
240 );
241
242 $contact = CRM_Contact_BAO_Contact::add($params);
243 $this->assertEquals($firstName, $contact->first_name, 'Check for updated first name.');
244 $contactId = $contact->id;
245 Contact::delete($contactId);
246
247 $householdName = 'Adams house';
248 $params = array(
249 'household_name' => $householdName,
250 'contact_type' => 'Household',
251 );
252 $contact = CRM_Contact_BAO_Contact::add($params);
253 $this->assertEquals($householdName, $contact->sort_name, 'Check for created household.');
254 $contactId = $contact->id;
255
256 //update and change name of household, using create
257 $householdName = 'Joans home';
258 $params = array(
259 'household_name' => $householdName,
260 'contact_type' => 'Household',
261 'contact_id' => $contactId,
262 );
263 $contact = CRM_Contact_BAO_Contact::add($params);
264 $this->assertEquals($householdName, $contact->sort_name, 'Check for updated household.');
265 Contact::delete($contactId);
266
267 $organizationName = 'My Organization';
268 $params = array(
269 'organization_name' => $organizationName,
270 'contact_type' => 'Organization',
271 );
272 $contact = CRM_Contact_BAO_Contact::add($params);
273 $this->assertEquals($organizationName, $contact->sort_name, 'Check for created organization.');
274 $contactId = $contact->id;
275
276 //update and change name of organization, using create
277 $organizationName = 'Your Changed Organization';
278 $params = array(
279 'organization_name' => $organizationName,
280 'contact_type' => 'Organization',
281 'contact_id' => $contactId,
282 );
283 $contact = CRM_Contact_BAO_Contact::add($params);
284 $this->assertEquals($organizationName, $contact->sort_name, 'Check for updated organization.');
285 Contact::delete($contactId);
286 }
287
288 /**
92c99a4a
EM
289 * Test case for create.
290 *
291 * Test with missing params.
6a488035 292 */
00be9182 293 public function testCreateWithEmptyParams() {
6a488035
TO
294 $params = array(
295 'first_name' => 'Bill',
296 'last_name' => 'Adams',
297 );
298 $contact = CRM_Contact_BAO_Contact::create($params);
299
300 //Now check Contact object
301 $this->assertNull($contact);
302 }
303
304 /**
92c99a4a
EM
305 * Test case for create.
306 *
307 * Test with all params.
6a488035
TO
308 * ( create and update modes ).
309 */
00be9182 310 public function testCreateWithAll() {
6a488035
TO
311 //take the common contact params
312 $params = $this->contactParams();
313 $params['note'] = 'test note';
314
315 //create the contact with given params.
316 $contact = CRM_Contact_BAO_Contact::create($params);
317
318 //Now check $contact is object of contact DAO..
319 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
320 $contactId = $contact->id;
321
322 //Now check values of contact object with params.
323 $this->assertEquals($params['first_name'], $contact->first_name, 'Check for first name creation.');
324 $this->assertEquals($params['last_name'], $contact->last_name, 'Check for last name creation.');
325 $this->assertEquals($params['contact_type'], $contact->contact_type, 'Check for contact type creation.');
326
327 //Now check DB for Address
328 $searchParams = array(
329 'contact_id' => $contactId,
330 'location_type_id' => 1,
331 'is_primary' => 1,
332 );
6c6e6187 333 $compareParams = array(
92915c55 334 'street_address' => CRM_Utils_Array::value('street_address', $params['address'][1]),
6a488035
TO
335 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1',
336 $params['address'][1]
337 ),
338 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2',
339 $params['address'][1]
340 ),
341 'city' => CRM_Utils_Array::value('city', $params['address'][1]),
342 'postal_code' => CRM_Utils_Array::value('postal_code', $params['address'][1]),
343 'country_id' => CRM_Utils_Array::value('country_id', $params['address'][1]),
344 'state_province_id' => CRM_Utils_Array::value('state_province_id',
345 $params['address'][1]
346 ),
347 'geo_code_1' => CRM_Utils_Array::value('geo_code_1', $params['address'][1]),
348 'geo_code_2' => CRM_Utils_Array::value('geo_code_2', $params['address'][1]),
349 );
350 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
351
352 //Now check DB for Email
353 $compareParams = array('email' => CRM_Utils_Array::value('email', $params['email'][1]));
354 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
355
356 //Now check DB for openid
357 $compareParams = array('openid' => CRM_Utils_Array::value('openid', $params['openid'][1]));
358 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
359
360 //Now check DB for IM
6c6e6187 361 $compareParams = array(
92915c55 362 'name' => CRM_Utils_Array::value('name', $params['im'][1]),
6a488035
TO
363 'provider_id' => CRM_Utils_Array::value('provider_id', $params['im'][1]),
364 );
365 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
366
367 //Now check DB for Phone
368 $searchParams = array(
369 'contact_id' => $contactId,
370 'location_type_id' => 1,
371 'is_primary' => 1,
372 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][1]),
373 );
374 $compareParams = array('phone' => CRM_Utils_Array::value('phone', $params['phone'][1]));
375 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
376
377 //Now check DB for Mobile
378 $searchParams = array(
379 'contact_id' => $contactId,
380 'location_type_id' => 1,
381 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
382 );
383 $compareParams = array('phone' => CRM_Utils_Array::value('phone', $params['phone'][2]));
384 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
385
386 //Now check DB for Note
387 $searchParams = array(
388 'entity_id' => $contactId,
389 'entity_table' => 'civicrm_contact',
390 );
391 $compareParams = array('note' => $params['note']);
392 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
393
394 //update the contact.
395 $updateParams = array(
396 'first_name' => 'John',
397 'last_name' => 'Doe',
398 'contact_type' => 'Individual',
399 'note' => 'new test note',
400 );
401 $updateParams['address'][1] = array(
402 'location_type_id' => 1,
403 'is_primary' => 1,
404 'street_address' => 'Oberoi Garden',
405 'supplemental_address_1' => 'A-wing:3037',
406 'supplemental_address_2' => 'Andhery',
407 'city' => 'Mumbai',
408 'postal_code' => '12345',
409 'country_id' => 1228,
410 'state_province_id' => 1004,
411 'geo_code_1' => '31.694842',
412 'geo_code_2' => '-106.29998',
413 );
414 $updateParams['email'][1] = array(
415 'location_type_id' => 1,
416 'is_primary' => 1,
417 'email' => 'john.doe@example.org',
418 );
419
420 $updateParams['phone'][1] = array(
421 'location_type_id' => 1,
422 'is_primary' => 1,
423 'phone_type_id' => 1,
424 'phone' => '02115245336',
425 );
426 $updateParams['phone'][2] = array(
427 'location_type_id' => 1,
428 'phone_type_id' => 2,
429 'phone' => '9766323895',
430 );
431
432 $updateParams['contact_id'] = $contactId;
433 //create the contact with given params.
434 $contact = CRM_Contact_BAO_Contact::create($updateParams);
435
436 //Now check $contact is object of contact DAO..
437 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
438 $contactId = $contact->id;
439
440 //Now check values of contact object with updated params.
441 $this->assertEquals($updateParams['first_name'], $contact->first_name, 'Check for first name creation.');
442 $this->assertEquals($updateParams['last_name'], $contact->last_name, 'Check for last name creation.');
443 $this->assertEquals($updateParams['contact_type'], $contact->contact_type, 'Check for contact type creation.');
444
445 //Now check DB for updated Address
446 $searchParams = array(
447 'contact_id' => $contactId,
448 'location_type_id' => 1,
449 'is_primary' => 1,
450 );
451 $compareParams = array(
452 'street_address' => 'Oberoi Garden',
453 'supplemental_address_1' => 'A-wing:3037',
454 'supplemental_address_2' => 'Andhery',
455 'city' => 'Mumbai',
456 'postal_code' => '12345',
457 'country_id' => 1228,
458 'state_province_id' => 1004,
459 'geo_code_1' => '31.694842',
460 'geo_code_2' => '-106.29998',
461 );
462 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
463
464 //Now check DB for updated Email
465 $compareParams = array('email' => 'john.doe@example.org');
466 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
467
468 //Now check DB for updated Phone
469 $searchParams = array(
470 'contact_id' => $contactId,
471 'location_type_id' => 1,
472 'is_primary' => 1,
473 'phone_type_id' => 1,
474 );
475 $compareParams = array('phone' => '02115245336');
476 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
477
478 //Now check DB for updated Mobile
479 $searchParams = array(
480 'contact_id' => $contactId,
481 'location_type_id' => 1,
482 'phone_type_id' => 2,
483 );
484 $compareParams = array('phone' => '9766323895');
485 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
22f80e87
EM
486 // As we are not updating note.
487 // Now check DB for New Note.
488 $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note',
6a488035
TO
489 'Database check for New created note '
490 );
491
22f80e87 492 // Delete all notes related to contact.
6a488035
TO
493 CRM_Core_BAO_Note::cleanContactNotes($contactId);
494
22f80e87 495 // Cleanup DB by deleting the contact.
6a488035
TO
496 Contact::delete($contactId);
497 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
498 }
499
500 /**
db7de9c1
EM
501 * Test case for resolveDefaults( ).
502 *
503 * Test all pseudoConstant, stateProvince, country.
6a488035 504 */
00be9182 505 public function testResolveDefaults() {
6a488035
TO
506 $params = array(
507 'prefix_id' => 3,
508 'suffix_id' => 2,
509 'gender_id' => 2,
510 'birth_date' => '1983-12-13',
511 );
512
513 $params['address'][1] = array(
514 'location_type_id' => 1,
515 'is_primary' => 1,
516 'country_id' => 1228,
517 'state_province_id' => 1004,
518 );
519 CRM_Contact_BAO_Contact::resolveDefaults($params);
520
521 //check the resolve values.
26cf88b5 522 $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
6a488035 523 $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
e6c4755b 524 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
6a488035 525 $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
e6c4755b 526 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
6a488035
TO
527 $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.');
528 $this->assertEquals(CRM_Core_PseudoConstant::stateProvince($params['address'][1]['state_province_id']),
529 $params['address'][1]['state_province'],
530 'Check for state province.'
531 );
532 $this->assertEquals(CRM_Core_PseudoConstant::country($params['address'][1]['country_id']),
533 $params['address'][1]['country'],
534 'Check for country.'
535 );
536 }
537
538 /**
442cf836
EM
539 * Test case for retrieve( ).
540 *
541 * Test with all values.
6a488035 542 */
00be9182 543 public function testRetrieve() {
6a488035
TO
544 //take the common contact params
545 $params = $this->contactParams();
546 $params['note'] = 'test note';
6a488035
TO
547
548 //create the contact with given params.
549 $contact = CRM_Contact_BAO_Contact::create($params);
550 //Now check $contact is object of contact DAO..
551 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
552 $contactId = $contact->id;
72142c57
AN
553 //create the organization contact with the given params.
554 $orgParams = array(
555 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
556 'contact_type' => 'Organization',
557 );
558 $orgContact = CRM_Contact_BAO_Contact::add($orgParams);
559 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
6a488035
TO
560
561 //create employee of relationship.
72142c57 562 CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $orgContact->id);
6a488035
TO
563
564 //retrieve the contact values from database.
92915c55
TO
565 $values = array();
566 $searchParams = array('contact_id' => $contactId);
6a488035
TO
567 $retrieveContact = CRM_Contact_BAO_Contact::retrieve($searchParams, $values);
568
569 //Now check $retrieveContact is object of contact DAO..
570 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
571
572 //Now check the ids.
573 $this->assertEquals($contactId, $retrieveContact->id, 'Check for contact id');
574
575 //Now check values retrieve from database with params.
576 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
577 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
578 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
579
580 //Now check values of address
72142c57
AN
581 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']),
582 CRM_Utils_Array::value('1', $values['address'])
583 );
6a488035
TO
584
585 //Now check values of email
586 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
587 CRM_Utils_Array::value('1', $values['email'])
588 );
589
590 //Now check values of phone
591 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
592 CRM_Utils_Array::value('1', $values['phone'])
593 );
594
595 //Now check values of mobile
596 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
597 CRM_Utils_Array::value('2', $values['phone'])
598 );
599
600 //Now check values of openid
601 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
602 CRM_Utils_Array::value('1', $values['openid'])
603 );
604
605 //Now check values of im
606 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
607 CRM_Utils_Array::value('1', $values['im'])
608 );
609
610 //Now check values of Note Count.
611 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
612
613 foreach ($values['note'] as $key => $val) {
614 $retrieveNote = CRM_Utils_Array::value('note', $val);
615 //check the note value
616 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
617 }
618
619 //Now check values of Relationship Count.
620 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
621 foreach ($values['relationship']['data'] as $key => $val) {
622 //Now check values of Relationship organization.
72142c57 623 $this->assertEquals($orgContact->id, $val['contact_id_b'], 'Check for organization');
6a488035
TO
624 //Now check values of Relationship type.
625 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
626 //delete the organization.
72142c57 627 Contact::delete(CRM_Utils_Array::value('contact_id_b', $val));
6a488035
TO
628 }
629
630 //delete all notes related to contact
631 CRM_Core_BAO_Note::cleanContactNotes($contactId);
632
6a488035
TO
633 //cleanup DB by deleting the contact
634 Contact::delete($contactId);
635 $this->quickCleanup(array('civicrm_contact'));
636 }
637
638 /**
442cf836 639 * Test case for deleteContact( ).
6a488035 640 */
00be9182 641 public function testDeleteContact() {
6a488035
TO
642 $contactParams = $this->contactParams();
643
644 //create custom data
645 require_once 'CiviTest/Custom.php';
646 $customGroup = Custom::createGroup(array(), 'Individual');
647 $fields = array(
648 'label' => 'testFld',
649 'data_type' => 'String',
650 'html_type' => 'Text',
651 'custom_group_id' => $customGroup->id,
652 );
653 $customField = CRM_Core_BAO_CustomField::create($fields);
654 $contactParams['custom'] = array(
655 $customField->id => array(
656 -1 => array(
657 'value' => 'Test custom value',
658 'type' => 'String',
659 'custom_field_id' => $customField->id,
660 'custom_group_id' => $customGroup->id,
661 'table_name' => $customGroup->table_name,
662 'column_name' => $customField->column_name,
663 'file_id' => NULL,
664 ),
665 ),
666 );
667
668 //create contact
669 $contact = CRM_Contact_BAO_Contact::create($contactParams);
670 $contactId = $contact->id;
671
4f5f00c9 672 //delete contact permanently.
673 CRM_Contact_BAO_Contact::deleteContact($contactId, FALSE, TRUE);
6a488035
TO
674
675 //Now check DB for location elements.
676 //Now check DB for Address
677
678 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
679 'id', 'street_address', 'Database check, Address deleted successfully.'
680 );
681
682 //Now check DB for Email
683 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
684 'id', 'email', 'Database check, Email deleted successfully.'
685 );
686 //Now check DB for Phone
687 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
688 'id', 'phone', 'Database check, Phone deleted successfully.'
689 );
690 //Now check DB for Mobile
691 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
692 'id', 'phone', 'Database check, Mobile deleted successfully.'
693 );
694 //Now check DB for IM
695 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
696 'id', 'name', 'Database check, IM deleted successfully.'
697 );
698 //Now check DB for openId
699 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
700 'id', 'openid', 'Database check, openId deleted successfully.'
701 );
702
6a488035
TO
703 // Check that the custom field value is no longer present
704 $params = array(
705 'entityID' => $contactId,
706 'custom_' . $customField->id => 1,
707 );
708 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
709 $this->assertEquals(CRM_Utils_Array::value("custom_" . $customField->id, $values), '',
710 'Verify that the data value is empty for contact ' . $contactId
711 );
712 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
713
714 //Now check DB for contact.
715 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
716 'id', 'sort_name', 'Database check, contact deleted successfully.'
717 );
718 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
719 Custom::deleteGroup($customGroup);
720 }
721
722 /**
55d2c6f1 723 * Test case for createProfileContact.
6a488035 724 */
00be9182 725 public function testCreateProfileContact() {
6a488035
TO
726 $fields = CRM_Contact_BAO_Contact::exportableFields('Individual');
727
728 //current employer field for individual
729 $fields['organization_name'] = array(
730 'name' => 'organization_name',
731 'where' => 'civicrm_organization.organization_name',
732 'title' => 'Current Employer',
733 );
734 //get the common params
735 $contactParams = $this->contactParams();
663fecd0 736 $unsetParams = array('location', 'privacy');
6a488035
TO
737 foreach ($unsetParams as $param) {
738 unset($contactParams[$param]);
739 }
740
741 $profileParams = array(
742 'organization_name' => 'Yahoo',
663fecd0 743 'gender_id' => '2',
744 'prefix_id' => '3',
745 'suffix_id' => '2',
6a488035
TO
746 'city-Primary' => 'Newark',
747 'contact_type' => 'Individual',
748 'country-Primary' => '1228',
749 'do_not_email' => '1',
750 'do_not_mail' => '1',
751 'do_not_phone' => '1',
752 'do_not_trade' => '1',
753 'do_not_sms' => '1',
754 'email-Primary' => 'john.smith@example.org',
755 'geo_code_1-Primary' => '18.219023',
756 'geo_code_2-Primary' => '-105.00973',
757 'im-Primary-provider_id' => '1',
758 'im-Primary' => 'john.smith',
759 'on_hold' => '1',
760 'openid' => 'john.smith@example.org',
761 'phone-Primary-1' => '303443689',
762 'phone-Primary-2' => '9833910234',
763 'postal_code-Primary' => '01903',
764 'postal_code_suffix-Primary' => '12345',
765 'state_province-Primary' => '1029',
766 'street_address-Primary' => 'Saint Helier St',
767 'supplemental_address_1-Primary' => 'Hallmark Ct',
768 'supplemental_address_2-Primary' => 'Jersey Village',
769 'user_unique_id' => '123456789',
770 'is_bulkmail' => '1',
771 'world_region' => 'India',
772 'tag' => array(
773 '3' => '1',
774 '4' => '1',
775 '1' => '1',
776 ),
777 );
778 $createParams = array_merge($contactParams, $profileParams);
779
780 //create the contact using create profile contact.
781 $contactId = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
782
783 //get the parameters to compare.
784 $params = $this->contactParams();
785
786 //check the values in DB.
787 foreach ($params as $key => $val) {
788 if (!is_array($params[$key])) {
789 if ($key == 'contact_source') {
790 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
791 'id', $params[$key], "Check for {$key} creation."
792 );
793 }
794 else {
795 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
796 'id', $params[$key], "Check for {$key} creation."
797 );
798 }
799 }
800 }
801
802 //check privacy options.
803 foreach ($params['privacy'] as $key => $value) {
804 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
805 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
806 );
807 }
808
809 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
810 'id', $profileParams['contact_type'], 'Check for contact type creation.'
811 );
812 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
813 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
814 );
815
816 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
817 'id', $params['birth_date'], 'Check for birth_date creation.'
818 );
819
820 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
821 'id', $params['deceased_date'], 'Check for deceased_date creation.'
822 );
823
824 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
92915c55 825 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)
6a488035
TO
826 );
827 $checkPrefComm = array();
828 foreach ($dbPrefComm as $key => $value) {
829 if ($value) {
830 $checkPrefComm[$value] = 1;
831 }
832 }
833 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
834
835 //Now check DB for Address
836 $searchParams = array(
837 'contact_id' => $contactId,
838 'location_type_id' => 1,
839 'is_primary' => 1,
840 );
6c6e6187 841 $compareParams = array(
92915c55 842 'street_address' => CRM_Utils_Array::value('street_address-Primary', $profileParams),
6a488035
TO
843 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $profileParams),
844 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $profileParams),
845 'city' => CRM_Utils_Array::value('city-Primary', $profileParams),
846 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $profileParams),
847 'country_id' => CRM_Utils_Array::value('country-Primary', $profileParams),
848 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $profileParams),
849 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $profileParams),
850 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $profileParams),
851 );
852 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
853
854 //Now check DB for Email
855 $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $profileParams));
856 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
857
858 //Now check DB for IM
6c6e6187 859 $compareParams = array(
92915c55 860 'name' => CRM_Utils_Array::value('im-Primary', $profileParams),
6a488035
TO
861 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $profileParams),
862 );
863 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
864
865 //Now check DB for Phone
866 $searchParams = array(
867 'contact_id' => $contactId,
868 'location_type_id' => 1,
869 'is_primary' => 1,
870 );
871 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $profileParams));
872 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
873
874 //Now check DB for Mobile
875 $searchParams = array(
876 'contact_id' => $contactId,
877 'location_type_id' => 1,
878 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
879 );
880 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $profileParams));
881
882 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
883
884 //get the value of relationship
92915c55 885 $values = array();
6a488035
TO
886 $searchParams = array('contact_id' => $contactId);
887 $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values);
888 //Now check values of Relationship Count.
889 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
890 foreach ($values['relationship']['data'] as $key => $val) {
891 //Now check values of Relationship organization.
892 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
893 //Now check values of Relationship type.
894 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
895 //delete the organization.
896 Contact::delete(CRM_Utils_Array::value('cid', $val));
897 }
898
899 //Now check values of tag ids.
900 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
901 foreach ($tags as $key => $val) {
902 $tagIds[$key] = 1;
903 }
904
905 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
906
907 //update Contact mode
908 $updateCParams = array(
909 'first_name' => 'john',
910 'last_name' => 'doe',
911 'contact_type' => 'Individual',
912 'middle_name' => 'abc',
913 'prefix_id' => 2,
914 'suffix_id' => 3,
915 'nick_name' => 'Nick Name Updated',
916 'job_title' => 'software Developer',
917 'gender_id' => 1,
918 'is_deceased' => 1,
919 'website' => array(
920 1 => array(
921 'website_type_id' => 1,
922 'url' => 'http://civicrmUpdate.org',
923 ),
924 ),
925 'contact_source' => 'test contact',
926 'external_identifier' => 111222333,
927 'preferred_mail_format' => 'Both',
928 'is_opt_out' => 0,
929 'legal_identifier' => '123123123123',
930 'image_URL' => 'http://imageupdate.com',
931 'deceased_date' => '1981-10-10',
932 'birth_date' => '1951-11-11',
933 'privacy' => array(
934 'do_not_phone' => 1,
935 'do_not_email' => 1,
936 ),
937 'preferred_communication_method' => array(
938 '1' => 0,
939 '2' => 1,
940 '3' => 0,
941 '4' => 1,
942 '5' => 0,
943 ),
944 );
945
946 $updatePfParams = array(
947 'organization_name' => 'Google',
6a488035
TO
948 'city-Primary' => 'Mumbai',
949 'contact_type' => 'Individual',
950 'country-Primary' => '1228',
951 'do_not_email' => '1',
952 'do_not_mail' => '1',
953 'do_not_phone' => '1',
954 'do_not_trade' => '1',
955 'do_not_sms' => '1',
956 'email-Primary' => 'john.doe@example.org',
957 'geo_code_1-Primary' => '31.694842',
958 'geo_code_2-Primary' => '-106.29998',
959 'im-Primary-provider_id' => '1',
960 'im-Primary' => 'john.doe',
961 'on_hold' => '1',
962 'openid' => 'john.doe@example.org',
963 'phone-Primary-1' => '02115245336',
964 'phone-Primary-2' => '9766323895',
965 'postal_code-Primary' => '12345',
966 'postal_code_suffix-Primary' => '123',
967 'state_province-Primary' => '1004',
968 'street_address-Primary' => 'Oberoi Garden',
969 'supplemental_address_1-Primary' => 'A-wing:3037',
970 'supplemental_address_2-Primary' => 'Andhery',
971 'user_unique_id' => '1122334455',
972 'is_bulkmail' => '1',
973 'world_region' => 'India',
974 'tag' => array(
975 '2' => '1',
976 '5' => '1',
977 ),
978 );
979
980 $createParams = array_merge($updateCParams, $updatePfParams);
981
982 //create the contact using create profile contact.
983 $contactID = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, $contactId,
984 NULL, NULL, NULL, TRUE
985 );
986
987 //check the contact ids
988 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
989
990 //check the values in DB.
991 foreach ($updateCParams as $key => $val) {
992 if (!is_array($updateCParams[$key])) {
993 if ($key == 'contact_source') {
994 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
995 'id', $updateCParams[$key], "Check for {$key} creation."
996 );
997 }
998 else {
999 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1000 'id', $updateCParams[$key], "Check for {$key} creation."
1001 );
1002 }
1003 }
1004 }
1005
1006 //check privacy options.
1007 foreach ($updateCParams['privacy'] as $key => $value) {
1008 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1009 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1010 );
1011 }
1012
1013 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1014 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1015 );
1016 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1017 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1018 );
1019
1020 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1021 $updateCParams['birth_date'], 'Check for birth_date creation.'
1022 );
1023
1024 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1025 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1026 );
1027
1028 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
92915c55 1029 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)
6a488035
TO
1030 );
1031 $checkPrefComm = array();
1032 foreach ($dbPrefComm as $key => $value) {
1033 if ($value) {
1034 $checkPrefComm[$value] = 1;
1035 }
1036 }
1037 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1038
1039 //Now check DB for Address
1040 $searchParams = array(
1041 'contact_id' => $contactId,
1042 'location_type_id' => 1,
1043 'is_primary' => 1,
1044 );
6c6e6187 1045 $compareParams = array(
92915c55 1046 'street_address' => CRM_Utils_Array::value('street_address-Primary', $updatePfParams),
6a488035
TO
1047 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $updatePfParams),
1048 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $updatePfParams),
1049 'city' => CRM_Utils_Array::value('city-Primary', $updatePfParams),
1050 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $updatePfParams),
1051 'country_id' => CRM_Utils_Array::value('country-Primary', $updatePfParams),
1052 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $updatePfParams),
1053 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $updatePfParams),
1054 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $updatePfParams),
1055 );
1056 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1057
1058 //Now check DB for Email
1059 $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $updatePfParams));
1060 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1061
1062 //Now check DB for IM
6c6e6187 1063 $compareParams = array(
92915c55 1064 'name' => CRM_Utils_Array::value('im-Primary', $updatePfParams),
6a488035
TO
1065 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $updatePfParams),
1066 );
1067 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1068
1069 //Now check DB for Phone
1070 $searchParams = array(
1071 'contact_id' => $contactId,
1072 'location_type_id' => 1,
1073 'is_primary' => 1,
1074 );
1075 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $updatePfParams));
1076 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1077
1078 //Now check DB for Mobile
1079 $searchParams = array(
1080 'contact_id' => $contactId,
1081 'location_type_id' => 1,
1082 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
1083 );
1084 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $updatePfParams));
1085 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1086
1087 //get the value of relationship
92915c55 1088 $values = array();
6a488035
TO
1089 $searchParams = array('contact_id' => $contactId);
1090 $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values);
1091 //Now check values of Relationship Count.
1092 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1093 foreach ($values['relationship']['data'] as $key => $val) {
1094 //Now check values of Relationship organization.
1095 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1096 //Now check values of Relationship type.
1097 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1098 //delete the organization.
1099 Contact::delete(CRM_Utils_Array::value('cid', $val));
1100 }
1101
1102 //Now check values of tag ids.
1103 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
1104 foreach ($tags as $key => $val) {
1105 $tagIds[$key] = 1;
1106 }
1107 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1108
1109 //cleanup DB by deleting the contact
1110 Contact::delete($contactId);
1111 }
1112
1113 /**
55d2c6f1 1114 * Test case for getContactDetails( ).
6a488035 1115 */
00be9182 1116 public function testGetContactDetails() {
6a488035
TO
1117 //get the contact params
1118 $params = $this->contactParams();
1119
1120 //create contact
1121 $contact = CRM_Contact_BAO_Contact::create($params);
1122 $contactId = $contact->id;
1123
1124 //get the contact details
1125 $contactDetails = CRM_Contact_BAO_Contact::getContactDetails($contactId);
1126 $compareParams = array(
1127 $params['first_name'] . ' ' . $params['last_name'],
1128 CRM_Utils_Array::value('email', $params['email'][1]),
1129 (bool ) $params['privacy']['do_not_email'],
1130 );
1131 //Now check the contact details
1132 $this->assertAttributesEquals($compareParams, $contactDetails);
1133
1134 //cleanup DB by deleting the contact
1135 Contact::delete($contactId);
1136 $this->quickCleanup(array('civicrm_contact'));
1137 }
1138
1139 /**
55d2c6f1 1140 * Test case for importableFields( ) and exportableFields( ).
6a488035 1141 */
00be9182 1142 public function testFields() {
6a488035
TO
1143 $allImpFileds = CRM_Contact_BAO_Contact::importableFields('All');
1144 $allExpFileds = CRM_Contact_BAO_Contact::importableFields('All');
1145 //Now check all fields
1146 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1147
1148 $individualImpFileds = CRM_Contact_BAO_Contact::importableFields('Individual');
1149 $individualExpFileds = CRM_Contact_BAO_Contact::importableFields('Individual');
1150 //Now check Individual fields
1151 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1152
1153 $householdImpFileds = CRM_Contact_BAO_Contact::importableFields('Household');
1154 $householdExpFileds = CRM_Contact_BAO_Contact::importableFields('Household');
1155 //Now check Household fields
1156 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1157
1158 $organizationImpFileds = CRM_Contact_BAO_Contact::importableFields('Organization');
1159 $organizationExpFileds = CRM_Contact_BAO_Contact::importableFields('Organization');
1160 //Now check Organization fields
1161 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1162 }
1163
1164 /**
92c99a4a 1165 * Test case for getPrimaryEmail.
6a488035 1166 */
00be9182 1167 public function testGetPrimaryEmail() {
6a488035
TO
1168 //get the contact params
1169 $params = $this->contactParams();
1170 $params['email'][2] = $params['email'][1];
1171 $params['email'][2]['email'] = 'primarymail@example.org';
1172 unset($params['email'][1]['is_primary']);
1173
1174 //create contact
1175 $contact = CRM_Contact_BAO_Contact::create($params);
1176 $contactId = $contact->id;
1177 //get the primary email.
1178 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contactId);
1179 //Now check the primary email
1180 $this->assertEquals($email, CRM_Utils_Array::value('email', $params['email'][2]), 'Check Primary Email');
1181
1182 //cleanup DB by deleting the contact
1183 Contact::delete($contactId);
1184 $this->quickCleanup(array('civicrm_contact'));
1185 }
1186
1187 /**
442cf836 1188 * Test case for getPrimaryOpenId( ).
6a488035 1189 */
00be9182 1190 public function testGetPrimaryOpenId() {
6a488035
TO
1191 //get the contact params
1192 $params = $this->contactParams();
1193 $params['openid'][2] = $params['openid'][1];
1194 $params['openid'][2]['location_type_id'] = 2;
1195 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1196 unset($params['openid'][1]['is_primary']);
1197
1198 //create contact
1199 $contact = CRM_Contact_BAO_Contact::create($params);
1200 $contactId = $contact->id;
1201 //get the primary openid
1202 $openID = CRM_Contact_BAO_Contact::getPrimaryOpenId($contactId);
1203
1204 //Now check the primary openid
1205 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1206
1207 //cleanup DB by deleting the contact
1208 Contact::delete($contactId);
1209 }
1210
1211 /**
442cf836 1212 * Test case for matchContactOnEmail( ).
6a488035 1213 */
00be9182 1214 public function testMatchContactOnEmail() {
6a488035
TO
1215 //get the contact params
1216 $params = $this->contactParams();
1217 //create contact
1218 $contact = CRM_Contact_BAO_Contact::create($params);
1219 $contactId = $contact->id;
1220
1221 //get the matching contact.
1222 $match = CRM_Contact_BAO_Contact::matchContactOnEmail(CRM_Utils_Array::value('email', $params['email'][1]),
1223 'Individual'
1224 );
1225 $this->assertEquals($contactId, $match->contact_id, 'Check For Matching Contact');
1226
1227 //cleanup DB by deleting the contact
1228 Contact::delete($contactId);
1229 $this->quickCleanup(array('civicrm_contact'));
1230 }
1231
1232 /**
442cf836 1233 * Test case for getContactType( ).
6a488035 1234 */
00be9182 1235 public function testGetContactType() {
6a488035
TO
1236 //get the contact params
1237 $params = $this->contactParams();
1238 //create contact
1239 $contact = CRM_Contact_BAO_Contact::create($params);
1240 $contactId = $contact->id;
1241
1242 //get contact type.
1243 $contactType = CRM_Contact_BAO_Contact::getContactType($contactId);
1244 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1245
1246 //cleanup DB by deleting the contact
1247 Contact::delete($contactId);
1248 $this->quickCleanup(array('civicrm_contact'));
1249 }
1250
1251 /**
442cf836 1252 * Test case for displayName( ).
6a488035 1253 */
00be9182 1254 public function testDisplayName() {
6a488035
TO
1255 //get the contact params
1256 $params = $this->contactParams();
1257
1258 //create contact
1259 $contact = CRM_Contact_BAO_Contact::create($params);
1260 $contactId = $contact->id;
1261
1262 //get display name.
1263 $dbDisplayName = CRM_Contact_BAO_Contact::displayName($contactId);
1264
e6c4755b
CW
1265 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1266 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
6a488035
TO
1267
1268 //build display name
1269 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1270
1271 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1272
1273 //cleanup DB by deleting the contact
1274 Contact::delete($contactId);
1275 $this->quickCleanup(array('civicrm_contact'));
1276 }
1277
1278 /**
442cf836 1279 * Test case for getDisplayAndImage( ).
6a488035 1280 */
00be9182 1281 public function testGetDisplayAndImage() {
6a488035
TO
1282 //get the contact params
1283 $params = $this->contactParams();
1284
1285 //create contact
1286 $contact = CRM_Contact_BAO_Contact::create($params);
1287 $contactId = $contact->id;
1288
1289 //get DisplayAndImage.
1290 list($displayName, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId);
1291
1292 $checkImage = CRM_Contact_BAO_Contact_Utils::getImage($params['contact_type'], FALSE, $contactId);
1293
e6c4755b
CW
1294 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1295 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
6a488035
TO
1296
1297 //build display name
1298 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1299
1300 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1301 $this->assertEquals($image, $checkImage, 'Check For Image');
1302
1303 //cleanup DB by deleting the contact
1304 Contact::delete($contactId);
1305 }
1306
6a488035 1307 /**
442cf836 1308 * Build common params.
6a488035
TO
1309 */
1310 private function contactParams() {
1311
1312 $params = array(
1313 'first_name' => 'john',
1314 'last_name' => 'smith',
1315 'contact_type' => 'Individual',
1316 'middle_name' => 'xyz',
1317 'prefix_id' => 3,
1318 'suffix_id' => 2,
1319 'nick_name' => 'Nick Name',
1320 'job_title' => 'software engg',
1321 'gender_id' => 2,
1322 'is_deceased' => 1,
1323 'website' => array(
1324 1 => array(
1325 'website_type_id' => 1,
1326 'url' => 'http://civicrm.org',
1327 ),
1328 ),
1329 'contact_source' => 'test contact',
1330 'external_identifier' => 123456789,
1331 'preferred_mail_format' => 'Both',
1332 'is_opt_out' => 1,
1333 'legal_identifier' => '123456789',
1334 'image_URL' => 'http://image.com',
1335 'deceased_date' => '1991-07-07',
1336 'birth_date' => '1961-06-06',
1337 'privacy' => array(
1338 'do_not_phone' => 1,
1339 'do_not_email' => 1,
1340 'do_not_mail' => 1,
1341 'do_not_trade' => 1,
1342 ),
1343 'preferred_communication_method' => array(
1344 '1' => 1,
1345 '2' => 0,
1346 '3' => 1,
1347 '4' => 0,
1348 '5' => 1,
1349 ),
1350 );
1351
1352 $params['address'] = array();
1353 $params['address'][1] = array(
1354 'location_type_id' => 1,
1355 'is_primary' => 1,
1356 'street_address' => 'Saint Helier St',
1357 'supplemental_address_1' => 'Hallmark Ct',
1358 'supplemental_address_2' => 'Jersey Village',
1359 'city' => 'Newark',
1360 'postal_code' => '01903',
1361 'country_id' => 1228,
1362 'state_province_id' => 1029,
1363 'geo_code_1' => '18.219023',
1364 'geo_code_2' => '-105.00973',
1365 );
1366
1367 $params['email'] = array();
1368 $params['email'][1] = array(
1369 'location_type_id' => 1,
1370 'is_primary' => 1,
1371 'email' => 'john.smith@example.org',
1372 );
1373
6a488035
TO
1374 $params['phone'] = array();
1375 $params['phone'][1] = array(
1376 'location_type_id' => 1,
1377 'is_primary' => 1,
1378 'phone_type_id' => 1,
1379 'phone' => '303443689',
1380 );
1381 $params['phone'][2] = array(
1382 'location_type_id' => 1,
1383 'phone_type_id' => 2,
1384 'phone' => '9833910234',
1385 );
1386
1387 $params['openid'] = array();
1388 $params['openid'][1] = array(
1389 'location_type_id' => 1,
1390 'is_primary' => 1,
1391 'openid' => 'http://civicrm.org/',
1392 );
1393
1394 $params['im'] = array();
1395 $params['im'][1] = array(
1396 'location_type_id' => 1,
1397 'is_primary' => 1,
1398 'name' => 'john.smith',
1399 'provider_id' => 1,
1400 );
1401
1402 return $params;
1403 }
1404
1405 /**
55d2c6f1 1406 * Ensure that created_date and modified_date are set.
6a488035 1407 */
55d2c6f1 1408 public function testTimestampContact() {
6a488035
TO
1409 $test = $this;
1410 $this->_testTimestamps(array(
92915c55
TO
1411 'UPDATE' => function ($contactId) use ($test) {
1412 $params = array(
6a488035
TO
1413 'first_name' => 'Testing',
1414 'contact_type' => 'Individual',
1415 'contact_id' => $contactId,
1416 );
e70a7fc0
TO
1417 $contact = CRM_Contact_BAO_Contact::add($params);
1418 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
92915c55 1419 },
6a488035
TO
1420 ));
1421 }
1422
1423 /**
55d2c6f1 1424 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record.
6a488035 1425 */
55d2c6f1 1426 public function testTimestampsEmail() {
6a488035
TO
1427 $test = $this;
1428 $this->_testTimestamps(array(
92915c55
TO
1429 'INSERT' => function ($contactId) use ($test) {
1430 $params = array(
6a488035
TO
1431 'email' => 'ex-1@example.com',
1432 'is_primary' => 1,
1433 'location_type_id' => 1,
1434 'contact_id' => $contactId,
1435 );
92915c55
TO
1436 CRM_Core_BAO_Email::add($params);
1437 $test->assertDBQuery('ex-1@example.com',
6a488035
TO
1438 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1439 array(1 => array($contactId, 'Integer'))
1440 );
92915c55
TO
1441 },
1442 'UPDATE' => function ($contactId) use ($test) {
1443 CRM_Core_DAO::executeQuery(
6a488035
TO
1444 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1445 array(1 => array($contactId, 'Integer'))
1446 );
92915c55
TO
1447 },
1448 'DELETE' => function ($contactId) use ($test) {
1449 CRM_Core_DAO::executeQuery(
6a488035
TO
1450 'DELETE FROM civicrm_email WHERE contact_id = %1',
1451 array(1 => array($contactId, 'Integer'))
1452 );
92915c55 1453 },
6a488035
TO
1454 ));
1455 }
1456
1457 /**
442cf836 1458 * Ensure that civicrm_contact.modified_date is updated when manipulating an email.
6a488035 1459 */
92c99a4a 1460 public function testTimestampsPhone() {
6a488035
TO
1461 $test = $this;
1462 $this->_testTimestamps(array(
92915c55
TO
1463 'INSERT' => function ($contactId) use ($test) {
1464 $params = array(
6a488035
TO
1465 'phone' => '202-555-1000',
1466 'is_primary' => 1,
1467 'location_type_id' => 1,
1468 'contact_id' => $contactId,
1469 );
92915c55
TO
1470 CRM_Core_BAO_Phone::add($params);
1471 $test->assertDBQuery('202-555-1000',
6a488035
TO
1472 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1473 array(1 => array($contactId, 'Integer'))
1474 );
92915c55
TO
1475 },
1476 'UPDATE' => function ($contactId) use ($test) {
1477 CRM_Core_DAO::executeQuery(
6a488035
TO
1478 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1479 array(1 => array($contactId, 'Integer'))
1480 );
92915c55
TO
1481 },
1482 'DELETE' => function ($contactId) use ($test) {
1483 CRM_Core_DAO::executeQuery(
6a488035
TO
1484 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1485 array(1 => array($contactId, 'Integer'))
1486 );
92915c55 1487 },
6a488035
TO
1488 ));
1489 }
1490
1491 /**
92c99a4a
EM
1492 * Ensure that civicrm_contact.modified_date is updated correctly.
1493 *
1494 * Looking at it when contact-related custom data is updated.
6a488035 1495 */
92c99a4a 1496 public function testTimestampsCustom() {
6a488035
TO
1497 $customGroup = Custom::createGroup(array(), 'Individual');
1498 $this->assertNotNull($customGroup);
1499 $fields = array(
fbd1b89e 1500 'custom_group_id' => $customGroup->id,
6a488035
TO
1501 'data_type' => 'String',
1502 'html_type' => 'Text',
1503 );
fbd1b89e
E
1504 $customField = $this->customFieldCreate($fields);
1505 $customField = $customField['values'][$customField['id']];
6a488035
TO
1506 $test = $this;
1507 $this->_testTimestamps(array(
92915c55
TO
1508 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1509 $result = civicrm_api3('contact', 'create', array(
6a488035 1510 'contact_id' => $contactId,
fbd1b89e 1511 'custom_' . $customField['id'] => 'test-1',
6a488035 1512 ));
92915c55
TO
1513 },
1514 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1515 CRM_Core_DAO::executeQuery(
fbd1b89e 1516 "UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
6a488035
TO
1517 array(1 => array($contactId, 'Integer'))
1518 );
92915c55
TO
1519 },
1520 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1521 CRM_Core_DAO::executeQuery(
6a488035
TO
1522 "DELETE FROM {$customGroup->table_name} WHERE entity_id = %1",
1523 array(1 => array($contactId, 'Integer'))
1524 );
92915c55 1525 },
6a488035
TO
1526 ));
1527
1528 Custom::deleteGroup($customGroup);
1529 }
1530
1531 /**
eceb18cc 1532 * Helper for testing timestamp manipulation.
6a488035
TO
1533 *
1534 * Create a contact and perform a series of steps with it; after each
1535 * step, ensure that the contact's modified_date has increased.
1536 *
5a4f6742
CW
1537 * @param array $callbacks
1538 * ($name => $callable).
6a488035 1539 */
00be9182 1540 public function _testTimestamps($callbacks) {
6a488035
TO
1541 CRM_Core_DAO::triggerRebuild();
1542 $contactId = Contact::createIndividual();
1543
1544 $origTimestamps = CRM_Contact_BAO_Contact::getTimestamps($contactId);
1545 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1546 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1547 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1548
1549 $prevTimestamps = $origTimestamps;
1550 foreach ($callbacks as $callbackName => $callback) {
1551 sleep(1); // advance clock by 1 second to ensure timestamps change
1552
1553 $callback($contactId);
1554 $newTimestamps = CRM_Contact_BAO_Contact::getTimestamps($contactId);
1555 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1556 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1557 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1558 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1559
1560 $prevTimestamps = $newTimestamps;
1561 }
1562
1563 Contact::delete($contactId);
1564 }
96025800 1565
6a488035 1566}