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