Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 4.3 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
30 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
31 | ||
32 | /** | |
33 | * Test class for API utils | |
34 | * | |
35 | * @package CiviCRM | |
36 | */ | |
37 | class api_v3_UtilsTest extends CiviUnitTestCase { | |
38 | protected $_apiversion; | |
39 | public $DBResetRequired = FALSE; | |
40 | public $_eNoticeCompliant = TRUE; | |
41 | public $_contactID = 1; | |
42 | ||
43 | /** | |
44 | * Sets up the fixture, for example, opens a network connection. | |
45 | * This method is called before a test is executed. | |
46 | * | |
47 | * @access protected | |
48 | */ | |
49 | protected function setUp() { | |
50 | parent::setUp(); | |
51 | $this->_apiversion = 3; | |
52 | } | |
53 | ||
54 | /** | |
55 | * Tears down the fixture, for example, closes a network connection. | |
56 | * This method is called after a test is executed. | |
57 | * | |
58 | * @access protected | |
59 | */ | |
60 | protected function tearDown() {} | |
61 | ||
62 | function testAddFormattedParam() { | |
63 | $values = array('contact_type' => 'Individual'); | |
64 | $params = array('something' => 1); | |
65 | $result = _civicrm_api3_deprecated_add_formatted_param($values, $params); | |
66 | $this->assertTrue($result); | |
67 | } | |
68 | ||
69 | function testCheckPermissionReturn() { | |
70 | $check = array('check_permissions' => TRUE); | |
71 | $config = CRM_Core_Config::singleton(); | |
72 | $config->userPermissionClass->permissions = array(); | |
73 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'empty permissions should not be enough'); | |
74 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
75 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'lacking permissions should not be enough'); | |
76 | $config->userPermissionClass->permissions = array('add contacts'); | |
77 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'lacking permissions should not be enough'); | |
78 | ||
79 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts'); | |
80 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'exact permissions should be enough'); | |
81 | ||
82 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts'); | |
83 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'overfluous permissions should be enough'); | |
84 | } | |
85 | ||
86 | function testCheckPermissionThrow() { | |
87 | $check = array('check_permissions' => TRUE); | |
88 | $config = CRM_Core_Config::singleton(); | |
89 | try { | |
90 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
91 | _civicrm_api3_api_check_permission('contact', 'create', $check); | |
92 | } | |
93 | catch(Exception $e) { | |
94 | $message = $e->getMessage(); | |
95 | } | |
96 | $this->assertEquals($message, 'API permission check failed for contact/create call; missing permission: add contacts.', 'lacking permissions should throw an exception'); | |
97 | ||
98 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts'); | |
99 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check), 'overfluous permissions should return true'); | |
100 | } | |
101 | ||
102 | function testCheckPermissionSkip() { | |
103 | $config = CRM_Core_Config::singleton(); | |
104 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
105 | $params = array('check_permissions' => TRUE); | |
106 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $params, FALSE), 'lacking permissions should not be enough'); | |
107 | $params = array('check_permissions' => FALSE); | |
108 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $params, FALSE), 'permission check should be skippable'); | |
109 | } | |
110 | ||
111 | /* | |
112 | * Test verify mandatory - includes DAO & passed as well as empty & NULL fields | |
113 | */ | |
114 | function testVerifyMandatory() { | |
115 | _civicrm_api3_initialize(TRUE); | |
116 | $params = array( | |
117 | 'entity_table' => 'civicrm_contact', | |
118 | 'note' => '', | |
119 | 'contact_id' => $this->_contactID, | |
120 | 'modified_date' => '2011-01-31', | |
121 | 'subject' => NULL, | |
122 | 'version' => $this->_apiversion, | |
123 | ); | |
124 | try { | |
125 | $result = civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject')); | |
126 | } | |
127 | catch(Exception $expected) { | |
128 | $this->assertEquals('Mandatory key(s) missing from params array: entity_id, note, subject', $expected->getMessage()); | |
129 | return; | |
130 | } | |
131 | ||
132 | $this->fail('An expected exception has not been raised.'); | |
133 | } | |
134 | ||
135 | /* | |
136 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
137 | */ | |
138 | function testVerifyOneMandatory() { | |
139 | _civicrm_api3_initialize(TRUE); | |
140 | $params = array( | |
141 | 'entity_table' => 'civicrm_contact', | |
142 | 'note' => '', | |
143 | 'contact_id' => $this->_contactID, | |
144 | 'modified_date' => '2011-01-31', | |
145 | 'subject' => NULL, | |
146 | 'version' => $this->_apiversion, | |
147 | ); | |
148 | ||
149 | try { | |
150 | $result = civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject')); | |
151 | } | |
152 | catch(Exception $expected) { | |
153 | $this->assertEquals('Mandatory key(s) missing from params array: entity_id, one of (note, subject)', $expected->getMessage()); | |
154 | return; | |
155 | } | |
156 | ||
157 | $this->fail('An expected exception has not been raised.'); | |
158 | } | |
159 | ||
160 | /* | |
161 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
162 | */ | |
163 | function testVerifyOneMandatoryOneSet() { | |
164 | _civicrm_api3_initialize(TRUE); | |
165 | $params = array('entity_table' => 'civicrm_contact', 'note' => 'note', 'contact_id' => $this->_contactID, 'modified_date' => '2011-01-31', 'subject' => NULL, 'version' => $this->_apiversion); | |
166 | ||
167 | try { | |
168 | civicrm_api3_verify_one_mandatory($params, NULL, array('note', 'subject')); | |
169 | } | |
170 | catch(Exception$expected) { | |
171 | $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__); | |
172 | } | |
173 | } | |
174 | ||
175 | ||
176 | /* | |
b6708aeb | 177 | * Test GET DAO function returns DAO |
178 | */ | |
6a488035 TO |
179 | function testGetDAO() { |
180 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_group_get'); | |
181 | $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO); | |
182 | $DAO = _civicrm_api3_get_DAO('custom_group'); | |
183 | $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO); | |
184 | $DAO = _civicrm_api3_get_DAO('CustomGroup'); | |
185 | $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO); | |
186 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_field_get'); | |
187 | $this->assertEquals('CRM_Core_DAO_CustomField', $DAO); | |
188 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_survey_get'); | |
189 | $this->assertEquals('CRM_Campaign_DAO_Survey', $DAO); | |
190 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_pledge_payment_get'); | |
191 | $this->assertEquals('CRM_Pledge_DAO_PledgePayment', $DAO); | |
192 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_website_get'); | |
193 | $this->assertEquals('CRM_Core_DAO_Website', $DAO); | |
194 | $DAO = _civicrm_api3_get_DAO('Membership'); | |
195 | $this->assertEquals('CRM_Member_DAO_Membership', $DAO); | |
196 | } | |
197 | /* | |
b6708aeb | 198 | * Test GET DAO function returns DAO |
199 | */ | |
6a488035 TO |
200 | function testGetBAO() { |
201 | $BAO = _civicrm_api3_get_BAO('civicrm_api3_website_get'); | |
202 | $this->assertEquals('CRM_Core_BAO_Website', $BAO); | |
203 | $BAO = _civicrm_api3_get_BAO('civicrm_api3_survey_get'); | |
204 | $this->assertEquals('CRM_Campaign_BAO_Survey', $BAO); | |
205 | $BAO = _civicrm_api3_get_BAO('civicrm_api3_pledge_payment_get'); | |
206 | $this->assertEquals('CRM_Pledge_BAO_PledgePayment', $BAO); | |
207 | } | |
208 | ||
209 | function test_civicrm_api3_validate_fields() { | |
210 | $params = array('start_date' => '2010-12-20', 'end_date' => ''); | |
211 | _civicrm_api3_validate_fields('relationship', 'get', $params); | |
212 | $this->assertEquals('20101220000000', $params['start_date']); | |
213 | $this->assertEquals('', $params['end_date']); | |
214 | } | |
215 | ||
216 | function test_civicrm_api3_validate_fields_membership() { | |
217 | $params = array('start_date' => '2010-12-20', 'end_date' => '', 'membership_end_date' => '0', 'join_date' => '2010-12-20', 'membership_start_date' => '2010-12-20'); | |
218 | _civicrm_api3_validate_fields('Membership', 'get', $params); | |
219 | $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__); | |
220 | $this->assertEquals('', $params['end_date']); | |
221 | $this->assertEquals('20101220000000', $params['join_date'], 'join_date not set in line ' . __LINE__); | |
222 | } | |
223 | ||
224 | function test_civicrm_api3_validate_fields_event() { | |
225 | ||
226 | $params = array( | |
227 | 'registration_start_date' => 20080601, | |
228 | 'registration_end_date' => '2008-10-15', 'start_date' => '2010-12-20', 'end_date' => '', | |
229 | ); | |
230 | _civicrm_api3_validate_fields('event', 'create', $params); | |
231 | $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__); | |
232 | $this->assertEquals('20081015000000', $params['registration_end_date'], 'in line ' . __LINE__); | |
233 | $this->assertEquals('', $params['end_date'], 'in line ' . __LINE__); | |
234 | $this->assertEquals('20080601000000', $params['registration_start_date']); | |
235 | } | |
236 | ||
237 | function test_civicrm_api3_validate_fields_exception() { | |
238 | $params = array( | |
239 | 'join_date' => 'abc', | |
240 | ); | |
241 | try { | |
242 | _civicrm_api3_validate_fields('Membership', 'get', $params); | |
243 | } | |
244 | catch(Exception$expected) { | |
245 | $this->assertEquals('join_date is not a valid date: abc', $expected->getMessage()); | |
246 | } | |
247 | } | |
248 | ||
249 | function testGetFields() { | |
250 | $result = civicrm_api('membership', 'getfields', array('version' => 3)); | |
251 | $this->assertArrayHasKey('values', $result); | |
252 | $result = civicrm_api('relationship', 'getfields', array('version' => 3)); | |
253 | $this->assertArrayHasKey('values', $result); | |
254 | $result = civicrm_api('event', 'getfields', array('version' => 3)); | |
255 | $this->assertArrayHasKey('values', $result); | |
256 | } | |
257 | } | |
258 |