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