3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class api_v3_ContactTypeTest
32 class api_v3_ContactTypeTest
extends CiviUnitTestCase
{
33 protected $_apiversion;
35 public function setUp() {
37 $this->useTransaction(TRUE);
38 $this->_apiversion
= 3;
40 'label' => 'sub_individual',
41 'name' => 'sub_individual',
46 $result = CRM_Contact_BAO_ContactType
::add($params);
47 $this->subTypeIndividual
= $params['name'];
50 'label' => 'sub_organization',
51 'name' => 'sub_organization',
56 $result = CRM_Contact_BAO_ContactType
::add($params);
57 $this->subTypeOrganization
= $params['name'];
60 'label' => 'sub_household',
61 'name' => 'sub_household',
66 $result = CRM_Contact_BAO_ContactType
::add($params);
67 $this->subTypeHousehold
= $params['name'];
71 * Test add methods with valid data.
74 public function testContactCreate() {
76 // check for Type:Individual Subtype:sub_individual
77 $contactParams = array(
78 'first_name' => 'Anne',
79 'last_name' => 'Grant',
80 'contact_type' => 'Individual',
81 'contact_sub_type' => $this->subTypeIndividual
,
83 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
85 'contact_id' => $contact['id'],
87 $result = $this->callAPISuccess('contact', 'get', $params);
88 $this->assertEquals($result['values'][$contact['id']]['first_name'], $contactParams['first_name'], "In line " . __LINE__
);
89 $this->assertEquals($result['values'][$contact['id']]['last_name'], $contactParams['last_name'], "In line " . __LINE__
);
90 $this->assertEquals($result['values'][$contact['id']]['contact_type'], $contactParams['contact_type'], "In line " . __LINE__
);
91 $this->assertEquals(end($result['values'][$contact['id']]['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__
);
92 $this->callAPISuccess('contact', 'delete', $params);
94 // check for Type:Organization Subtype:sub_organization
95 $contactParams = array(
96 'organization_name' => 'Compumentor',
97 'contact_type' => 'Organization',
98 'contact_sub_type' => $this->subTypeOrganization
,
100 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
103 'contact_id' => $contact['id'],
105 $getContacts = $this->callAPISuccess('contact', 'get', $params);
106 $result = $getContacts['values'][$contact['id']];
107 $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__
);
108 $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__
);
109 $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__
);
110 $this->callAPISuccess('contact', 'delete', $params);
115 * Test add with invalid data.
117 public function testContactAddInvalidData() {
119 // check for Type:Individual Subtype:sub_household
120 $contactParams = array(
121 'first_name' => 'Anne',
122 'last_name' => 'Grant',
123 'contact_type' => 'Individual',
124 'contact_sub_type' => $this->subTypeHousehold
,
126 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
128 // check for Type:Organization Subtype:sub_individual
129 $contactParams = array(
130 'organization_name' => 'Compumentor',
131 'contact_type' => 'Organization',
132 'contact_sub_type' => $this->subTypeIndividual
,
134 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
139 * Test update with no subtype to valid subtype.
142 public function testContactUpdateNoSubtypeValid() {
144 // check for Type:Individual
145 $contactParams = array(
146 'first_name' => 'Anne',
147 'last_name' => 'Grant',
148 'contact_type' => 'Individual',
150 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
151 // subype:sub_individual
152 $updateParams = array(
153 'first_name' => 'John',
154 'last_name' => 'Grant',
155 'contact_id' => $contact['id'],
156 'contact_type' => 'Individual',
157 'contact_sub_type' => $this->subTypeIndividual
,
159 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
160 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
163 'contact_id' => $contact['id'],
165 $getContacts = $this->callAPISuccess('contact', 'get', $params);
166 $result = $getContacts['values'][$contact['id']];
168 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
169 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
170 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
171 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
172 $this->callAPISuccess('contact', 'delete', $params);
174 // check for Type:Organization
175 $contactParams = array(
176 'organization_name' => 'Compumentor',
177 'contact_type' => 'Organization',
179 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
181 // subype:sub_organization
182 $updateParams = array(
183 'organization_name' => 'Intel Arts',
184 'contact_id' => $contact['id'],
185 'contact_type' => 'Organization',
186 'contact_sub_type' => $this->subTypeOrganization
,
188 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
189 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
192 'contact_id' => $contact['id'],
194 $getContacts = $this->callAPISuccess('contact', 'get', $params);
195 $result = $getContacts['values'][$contact['id']];
197 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
198 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
199 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
200 $this->callAPISuccess('contact', 'delete', $params);
205 * Test update with no subtype to invalid subtype.
207 public function testContactUpdateNoSubtypeInvalid() {
209 // check for Type:Individual
210 $contactParams = array(
211 'first_name' => 'Anne',
212 'last_name' => 'Grant',
213 'contact_type' => 'Individual',
215 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
217 // subype:sub_household
218 $updateParams = array(
219 'first_name' => 'John',
220 'last_name' => 'Grant',
221 'contact_id' => $contact['id'],
222 'contact_type' => 'Individual',
223 'contact_sub_type' => $this->subTypeHousehold
,
225 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
227 'contact_id' => $contact['id'],
229 $this->callAPISuccess('contact', 'delete', $params);
231 // check for Type:Organization
232 $contactParams = array(
233 'organization_name' => 'Compumentor',
234 'contact_type' => 'Organization',
236 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
238 $updateParams = array(
239 'organization_name' => 'Intel Arts',
240 'contact_id' => $contact['id'],
241 'contact_type' => 'Organization',
242 'contact_sub_type' => $this->subTypeIndividual
,
244 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
246 'contact_id' => $contact['id'],
248 $this->callAPISuccess('contact', 'delete', $params);
252 * Test update with no subtype to valid subtype.
255 public function testContactUpdateSubtypeValid() {
258 'label' => 'sub2_individual',
259 'name' => 'sub2_individual',
264 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
265 $subtype = $params['name'];
267 // check for Type:Individual subype:sub_individual
268 $contactParams = array(
269 'first_name' => 'Anne',
270 'last_name' => 'Grant',
271 'contact_type' => 'Individual',
272 'contact_sub_type' => $this->subTypeIndividual
,
274 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
275 // subype:sub2_individual
276 $updateParams = array(
277 'id' => $contact['id'],
278 'first_name' => 'John',
279 'last_name' => 'Grant',
280 'contact_id' => $contact['id'],
281 'contact_type' => 'Individual',
282 'contact_sub_type' => $subtype,
284 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
286 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
289 'contact_id' => $contact['id'],
291 $getContacts = $this->callAPISuccess('contact', 'get', $params);
292 $result = $getContacts['values'][$contact['id']];
294 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
295 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
296 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
297 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
298 $this->callAPISuccess('contact', 'delete', $params);
301 'label' => 'sub2_organization',
302 'name' => 'sub2_organization',
307 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
308 $subtype = $params['name'];
310 // check for Type:Organization subype:sub_organization
311 $contactParams = array(
312 'organization_name' => 'Compumentor',
313 'contact_type' => 'Organization',
314 'contact_sub_type' => $this->subTypeOrganization
,
316 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
318 // subype:sub2_organization
319 $updateParams = array(
320 'organization_name' => 'Intel Arts',
321 'contact_id' => $contact['id'],
322 'contact_type' => 'Organization',
323 'contact_sub_type' => $subtype,
325 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
326 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
329 'contact_id' => $contact['id'],
331 $getContacts = $this->callAPISuccess('contact', 'get', $params);
332 $result = $getContacts['values'][$contact['id']];
334 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
335 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
336 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
337 $this->callAPISuccess('contact', 'delete', $params);
341 * Test update with no subtype to invalid subtype.
343 public function testContactUpdateSubtypeInvalid() {
345 // check for Type:Individual subtype:sub_individual
346 $contactParams = array(
347 'first_name' => 'Anne',
348 'last_name' => 'Grant',
349 'contact_type' => 'Individual',
350 'contact_sub_type' => $this->subTypeIndividual
,
352 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
354 // subype:sub_household
355 $updateParams = array(
356 'first_name' => 'John',
357 'last_name' => 'Grant',
358 'contact_id' => $contact['id'],
359 'contact_type' => 'Individual',
360 'contact_sub_type' => $this->subTypeHousehold
,
362 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
364 'contact_id' => $contact['id'],
366 $this->callAPISuccess('contact', 'delete', $params);
368 // check for Type:Organization subtype:
369 $contactParams = array(
370 'organization_name' => 'Compumentor',
371 'contact_type' => 'Organization',
372 'contact_sub_type' => $this->subTypeOrganization
,
374 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
376 $updateParams = array(
377 'organization_name' => 'Intel Arts',
378 'contact_id' => $contact['id'],
379 'contact_sub_type' => $this->subTypeIndividual
,
381 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
383 'contact_id' => $contact['id'],
385 $this->callAPISuccess('contact', 'delete', $params);