test fix, remove old param & fix comments
[civicrm-core.git] / tests / phpunit / api / v3 / GrantTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28require_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
37class 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__);
ba4a1892
TM
71 $this->assertEquals(1, $result['count']);
72 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
73 $this->getAndCheck($this->params, $result['id'], $this->_entity);
74 }
75
2a4096af 76 /**
eceb18cc 77 * Check checkbox type custom fields are created correctly.
fe482240 78 *
2a4096af
EM
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(
82e4a144 87 'html_type' => 'CheckBox',
2a4096af
EM
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),
389bcebf 94 ),
92915c55 95 ));
2a4096af 96 $columnName = $result['values'][$result['id']]['column_name'];
92915c55 97 $ids['custom_field_id'] = $result['id'];
2a4096af
EM
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
86bfa4f6 116 $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR;
2a4096af
EM
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
2a4096af
EM
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
6a488035 132 public function testGetGrant() {
ca985406 133 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 134 $this->ids['grant'][0] = $result['id'];
ca985406 135 $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__);
a15773db 136 $this->assertAPISuccess($result);
ba4a1892 137 $this->assertEquals(1, $result['count']);
6a488035
TO
138 }
139
140 public function testDeleteGrant() {
ca985406 141 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
142 $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
a15773db 143 $this->assertAPISuccess($result);
6c6e6187 144 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
ba4a1892 145 $this->assertEquals(0, $checkDeleted['count']);
6a488035 146 }
92915c55 147
389bcebf 148 /**
eceb18cc 149 * This is a test to check if setting fields one at a time alters other fields.
fe482240 150 *
389bcebf 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 */
6a488035
TO
156 public function testCreateAutoGrant() {
157 $entityName = $this->_entity;
92915c55
TO
158 $baoString = 'CRM_Grant_BAO_Grant';
159 $fields = $this->callAPISuccess($entityName, 'getfields', array(
160 'action' => 'create',
6a488035
TO
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);
ca985406 168 $getentities = $this->callAPISuccess($entityName, 'get', array(
92915c55
TO
169 'sequential' => 1,
170 'return' => $return,
171 ));
6a488035
TO
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;
a7488080 198 if (!empty($specs['FKClassName'])) {
6a488035
TO
199 $entity[$field] = empty($entity2[$field]) ? $entity2[$specs]['uniqueName'] : $entity2[$field];
200 }
201 break;
202
6a488035
TO
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(
92915c55
TO
217 'id' => $entity['id'],
218 $field => $entity[$field],
6a488035
TO
219 'debug' => 1,
220 );
ca985406 221 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
6a488035
TO
222 $this->assertAPISuccess($update, "setting $field to {$entity[$field]} in line " . __LINE__);
223 $checkParams = array(
224 'id' => $entity['id'],
6a488035
TO
225 'sequential' => 1,
226 );
ca985406 227 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
228 $this->assertAPIArrayComparison((array) $entity, $checkEntity);
6a488035
TO
229 }
230 $baoObj->deleteTestObjects($baoString);
231 $baoObj->free();
232 }
96025800 233
6a488035 234}