Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | * | |
6c6e6187 TO |
34 | * @package CiviCRM_APIv3 |
35 | * @subpackage API_Grant | |
6a488035 | 36 | */ |
6a488035 TO |
37 | class api_v3_GrantTest extends CiviUnitTestCase { |
38 | protected $_apiversion = 3; | |
39 | protected $params; | |
40 | protected $ids = array(); | |
41 | protected $_entity = 'Grant'; | |
b7c9bc4c | 42 | |
6a488035 TO |
43 | public $DBResetRequired = FALSE; |
44 | ||
00be9182 | 45 | public function setUp() { |
6a488035 TO |
46 | parent::setUp(); |
47 | $this->ids['contact'][0] = $this->individualCreate(); | |
48 | $this->params = array( | |
92915c55 | 49 | 'contact_id' => $this->ids['contact'][0], |
6a488035 TO |
50 | 'application_received_date' => 'now', |
51 | 'decision_date' => 'next Monday', | |
52 | 'amount_total' => '500', | |
53 | 'status_id' => 1, | |
54 | 'rationale' => 'Just Because', | |
55 | 'currency' => 'USD', | |
56 | 'grant_type_id' => 1, | |
57 | ); | |
58 | } | |
59 | ||
00be9182 | 60 | public function tearDown() { |
6a488035 TO |
61 | foreach ($this->ids as $entity => $entities) { |
62 | foreach ($entities as $id) { | |
481a74f4 | 63 | $this->callAPISuccess($entity, 'delete', array('id' => $id)); |
6a488035 TO |
64 | } |
65 | } | |
2a4096af | 66 | $this->quickCleanup(array('civicrm_grant')); |
6a488035 TO |
67 | } |
68 | ||
69 | public function testCreateGrant() { | |
ca985406 | 70 | $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__); |
6a488035 TO |
71 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
72 | $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__); | |
73 | $this->getAndCheck($this->params, $result['id'], $this->_entity); | |
74 | } | |
75 | ||
2a4096af EM |
76 | /** |
77 | * Check checkbox type custom fields are created correctly | |
78 | * We want to ensure they are saved with separators as appropriate | |
79 | */ | |
80 | public function testCreateCustomCheckboxGrant() { | |
81 | $ids = array(); | |
82 | $result = $this->customGroupCreate(array('extends' => 'Grant')); | |
83 | $ids['custom_group_id'] = $result['id']; | |
84 | $customTable = $result['values'][$result['id']]['table_name']; | |
85 | $result = $this->customFieldCreate(array( | |
82e4a144 | 86 | 'html_type' => 'CheckBox', |
2a4096af EM |
87 | 'custom_group_id' => $ids['custom_group_id'], |
88 | 'option_values' => array( | |
89 | array('label' => 'my valley', 'value' => 'valley', 'is_active' => TRUE, 'weight' => 1), | |
90 | array('label' => 'my goat', 'value' => 'goat', 'is_active' => TRUE, 'weight' => 2), | |
91 | array('label' => 'mohair', 'value' => 'wool', 'is_active' => TRUE, 'weight' => 3), | |
92 | array('label' => 'hungry', 'value' => '', 'is_active' => TRUE, 'weight' => 3), | |
92915c55 TO |
93 | ) |
94 | )); | |
2a4096af | 95 | $columnName = $result['values'][$result['id']]['column_name']; |
92915c55 | 96 | $ids['custom_field_id'] = $result['id']; |
2a4096af EM |
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 | |
86bfa4f6 | 115 | $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR; |
2a4096af EM |
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__); |
6c6e6187 | 156 | $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array()); |
6a488035 TO |
157 | $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__); |
158 | } | |
92915c55 | 159 | |
6a488035 | 160 | /* |
6c6e6187 TO |
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 | */ | |
6a488035 TO |
167 | |
168 | public function testCreateAutoGrant() { | |
169 | $entityName = $this->_entity; | |
92915c55 TO |
170 | $baoString = 'CRM_Grant_BAO_Grant'; |
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( |
92915c55 TO |
181 | 'sequential' => 1, |
182 | 'return' => $return, | |
183 | )); | |
6a488035 TO |
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( | |
92915c55 TO |
229 | 'id' => $entity['id'], |
230 | $field => $entity[$field], | |
6a488035 TO |
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 | } |