Merge pull request #17588 from artfulrobot/artfulrobot-property-bag-support-empty
[civicrm-core.git] / tests / phpunit / api / v3 / GrantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_grant* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Grant
17 * @group headless
18 */
19 class api_v3_GrantTest extends CiviUnitTestCase {
20 protected $_apiversion = 3;
21 protected $params;
22 protected $ids = [];
23 protected $_entity = 'Grant';
24
25 public $DBResetRequired = FALSE;
26
27 public function setUp() {
28 parent::setUp();
29 $this->ids['contact'][0] = $this->individualCreate();
30 $this->params = [
31 'contact_id' => $this->ids['contact'][0],
32 'application_received_date' => 'now',
33 'decision_date' => 'next Monday',
34 'amount_total' => '500.00',
35 'status_id' => 1,
36 'rationale' => 'Just Because',
37 'currency' => 'USD',
38 'grant_type_id' => 1,
39 ];
40 }
41
42 /**
43 * Cleanup after test.
44 *
45 * @throws \Exception
46 */
47 public function tearDown() {
48 foreach ($this->ids as $entity => $entities) {
49 foreach ($entities as $id) {
50 $this->callAPISuccess($entity, 'delete', ['id' => $id]);
51 }
52 }
53 $this->quickCleanup(['civicrm_grant']);
54 parent::tearDown();
55 }
56
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);
62 }
63
64 /**
65 * Check checkbox type custom fields are created correctly.
66 *
67 * We want to ensure they are saved with separators as appropriate
68 */
69 public function testCreateCustomCheckboxGrant() {
70 $ids = [];
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'],
77 'option_values' => [
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],
82 ],
83 ]);
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'];
92
93 $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}");
94
95 $this->assertEquals($expectedValue, $savedValue);
96
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);
102
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);
109
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);
115
116 $this->customFieldDelete($ids['custom_field_id']);
117 $this->customGroupDelete($ids['custom_group_id']);
118 }
119
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']);
126 }
127
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']);
134 }
135
136 /**
137 * Test Grant status with `0` value.
138 */
139 public function testGrantWithZeroStatus() {
140 $params = [
141 'action' => 'create',
142 'grant_type_id' => "Emergency",
143 'amount_total' => 100,
144 'contact_id' => "1",
145 'status_id' => 0,
146 'id' => 1,
147 ];
148 $validation = $this->callAPISuccess('Grant', 'validate', $params);
149
150 $expectedOut = [
151 'status_id' => [
152 'message' => "'0' is not a valid option for field status_id",
153 'code' => "incorrect_value",
154 ],
155 ];
156 $this->assertEquals($validation['values'][0], $expectedOut);
157 }
158
159 }