3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test APIv3 civicrm_grant* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Grant
38 class api_v3_GrantTest
extends CiviUnitTestCase
{
39 protected $_apiversion = 3;
41 protected $ids = array();
42 protected $_entity = 'Grant';
44 public $DBResetRequired = FALSE;
48 $this->ids
['contact'][0] = $this->individualCreate();
49 $this->params
= array(
50 'contact_id' => $this->ids
['contact'][0],
51 'application_received_date' => 'now',
52 'decision_date' => 'next Monday',
53 'amount_total' => '500',
55 'rationale' => 'Just Because',
62 foreach ($this->ids
as $entity => $entities) {
63 foreach ($entities as $id) {
64 $this->callAPISuccess($entity, 'delete', array( 'id' => $id));
67 $this->quickCleanup(array('civicrm_grant'));
70 public function testCreateGrant() {
71 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
);
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
);
78 * Check checkbox type custom fields are created correctly
79 * We want to ensure they are saved with separators as appropriate
81 public function testCreateCustomCheckboxGrant() {
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),
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'];
104 $savedValue = CRM_Core_DAO
::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
106 $this->assertEquals($expectedValue, $savedValue);
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);
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);
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);
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);
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);
140 $this->customFieldDelete($ids['custom_field_id']);
141 $this->customGroupDelete($ids['custom_group_id']);
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__
);
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(
158 $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__
);
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
168 public function testCreateAutoGrant() {
169 $entityName = $this->_entity
;
170 $baoString = 'CRM_Grant_BAO_Grant';
171 $fields = $this->callAPISuccess($entityName, 'getfields', array(
172 'action' => 'create',
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(
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') {
192 switch ($specs['type']) {
193 case CRM_Utils_Type
::T_DATE
:
194 case CRM_Utils_Type
::T_TIMESTAMP
:
195 $entity[$field] = '2012-05-20';
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';
207 case CRM_Utils_Type
::T_INT
:
208 // probably created with a 1
210 if (!empty($specs['FKClassName'])) {
211 $entity[$field] = empty($entity2[$field]) ?
$entity2[$specs]['uniqueName'] : $entity2[$field];
215 case CRM_Utils_Type
::T_BOOLEAN
:
216 // probably created with a 1
220 case CRM_Utils_Type
::T_FLOAT
:
221 case CRM_Utils_Type
::T_MONEY
:
222 $entity[$field] = 222;
225 case CRM_Utils_Type
::T_URL
:
226 $entity[$field] = 'warm.beer.com';
228 $updateParams = array(
229 'id' => $entity['id'],
230 $field => $entity[$field],
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'],
239 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
240 $this->assertAPIArrayComparison((array) $entity, $checkEntity);
242 $baoObj->deleteTestObjects($baoString);