Merge pull request #12026 from michaelmcandrew/pass-mailingJobId-to-hookTokenValues
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class api_v3_ContactTypeTest
30 * @group headless
31 */
32 class api_v3_ContactTypeTest extends CiviUnitTestCase {
33 protected $_apiversion;
34
35 public function setUp() {
36 parent::setUp();
37 $this->useTransaction(TRUE);
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'];
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'];
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'];
68 }
69
70 /**
71 * Test add methods with valid data.
72 * success expected
73 */
74 public function testContactCreate() {
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,
82 );
83 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
84 $params = array(
85 'contact_id' => $contact['id'],
86 );
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);
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,
99 );
100 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
101
102 $params = array(
103 'contact_id' => $contact['id'],
104 );
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);
111 }
112
113
114 /**
115 * Test add with invalid data.
116 */
117 public function testContactAddInvalidData() {
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,
125 );
126 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
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,
133 );
134 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
135 }
136
137
138 /**
139 * Test update with no subtype to valid subtype.
140 * success expected
141 */
142 public function testContactUpdateNoSubtypeValid() {
143
144 // check for Type:Individual
145 $contactParams = array(
146 'first_name' => 'Anne',
147 'last_name' => 'Grant',
148 'contact_type' => 'Individual',
149 );
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,
158 );
159 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
160 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
161
162 $params = array(
163 'contact_id' => $contact['id'],
164 );
165 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
172 $this->callAPISuccess('contact', 'delete', $params);
173
174 // check for Type:Organization
175 $contactParams = array(
176 'organization_name' => 'Compumentor',
177 'contact_type' => 'Organization',
178 );
179 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
187 );
188 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
189 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
190
191 $params = array(
192 'contact_id' => $contact['id'],
193 );
194 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
200 $this->callAPISuccess('contact', 'delete', $params);
201 }
202
203
204 /**
205 * Test update with no subtype to invalid subtype.
206 */
207 public function testContactUpdateNoSubtypeInvalid() {
208
209 // check for Type:Individual
210 $contactParams = array(
211 'first_name' => 'Anne',
212 'last_name' => 'Grant',
213 'contact_type' => 'Individual',
214 );
215 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
224 );
225 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
226 $params = array(
227 'contact_id' => $contact['id'],
228 );
229 $this->callAPISuccess('contact', 'delete', $params);
230
231 // check for Type:Organization
232 $contactParams = array(
233 'organization_name' => 'Compumentor',
234 'contact_type' => 'Organization',
235 );
236 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
237
238 $updateParams = array(
239 'organization_name' => 'Intel Arts',
240 'contact_id' => $contact['id'],
241 'contact_type' => 'Organization',
242 'contact_sub_type' => $this->subTypeIndividual,
243 );
244 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
245 $params = array(
246 'contact_id' => $contact['id'],
247 );
248 $this->callAPISuccess('contact', 'delete', $params);
249 }
250
251 /**
252 * Test update with no subtype to valid subtype.
253 * success expected
254 */
255 public function testContactUpdateSubtypeValid() {
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,
273 );
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,
283 );
284 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
285
286 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
287
288 $params = array(
289 'contact_id' => $contact['id'],
290 );
291 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
298 $this->callAPISuccess('contact', 'delete', $params);
299
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,
315 );
316 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
324 );
325 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
326 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
327
328 $params = array(
329 'contact_id' => $contact['id'],
330 );
331 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
337 $this->callAPISuccess('contact', 'delete', $params);
338 }
339
340 /**
341 * Test update with no subtype to invalid subtype.
342 */
343 public function testContactUpdateSubtypeInvalid() {
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,
351 );
352 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
361 );
362 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
363 $params = array(
364 'contact_id' => $contact['id'],
365 );
366 $this->callAPISuccess('contact', 'delete', $params);
367
368 // check for Type:Organization subtype:
369 $contactParams = array(
370 'organization_name' => 'Compumentor',
371 'contact_type' => 'Organization',
372 'contact_sub_type' => $this->subTypeOrganization,
373 );
374 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
375
376 $updateParams = array(
377 'organization_name' => 'Intel Arts',
378 'contact_id' => $contact['id'],
379 'contact_sub_type' => $this->subTypeIndividual,
380 );
381 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
382 $params = array(
383 'contact_id' => $contact['id'],
384 );
385 $this->callAPISuccess('contact', 'delete', $params);
386 }
387
388 }