INFRA-132 - Drupal.Array.Array.CommaLastItem
[civicrm-core.git] / tests / phpunit / api / v3 / GrantTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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__);
6a488035
TO
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
2a4096af
EM
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(
82e4a144 86 'html_type' => 'CheckBox',
2a4096af
EM
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),
389bcebf 93 ),
92915c55 94 ));
2a4096af 95 $columnName = $result['values'][$result['id']]['column_name'];
92915c55 96 $ids['custom_field_id'] = $result['id'];
2a4096af
EM
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
86bfa4f6 115 $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR;
2a4096af
EM
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
6a488035 144 public function testGetGrant() {
ca985406 145 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 146 $this->ids['grant'][0] = $result['id'];
ca985406 147 $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__);
6a488035
TO
148 $this->assertAPISuccess($result, 'In line ' . __LINE__);
149 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
150 }
151
152 public function testDeleteGrant() {
ca985406 153 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
154 $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
6a488035 155 $this->assertAPISuccess($result, 'In line ' . __LINE__);
6c6e6187 156 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
6a488035
TO
157 $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__);
158 }
92915c55 159
389bcebf 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 */
6a488035
TO
167 public function testCreateAutoGrant() {
168 $entityName = $this->_entity;
92915c55
TO
169 $baoString = 'CRM_Grant_BAO_Grant';
170 $fields = $this->callAPISuccess($entityName, 'getfields', array(
171 'action' => 'create',
6a488035
TO
172 )
173 );
174
175 $fields = $fields['values'];
176 $return = array_keys($fields);
177 $baoObj = new CRM_Core_DAO();
178 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
ca985406 179 $getentities = $this->callAPISuccess($entityName, 'get', array(
92915c55
TO
180 'sequential' => 1,
181 'return' => $return,
182 ));
6a488035
TO
183
184 // lets use first rather than assume only one exists
185 $entity = $getentities['values'][0];
186 $entity2 = $getentities['values'][1];
187 foreach ($fields as $field => $specs) {
188 if ($field == 'currency' || $field == 'id') {
189 continue;
190 }
191 switch ($specs['type']) {
192 case CRM_Utils_Type::T_DATE:
193 case CRM_Utils_Type::T_TIMESTAMP:
194 $entity[$field] = '2012-05-20';
195 break;
196
197 case CRM_Utils_Type::T_STRING:
198 case CRM_Utils_Type::T_BLOB:
199 case CRM_Utils_Type::T_MEDIUMBLOB:
200 case CRM_Utils_Type::T_TEXT:
201 case CRM_Utils_Type::T_LONGTEXT:
202 case CRM_Utils_Type::T_EMAIL:
203 $entity[$field] = 'New String';
204 break;
205
206 case CRM_Utils_Type::T_INT:
207 // probably created with a 1
208 $entity[$field] = 2;
a7488080 209 if (!empty($specs['FKClassName'])) {
6a488035
TO
210 $entity[$field] = empty($entity2[$field]) ? $entity2[$specs]['uniqueName'] : $entity2[$field];
211 }
212 break;
213
6a488035
TO
214 case CRM_Utils_Type::T_BOOLEAN:
215 // probably created with a 1
216 $entity[$field] = 0;
217 break;
218
219 case CRM_Utils_Type::T_FLOAT:
220 case CRM_Utils_Type::T_MONEY:
221 $entity[$field] = 222;
222 break;
223
224 case CRM_Utils_Type::T_URL:
225 $entity[$field] = 'warm.beer.com';
226 }
227 $updateParams = array(
92915c55
TO
228 'id' => $entity['id'],
229 $field => $entity[$field],
6a488035
TO
230 'debug' => 1,
231 );
ca985406 232 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
6a488035
TO
233 $this->assertAPISuccess($update, "setting $field to {$entity[$field]} in line " . __LINE__);
234 $checkParams = array(
235 'id' => $entity['id'],
6a488035
TO
236 'sequential' => 1,
237 );
ca985406 238 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
239 $this->assertAPIArrayComparison((array) $entity, $checkEntity);
6a488035
TO
240 }
241 $baoObj->deleteTestObjects($baoString);
242 $baoObj->free();
243 }
96025800 244
6a488035 245}