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