5ff88410506a62190eb635b4811e8c48fc937c74
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_grant* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Grant
19 class api_v3_GrantTest
extends CiviUnitTestCase
{
20 protected $_apiversion = 3;
23 protected $_entity = 'Grant';
25 public $DBResetRequired = FALSE;
27 public function setUp() {
29 $this->ids
['contact'][0] = $this->individualCreate();
31 'contact_id' => $this->ids
['contact'][0],
32 'application_received_date' => 'now',
33 'decision_date' => 'next Monday',
34 'amount_total' => '500',
36 'rationale' => 'Just Because',
47 public function tearDown() {
48 foreach ($this->ids
as $entity => $entities) {
49 foreach ($entities as $id) {
50 $this->callAPISuccess($entity, 'delete', ['id' => $id]);
53 $this->quickCleanup(['civicrm_grant']);
57 public function testCreateGrant() {
58 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
);
59 $this->assertEquals(1, $result['count']);
60 $this->assertNotNull($result['values'][$result['id']]['id']);
61 $this->getAndCheck($this->params
, $result['id'], $this->_entity
);
65 * Check checkbox type custom fields are created correctly.
67 * We want to ensure they are saved with separators as appropriate
69 public function testCreateCustomCheckboxGrant() {
71 $result = $this->customGroupCreate(['extends' => 'Grant']);
72 $ids['custom_group_id'] = $result['id'];
73 $customTable = $result['values'][$result['id']]['table_name'];
74 $result = $this->customFieldCreate([
75 'html_type' => 'CheckBox',
76 'custom_group_id' => $ids['custom_group_id'],
78 ['label' => 'my valley', 'value' => 'valley', 'is_active' => TRUE, 'weight' => 1],
79 ['label' => 'my goat', 'value' => 'goat', 'is_active' => TRUE, 'weight' => 2],
80 ['label' => 'mohair', 'value' => 'wool', 'is_active' => TRUE, 'weight' => 3],
81 ['label' => 'hungry', 'value' => '', 'is_active' => TRUE, 'weight' => 3],
84 $columnName = $result['values'][$result['id']]['column_name'];
85 $ids['custom_field_id'] = $result['id'];
86 $customFieldLabel = 'custom_' . $ids['custom_field_id'];
87 $expectedValue = CRM_Core_DAO
::VALUE_SEPARATOR
. 'valley' . CRM_Core_DAO
::VALUE_SEPARATOR
;
88 //first we pass in the core separators ourselves
89 $this->params
[$customFieldLabel] = $expectedValue;
90 $result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
91 $this->params
['id'] = $result['id'];
93 $savedValue = CRM_Core_DAO
::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
95 $this->assertEquals($expectedValue, $savedValue);
97 // now we ask CiviCRM to add the separators
98 $this->params
[$customFieldLabel] = "valley";
99 $result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
100 $savedValue = CRM_Core_DAO
::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
101 $this->assertEquals($expectedValue, $savedValue);
103 //let's try with 2 params already separated
104 $expectedValue = CRM_Core_DAO
::VALUE_SEPARATOR
. 'valley' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'goat' . CRM_Core_DAO
::VALUE_SEPARATOR
;
105 $this->params
[$customFieldLabel] = $expectedValue;
106 $result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
107 $savedValue = CRM_Core_DAO
::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
108 $this->assertEquals($expectedValue, $savedValue);
110 //& an array for good measure
111 $this->params
[$customFieldLabel] = ['valley', 'goat'];
112 $result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
113 $savedValue = CRM_Core_DAO
::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
114 $this->assertEquals($expectedValue, $savedValue);
116 $this->customFieldDelete($ids['custom_field_id']);
117 $this->customGroupDelete($ids['custom_group_id']);
120 public function testGetGrant() {
121 $result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
122 $this->ids
['grant'][0] = $result['id'];
123 $result = $this->callAPIAndDocument($this->_entity
, 'get', ['rationale' => 'Just Because'], __FUNCTION__
, __FILE__
);
124 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
125 $this->assertEquals(1, $result['count']);
128 public function testDeleteGrant() {
129 $result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
130 $result = $this->callAPIAndDocument($this->_entity
, 'delete', ['id' => $result['id']], __FUNCTION__
, __FILE__
);
131 $this->assertAPISuccess($result);
132 $checkDeleted = $this->callAPISuccess($this->_entity
, 'get', []);
133 $this->assertEquals(0, $checkDeleted['count']);
137 * Test Grant status with `0` value.
139 public function testGrantWithZeroStatus() {
141 'action' => 'create',
142 'grant_type_id' => "Emergency",
143 'amount_total' => 100,
148 $validation = $this->callAPISuccess('Grant', 'validate', $params);
152 'message' => "'0' is not a valid option for field status_id",
153 'code' => "incorrect_value",
156 $this->assertEquals($validation['values'][0], $expectedOut);