3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
37 class api_v3_GrantTest
extends CiviUnitTestCase
{
38 protected $_apiversion = 3;
40 protected $ids = array();
41 protected $_entity = 'Grant';
43 public $DBResetRequired = FALSE;
45 public function 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',
54 'rationale' => 'Just Because',
60 public function tearDown() {
61 foreach ($this->ids
as $entity => $entities) {
62 foreach ($entities as $id) {
63 $this->callAPISuccess($entity, 'delete', array('id' => $id));
66 $this->quickCleanup(array('civicrm_grant'));
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
);
77 * 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),
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'];
105 $savedValue = CRM_Core_DAO
::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
107 $this->assertEquals($expectedValue, $savedValue);
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);
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);
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);
128 $this->customFieldDelete($ids['custom_field_id']);
129 $this->customGroupDelete($ids['custom_group_id']);
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__
);
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__
);
149 * This is a test to check if setting fields one at a time alters other fields.
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
156 public function testCreateAutoGrant() {
157 $entityName = $this->_entity
;
158 $baoString = 'CRM_Grant_BAO_Grant';
159 $fields = $this->callAPISuccess($entityName, 'getfields', array(
160 'action' => 'create',
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(
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') {
180 switch ($specs['type']) {
181 case CRM_Utils_Type
::T_DATE
:
182 case CRM_Utils_Type
::T_TIMESTAMP
:
183 $entity[$field] = '2012-05-20';
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';
195 case CRM_Utils_Type
::T_INT
:
196 // probably created with a 1
198 if (!empty($specs['FKClassName'])) {
199 $entity[$field] = empty($entity2[$field]) ?
$entity2[$specs]['uniqueName'] : $entity2[$field];
203 case CRM_Utils_Type
::T_BOOLEAN
:
204 // probably created with a 1
208 case CRM_Utils_Type
::T_FLOAT
:
209 case CRM_Utils_Type
::T_MONEY
:
210 $entity[$field] = 222;
213 case CRM_Utils_Type
::T_URL
:
214 $entity[$field] = 'warm.beer.com';
216 $updateParams = array(
217 'id' => $entity['id'],
218 $field => $entity[$field],
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'],
227 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
228 $this->assertAPIArrayComparison((array) $entity, $checkEntity);
230 $baoObj->deleteTestObjects($baoString);