Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06a1bc01 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
30 | ||
31 | /** | |
32 | * Test class for API utils | |
33 | * | |
34 | * @package CiviCRM | |
35 | */ | |
36 | class api_v3_UtilsTest extends CiviUnitTestCase { | |
ca985406 | 37 | protected $_apiversion = 3; |
6a488035 | 38 | public $DBResetRequired = FALSE; |
b7c9bc4c | 39 | |
6a488035 TO |
40 | public $_contactID = 1; |
41 | ||
42 | /** | |
43 | * Sets up the fixture, for example, opens a network connection. | |
44 | * This method is called before a test is executed. | |
45 | * | |
46 | * @access protected | |
47 | */ | |
48 | protected function setUp() { | |
49 | parent::setUp(); | |
6a488035 TO |
50 | } |
51 | ||
52 | /** | |
53 | * Tears down the fixture, for example, closes a network connection. | |
54 | * This method is called after a test is executed. | |
55 | * | |
56 | * @access protected | |
57 | */ | |
58 | protected function tearDown() {} | |
59 | ||
60 | function testAddFormattedParam() { | |
61 | $values = array('contact_type' => 'Individual'); | |
62 | $params = array('something' => 1); | |
63 | $result = _civicrm_api3_deprecated_add_formatted_param($values, $params); | |
64 | $this->assertTrue($result); | |
65 | } | |
66 | ||
67 | function testCheckPermissionReturn() { | |
68 | $check = array('check_permissions' => TRUE); | |
69 | $config = CRM_Core_Config::singleton(); | |
70 | $config->userPermissionClass->permissions = array(); | |
d0c9daa4 | 71 | $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'empty permissions should not be enough'); |
6a488035 | 72 | $config->userPermissionClass->permissions = array('access CiviCRM'); |
d0c9daa4 | 73 | $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough'); |
6a488035 | 74 | $config->userPermissionClass->permissions = array('add contacts'); |
d0c9daa4 | 75 | $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough'); |
6a488035 TO |
76 | |
77 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts'); | |
d0c9daa4 | 78 | $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'exact permissions should be enough'); |
6a488035 TO |
79 | |
80 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts'); | |
d0c9daa4 | 81 | $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should be enough'); |
6a488035 TO |
82 | } |
83 | ||
84 | function testCheckPermissionThrow() { | |
85 | $check = array('check_permissions' => TRUE); | |
86 | $config = CRM_Core_Config::singleton(); | |
87 | try { | |
88 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
d0c9daa4 | 89 | $this->runPermissionCheck('contact', 'create', $check, TRUE); |
6a488035 TO |
90 | } |
91 | catch(Exception $e) { | |
92 | $message = $e->getMessage(); | |
93 | } | |
60ec9f43 | 94 | $this->assertEquals($message, 'API permission check failed for contact/create call; insufficient permission: require access CiviCRM and add contacts', 'lacking permissions should throw an exception'); |
6a488035 TO |
95 | |
96 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts'); | |
d0c9daa4 | 97 | $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should return true'); |
6a488035 TO |
98 | } |
99 | ||
100 | function testCheckPermissionSkip() { | |
101 | $config = CRM_Core_Config::singleton(); | |
102 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
103 | $params = array('check_permissions' => TRUE); | |
d0c9daa4 | 104 | $this->assertFalse($this->runPermissionCheck('contact', 'create', $params), 'lacking permissions should not be enough'); |
6a488035 | 105 | $params = array('check_permissions' => FALSE); |
d0c9daa4 TO |
106 | $this->assertTrue($this->runPermissionCheck('contact', 'create', $params), 'permission check should be skippable'); |
107 | } | |
108 | ||
109 | /** | |
110 | * @param string $entity | |
111 | * @param string $action | |
112 | * @param array $params | |
113 | * @param bool $throws whether we should pass any exceptions for authorization failures | |
cbdcc634 EM |
114 | * |
115 | * @throws API_Exception | |
116 | * @throws Exception | |
d0c9daa4 TO |
117 | * @return bool TRUE or FALSE depending on the outcome of the authorization check |
118 | */ | |
119 | function runPermissionCheck($entity, $action, $params, $throws = FALSE) { | |
120 | $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); | |
121 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); | |
122 | $kernel = new \Civi\API\Kernel($dispatcher); | |
cde12543 | 123 | $apiRequest = \Civi\API\Request::create($entity, $action, $params, NULL); |
d0c9daa4 TO |
124 | try { |
125 | $kernel->authorize(NULL, $apiRequest); | |
126 | return TRUE; | |
127 | } catch (\API_Exception $e) { | |
128 | $extra = $e->getExtraParams(); | |
129 | if (!$throws && $extra['error_code'] == API_Exception::UNAUTHORIZED) { | |
130 | return FALSE; | |
131 | } else { | |
132 | throw $e; | |
133 | } | |
134 | } | |
6a488035 TO |
135 | } |
136 | ||
137 | /* | |
138 | * Test verify mandatory - includes DAO & passed as well as empty & NULL fields | |
139 | */ | |
140 | function testVerifyMandatory() { | |
141 | _civicrm_api3_initialize(TRUE); | |
142 | $params = array( | |
143 | 'entity_table' => 'civicrm_contact', | |
144 | 'note' => '', | |
145 | 'contact_id' => $this->_contactID, | |
146 | 'modified_date' => '2011-01-31', | |
147 | 'subject' => NULL, | |
ca985406 | 148 | 'version' => $this->_apiversion |
6a488035 TO |
149 | ); |
150 | try { | |
151 | $result = civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject')); | |
152 | } | |
153 | catch(Exception $expected) { | |
154 | $this->assertEquals('Mandatory key(s) missing from params array: entity_id, note, subject', $expected->getMessage()); | |
155 | return; | |
156 | } | |
157 | ||
158 | $this->fail('An expected exception has not been raised.'); | |
159 | } | |
160 | ||
161 | /* | |
162 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
163 | */ | |
164 | function testVerifyOneMandatory() { | |
165 | _civicrm_api3_initialize(TRUE); | |
166 | $params = array( | |
167 | 'entity_table' => 'civicrm_contact', | |
168 | 'note' => '', | |
169 | 'contact_id' => $this->_contactID, | |
170 | 'modified_date' => '2011-01-31', | |
171 | 'subject' => NULL, | |
172 | 'version' => $this->_apiversion, | |
173 | ); | |
174 | ||
175 | try { | |
176 | $result = civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject')); | |
177 | } | |
178 | catch(Exception $expected) { | |
179 | $this->assertEquals('Mandatory key(s) missing from params array: entity_id, one of (note, subject)', $expected->getMessage()); | |
180 | return; | |
181 | } | |
182 | ||
183 | $this->fail('An expected exception has not been raised.'); | |
184 | } | |
185 | ||
186 | /* | |
187 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
188 | */ | |
189 | function testVerifyOneMandatoryOneSet() { | |
190 | _civicrm_api3_initialize(TRUE); | |
ca985406 | 191 | $params = array('version' => 3, 'entity_table' => 'civicrm_contact', 'note' => 'note', 'contact_id' => $this->_contactID, 'modified_date' => '2011-01-31', 'subject' => NULL); |
6a488035 TO |
192 | |
193 | try { | |
194 | civicrm_api3_verify_one_mandatory($params, NULL, array('note', 'subject')); | |
195 | } | |
196 | catch(Exception$expected) { | |
197 | $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__); | |
198 | } | |
199 | } | |
200 | ||
201 | ||
202 | /* | |
b6708aeb | 203 | * Test GET DAO function returns DAO |
204 | */ | |
6a488035 | 205 | function testGetDAO() { |
49e101d0 CW |
206 | $params = array( |
207 | 'civicrm_api3_custom_group_get' => 'CRM_Core_DAO_CustomGroup', | |
208 | 'custom_group' => 'CRM_Core_DAO_CustomGroup', | |
209 | 'CustomGroup' => 'CRM_Core_DAO_CustomGroup', | |
210 | 'civicrm_api3_custom_field_get' => 'CRM_Core_DAO_CustomField', | |
211 | 'civicrm_api3_survey_get' => 'CRM_Campaign_DAO_Survey', | |
212 | 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_DAO_PledgePayment', | |
213 | 'civicrm_api3_website_get' => 'CRM_Core_DAO_Website', | |
214 | 'Membership' => 'CRM_Member_DAO_Membership', | |
215 | ); | |
216 | foreach ($params as $input => $expected) { | |
217 | $result = _civicrm_api3_get_DAO($input); | |
218 | $this->assertEquals($expected, $result); | |
219 | } | |
6a488035 TO |
220 | } |
221 | /* | |
49e101d0 | 222 | * Test GET BAO function returns BAO when it exists |
b6708aeb | 223 | */ |
6a488035 | 224 | function testGetBAO() { |
49e101d0 CW |
225 | $params = array( |
226 | 'civicrm_api3_website_get' => 'CRM_Core_BAO_Website', | |
227 | 'civicrm_api3_survey_get' => 'CRM_Campaign_BAO_Survey', | |
228 | 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_BAO_PledgePayment', | |
229 | 'Household' => 'CRM_Contact_BAO_Contact', | |
230 | // Note this one DOES NOT have a BAO so we expect to fall back on returning the DAO | |
231 | 'mailing_group' => 'CRM_Mailing_DAO_MailingGroup', | |
5c1174d3 CW |
232 | // Make sure we get null back with nonexistant entities |
233 | 'civicrm_this_does_not_exist' => NULL, | |
49e101d0 CW |
234 | ); |
235 | foreach ($params as $input => $expected) { | |
236 | $result = _civicrm_api3_get_BAO($input); | |
237 | $this->assertEquals($expected, $result); | |
238 | } | |
6a488035 TO |
239 | } |
240 | ||
241 | function test_civicrm_api3_validate_fields() { | |
242 | $params = array('start_date' => '2010-12-20', 'end_date' => ''); | |
7f8c98cd E |
243 | $fields = civicrm_api3('relationship', 'getfields', array('action' => 'get')); |
244 | _civicrm_api3_validate_fields('relationship', 'get', $params, $fields['values']); | |
6a488035 TO |
245 | $this->assertEquals('20101220000000', $params['start_date']); |
246 | $this->assertEquals('', $params['end_date']); | |
247 | } | |
248 | ||
249 | function test_civicrm_api3_validate_fields_membership() { | |
250 | $params = array('start_date' => '2010-12-20', 'end_date' => '', 'membership_end_date' => '0', 'join_date' => '2010-12-20', 'membership_start_date' => '2010-12-20'); | |
7f8c98cd E |
251 | $fields = civicrm_api3('Membership', 'getfields', array('action' => 'get')); |
252 | _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']); | |
6a488035 TO |
253 | $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__); |
254 | $this->assertEquals('', $params['end_date']); | |
255 | $this->assertEquals('20101220000000', $params['join_date'], 'join_date not set in line ' . __LINE__); | |
256 | } | |
257 | ||
258 | function test_civicrm_api3_validate_fields_event() { | |
259 | ||
260 | $params = array( | |
261 | 'registration_start_date' => 20080601, | |
262 | 'registration_end_date' => '2008-10-15', 'start_date' => '2010-12-20', 'end_date' => '', | |
263 | ); | |
7f8c98cd E |
264 | $fields = civicrm_api3('Event', 'getfields', array('action' => 'create')); |
265 | _civicrm_api3_validate_fields('event', 'create', $params, $fields['values']); | |
6a488035 TO |
266 | $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__); |
267 | $this->assertEquals('20081015000000', $params['registration_end_date'], 'in line ' . __LINE__); | |
268 | $this->assertEquals('', $params['end_date'], 'in line ' . __LINE__); | |
269 | $this->assertEquals('20080601000000', $params['registration_start_date']); | |
270 | } | |
271 | ||
272 | function test_civicrm_api3_validate_fields_exception() { | |
273 | $params = array( | |
274 | 'join_date' => 'abc', | |
275 | ); | |
276 | try { | |
7f8c98cd E |
277 | $fields = civicrm_api3('Membership', 'getfields', array('action' => 'get')); |
278 | _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']); | |
6a488035 TO |
279 | } |
280 | catch(Exception$expected) { | |
281 | $this->assertEquals('join_date is not a valid date: abc', $expected->getMessage()); | |
282 | } | |
283 | } | |
284 | ||
285 | function testGetFields() { | |
ca985406 | 286 | $result = $this->callAPISuccess('membership', 'getfields', array()); |
6a488035 | 287 | $this->assertArrayHasKey('values', $result); |
ca985406 | 288 | $result = $this->callAPISuccess('relationship', 'getfields', array()); |
6a488035 | 289 | $this->assertArrayHasKey('values', $result); |
ca985406 | 290 | $result = $this->callAPISuccess('event', 'getfields', array()); |
6a488035 TO |
291 | $this->assertArrayHasKey('values', $result); |
292 | } | |
41d89fcb TO |
293 | |
294 | function testGetFields_AllOptions() { | |
ca985406 | 295 | $result = $this->callAPISuccess('contact', 'getfields', array( |
41d89fcb TO |
296 | 'options' => array( |
297 | 'get_options' => 'all', | |
298 | ), | |
41d89fcb | 299 | )); |
41d89fcb TO |
300 | $this->assertEquals('Household', $result['values']['contact_type']['options']['Household']); |
301 | $this->assertEquals('HTML', $result['values']['preferred_mail_format']['options']['HTML']); | |
302 | } | |
6a488035 TO |
303 | } |
304 |