Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / GrantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
38 class api_v3_GrantTest extends CiviUnitTestCase {
39 protected $_apiversion = 3;
40 protected $params;
41 protected $ids = array();
42 protected $_entity = 'Grant';
43 public $_eNoticeCompliant = TRUE;
44 public $DBResetRequired = FALSE;
45
46 function setUp() {
47 parent::setUp();
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',
54 'status_id' => 1,
55 'rationale' => 'Just Because',
56 'currency' => 'USD',
57 'grant_type_id' => 1,
58 );
59 }
60
61 function tearDown() {
62 foreach ($this->ids as $entity => $entities) {
63 foreach ($entities as $id) {
64 $this->callAPISuccess($entity, 'delete', array( 'id' => $id));
65 }
66 }
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 public function testGetGrant() {
77 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
78 $this->ids['grant'][0] = $result['id'];
79 $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__);
80 $this->assertAPISuccess($result, 'In line ' . __LINE__);
81 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
82 }
83
84 public function testDeleteGrant() {
85 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
86 $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
87 $this->assertAPISuccess($result, 'In line ' . __LINE__);
88 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array(
89 ));
90 $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__);
91 }
92 /*
93 * This is a test to check if setting fields one at a time alters other fields
94 * Issues Hit so far =
95 * 1) Currency keeps getting reset to USD - BUT this may be the only enabled currency
96 * - in which case it is valid
97 * 2)
98 */
99
100 public function testCreateAutoGrant() {
101 $entityName = $this->_entity;
102 $baoString = 'CRM_Grant_BAO_Grant';
103 $fields = $this->callAPISuccess($entityName, 'getfields', array(
104 'action' => 'create',
105 )
106 );
107
108 $fields = $fields['values'];
109 $return = array_keys($fields);
110 $baoObj = new CRM_Core_DAO();
111 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
112 $getentities = $this->callAPISuccess($entityName, 'get', array(
113 'sequential' => 1,
114 'return' => $return,
115 ));
116
117 // lets use first rather than assume only one exists
118 $entity = $getentities['values'][0];
119 $entity2 = $getentities['values'][1];
120 foreach ($fields as $field => $specs) {
121 if ($field == 'currency' || $field == 'id') {
122 continue;
123 }
124 switch ($specs['type']) {
125 case CRM_Utils_Type::T_DATE:
126 case CRM_Utils_Type::T_TIMESTAMP:
127 $entity[$field] = '2012-05-20';
128 break;
129
130 case CRM_Utils_Type::T_STRING:
131 case CRM_Utils_Type::T_BLOB:
132 case CRM_Utils_Type::T_MEDIUMBLOB:
133 case CRM_Utils_Type::T_TEXT:
134 case CRM_Utils_Type::T_LONGTEXT:
135 case CRM_Utils_Type::T_EMAIL:
136 $entity[$field] = 'New String';
137 break;
138
139 case CRM_Utils_Type::T_INT:
140 // probably created with a 1
141 $entity[$field] = 2;
142 if (CRM_Utils_Array::value('FKClassName', $specs)) {
143 $entity[$field] = empty($entity2[$field]) ? $entity2[$specs]['uniqueName'] : $entity2[$field];
144 }
145 break;
146
147 case CRM_Utils_Type::T_BOOLEAN:
148 // probably created with a 1
149 $entity[$field] = 0;
150 break;
151
152 case CRM_Utils_Type::T_FLOAT:
153 case CRM_Utils_Type::T_MONEY:
154 $entity[$field] = 222;
155 break;
156
157 case CRM_Utils_Type::T_URL:
158 $entity[$field] = 'warm.beer.com';
159 }
160 $updateParams = array(
161 'id' => $entity['id'],
162 $field => $entity[$field],
163 'debug' => 1,
164 );
165 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
166 $this->assertAPISuccess($update, "setting $field to {$entity[$field]} in line " . __LINE__);
167 $checkParams = array(
168 'id' => $entity['id'],
169 'sequential' => 1,
170 );
171 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
172 $this->assertAPIArrayComparison((array) $entity, $checkEntity);
173 }
174 $baoObj->deleteTestObjects($baoString);
175 $baoObj->free();
176 }
177 }
178