3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
29 * Test class for UFGroup API - civicrm_uf_*
30 * @todo Split UFGroup and UFJoin tests
34 class api_v3_UFFieldTest
extends CiviUnitTestCase
{
35 // ids from the uf_group_test.xml fixture
36 protected $_ufGroupId = 11;
37 protected $_ufFieldId;
38 protected $_contactId = 69;
39 protected $_apiversion = 3;
41 protected $_entity = 'uf_field';
48 protected function setUp() {
61 $op = new PHPUnit_Extensions_Database_Operation_Insert();
64 $this->createFlatXMLDataSet(dirname(__FILE__
) . '/dataset/uf_group_test.xml')
67 $this->callAPISuccess('uf_field', 'getfields', array('cache_clear' => 1));
69 $this->_params
= array(
70 'field_name' => 'phone',
71 'field_type' => 'Contact',
72 'visibility' => 'Public Pages and Listings',
74 'label' => 'Test Phone',
77 'location_type_id' => 1,
79 'uf_group_id' => $this->_ufGroupId
,
88 public function tearDown() {
101 * Create / updating field.
103 public function testCreateUFField() {
104 $params = $this->_params
;
105 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__
, __FILE__
);
106 unset($params['uf_group_id']);
107 $this->_ufFieldId
= $ufField['id'];
108 foreach ($params as $key => $value) {
109 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
114 * Failure test for field_name.
116 public function testCreateUFFieldWithBadFieldName() {
117 $params = $this->_params
;
118 $params['field_name'] = 'custom_98789';
119 $this->callAPIFailure('uf_field', 'create', $params);
123 * Failure test for bad parameters.
125 public function testCreateUFFieldWithWrongParams() {
126 $this->callAPIFailure('uf_field', 'create', array('field_name' => 'test field'));
127 $this->callAPIFailure('uf_field', 'create', array('label' => 'name-less field'));
131 * Create a field with 'weight=1' and then a second with 'weight=1'.
133 * The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
135 public function testCreateUFFieldWithDefaultAutoWeight() {
136 $params1 = $this->_params
;
137 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
138 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
139 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
140 1 => array($ufField1['id'], 'Int'),
143 $params2 = $this->_params
;
144 // needs to be a different field
145 $params2['location_type_id'] = 2;
146 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
147 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
148 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
149 1 => array($ufField2['id'], 'Int'),
151 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
152 1 => array($ufField1['id'], 'Int'),
159 public function testDeleteUFField() {
160 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params
);
162 'field_id' => $ufField['id'],
164 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__
, __FILE__
);
168 * Test getting ufField.
170 public function testGetUFFieldSuccess() {
171 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
172 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(), __FUNCTION__
, __FILE__
);
173 $this->getAndCheck($this->_params
, $result['id'], $this->_entity
);
177 * Create / updating field.
179 public function testReplaceUFFields() {
180 $baseFields = array();
181 $baseFields[] = array(
182 'field_name' => 'first_name',
183 'field_type' => 'Contact',
184 'visibility' => 'Public Pages and Listings',
186 'label' => 'Test First Name',
187 'is_searchable' => 1,
190 $baseFields[] = array(
191 'field_name' => 'country',
192 'field_type' => 'Contact',
193 'visibility' => 'Public Pages and Listings',
195 'label' => 'Test Country',
196 'is_searchable' => 1,
198 'location_type_id' => 1,
200 $baseFields[] = array(
201 'field_name' => 'phone',
202 'field_type' => 'Contact',
203 'visibility' => 'Public Pages and Listings',
205 'label' => 'Test Phone',
206 'is_searchable' => 1,
208 'location_type_id' => 1,
209 'phone_type_id' => 1,
213 'uf_group_id' => $this->_ufGroupId
,
214 'option.autoweight' => FALSE,
215 'values' => $baseFields,
216 'check_permissions' => TRUE,
219 $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__
, __FILE__
);
220 $inputsByName = CRM_Utils_Array
::index(array('field_name'), $params['values']);
221 $this->assertEquals(count($params['values']), count($result['values']));
222 foreach ($result['values'] as $outUfField) {
223 $this->assertTrue(is_string($outUfField['field_name']));
224 $inUfField = $inputsByName[$outUfField['field_name']];
225 foreach ($inUfField as $key => $inValue) {
226 $this->assertEquals($inValue, $outUfField[$key],
227 sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]",
228 $outUfField['field_name'],
239 * Check Profile API permission without ACL.
241 public function testProfilesWithoutACL() {
242 $this->createLoggedInUser();
243 $baseFields[] = array(
244 'field_name' => 'first_name',
245 'field_type' => 'Contact',
246 'visibility' => 'Public Pages and Listings',
248 'label' => 'Test First Name',
249 'is_searchable' => 1,
252 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
254 'uf_group_id' => $this->_ufGroupId
,
255 'option.autoweight' => FALSE,
256 'values' => $baseFields,
257 'check_permissions' => TRUE,
259 $this->_loggedInUser
= CRM_Core_Session
::singleton()->get('userID');
260 $this->callAPIFailure('uf_field', 'replace', $params);
264 * Check Profile ACL for API permission.
266 public function testACLPermissionforProfiles() {
267 $this->createLoggedInUser();
268 $this->_permissionedGroup
= $this->groupCreate(array(
269 'title' => 'Edit Profiles',
271 'name' => 'edit-profiles',
273 $this->setupACL(TRUE);
274 $this->testReplaceUFFields();