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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test class for UFGroup API - civicrm_uf_*
33 * @todo Split UFGroup and UFJoin tests
37 class api_v3_UFFieldTest
extends CiviUnitTestCase
{
38 // ids from the uf_group_test.xml fixture
39 protected $_ufGroupId = 11;
40 protected $_ufFieldId;
41 protected $_contactId = 69;
42 protected $_apiversion = 3;
44 protected $_entity = 'uf_field';
47 protected function setUp() {
60 $op = new PHPUnit_Extensions_Database_Operation_Insert
;
63 $this->createFlatXMLDataSet(dirname(__FILE__
) . '/dataset/uf_group_test.xml')
66 $this->callAPISuccess('uf_field', 'getfields', array('cache_clear' => 1));
68 $this->_params
= array(
69 'field_name' => 'phone',
70 'field_type' => 'Contact',
71 'visibility' => 'Public Pages and Listings',
73 'label' => 'Test Phone',
76 'location_type_id' => 1,
78 'uf_group_id' => $this->_ufGroupId
,
82 public function tearDown() {
95 * Create / updating field
97 public function testCreateUFField() {
98 $params = $this->_params
; // copy
99 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__
, __FILE__
);
100 unset($params['uf_group_id']);
101 $this->_ufFieldId
= $ufField['id'];
102 foreach ($params as $key => $value) {
103 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
107 public function testCreateUFFieldWithBadFieldName() {
108 $params = $this->_params
; // copy
109 $params['field_name'] = 'custom_98789'; // invalid field
110 $this->callAPIFailure('uf_field', 'create', $params);
113 public function testCreateUFFieldWithWrongParams() {
114 $this->callAPIFailure('uf_field', 'create', array('field_name' => 'test field'));
115 $this->callAPIFailure('uf_field', 'create', array('label' => 'name-less field'));
118 * Create a field with 'weight=1' and then a second with 'weight=1'. The second field
119 * winds up with weight=1, and the first field gets bumped to 'weight=2'.
121 public function testCreateUFFieldWithDefaultAutoWeight() {
122 $params1 = $this->_params
; // copy
123 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
124 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
125 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
126 1 => array($ufField1['id'], 'Int'),
129 $params2 = $this->_params
; // copy
130 $params2['location_type_id'] = 2; // needs to be a different field
131 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
132 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
133 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
134 1 => array($ufField2['id'], 'Int'),
136 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
137 1 => array($ufField1['id'], 'Int'),
144 public function testDeleteUFField() {
145 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params
);
147 'field_id' => $ufField['id'],
149 $result = $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__
, __FILE__
);
152 public function testGetUFFieldSuccess() {
153 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
154 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(), __FUNCTION__
, __FILE__
);
155 $this->getAndCheck($this->_params
, $result['id'], $this->_entity
);
159 * Create / updating field
161 public function testReplaceUFFields() {
162 $baseFields = array();
163 $baseFields[] = array(
164 'field_name' => 'first_name',
165 'field_type' => 'Contact',
166 'visibility' => 'Public Pages and Listings',
168 'label' => 'Test First Name',
169 'is_searchable' => 1,
172 $baseFields[] = array(
173 'field_name' => 'country',
174 'field_type' => 'Contact',
175 'visibility' => 'Public Pages and Listings',
177 'label' => 'Test Country',
178 'is_searchable' => 1,
180 'location_type_id' => 1,
182 $baseFields[] = array(
183 'field_name' => 'phone',
184 'field_type' => 'Contact',
185 'visibility' => 'Public Pages and Listings',
187 'label' => 'Test Phone',
188 'is_searchable' => 1,
190 'location_type_id' => 1,
191 'phone_type_id' => 1,
195 'uf_group_id' => $this->_ufGroupId
,
196 'option.autoweight' => FALSE,
197 'values' => $baseFields,
200 $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__
, __FILE__
);
201 $inputsByName = CRM_Utils_Array
::index(array('field_name'), $params['values']);
202 $this->assertEquals(count($params['values']), count($result['values']));
203 foreach ($result['values'] as $outUfField) {
204 $this->assertTrue(is_string($outUfField['field_name']));
205 $inUfField = $inputsByName[$outUfField['field_name']];
206 foreach ($inUfField as $key => $inValue) {
207 $this->assertEquals($inValue, $outUfField[$key],
208 sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]",
209 $outUfField['field_name'],