Commit | Line | Data |
---|---|---|
b6708aeb | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
b6708aeb | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
b6708aeb | 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 | */ |
b6708aeb | 27 | |
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
30 | ||
31 | /** | |
32 | * Test class for UFGroup API - civicrm_uf_* | |
33 | * @todo Split UFGroup and UFJoin tests | |
34 | * | |
35 | * @package CiviCRM | |
36 | */ | |
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; | |
e6ff1593 | 42 | protected $_apiversion = 3; |
b6708aeb | 43 | protected $_params; |
9443c775 | 44 | protected $_entity = 'uf_field'; |
b7c9bc4c | 45 | |
b6708aeb | 46 | |
47 | protected function setUp() { | |
48 | parent::setUp(); | |
49 | $this->quickCleanup( | |
50 | array( | |
51 | 'civicrm_group', | |
52 | 'civicrm_contact', | |
53 | 'civicrm_uf_group', | |
54 | 'civicrm_uf_field', | |
55 | 'civicrm_uf_join', | |
56 | 'civicrm_uf_match', | |
57 | ) | |
58 | ); | |
59 | ||
bed98343 | 60 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
b6708aeb | 61 | $op->execute( |
62 | $this->_dbconn, | |
bbfd46a5 | 63 | $this->createFlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml') |
b6708aeb | 64 | ); |
b6708aeb | 65 | |
e6ff1593 | 66 | $this->callAPISuccess('uf_field', 'getfields', array('cache_clear' => 1)); |
b4bb913e | 67 | |
b6708aeb | 68 | $this->_params = array( |
69 | 'field_name' => 'phone', | |
70 | 'field_type' => 'Contact', | |
71 | 'visibility' => 'Public Pages and Listings', | |
72 | 'weight' => 1, | |
73 | 'label' => 'Test Phone', | |
74 | 'is_searchable' => 1, | |
75 | 'is_active' => 1, | |
76 | 'location_type_id' => 1, | |
77 | 'phone_type_id' => 1, | |
b6708aeb | 78 | 'uf_group_id' => $this->_ufGroupId, |
79 | ); | |
80 | } | |
81 | ||
00be9182 | 82 | public function tearDown() { |
b6708aeb | 83 | $this->quickCleanup( |
84 | array( | |
85 | 'civicrm_group', | |
86 | 'civicrm_contact', | |
87 | 'civicrm_uf_group', | |
88 | 'civicrm_uf_join', | |
89 | 'civicrm_uf_match', | |
90 | ) | |
91 | ); | |
92 | } | |
93 | ||
94 | /** | |
100fef9d | 95 | * Create / updating field |
b6708aeb | 96 | */ |
97 | public function testCreateUFField() { | |
98 | $params = $this->_params; // copy | |
e6ff1593 | 99 | $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__, __FILE__); |
b6708aeb | 100 | unset($params['uf_group_id']); |
101 | $this->_ufFieldId = $ufField['id']; | |
b6708aeb | 102 | foreach ($params as $key => $value) { |
103 | $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]); | |
104 | } | |
105 | } | |
106 | ||
107 | public function testCreateUFFieldWithBadFieldName() { | |
108 | $params = $this->_params; // copy | |
109 | $params['field_name'] = 'custom_98789'; // invalid field | |
9443c775 | 110 | $this->callAPIFailure('uf_field', 'create', $params); |
b6708aeb | 111 | } |
112 | ||
00be9182 | 113 | public function testCreateUFFieldWithWrongParams() { |
9443c775 CW |
114 | $this->callAPIFailure('uf_field', 'create', array('field_name' => 'test field')); |
115 | $this->callAPIFailure('uf_field', 'create', array('label' => 'name-less field')); | |
b6708aeb | 116 | } |
92915c55 | 117 | |
b6708aeb | 118 | /** |
119 | * Create a field with 'weight=1' and then a second with 'weight=1'. The second field | |
120 | * winds up with weight=1, and the first field gets bumped to 'weight=2'. | |
121 | */ | |
122 | public function testCreateUFFieldWithDefaultAutoWeight() { | |
123 | $params1 = $this->_params; // copy | |
9443c775 | 124 | $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1); |
b6708aeb | 125 | $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']); |
126 | $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array( | |
127 | 1 => array($ufField1['id'], 'Int'), | |
128 | )); | |
129 | ||
130 | $params2 = $this->_params; // copy | |
131 | $params2['location_type_id'] = 2; // needs to be a different field | |
9443c775 | 132 | $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2); |
b6708aeb | 133 | $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']); |
134 | $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array( | |
135 | 1 => array($ufField2['id'], 'Int'), | |
136 | )); | |
137 | $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array( | |
138 | 1 => array($ufField1['id'], 'Int'), | |
139 | )); | |
140 | } | |
141 | ||
142 | /** | |
eceb18cc | 143 | * Deleting field. |
b6708aeb | 144 | */ |
145 | public function testDeleteUFField() { | |
9443c775 | 146 | $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params); |
b6708aeb | 147 | $params = array( |
b6708aeb | 148 | 'field_id' => $ufField['id'], |
149 | ); | |
9443c775 | 150 | $result = $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__, __FILE__); |
b6708aeb | 151 | } |
152 | ||
153 | public function testGetUFFieldSuccess() { | |
54bd1003 | 154 | $this->callAPISuccess($this->_entity, 'create', $this->_params); |
9443c775 | 155 | $result = $this->callAPIAndDocument($this->_entity, 'get', array(), __FUNCTION__, __FILE__); |
b6708aeb | 156 | $this->getAndCheck($this->_params, $result['id'], $this->_entity); |
157 | } | |
158 | ||
159 | /** | |
100fef9d | 160 | * Create / updating field |
b6708aeb | 161 | */ |
162 | public function testReplaceUFFields() { | |
163 | $baseFields = array(); | |
164 | $baseFields[] = array( | |
165 | 'field_name' => 'first_name', | |
166 | 'field_type' => 'Contact', | |
167 | 'visibility' => 'Public Pages and Listings', | |
168 | 'weight' => 3, | |
169 | 'label' => 'Test First Name', | |
170 | 'is_searchable' => 1, | |
171 | 'is_active' => 1, | |
172 | ); | |
173 | $baseFields[] = array( | |
174 | 'field_name' => 'country', | |
175 | 'field_type' => 'Contact', | |
176 | 'visibility' => 'Public Pages and Listings', | |
177 | 'weight' => 2, | |
178 | 'label' => 'Test Country', | |
179 | 'is_searchable' => 1, | |
180 | 'is_active' => 1, | |
181 | 'location_type_id' => 1, | |
182 | ); | |
183 | $baseFields[] = array( | |
184 | 'field_name' => 'phone', | |
185 | 'field_type' => 'Contact', | |
186 | 'visibility' => 'Public Pages and Listings', | |
187 | 'weight' => 1, | |
188 | 'label' => 'Test Phone', | |
189 | 'is_searchable' => 1, | |
190 | 'is_active' => 1, | |
191 | 'location_type_id' => 1, | |
192 | 'phone_type_id' => 1, | |
193 | ); | |
194 | ||
195 | $params = array( | |
b6708aeb | 196 | 'uf_group_id' => $this->_ufGroupId, |
197 | 'option.autoweight' => FALSE, | |
198 | 'values' => $baseFields, | |
181f536c | 199 | 'check_permissions' => TRUE, |
b6708aeb | 200 | ); |
201 | ||
e6ff1593 | 202 | $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__, __FILE__); |
b6708aeb | 203 | $inputsByName = CRM_Utils_Array::index(array('field_name'), $params['values']); |
204 | $this->assertEquals(count($params['values']), count($result['values'])); | |
205 | foreach ($result['values'] as $outUfField) { | |
206 | $this->assertTrue(is_string($outUfField['field_name'])); | |
207 | $inUfField = $inputsByName[$outUfField['field_name']]; | |
208 | foreach ($inUfField as $key => $inValue) { | |
209 | $this->assertEquals($inValue, $outUfField[$key], | |
210 | sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]", | |
211 | $outUfField['field_name'], | |
212 | $key, | |
213 | $inValue, | |
214 | $outUfField[$key] | |
215 | ) | |
216 | ); | |
217 | } | |
218 | } | |
219 | } | |
96025800 | 220 | |
181f536c | 221 | /** |
222 | * Check Profile API permission without ACL. | |
223 | */ | |
224 | public function testProfilesWithoutACL() { | |
225 | $this->createLoggedInUser(); | |
226 | $baseFields[] = array( | |
227 | 'field_name' => 'first_name', | |
228 | 'field_type' => 'Contact', | |
229 | 'visibility' => 'Public Pages and Listings', | |
230 | 'weight' => 3, | |
231 | 'label' => 'Test First Name', | |
232 | 'is_searchable' => 1, | |
233 | 'is_active' => 1, | |
234 | ); | |
235 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
236 | $params = array( | |
237 | 'uf_group_id' => $this->_ufGroupId, | |
238 | 'option.autoweight' => FALSE, | |
239 | 'values' => $baseFields, | |
240 | 'check_permissions' => TRUE, | |
241 | ); | |
242 | $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID'); | |
243 | $result = $this->callAPIFailure('uf_field', 'replace', $params); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Check Profile ACL for API permission. | |
248 | */ | |
249 | public function testACLPermissionforProfiles() { | |
250 | $this->createLoggedInUser(); | |
251 | $this->_permissionedGroup = $this->groupCreate(array( | |
252 | 'title' => 'Edit Profiles', | |
253 | 'is_active' => 1, | |
254 | 'name' => 'edit-profiles', | |
255 | )); | |
256 | $this->setupACL(TRUE); | |
257 | $this->testReplaceUFFields(); | |
258 | } | |
259 | ||
b6708aeb | 260 | } |