Merge pull request #4887 from pratikshad/broken-webtest
[civicrm-core.git] / tests / phpunit / api / v3 / GrantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 class api_v3_GrantTest extends CiviUnitTestCase {
38 protected $_apiversion = 3;
39 protected $params;
40 protected $ids = array();
41 protected $_entity = 'Grant';
42
43 public $DBResetRequired = FALSE;
44
45 public function setUp() {
46 parent::setUp();
47 $this->ids['contact'][0] = $this->individualCreate();
48 $this->params = array(
49 'contact_id' => $this->ids['contact'][0],
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
60 public function tearDown() {
61 foreach ($this->ids as $entity => $entities) {
62 foreach ($entities as $id) {
63 $this->callAPISuccess($entity, 'delete', array('id' => $id));
64 }
65 }
66 $this->quickCleanup(array('civicrm_grant'));
67 }
68
69 public function testCreateGrant() {
70 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
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
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(
86 'html_type' => 'CheckBox',
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),
93 )
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
144 public function testGetGrant() {
145 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
146 $this->ids['grant'][0] = $result['id'];
147 $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__);
148 $this->assertAPISuccess($result, 'In line ' . __LINE__);
149 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
150 }
151
152 public function testDeleteGrant() {
153 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
154 $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
155 $this->assertAPISuccess($result, 'In line ' . __LINE__);
156 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
157 $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__);
158 }
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';
171 $fields = $this->callAPISuccess($entityName, 'getfields', array(
172 'action' => 'create',
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);
180 $getentities = $this->callAPISuccess($entityName, 'get', array(
181 'sequential' => 1,
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;
210 if (!empty($specs['FKClassName'])) {
211 $entity[$field] = empty($entity2[$field]) ? $entity2[$specs]['uniqueName'] : $entity2[$field];
212 }
213 break;
214
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(
229 'id' => $entity['id'],
230 $field => $entity[$field],
231 'debug' => 1,
232 );
233 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
234 $this->assertAPISuccess($update, "setting $field to {$entity[$field]} in line " . __LINE__);
235 $checkParams = array(
236 'id' => $entity['id'],
237 'sequential' => 1,
238 );
239 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
240 $this->assertAPIArrayComparison((array) $entity, $checkEntity);
241 }
242 $baoObj->deleteTestObjects($baoString);
243 $baoObj->free();
244 }
245 }