4 * Class CRM_Contact_BAO_ContactType_ContactTest
7 class CRM_Contact_BAO_ContactType_ContactTest
extends CiviUnitTestCase
{
9 public function setUp() {
13 'label' => 'indiv_student',
14 'name' => 'indiv_student',
19 $result = CRM_Contact_BAO_ContactType
::add($params);
20 $this->student
= $params['name'];
23 'label' => 'indiv_parent',
24 'name' => 'indiv_parent',
29 $result = CRM_Contact_BAO_ContactType
::add($params);
30 $this->parent
= $params['name'];
33 'label' => 'org_sponsor',
34 'name' => 'org_sponsor',
39 $result = CRM_Contact_BAO_ContactType
::add($params);
40 $this->sponsor
= $params['name'];
43 'label' => 'org_team',
49 $result = CRM_Contact_BAO_ContactType
::add($params);
50 $this->team
= $params['name'];
53 public function tearDown() {
54 $this->quickCleanup(['civicrm_contact']);
56 DELETE FROM civicrm_contact_type
57 WHERE name IN ('{$this->student}','{$this->parent}','{$this->sponsor}', '{$this->team}');";
58 CRM_Core_DAO
::executeQuery($query);
62 * Methods create Contact with valid data.
66 public function testCreateContact() {
67 //check for Type:Individual
69 'first_name' => 'Anne',
70 'last_name' => 'Grant',
71 'contact_type' => 'Individual',
74 $contact = CRM_Contact_BAO_Contact
::add($params);
76 catch (Exception
$expected) {
78 $this->assertEquals($contact->first_name
, 'Anne');
79 $this->assertEquals($contact->contact_type
, 'Individual');
80 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
82 //check for Type:Organization
84 'organization_name' => 'Compumentor',
85 'contact_type' => 'Organization',
88 $contact = CRM_Contact_BAO_Contact
::add($params);
90 catch (Exception
$expected) {
92 $this->assertEquals($contact->organization_name
, 'Compumentor');
93 $this->assertEquals($contact->contact_type
, 'Organization');
94 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
96 //check for Type:Household
98 'household_name' => 'John Does home',
99 'contact_type' => 'Household',
102 $contact = CRM_Contact_BAO_Contact
::add($params);
104 catch (Exception
$expected) {
106 $this->assertEquals($contact->household_name
, 'John Does home');
107 $this->assertEquals($contact->contact_type
, 'Household');
108 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
110 //check for Type:Individual, Subtype:Student
112 'first_name' => 'Bill',
113 'last_name' => 'Adams',
114 'contact_type' => 'Individual',
115 'contact_sub_type' => $this->student
,
118 $contact = CRM_Contact_BAO_Contact
::add($params);
120 catch (Exception
$expected) {
122 $this->assertEquals($contact->first_name
, 'Bill');
123 $this->assertEquals($contact->contact_type
, 'Individual');
124 $this->assertEquals(str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $contact->contact_sub_type
), $this->student
);
125 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
127 //check for Type:Organization, Subtype:Sponsor
129 'organization_name' => 'Conservation Corp',
130 'contact_type' => 'Organization',
131 'contact_sub_type' => $this->sponsor
,
134 $contact = CRM_Contact_BAO_Contact
::add($params);
136 catch (Exception
$expected) {
138 $this->assertEquals($contact->organization_name
, 'Conservation Corp');
139 $this->assertEquals($contact->contact_type
, 'Organization');
140 $this->assertEquals(str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $contact->contact_sub_type
), $this->sponsor
);
141 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
145 * Update the contact with no subtype to a valid subtype.
149 public function testUpdateContactNoSubtypeToValid() {
151 'first_name' => 'Anne',
152 'last_name' => 'Grant',
153 'contact_type' => 'Individual',
156 $contact = CRM_Contact_BAO_Contact
::add($params);
158 catch (Exception
$expected) {
161 'contact_sub_type' => $this->student
,
162 'contact_type' => 'Individual',
163 'contact_id' => $contact->id
,
166 $updatedContact = CRM_Contact_BAO_Contact
::add($updateParams);
168 catch (Exception
$expected) {
170 $this->assertEquals($updatedContact->id
, $contact->id
);
171 $this->assertEquals($updatedContact->contact_type
, 'Individual');
172 $this->assertEquals(str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $updatedContact->contact_sub_type
), $this->student
);
173 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
176 'organization_name' => 'Compumentor',
177 'contact_type' => 'Organization',
180 $contact = CRM_Contact_BAO_Contact
::add($params);
182 catch (Exception
$expected) {
186 'contact_sub_type' => $this->sponsor
,
187 'contact_type' => 'Organization',
188 'contact_id' => $contact->id
,
191 $updatedContact = CRM_Contact_BAO_Contact
::add($updateParams);
193 catch (Exception
$expected) {
195 $this->assertEquals($updatedContact->id
, $contact->id
);
196 $this->assertEquals($updatedContact->contact_type
, 'Organization');
197 $this->assertEquals(str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $updatedContact->contact_sub_type
), $this->sponsor
);
198 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
202 * Update the contact with subtype to another valid subtype.
205 public function testUpdateContactSubtype() {
207 'first_name' => 'Anne',
208 'last_name' => 'Grant',
209 'contact_type' => 'Individual',
210 'contact_sub_type' => $this->student
,
213 $contact = CRM_Contact_BAO_Contact
::add($params);
215 catch (Exception
$expected) {
219 'contact_sub_type' => $this->parent
,
220 'contact_type' => 'Individual',
221 'contact_id' => $contact->id
,
224 $updatedContact = CRM_Contact_BAO_Contact
::add($updateParams);
226 catch (Exception
$expected) {
228 $this->assertEquals($updatedContact->id
, $contact->id
);
229 $this->assertEquals($updatedContact->contact_type
, 'Individual');
230 $this->assertEquals(str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $updatedContact->contact_sub_type
), $this->parent
);
231 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
234 'organization_name' => 'Compumentor',
235 'contact_type' => 'Organization',
236 'contact_sub_type' => $this->sponsor
,
239 $contact = CRM_Contact_BAO_Contact
::add($params);
241 catch (Exception
$expected) {
245 'contact_sub_type' => $this->team
,
246 'contact_type' => 'Organization',
247 'contact_id' => $contact->id
,
250 $updatedContact = CRM_Contact_BAO_Contact
::add($updateParams);
252 catch (Exception
$expected) {
255 $this->assertEquals($updatedContact->id
, $contact->id
);
256 $this->assertEquals($updatedContact->contact_type
, 'Organization');
257 $this->assertEquals(str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $updatedContact->contact_sub_type
), $this->team
);
258 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
261 'first_name' => 'Anne',
262 'last_name' => 'Grant',
263 'contact_type' => 'Individual',
264 'contact_sub_type' => $this->student
,
267 $contact = CRM_Contact_BAO_Contact
::add($params);
269 catch (Exception
$expected) {
273 'contact_sub_type' => 'null',
274 'contact_type' => 'Individual',
275 'contact_id' => $contact->id
,
278 $updatedContact = CRM_Contact_BAO_Contact
::add($updateParams);
280 catch (Exception
$expected) {
283 $this->assertEquals($updatedContact->id
, $contact->id
);
284 $this->assertEquals($updatedContact->contact_type
, 'Individual');
285 $this->assertEquals($updatedContact->contact_sub_type
, 'null');
286 CRM_Contact_BAO_Contact
::deleteContact($contact->id
);
290 * Unit test to ensure that removing any subtype from CustomGroup's
291 * extend_for setting, won't delete any custom data of contact
295 public function testCRM19133() {
296 $subtypesToPreserve = [$this->student
, $this->parent
];
298 // Create custom group that extends student and parent subtype
300 'title' => 'custom group',
301 'extends' => ['Individual', $subtypesToPreserve],
304 $result = civicrm_api3('customGroup', 'create', $apiParams);
305 $customGroupId = $result['id'];
307 // Create desired custom field
310 'custom_group_id' => $result['id'],
311 'label' => 'custom field',
312 'html_type' => 'Text',
313 'data_type' => 'String',
316 $result = civicrm_api3('custom_field', 'create', $apiParams);
317 $customFieldId = $result['id'];
319 // Create contact of subtype parent and student
321 'first_name' => 'Anne',
322 'last_name' => 'Grant',
323 'contact_type' => 'Individual',
324 'contact_sub_type' => [$this->student
, $this->parent
],
326 $contact = CRM_Contact_BAO_Contact
::add($params);
328 // Record custom value for desired customGroup
329 $this->callAPISuccess('CustomValue', 'create', [
330 'entity_id' => $contact->id
,
331 'custom_' . $customFieldId => 'value 1',
334 // Subtype to be removed from customGroup setting
335 $subtypesToBeRemoved = [$this->student
];
336 CRM_Contact_BAO_ContactType
::deleteCustomRowsOfSubtype($customGroupId, $subtypesToBeRemoved, $subtypesToPreserve);
338 // Check with correct value to assert that custom data is not deleted
339 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customFieldId => 'value 1']);
340 $this->assertEquals(1, $result['count']);
341 $this->assertEquals($contact->id
, $result['id']);
343 //Check with incorrect custom value that our previous assertion was correct
344 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customFieldId => 'wrong value']);
345 $this->assertEquals(0, $result['count']);