Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
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 | ||
30 | ||
31 | /** | |
32 | * Test APIv3 civicrm_grant* functions | |
33 | * | |
34 | * @package CiviCRM_APIv3 | |
35 | * @subpackage API_Grant | |
36 | */ | |
37 | ||
38 | class api_v3_GrantTest extends CiviUnitTestCase { | |
39 | protected $_apiversion = 3; | |
40 | protected $params; | |
41 | protected $ids = array(); | |
42 | protected $_entity = 'Grant'; | |
b7c9bc4c | 43 | |
6a488035 TO |
44 | public $DBResetRequired = FALSE; |
45 | ||
46 | function setUp() { | |
47 | parent::setUp(); | |
48 | $this->ids['contact'][0] = $this->individualCreate(); | |
49 | $this->params = array( | |
ca985406 | 50 | 'contact_id' => $this->ids['contact'][0], |
6a488035 TO |
51 | 'application_received_date' => 'now', |
52 | 'decision_date' => 'next Monday', | |
53 | 'amount_total' => '500', | |
54 | 'status_id' => 1, | |
55 | 'rationale' => 'Just Because', | |
56 | 'currency' => 'USD', | |
57 | 'grant_type_id' => 1, | |
58 | ); | |
59 | } | |
60 | ||
61 | function tearDown() { | |
62 | foreach ($this->ids as $entity => $entities) { | |
63 | foreach ($entities as $id) { | |
ca985406 | 64 | $this->callAPISuccess($entity, 'delete', array( 'id' => $id)); |
6a488035 TO |
65 | } |
66 | } | |
2a4096af | 67 | $this->quickCleanup(array('civicrm_grant')); |
6a488035 TO |
68 | } |
69 | ||
70 | public function testCreateGrant() { | |
ca985406 | 71 | $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__); |
6a488035 TO |
72 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
73 | $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__); | |
74 | $this->getAndCheck($this->params, $result['id'], $this->_entity); | |
75 | } | |
76 | ||
2a4096af EM |
77 | /** |
78 | * Check checkbox type custom fields are created correctly | |
79 | * We want to ensure they are saved with separators as appropriate | |
80 | */ | |
81 | public function testCreateCustomCheckboxGrant() { | |
82 | $ids = array(); | |
83 | $result = $this->customGroupCreate(array('extends' => 'Grant')); | |
84 | $ids['custom_group_id'] = $result['id']; | |
85 | $customTable = $result['values'][$result['id']]['table_name']; | |
86 | $result = $this->customFieldCreate(array( | |
82e4a144 | 87 | 'html_type' => 'CheckBox', |
2a4096af EM |
88 | 'custom_group_id' => $ids['custom_group_id'], |
89 | 'option_values' => array( | |
90 | array('label' => 'my valley', 'value' => 'valley', 'is_active' => TRUE, 'weight' => 1), | |
91 | array('label' => 'my goat', 'value' => 'goat', 'is_active' => TRUE, 'weight' => 2), | |
92 | array('label' => 'mohair', 'value' => 'wool', 'is_active' => TRUE, 'weight' => 3), | |
93 | array('label' => 'hungry', 'value' => '', 'is_active' => TRUE, 'weight' => 3), | |
94 | ))); | |
95 | $columnName = $result['values'][$result['id']]['column_name']; | |
96 | $ids['custom_field_id'] = $result['id']; | |
97 | $customFieldLabel = 'custom_' . $ids['custom_field_id']; | |
98 | $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR; | |
99 | //first we pass in the core separators ourselves | |
100 | $this->params[$customFieldLabel] = $expectedValue; | |
101 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
102 | $this->params['id'] = $result['id']; | |
103 | ||
104 | $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}"); | |
105 | ||
106 | $this->assertEquals($expectedValue, $savedValue); | |
107 | ||
108 | // now we ask CiviCRM to add the separators | |
109 | $this->params[$customFieldLabel] = "valley"; | |
110 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
111 | $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}"); | |
112 | $this->assertEquals($expectedValue, $savedValue); | |
113 | ||
114 | //let's try with 2 params already separated | |
115 | $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR; | |
116 | $this->params[$customFieldLabel] = $expectedValue; | |
117 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
118 | $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}"); | |
119 | $this->assertEquals($expectedValue, $savedValue); | |
120 | ||
121 | // & check it will do the separating | |
122 | $this->params[$customFieldLabel] = 'valley,goat'; | |
123 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
124 | $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}"); | |
125 | $this->assertEquals($expectedValue, $savedValue); | |
126 | ||
127 | //& here is the odd but previously supported (form-oriented) format | |
128 | //& an array for good measure | |
129 | $this->params[$customFieldLabel] = array('valley' => 1, 'goat' => 1); | |
130 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
131 | $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}"); | |
132 | $this->assertEquals($expectedValue, $savedValue); | |
133 | ||
134 | //& an array for good measure | |
135 | $this->params[$customFieldLabel] = array('valley', 'goat'); | |
136 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
137 | $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}"); | |
138 | $this->assertEquals($expectedValue, $savedValue); | |
139 | ||
140 | $this->customFieldDelete($ids['custom_field_id']); | |
141 | $this->customGroupDelete($ids['custom_group_id']); | |
142 | } | |
143 | ||
6a488035 | 144 | public function testGetGrant() { |
ca985406 | 145 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); |
6a488035 | 146 | $this->ids['grant'][0] = $result['id']; |
ca985406 | 147 | $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__); |
6a488035 TO |
148 | $this->assertAPISuccess($result, 'In line ' . __LINE__); |
149 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); | |
150 | } | |
151 | ||
152 | public function testDeleteGrant() { | |
ca985406 | 153 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); |
154 | $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__); | |
6a488035 | 155 | $this->assertAPISuccess($result, 'In line ' . __LINE__); |
ca985406 | 156 | $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array( |
157 | )); | |
6a488035 TO |
158 | $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__); |
159 | } | |
160 | /* | |
161 | * This is a test to check if setting fields one at a time alters other fields | |
162 | * Issues Hit so far = | |
163 | * 1) Currency keeps getting reset to USD - BUT this may be the only enabled currency | |
164 | * - in which case it is valid | |
165 | * 2) | |
166 | */ | |
167 | ||
168 | public function testCreateAutoGrant() { | |
169 | $entityName = $this->_entity; | |
170 | $baoString = 'CRM_Grant_BAO_Grant'; | |
ca985406 | 171 | $fields = $this->callAPISuccess($entityName, 'getfields', array( |
172 | 'action' => 'create', | |
6a488035 TO |
173 | ) |
174 | ); | |
175 | ||
176 | $fields = $fields['values']; | |
177 | $return = array_keys($fields); | |
178 | $baoObj = new CRM_Core_DAO(); | |
179 | $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0); | |
ca985406 | 180 | $getentities = $this->callAPISuccess($entityName, 'get', array( |
181 | 'sequential' => 1, | |
6a488035 TO |
182 | 'return' => $return, |
183 | )); | |
184 | ||
185 | // lets use first rather than assume only one exists | |
186 | $entity = $getentities['values'][0]; | |
187 | $entity2 = $getentities['values'][1]; | |
188 | foreach ($fields as $field => $specs) { | |
189 | if ($field == 'currency' || $field == 'id') { | |
190 | continue; | |
191 | } | |
192 | switch ($specs['type']) { | |
193 | case CRM_Utils_Type::T_DATE: | |
194 | case CRM_Utils_Type::T_TIMESTAMP: | |
195 | $entity[$field] = '2012-05-20'; | |
196 | break; | |
197 | ||
198 | case CRM_Utils_Type::T_STRING: | |
199 | case CRM_Utils_Type::T_BLOB: | |
200 | case CRM_Utils_Type::T_MEDIUMBLOB: | |
201 | case CRM_Utils_Type::T_TEXT: | |
202 | case CRM_Utils_Type::T_LONGTEXT: | |
203 | case CRM_Utils_Type::T_EMAIL: | |
204 | $entity[$field] = 'New String'; | |
205 | break; | |
206 | ||
207 | case CRM_Utils_Type::T_INT: | |
208 | // probably created with a 1 | |
209 | $entity[$field] = 2; | |
a7488080 | 210 | if (!empty($specs['FKClassName'])) { |
6a488035 TO |
211 | $entity[$field] = empty($entity2[$field]) ? $entity2[$specs]['uniqueName'] : $entity2[$field]; |
212 | } | |
213 | break; | |
214 | ||
6a488035 TO |
215 | case CRM_Utils_Type::T_BOOLEAN: |
216 | // probably created with a 1 | |
217 | $entity[$field] = 0; | |
218 | break; | |
219 | ||
220 | case CRM_Utils_Type::T_FLOAT: | |
221 | case CRM_Utils_Type::T_MONEY: | |
222 | $entity[$field] = 222; | |
223 | break; | |
224 | ||
225 | case CRM_Utils_Type::T_URL: | |
226 | $entity[$field] = 'warm.beer.com'; | |
227 | } | |
228 | $updateParams = array( | |
ca985406 | 229 | 'id' => $entity['id'], |
6a488035 TO |
230 | $field => $entity[$field], |
231 | 'debug' => 1, | |
232 | ); | |
ca985406 | 233 | $update = $this->callAPISuccess($entityName, 'create', $updateParams); |
6a488035 TO |
234 | $this->assertAPISuccess($update, "setting $field to {$entity[$field]} in line " . __LINE__); |
235 | $checkParams = array( | |
236 | 'id' => $entity['id'], | |
6a488035 TO |
237 | 'sequential' => 1, |
238 | ); | |
ca985406 | 239 | $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams); |
240 | $this->assertAPIArrayComparison((array) $entity, $checkEntity); | |
6a488035 TO |
241 | } |
242 | $baoObj->deleteTestObjects($baoString); | |
243 | $baoObj->free(); | |
244 | } | |
245 | } | |
246 |