Merge pull request #11578 from jitendrapurohit/CRM-20697
[civicrm-core.git] / tests / phpunit / api / v3 / GrantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 /**
29 * Test APIv3 civicrm_grant* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Grant
33 * @group headless
34 */
35 class api_v3_GrantTest extends CiviUnitTestCase {
36 protected $_apiversion = 3;
37 protected $params;
38 protected $ids = array();
39 protected $_entity = 'Grant';
40
41 public $DBResetRequired = FALSE;
42
43 public function setUp() {
44 parent::setUp();
45 $this->ids['contact'][0] = $this->individualCreate();
46 $this->params = array(
47 'contact_id' => $this->ids['contact'][0],
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
58 public function tearDown() {
59 foreach ($this->ids as $entity => $entities) {
60 foreach ($entities as $id) {
61 $this->callAPISuccess($entity, 'delete', array('id' => $id));
62 }
63 }
64 $this->quickCleanup(array('civicrm_grant'));
65 }
66
67 public function testCreateGrant() {
68 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
69 $this->assertEquals(1, $result['count']);
70 $this->assertNotNull($result['values'][$result['id']]['id']);
71 $this->getAndCheck($this->params, $result['id'], $this->_entity);
72 }
73
74 /**
75 * Check checkbox type custom fields are created correctly.
76 *
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(
85 'html_type' => 'CheckBox',
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),
92 ),
93 ));
94 $columnName = $result['values'][$result['id']]['column_name'];
95 $ids['custom_field_id'] = $result['id'];
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
114 $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR;
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
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
130 public function testGetGrant() {
131 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
132 $this->ids['grant'][0] = $result['id'];
133 $result = $this->callAPIAndDocument($this->_entity, 'get', array('rationale' => 'Just Because'), __FUNCTION__, __FILE__);
134 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
135 $this->assertEquals(1, $result['count']);
136 }
137
138 public function testDeleteGrant() {
139 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
140 $result = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
141 $this->assertAPISuccess($result);
142 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
143 $this->assertEquals(0, $checkDeleted['count']);
144 }
145
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
169 }