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