fix header
[civicrm-core.git] / tests / phpunit / api / v3 / GrantTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 27
6a488035
TO
28/**
29 * Test APIv3 civicrm_grant* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Grant
acb109b7 33 * @group headless
6a488035 34 */
6a488035
TO
35class api_v3_GrantTest extends CiviUnitTestCase {
36 protected $_apiversion = 3;
37 protected $params;
38 protected $ids = array();
39 protected $_entity = 'Grant';
b7c9bc4c 40
6a488035
TO
41 public $DBResetRequired = FALSE;
42
00be9182 43 public function setUp() {
6a488035
TO
44 parent::setUp();
45 $this->ids['contact'][0] = $this->individualCreate();
46 $this->params = array(
92915c55 47 'contact_id' => $this->ids['contact'][0],
6a488035
TO
48 'application_received_date' => 'now',
49 'decision_date' => 'next Monday',
50 'amount_total' => '500',
51 'status_id' => 1,
52 'rationale' => 'Just Because',
53 'currency' => 'USD',
54 'grant_type_id' => 1,
55 );
56 }
57
00be9182 58 public function tearDown() {
6a488035
TO
59 foreach ($this->ids as $entity => $entities) {
60 foreach ($entities as $id) {
481a74f4 61 $this->callAPISuccess($entity, 'delete', array('id' => $id));
6a488035
TO
62 }
63 }
2a4096af 64 $this->quickCleanup(array('civicrm_grant'));
6a488035
TO
65 }
66
67 public function testCreateGrant() {
ca985406 68 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
ba4a1892
TM
69 $this->assertEquals(1, $result['count']);
70 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
71 $this->getAndCheck($this->params, $result['id'], $this->_entity);
72 }
73
2a4096af 74 /**
eceb18cc 75 * Check checkbox type custom fields are created correctly.
fe482240 76 *
2a4096af
EM
77 * We want to ensure they are saved with separators as appropriate
78 */
79 public function testCreateCustomCheckboxGrant() {
80 $ids = array();
81 $result = $this->customGroupCreate(array('extends' => 'Grant'));
82 $ids['custom_group_id'] = $result['id'];
83 $customTable = $result['values'][$result['id']]['table_name'];
84 $result = $this->customFieldCreate(array(
82e4a144 85 'html_type' => 'CheckBox',
2a4096af
EM
86 'custom_group_id' => $ids['custom_group_id'],
87 'option_values' => array(
88 array('label' => 'my valley', 'value' => 'valley', 'is_active' => TRUE, 'weight' => 1),
89 array('label' => 'my goat', 'value' => 'goat', 'is_active' => TRUE, 'weight' => 2),
90 array('label' => 'mohair', 'value' => 'wool', 'is_active' => TRUE, 'weight' => 3),
91 array('label' => 'hungry', 'value' => '', 'is_active' => TRUE, 'weight' => 3),
389bcebf 92 ),
92915c55 93 ));
2a4096af 94 $columnName = $result['values'][$result['id']]['column_name'];
92915c55 95 $ids['custom_field_id'] = $result['id'];
2a4096af
EM
96 $customFieldLabel = 'custom_' . $ids['custom_field_id'];
97 $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR;
98 //first we pass in the core separators ourselves
99 $this->params[$customFieldLabel] = $expectedValue;
100 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
101 $this->params['id'] = $result['id'];
102
103 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
104
105 $this->assertEquals($expectedValue, $savedValue);
106
107 // now we ask CiviCRM to add the separators
108 $this->params[$customFieldLabel] = "valley";
109 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
110 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
111 $this->assertEquals($expectedValue, $savedValue);
112
113 //let's try with 2 params already separated
86bfa4f6 114 $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR;
2a4096af
EM
115 $this->params[$customFieldLabel] = $expectedValue;
116 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
117 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
118 $this->assertEquals($expectedValue, $savedValue);
119
2a4096af
EM
120 //& an array for good measure
121 $this->params[$customFieldLabel] = array('valley', 'goat');
122 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
123 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
124 $this->assertEquals($expectedValue, $savedValue);
125
126 $this->customFieldDelete($ids['custom_field_id']);
127 $this->customGroupDelete($ids['custom_group_id']);
128 }
129
6a488035 130 public function testGetGrant() {
ca985406 131 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 132 $this->ids['grant'][0] = $result['id'];
ca985406 133 $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__);
523c222f 134 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
ba4a1892 135 $this->assertEquals(1, $result['count']);
6a488035
TO
136 }
137
138 public function testDeleteGrant() {
ca985406 139 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
140 $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
a15773db 141 $this->assertAPISuccess($result);
6c6e6187 142 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
ba4a1892 143 $this->assertEquals(0, $checkDeleted['count']);
6a488035 144 }
92915c55 145
80452d96
CW
146 /**
147 * Test Grant status with `0` value.
148 */
149 public function testGrantWithZeroStatus() {
150 $params = array(
151 'action' => 'create',
152 'grant_type_id' => "Emergency",
153 'amount_total' => 100,
154 'contact_id' => "1",
155 'status_id' => 0,
156 'id' => 1,
157 );
158 $validation = $this->callAPISuccess('Grant', 'validate', $params);
159
160 $expectedOut = array(
161 'status_id' => array(
162 'message' => "'0' is not a valid option for field status_id",
163 'code' => "incorrect_value",
164 ),
165 );
166 $this->assertEquals($validation['values'][0], $expectedOut);
167 }
168
6a488035 169}