Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
4cbe18b8 EM |
28 | /** |
29 | * Class api_v3_ContactTypeTest | |
acb109b7 | 30 | * @group headless |
4cbe18b8 | 31 | */ |
6a488035 TO |
32 | class api_v3_ContactTypeTest extends CiviUnitTestCase { |
33 | protected $_apiversion; | |
b7c9bc4c | 34 | |
00be9182 | 35 | public function setUp() { |
6a488035 | 36 | parent::setUp(); |
77a09a77 | 37 | $this->useTransaction(TRUE); |
6a488035 TO |
38 | $this->_apiversion = 3; |
39 | $params = array( | |
40 | 'label' => 'sub_individual', | |
41 | 'name' => 'sub_individual', | |
42 | // Individual | |
43 | 'parent_id' => 1, | |
44 | 'is_active' => 1, | |
45 | ); | |
46 | $result = CRM_Contact_BAO_ContactType::add($params); | |
47 | $this->subTypeIndividual = $params['name']; | |
6a488035 TO |
48 | |
49 | $params = array( | |
50 | 'label' => 'sub_organization', | |
51 | 'name' => 'sub_organization', | |
52 | // Organization | |
53 | 'parent_id' => 3, | |
54 | 'is_active' => 1, | |
55 | ); | |
56 | $result = CRM_Contact_BAO_ContactType::add($params); | |
57 | $this->subTypeOrganization = $params['name']; | |
6a488035 TO |
58 | |
59 | $params = array( | |
60 | 'label' => 'sub_household', | |
61 | 'name' => 'sub_household', | |
62 | // Household | |
63 | 'parent_id' => 2, | |
64 | 'is_active' => 1, | |
65 | ); | |
66 | $result = CRM_Contact_BAO_ContactType::add($params); | |
67 | $this->subTypeHousehold = $params['name']; | |
6a488035 TO |
68 | } |
69 | ||
c490a46a | 70 | /** |
eceb18cc | 71 | * Test add methods with valid data. |
c490a46a CW |
72 | * success expected |
73 | */ | |
00be9182 | 74 | public function testContactCreate() { |
6a488035 TO |
75 | |
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, | |
6a488035 | 82 | ); |
fc928539 | 83 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
84 | $params = array( |
85 | 'contact_id' => $contact['id'], | |
6a488035 | 86 | ); |
fc928539 | 87 | $result = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 92 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
93 | |
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, | |
6a488035 | 99 | ); |
fc928539 | 100 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
101 | |
102 | $params = array( | |
103 | 'contact_id' => $contact['id'], | |
6a488035 | 104 | ); |
fc928539 | 105 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 110 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
111 | } |
112 | ||
113 | ||
c490a46a | 114 | /** |
eceb18cc | 115 | * Test add with invalid data. |
c490a46a | 116 | */ |
00be9182 | 117 | public function testContactAddInvalidData() { |
6a488035 TO |
118 | |
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, | |
6a488035 | 125 | ); |
d0e1eff2 | 126 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
6a488035 TO |
127 | |
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, | |
6a488035 | 133 | ); |
d0e1eff2 | 134 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
6a488035 TO |
135 | } |
136 | ||
137 | ||
c490a46a | 138 | /** |
eceb18cc | 139 | * Test update with no subtype to valid subtype. |
c490a46a CW |
140 | * success expected |
141 | */ | |
00be9182 | 142 | public function testContactUpdateNoSubtypeValid() { |
6a488035 TO |
143 | |
144 | // check for Type:Individual | |
145 | $contactParams = array( | |
146 | 'first_name' => 'Anne', | |
147 | 'last_name' => 'Grant', | |
148 | 'contact_type' => 'Individual', | |
6a488035 | 149 | ); |
fc928539 | 150 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 158 | ); |
fc928539 | 159 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
160 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
161 | ||
162 | $params = array( | |
163 | 'contact_id' => $contact['id'], | |
6a488035 | 164 | ); |
fc928539 | 165 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
166 | $result = $getContacts['values'][$contact['id']]; |
167 | ||
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__); | |
fc928539 | 172 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
173 | |
174 | // check for Type:Organization | |
175 | $contactParams = array( | |
176 | 'organization_name' => 'Compumentor', | |
177 | 'contact_type' => 'Organization', | |
6a488035 | 178 | ); |
fc928539 | 179 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
180 | |
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, | |
6a488035 | 187 | ); |
fc928539 | 188 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
189 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
190 | ||
191 | $params = array( | |
192 | 'contact_id' => $contact['id'], | |
6a488035 | 193 | ); |
fc928539 | 194 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
195 | $result = $getContacts['values'][$contact['id']]; |
196 | ||
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__); | |
fc928539 | 200 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
201 | } |
202 | ||
203 | ||
c490a46a | 204 | /** |
eceb18cc | 205 | * Test update with no subtype to invalid subtype. |
c490a46a | 206 | */ |
00be9182 | 207 | public function testContactUpdateNoSubtypeInvalid() { |
6a488035 TO |
208 | |
209 | // check for Type:Individual | |
210 | $contactParams = array( | |
211 | 'first_name' => 'Anne', | |
212 | 'last_name' => 'Grant', | |
213 | 'contact_type' => 'Individual', | |
6a488035 | 214 | ); |
fc928539 | 215 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
216 | |
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, | |
6a488035 | 224 | ); |
d0e1eff2 | 225 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
226 | $params = array( |
227 | 'contact_id' => $contact['id'], | |
92915c55 | 228 | ); |
fc928539 | 229 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
230 | |
231 | // check for Type:Organization | |
232 | $contactParams = array( | |
233 | 'organization_name' => 'Compumentor', | |
234 | 'contact_type' => 'Organization', | |
6a488035 | 235 | ); |
fc928539 | 236 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
237 | |
238 | $updateParams = array( | |
239 | 'organization_name' => 'Intel Arts', | |
240 | 'contact_id' => $contact['id'], | |
241 | 'contact_type' => 'Organization', | |
242 | 'contact_sub_type' => $this->subTypeIndividual, | |
6a488035 | 243 | ); |
d0e1eff2 | 244 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
245 | $params = array( |
246 | 'contact_id' => $contact['id'], | |
6a488035 | 247 | ); |
fc928539 | 248 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
249 | } |
250 | ||
c490a46a | 251 | /** |
eceb18cc | 252 | * Test update with no subtype to valid subtype. |
c490a46a CW |
253 | * success expected |
254 | */ | |
00be9182 | 255 | public function testContactUpdateSubtypeValid() { |
6a488035 TO |
256 | |
257 | $params = array( | |
258 | 'label' => 'sub2_individual', | |
259 | 'name' => 'sub2_individual', | |
260 | // Individual | |
261 | 'parent_id' => 1, | |
262 | 'is_active' => 1, | |
263 | ); | |
264 | $getSubtype = CRM_Contact_BAO_ContactType::add($params); | |
265 | $subtype = $params['name']; | |
266 | ||
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, | |
6a488035 | 273 | ); |
fc928539 | 274 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 283 | ); |
fc928539 | 284 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 | 285 | |
6a488035 TO |
286 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
287 | ||
288 | $params = array( | |
289 | 'contact_id' => $contact['id'], | |
6a488035 | 290 | ); |
fc928539 | 291 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
292 | $result = $getContacts['values'][$contact['id']]; |
293 | ||
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__); | |
fc928539 | 298 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 | 299 | |
6a488035 TO |
300 | $params = array( |
301 | 'label' => 'sub2_organization', | |
302 | 'name' => 'sub2_organization', | |
303 | // Organization | |
304 | 'parent_id' => 3, | |
305 | 'is_active' => 1, | |
306 | ); | |
307 | $getSubtype = CRM_Contact_BAO_ContactType::add($params); | |
308 | $subtype = $params['name']; | |
309 | ||
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, | |
6a488035 | 315 | ); |
fc928539 | 316 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
317 | |
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, | |
6a488035 | 324 | ); |
fc928539 | 325 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
326 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
327 | ||
328 | $params = array( | |
329 | 'contact_id' => $contact['id'], | |
6a488035 | 330 | ); |
fc928539 | 331 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
332 | $result = $getContacts['values'][$contact['id']]; |
333 | ||
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__); | |
fc928539 | 337 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
338 | } |
339 | ||
c490a46a | 340 | /** |
eceb18cc | 341 | * Test update with no subtype to invalid subtype. |
c490a46a | 342 | */ |
00be9182 | 343 | public function testContactUpdateSubtypeInvalid() { |
6a488035 TO |
344 | |
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, | |
6a488035 | 351 | ); |
fc928539 | 352 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
353 | |
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, | |
6a488035 | 361 | ); |
d0e1eff2 | 362 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
363 | $params = array( |
364 | 'contact_id' => $contact['id'], | |
6a488035 | 365 | ); |
fc928539 | 366 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
367 | |
368 | // check for Type:Organization subtype: | |
369 | $contactParams = array( | |
370 | 'organization_name' => 'Compumentor', | |
371 | 'contact_type' => 'Organization', | |
372 | 'contact_sub_type' => $this->subTypeOrganization, | |
6a488035 | 373 | ); |
fc928539 | 374 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
375 | |
376 | $updateParams = array( | |
377 | 'organization_name' => 'Intel Arts', | |
378 | 'contact_id' => $contact['id'], | |
379 | 'contact_sub_type' => $this->subTypeIndividual, | |
6a488035 | 380 | ); |
d0e1eff2 | 381 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
382 | $params = array( |
383 | 'contact_id' => $contact['id'], | |
6a488035 | 384 | ); |
fc928539 | 385 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 | 386 | } |
96025800 | 387 | |
6a488035 | 388 | } |