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