Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
6a488035 TO |
12 | /** |
13 | * Test APIv3 civicrm_grant* functions | |
14 | * | |
6c6e6187 TO |
15 | * @package CiviCRM_APIv3 |
16 | * @subpackage API_Grant | |
acb109b7 | 17 | * @group headless |
6a488035 | 18 | */ |
6a488035 TO |
19 | class api_v3_GrantTest extends CiviUnitTestCase { |
20 | protected $_apiversion = 3; | |
21 | protected $params; | |
9099cab3 | 22 | protected $ids = []; |
6a488035 | 23 | protected $_entity = 'Grant'; |
b7c9bc4c | 24 | |
6a488035 TO |
25 | public $DBResetRequired = FALSE; |
26 | ||
00be9182 | 27 | public function setUp() { |
6a488035 TO |
28 | parent::setUp(); |
29 | $this->ids['contact'][0] = $this->individualCreate(); | |
9099cab3 | 30 | $this->params = [ |
92915c55 | 31 | 'contact_id' => $this->ids['contact'][0], |
6a488035 TO |
32 | 'application_received_date' => 'now', |
33 | 'decision_date' => 'next Monday', | |
34 | 'amount_total' => '500', | |
35 | 'status_id' => 1, | |
36 | 'rationale' => 'Just Because', | |
37 | 'currency' => 'USD', | |
38 | 'grant_type_id' => 1, | |
9099cab3 | 39 | ]; |
6a488035 TO |
40 | } |
41 | ||
fda18dc3 | 42 | /** |
43 | * Cleanup after test. | |
44 | * | |
45 | * @throws \Exception | |
46 | */ | |
00be9182 | 47 | public function tearDown() { |
6a488035 TO |
48 | foreach ($this->ids as $entity => $entities) { |
49 | foreach ($entities as $id) { | |
9099cab3 | 50 | $this->callAPISuccess($entity, 'delete', ['id' => $id]); |
6a488035 TO |
51 | } |
52 | } | |
fda18dc3 | 53 | $this->quickCleanup(['civicrm_grant']); |
54 | parent::tearDown(); | |
6a488035 TO |
55 | } |
56 | ||
57 | public function testCreateGrant() { | |
ca985406 | 58 | $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__); |
ba4a1892 TM |
59 | $this->assertEquals(1, $result['count']); |
60 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
6a488035 TO |
61 | $this->getAndCheck($this->params, $result['id'], $this->_entity); |
62 | } | |
63 | ||
2a4096af | 64 | /** |
eceb18cc | 65 | * Check checkbox type custom fields are created correctly. |
fe482240 | 66 | * |
2a4096af EM |
67 | * We want to ensure they are saved with separators as appropriate |
68 | */ | |
69 | public function testCreateCustomCheckboxGrant() { | |
9099cab3 CW |
70 | $ids = []; |
71 | $result = $this->customGroupCreate(['extends' => 'Grant']); | |
2a4096af EM |
72 | $ids['custom_group_id'] = $result['id']; |
73 | $customTable = $result['values'][$result['id']]['table_name']; | |
9099cab3 | 74 | $result = $this->customFieldCreate([ |
82e4a144 | 75 | 'html_type' => 'CheckBox', |
2a4096af | 76 | 'custom_group_id' => $ids['custom_group_id'], |
9099cab3 CW |
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 | ]); | |
2a4096af | 84 | $columnName = $result['values'][$result['id']]['column_name']; |
92915c55 | 85 | $ids['custom_field_id'] = $result['id']; |
2a4096af EM |
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 | |
86bfa4f6 | 104 | $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR . 'goat' . CRM_Core_DAO::VALUE_SEPARATOR; |
2a4096af EM |
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 | ||
2a4096af | 110 | //& an array for good measure |
9099cab3 | 111 | $this->params[$customFieldLabel] = ['valley', 'goat']; |
2a4096af EM |
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 | ||
6a488035 | 120 | public function testGetGrant() { |
ca985406 | 121 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); |
6a488035 | 122 | $this->ids['grant'][0] = $result['id']; |
9099cab3 | 123 | $result = $this->callAPIAndDocument($this->_entity, 'get', ['rationale' => 'Just Because'], __FUNCTION__, __FILE__); |
523c222f | 124 | $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); |
ba4a1892 | 125 | $this->assertEquals(1, $result['count']); |
6a488035 TO |
126 | } |
127 | ||
128 | public function testDeleteGrant() { | |
ca985406 | 129 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); |
9099cab3 | 130 | $result = $this->callAPIAndDocument($this->_entity, 'delete', ['id' => $result['id']], __FUNCTION__, __FILE__); |
a15773db | 131 | $this->assertAPISuccess($result); |
9099cab3 | 132 | $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []); |
ba4a1892 | 133 | $this->assertEquals(0, $checkDeleted['count']); |
6a488035 | 134 | } |
92915c55 | 135 | |
80452d96 CW |
136 | /** |
137 | * Test Grant status with `0` value. | |
138 | */ | |
139 | public function testGrantWithZeroStatus() { | |
9099cab3 | 140 | $params = [ |
80452d96 CW |
141 | 'action' => 'create', |
142 | 'grant_type_id' => "Emergency", | |
143 | 'amount_total' => 100, | |
144 | 'contact_id' => "1", | |
145 | 'status_id' => 0, | |
146 | 'id' => 1, | |
9099cab3 | 147 | ]; |
80452d96 CW |
148 | $validation = $this->callAPISuccess('Grant', 'validate', $params); |
149 | ||
9099cab3 CW |
150 | $expectedOut = [ |
151 | 'status_id' => [ | |
80452d96 CW |
152 | 'message' => "'0' is not a valid option for field status_id", |
153 | 'code' => "incorrect_value", | |
9099cab3 CW |
154 | ], |
155 | ]; | |
80452d96 CW |
156 | $this->assertEquals($validation['values'][0], $expectedOut); |
157 | } | |
158 | ||
6a488035 | 159 | } |