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