3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test APIv3 civicrm_activity_* functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
37 class api_v3_CustomValueContactTypeTest
extends CiviUnitTestCase
{
38 protected $_contactID;
39 protected $_apiversion = 3;
40 protected $CustomGroupIndividual;
41 protected $individualStudent;
43 public function setUp() {
45 // Create Group For Individual Contact Type
46 $groupIndividual = array(
47 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
48 'extends' => array('Individual'),
53 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
55 $this->IndividualField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual
['id']));
57 // Create Group For Individual-Student Contact Sub Type
58 $groupIndiStudent = array(
59 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
60 'extends' => array('Individual', array('Student')),
65 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
67 $this->IndiStudentField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent
['id']));
70 'first_name' => 'Mathev',
71 'last_name' => 'Adison',
72 'contact_type' => 'Individual',
75 $this->individual
= $this->individualCreate($params);
78 'first_name' => 'Steve',
79 'last_name' => 'Tosun',
80 'contact_type' => 'Individual',
81 'contact_sub_type' => 'Student',
83 $this->individualStudent
= $this->individualCreate($params);
86 'first_name' => 'Mark',
87 'last_name' => 'Dawson',
88 'contact_type' => 'Individual',
89 'contact_sub_type' => 'Parent',
91 $this->individualParent
= $this->individualCreate($params);
94 'organization_name' => 'Wellspring',
95 'contact_type' => 'Organization',
97 $this->organization
= $this->organizationCreate($params);
100 'organization_name' => 'SubUrban',
101 'contact_type' => 'Organization',
102 'contact_sub_type' => 'Sponsor',
104 $this->organizationSponsor
= $this->organizationCreate($params);
105 //refresh php cached variables
106 CRM_Core_PseudoConstant
::flush();
107 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndividualField
['id'], TRUE);
108 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndiStudentField
['id'], TRUE);
111 public function tearDown() {
112 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
113 $this->quickCleanup($tablesToTruncate, TRUE);
116 * Test that custom fields is returned for correct contact type only
118 public function testGetFields() {
119 $result = $this->callAPISuccess('Contact', 'getfields', array());
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));
121 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Individual'), 'in line' . __LINE__
);
122 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
123 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Organization'));
124 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
125 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__
);
126 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
130 * Add Custom data of Contact Type : Individual to a Contact type: Organization
132 public function testAddIndividualCustomDataToOrganization() {
135 'id' => $this->organization
,
136 'contact_type' => 'Organization',
137 "custom_{$this->IndividualField['id']}" => 'Test String',
138 'debug' => 1, // so that undefined_fields is returned
141 $contact = $this->callAPISuccess('contact', 'create', $params);
142 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
143 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__
);
147 * Add valid Empty params to a Contact Type : Individual
148 * note - don't copy & paste this - is of marginal value
150 public function testAddCustomDataEmptyToIndividual() {
151 $contact = $this->callAPIFailure('contact', 'create', array(),
152 'Mandatory key(s) missing from params array: contact_type'
157 * Add valid custom data to a Contact Type : Individual
159 public function testAddValidCustomDataToIndividual() {
162 'contact_id' => $this->individual
,
163 'contact_type' => 'Individual',
164 "custom_{$this->IndividualField['id']}" => 'Test String',
166 $contact = $this->callAPISuccess('contact', 'create', $params);
168 $this->assertNotNull($contact['id'], 'In line ' . __LINE__
);
169 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individual
);
170 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
172 // Check the Value in Database
173 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__
);
177 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
179 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
182 'contact_id' => $this->organizationSponsor
,
183 'contact_type' => 'Organization',
184 "custom_{$this->IndiStudentField['id']}" => 'Test String',
185 'debug' => 1, // so that undefined_fields is returned
188 $contact = $this->callAPISuccess('contact', 'create', $params);
189 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
190 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
194 * Add valid custom data to a Contact Type : Individual Subtype: Student
196 public function testCreateValidCustomDataToIndividualStudent() {
199 'contact_id' => $this->individualStudent
,
200 'contact_type' => 'Individual',
201 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
203 $result = $this->callAPISuccess('contact', 'create', $params);
205 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
206 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individualStudent
);
207 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
209 // Check the Value in Database
210 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__
);
214 * Add custom data of Individual Student to a Contact Type : Individual - parent
216 public function testAddIndividualStudentCustomDataToIndividualParent() {
219 'contact_id' => $this->individualParent
,
220 'contact_type' => 'Individual',
221 "custom_{$this->IndiStudentField['id']}" => 'Test String',
222 'debug' => 1, // so that undefined_fields is returned
224 $contact = $this->callAPISuccess('contact', 'create', $params);
225 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
226 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
234 * Retrieve Valid custom Data added to Individual Contact Type
236 public function testRetrieveValidCustomDataToIndividual() {
239 'contact_id' => $this->individual
,
240 'contact_type' => 'Individual',
241 "custom_" . $this->IndividualField
['id'] => 'Test String', );
243 $contact = $this->callAPISuccess('contact', 'create', $params);
245 $this->assertAPISuccess($contact);
247 'contact_id' => $this->individual
,
248 'contact_type' => 'Individual',
249 "return.custom_{$this->IndividualField['id']}" => 1,
252 $getContact = $this->callAPISuccess('contact', 'get', $params);
254 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String', 'In line ' . __LINE__
);
258 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
260 public function testRetrieveValidCustomDataToIndividualStudent() {
263 'contact_id' => $this->individualStudent
,
264 'contact_type' => 'Individual',
265 'contact_sub_type' => 'Student',
266 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
268 $contact = $this->callAPISuccess('contact', 'create', $params);
269 $this->assertAPISuccess($contact);
271 'contact_id' => $this->individualStudent
,
272 'contact_type' => 'Individual',
273 'contact_sub_type' => 'Student',
274 "return.custom_{$this->IndiStudentField['id']}" => 1,
277 $getContact = $this->callAPISuccess('contact', 'get', $params);
279 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__
);