3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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);
39 'label' => 'sub_individual',
40 'name' => 'sub_individual',
45 $result = CRM_Contact_BAO_ContactType
::add($params);
46 $this->subTypeIndividual
= $params['name'];
49 'label' => 'sub_organization',
50 'name' => 'sub_organization',
55 $result = CRM_Contact_BAO_ContactType
::add($params);
56 $this->subTypeOrganization
= $params['name'];
59 'label' => 'sub_household',
60 'name' => 'sub_household',
65 $result = CRM_Contact_BAO_ContactType
::add($params);
66 $this->subTypeHousehold
= $params['name'];
70 * Test add methods with valid data.
73 * @dataProvider versionThreeAndFour
75 public function testContactCreate($version) {
76 $this->_apiversion
= $version;
78 // check for Type:Individual Subtype:sub_individual
79 $contactParams = array(
80 'first_name' => 'Anne',
81 'last_name' => 'Grant',
82 'contact_type' => 'Individual',
83 'contact_sub_type' => $this->subTypeIndividual
,
85 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
87 'contact_id' => $contact['id'],
89 $result = $this->callAPISuccess('contact', 'get', $params);
90 $this->assertEquals($result['values'][$contact['id']]['first_name'], $contactParams['first_name'], "In line " . __LINE__
);
91 $this->assertEquals($result['values'][$contact['id']]['last_name'], $contactParams['last_name'], "In line " . __LINE__
);
92 $this->assertEquals($result['values'][$contact['id']]['contact_type'], $contactParams['contact_type'], "In line " . __LINE__
);
93 $this->assertEquals(end($result['values'][$contact['id']]['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__
);
94 $this->callAPISuccess('contact', 'delete', $params);
96 // check for Type:Organization Subtype:sub_organization
97 $contactParams = array(
98 'organization_name' => 'Compumentor',
99 'contact_type' => 'Organization',
100 'contact_sub_type' => $this->subTypeOrganization
,
102 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
105 'contact_id' => $contact['id'],
107 $getContacts = $this->callAPISuccess('contact', 'get', $params);
108 $result = $getContacts['values'][$contact['id']];
109 $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__
);
110 $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__
);
111 $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__
);
112 $this->callAPISuccess('contact', 'delete', $params);
116 * Test add with invalid data.
117 * @param int $version
118 * @dataProvider versionThreeAndFour
120 public function testContactAddInvalidData($version) {
121 $this->_apiversion
= $version;
123 // check for Type:Individual Subtype:sub_household
124 $contactParams = array(
125 'first_name' => 'Anne',
126 'last_name' => 'Grant',
127 'contact_type' => 'Individual',
128 'contact_sub_type' => $this->subTypeHousehold
,
130 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
132 // check for Type:Organization Subtype:sub_individual
133 $contactParams = array(
134 'organization_name' => 'Compumentor',
135 'contact_type' => 'Organization',
136 'contact_sub_type' => $this->subTypeIndividual
,
138 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
142 * Test update with no subtype to valid subtype.
144 * @param int $version
145 * @dataProvider versionThreeAndFour
147 public function testContactUpdateNoSubtypeValid($version) {
148 $this->_apiversion
= $version;
150 // check for Type:Individual
151 $contactParams = array(
152 'first_name' => 'Anne',
153 'last_name' => 'Grant',
154 'contact_type' => 'Individual',
156 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
157 // subype:sub_individual
158 $updateParams = array(
159 'first_name' => 'John',
160 'last_name' => 'Grant',
161 'contact_id' => $contact['id'],
162 'contact_type' => 'Individual',
163 'contact_sub_type' => $this->subTypeIndividual
,
165 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
166 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
169 'contact_id' => $contact['id'],
171 $getContacts = $this->callAPISuccess('contact', 'get', $params);
172 $result = $getContacts['values'][$contact['id']];
174 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
175 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
176 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
177 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
178 $this->callAPISuccess('contact', 'delete', $params);
180 // check for Type:Organization
181 $contactParams = array(
182 'organization_name' => 'Compumentor',
183 'contact_type' => 'Organization',
185 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
187 // subype:sub_organization
188 $updateParams = array(
189 'organization_name' => 'Intel Arts',
190 'contact_id' => $contact['id'],
191 'contact_type' => 'Organization',
192 'contact_sub_type' => $this->subTypeOrganization
,
194 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
195 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
198 'contact_id' => $contact['id'],
200 $getContacts = $this->callAPISuccess('contact', 'get', $params);
201 $result = $getContacts['values'][$contact['id']];
203 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
204 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
205 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
206 $this->callAPISuccess('contact', 'delete', $params);
210 * Test update with no subtype to invalid subtype.
211 * @param int $version
212 * @dataProvider versionThreeAndFour
214 public function testContactUpdateNoSubtypeInvalid($version) {
215 $this->_apiversion
= $version;
217 // check for Type:Individual
218 $contactParams = array(
219 'first_name' => 'Anne',
220 'last_name' => 'Grant',
221 'contact_type' => 'Individual',
223 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
225 // subype:sub_household
226 $updateParams = array(
227 'first_name' => 'John',
228 'last_name' => 'Grant',
229 'contact_id' => $contact['id'],
230 'contact_type' => 'Individual',
231 'contact_sub_type' => $this->subTypeHousehold
,
233 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
235 'contact_id' => $contact['id'],
237 $this->callAPISuccess('contact', 'delete', $params);
239 // check for Type:Organization
240 $contactParams = array(
241 'organization_name' => 'Compumentor',
242 'contact_type' => 'Organization',
244 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
246 $updateParams = array(
247 'organization_name' => 'Intel Arts',
248 'contact_id' => $contact['id'],
249 'contact_type' => 'Organization',
250 'contact_sub_type' => $this->subTypeIndividual
,
252 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
254 'contact_id' => $contact['id'],
256 $this->callAPISuccess('contact', 'delete', $params);
260 * Test update with no subtype to valid subtype.
262 * @param int $version
263 * @dataProvider versionThreeAndFour
265 public function testContactUpdateSubtypeValid($version) {
266 $this->_apiversion
= $version;
269 'label' => 'sub2_individual',
270 'name' => 'sub2_individual',
275 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
276 $subtype = $params['name'];
278 // check for Type:Individual subype:sub_individual
279 $contactParams = array(
280 'first_name' => 'Anne',
281 'last_name' => 'Grant',
282 'contact_type' => 'Individual',
283 'contact_sub_type' => $this->subTypeIndividual
,
285 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
286 // subype:sub2_individual
287 $updateParams = array(
288 'id' => $contact['id'],
289 'first_name' => 'John',
290 'last_name' => 'Grant',
291 'contact_id' => $contact['id'],
292 'contact_type' => 'Individual',
293 'contact_sub_type' => $subtype,
295 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
297 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
300 'contact_id' => $contact['id'],
302 $getContacts = $this->callAPISuccess('contact', 'get', $params);
303 $result = $getContacts['values'][$contact['id']];
305 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
306 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
307 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
308 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
309 $this->callAPISuccess('contact', 'delete', $params);
312 'label' => 'sub2_organization',
313 'name' => 'sub2_organization',
318 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
319 $subtype = $params['name'];
321 // check for Type:Organization subype:sub_organization
322 $contactParams = array(
323 'organization_name' => 'Compumentor',
324 'contact_type' => 'Organization',
325 'contact_sub_type' => $this->subTypeOrganization
,
327 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
329 // subype:sub2_organization
330 $updateParams = array(
331 'organization_name' => 'Intel Arts',
332 'contact_id' => $contact['id'],
333 'contact_type' => 'Organization',
334 'contact_sub_type' => $subtype,
336 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
337 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
340 'contact_id' => $contact['id'],
342 $getContacts = $this->callAPISuccess('contact', 'get', $params);
343 $result = $getContacts['values'][$contact['id']];
345 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
346 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
347 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
348 $this->callAPISuccess('contact', 'delete', $params);
352 * Test update with no subtype to invalid subtype.
353 * @param int $version
354 * @dataProvider versionThreeAndFour
356 public function testContactUpdateSubtypeInvalid($version) {
357 $this->_apiversion
= $version;
359 // check for Type:Individual subtype:sub_individual
360 $contactParams = array(
361 'first_name' => 'Anne',
362 'last_name' => 'Grant',
363 'contact_type' => 'Individual',
364 'contact_sub_type' => $this->subTypeIndividual
,
366 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
368 // subype:sub_household
369 $updateParams = array(
370 'first_name' => 'John',
371 'last_name' => 'Grant',
372 'contact_id' => $contact['id'],
373 'contact_type' => 'Individual',
374 'contact_sub_type' => $this->subTypeHousehold
,
376 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
378 'contact_id' => $contact['id'],
380 $this->callAPISuccess('contact', 'delete', $params);
382 // check for Type:Organization subtype:
383 $contactParams = array(
384 'organization_name' => 'Compumentor',
385 'contact_type' => 'Organization',
386 'contact_sub_type' => $this->subTypeOrganization
,
388 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
390 $updateParams = array(
391 'organization_name' => 'Intel Arts',
392 'contact_id' => $contact['id'],
393 'contact_sub_type' => $this->subTypeIndividual
,
395 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
397 'contact_id' => $contact['id'],
399 $this->callAPISuccess('contact', 'delete', $params);