Merge pull request #5536 from totten/4.5-httpclient
[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-2015 |
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 *
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(
87 'html_type' => 'CheckBox',
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 ));
96 $columnName = $result['values'][$result['id']]['column_name'];
97 $ids['custom_field_id'] = $result['id'];
98 $customFieldLabel = 'custom_' . $ids['custom_field_id'];
99 $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR;
100 //first we pass in the core separators ourselves
101 $this->params[$customFieldLabel] = $expectedValue;
102 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
103 $this->params['id'] = $result['id'];
104
105 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
106
107 $this->assertEquals($expectedValue, $savedValue);
108
109 // now we ask CiviCRM to add the separators
110 $this->params[$customFieldLabel] = "valley";
111 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
112 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
113 $this->assertEquals($expectedValue, $savedValue);
114
115 //let's try with 2 params already separated
116 $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR;
117 $this->params[$customFieldLabel] = $expectedValue;
118 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
119 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
120 $this->assertEquals($expectedValue, $savedValue);
121
122 //& an array for good measure
123 $this->params[$customFieldLabel] = array('valley', 'goat');
124 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
125 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
126 $this->assertEquals($expectedValue, $savedValue);
127
128 $this->customFieldDelete($ids['custom_field_id']);
129 $this->customGroupDelete($ids['custom_group_id']);
130 }
131
132 public function testGetGrant() {
133 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
134 $this->ids['grant'][0] = $result['id'];
135 $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__);
136 $this->assertAPISuccess($result, 'In line ' . __LINE__);
137 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
138 }
139
140 public function testDeleteGrant() {
141 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
142 $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
143 $this->assertAPISuccess($result, 'In line ' . __LINE__);
144 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
145 $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__);
146 }
147
148 /**
149 * This is a test to check if setting fields one at a time alters other fields.
150 *
151 * Issues Hit so far =
152 * 1) Currency keeps getting reset to USD - BUT this may be the only enabled currency
153 * - in which case it is valid
154 * 2)
155 */
156 public function testCreateAutoGrant() {
157 $entityName = $this->_entity;
158 $baoString = 'CRM_Grant_BAO_Grant';
159 $fields = $this->callAPISuccess($entityName, 'getfields', array(
160 'action' => 'create',
161 )
162 );
163
164 $fields = $fields['values'];
165 $return = array_keys($fields);
166 $baoObj = new CRM_Core_DAO();
167 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
168 $getentities = $this->callAPISuccess($entityName, 'get', array(
169 'sequential' => 1,
170 'return' => $return,
171 ));
172
173 // lets use first rather than assume only one exists
174 $entity = $getentities['values'][0];
175 $entity2 = $getentities['values'][1];
176 foreach ($fields as $field => $specs) {
177 if ($field == 'currency' || $field == 'id') {
178 continue;
179 }
180 switch ($specs['type']) {
181 case CRM_Utils_Type::T_DATE:
182 case CRM_Utils_Type::T_TIMESTAMP:
183 $entity[$field] = '2012-05-20';
184 break;
185
186 case CRM_Utils_Type::T_STRING:
187 case CRM_Utils_Type::T_BLOB:
188 case CRM_Utils_Type::T_MEDIUMBLOB:
189 case CRM_Utils_Type::T_TEXT:
190 case CRM_Utils_Type::T_LONGTEXT:
191 case CRM_Utils_Type::T_EMAIL:
192 $entity[$field] = 'New String';
193 break;
194
195 case CRM_Utils_Type::T_INT:
196 // probably created with a 1
197 $entity[$field] = 2;
198 if (!empty($specs['FKClassName'])) {
199 $entity[$field] = empty($entity2[$field]) ? $entity2[$specs]['uniqueName'] : $entity2[$field];
200 }
201 break;
202
203 case CRM_Utils_Type::T_BOOLEAN:
204 // probably created with a 1
205 $entity[$field] = 0;
206 break;
207
208 case CRM_Utils_Type::T_FLOAT:
209 case CRM_Utils_Type::T_MONEY:
210 $entity[$field] = 222;
211 break;
212
213 case CRM_Utils_Type::T_URL:
214 $entity[$field] = 'warm.beer.com';
215 }
216 $updateParams = array(
217 'id' => $entity['id'],
218 $field => $entity[$field],
219 'debug' => 1,
220 );
221 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
222 $this->assertAPISuccess($update, "setting $field to {$entity[$field]} in line " . __LINE__);
223 $checkParams = array(
224 'id' => $entity['id'],
225 'sequential' => 1,
226 );
227 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
228 $this->assertAPIArrayComparison((array) $entity, $checkEntity);
229 }
230 $baoObj->deleteTestObjects($baoString);
231 $baoObj->free();
232 }
233
234 }