Ian province abbreviation patch - issue 724
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueContactTypeTest.php
CommitLineData
6a488035
TO
1<?php
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
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Test APIv3 civicrm_activity_* functions
32 *
6c6e6187
TO
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
6a488035 35 */
6a488035
TO
36class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase {
37 protected $_contactID;
6c6e6187 38 protected $_apiversion = 3;
6a488035
TO
39 protected $CustomGroupIndividual;
40 protected $individualStudent;
b7c9bc4c 41
00be9182 42 public function setUp() {
6a488035 43 parent::setUp();
6a488035 44 // Create Group For Individual Contact Type
fc928539 45 $groupIndividual = array(
46 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
6a488035
TO
47 'extends' => array('Individual'),
48 'style' => 'Inline',
49 'is_active' => 1,
6a488035
TO
50 );
51
52 $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
53
b422b715 54 $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id']));
6a488035
TO
55
56 // Create Group For Individual-Student Contact Sub Type
57 $groupIndiStudent = array(
58 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
59 'extends' => array('Individual', array('Student')),
60 'style' => 'Inline',
61 'is_active' => 1,
6a488035
TO
62 );
63
64 $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
65
b422b715 66 $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id']));
6a488035
TO
67
68 $params = array(
69 'first_name' => 'Mathev',
70 'last_name' => 'Adison',
71 'contact_type' => 'Individual',
6a488035
TO
72 );
73
74 $this->individual = $this->individualCreate($params);
75
76 $params = array(
77 'first_name' => 'Steve',
78 'last_name' => 'Tosun',
79 'contact_type' => 'Individual',
80 'contact_sub_type' => 'Student',
6a488035
TO
81 );
82 $this->individualStudent = $this->individualCreate($params);
83
84 $params = array(
85 'first_name' => 'Mark',
86 'last_name' => 'Dawson',
87 'contact_type' => 'Individual',
88 'contact_sub_type' => 'Parent',
6a488035
TO
89 );
90 $this->individualParent = $this->individualCreate($params);
91
92 $params = array(
93 'organization_name' => 'Wellspring',
94 'contact_type' => 'Organization',
6a488035
TO
95 );
96 $this->organization = $this->organizationCreate($params);
97
98 $params = array(
99 'organization_name' => 'SubUrban',
100 'contact_type' => 'Organization',
101 'contact_sub_type' => 'Sponsor',
6a488035
TO
102 );
103 $this->organizationSponsor = $this->organizationCreate($params);
104 //refresh php cached variables
80085473 105 CRM_Core_PseudoConstant::flush();
6c6e6187
TO
106 CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndividualField['id'], TRUE);
107 CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndiStudentField['id'], TRUE);
6a488035
TO
108 }
109
00be9182 110 public function tearDown() {
6a488035
TO
111 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
112 $this->quickCleanup($tablesToTruncate, TRUE);
113 }
92915c55 114
acb1052e 115 /**
eceb18cc 116 * Test that custom fields is returned for correct contact type only.
6a488035 117 */
00be9182 118 public function testGetFields() {
fc928539 119 $result = $this->callAPISuccess('Contact', 'getfields', array());
c206647d 120 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'If This fails there is probably a caching issue - failure in line' . __LINE__ . print_r(array_keys($result['values']), TRUE));
92915c55
TO
121 $result = $this->callAPISuccess('Contact', 'getfields', array(
122 'action' => 'create',
acb1052e 123 'contact_type' => 'Individual',
92915c55 124 ), 'in line' . __LINE__);
6a488035 125 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
92915c55
TO
126 $result = $this->callAPISuccess('Contact', 'getfields', array(
127 'action' => 'create',
acb1052e 128 'contact_type' => 'Organization',
92915c55 129 ));
6a488035 130 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__ . print_r(array_keys($result['values']), TRUE));
fc928539 131 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__);
6a488035
TO
132 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
133 }
134
135 /**
136 * Add Custom data of Contact Type : Individual to a Contact type: Organization
137 */
00be9182 138 public function testAddIndividualCustomDataToOrganization() {
6a488035
TO
139
140 $params = array(
141 'id' => $this->organization,
142 'contact_type' => 'Organization',
6c6e6187 143 "custom_{$this->IndividualField['id']}" => 'Test String',
92915c55 144 'debug' => 1, // so that undefined_fields is returned
6a488035
TO
145 );
146
fc928539 147 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
148 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
149 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__);
150 }
151
152 /**
153 * Add valid Empty params to a Contact Type : Individual
fc928539 154 * note - don't copy & paste this - is of marginal value
6a488035 155 */
00be9182 156 public function testAddCustomDataEmptyToIndividual() {
fc928539 157 $contact = $this->callAPIFailure('contact', 'create', array(),
92915c55 158 'Mandatory key(s) missing from params array: contact_type'
6c6e6187 159 );
6a488035
TO
160 }
161
162 /**
163 * Add valid custom data to a Contact Type : Individual
164 */
00be9182 165 public function testAddValidCustomDataToIndividual() {
6a488035
TO
166
167 $params = array(
168 'contact_id' => $this->individual,
169 'contact_type' => 'Individual',
170 "custom_{$this->IndividualField['id']}" => 'Test String',
6a488035 171 );
fc928539 172 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035 173
ba4a1892 174 $this->assertNotNull($contact['id']);
6a488035
TO
175 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual);
176 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
177
178 // Check the Value in Database
ba4a1892 179 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String');
6a488035
TO
180 }
181
182 /**
183 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
184 */
00be9182 185 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
6a488035
TO
186
187 $params = array(
188 'contact_id' => $this->organizationSponsor,
189 'contact_type' => 'Organization',
190 "custom_{$this->IndiStudentField['id']}" => 'Test String',
6c6e6187 191 'debug' => 1, // so that undefined_fields is returned
6a488035
TO
192 );
193
fc928539 194 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
195 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
196 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
197 }
198
199 /**
200 * Add valid custom data to a Contact Type : Individual Subtype: Student
201 */
00be9182 202 public function testCreateValidCustomDataToIndividualStudent() {
6a488035
TO
203
204 $params = array(
205 'contact_id' => $this->individualStudent,
206 'contact_type' => 'Individual',
92915c55
TO
207 "custom_{$this->IndiStudentField['id']}" => 'Test String',
208 );
6a488035 209
fc928539 210 $result = $this->callAPISuccess('contact', 'create', $params);
6a488035 211
ba4a1892 212 $this->assertNotNull($result['id']);
6a488035
TO
213 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent);
214 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
215
216 // Check the Value in Database
ba4a1892 217 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String');
6a488035
TO
218 }
219
220 /**
221 * Add custom data of Individual Student to a Contact Type : Individual - parent
222 */
00be9182 223 public function testAddIndividualStudentCustomDataToIndividualParent() {
6a488035
TO
224
225 $params = array(
226 'contact_id' => $this->individualParent,
227 'contact_type' => 'Individual',
6c6e6187 228 "custom_{$this->IndiStudentField['id']}" => 'Test String',
92915c55 229 'debug' => 1, // so that undefined_fields is returned
6a488035 230 );
fc928539 231 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
232 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
233 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
234 }
235
6a488035
TO
236 // Retrieve Methods
237
238 /**
eceb18cc 239 * Retrieve Valid custom Data added to Individual Contact Type.
6a488035 240 */
00be9182 241 public function testRetrieveValidCustomDataToIndividual() {
6a488035
TO
242
243 $params = array(
244 'contact_id' => $this->individual,
245 'contact_type' => 'Individual',
92915c55
TO
246 "custom_" . $this->IndividualField['id'] => 'Test String',
247 );
6a488035 248
fc928539 249 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
250
251 $this->assertAPISuccess($contact);
252 $params = array(
253 'contact_id' => $this->individual,
254 'contact_type' => 'Individual',
255 "return.custom_{$this->IndividualField['id']}" => 1,
6a488035
TO
256 );
257
fc928539 258 $getContact = $this->callAPISuccess('contact', 'get', $params);
6a488035 259
ba4a1892 260 $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String');
6a488035
TO
261 }
262
263 /**
264 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
265 */
00be9182 266 public function testRetrieveValidCustomDataToIndividualStudent() {
6a488035
TO
267
268 $params = array(
269 'contact_id' => $this->individualStudent,
270 'contact_type' => 'Individual',
271 'contact_sub_type' => 'Student',
92915c55
TO
272 "custom_{$this->IndiStudentField['id']}" => 'Test String',
273 );
6a488035 274
fc928539 275 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
276 $this->assertAPISuccess($contact);
277 $params = array(
278 'contact_id' => $this->individualStudent,
279 'contact_type' => 'Individual',
6c6e6187 280 'contact_sub_type' => 'Student',
92915c55 281 "return.custom_{$this->IndiStudentField['id']}" => 1,
6a488035
TO
282 );
283
fc928539 284 $getContact = $this->callAPISuccess('contact', 'get', $params);
6a488035 285
ba4a1892 286 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String');
6a488035 287 }
96025800 288
6a488035 289}