Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | /** | |
31 | * Test class for Batch API - civicrm_batch_* | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | */ | |
35 | class api_v3_BatchTest extends CiviUnitTestCase { | |
36 | public $_eNoticeCompliant = TRUE; | |
37 | protected $_apiversion; | |
38 | protected $_params; | |
39 | /** | |
40 | * Constructor | |
41 | * | |
42 | * Initialize configuration | |
43 | */ | |
44 | function __construct() { | |
45 | parent::__construct(); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Sets up the fixture, for example, opens a network connection. | |
50 | * This method is called before a test is executed. | |
51 | * | |
52 | * @access protected | |
53 | */ | |
54 | protected function setUp() { | |
55 | $this->_apiversion = 3; | |
56 | $this->_params = array( | |
57 | 'version' => $this->_apiversion, | |
58 | ); | |
59 | parent::setUp(); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Tears down the fixture, for example, closes a network connection. | |
64 | * This method is called after a test is executed. | |
65 | * | |
66 | * @access protected | |
67 | */ | |
68 | protected function tearDown() {} | |
69 | ||
70 | /** | |
71 | * Create a sample batch | |
72 | */ | |
73 | function batchCreate() { | |
74 | $params = $this->_params; | |
75 | $params['name'] = $params['title'] = 'Batch_' . mt_rand(0, 999999); | |
76 | $params['status_id'] = 1; | |
77 | $result = civicrm_api('batch', 'create', $params); | |
78 | return $result['id']; | |
79 | } | |
80 | ||
81 | ///////////////// civicrm_batch_get methods | |
82 | ||
83 | /** | |
84 | * Test civicrm_batch_get with wrong params type. | |
85 | */ | |
86 | public function testGetWrongParamsType() { | |
87 | $params = 'is_string'; | |
88 | $result = civicrm_api('batch', 'get', $params); | |
89 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
90 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Test civicrm_batch_get with empty params. | |
95 | */ | |
96 | public function testGetEmptyParams() { | |
97 | $params = array(); | |
98 | $result = civicrm_api('batch', 'get', $params); | |
99 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
100 | $this->assertEquals('Mandatory key(s) missing from params array: version', $result['error_message'], 'In line ' . __LINE__); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Test civicrm_batch_get - success expected. | |
105 | */ | |
106 | public function testGet() { | |
107 | $params = array( | |
108 | 'id' => $this->batchCreate(), | |
109 | 'version' => $this->_apiversion, | |
110 | ); | |
111 | $result = civicrm_api('batch', 'get', $params); | |
112 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
113 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
114 | $this->assertEquals($params['id'], $result['id'], 'In line ' . __LINE__); | |
115 | } | |
116 | ||
117 | ||
118 | ///////////////// civicrm_batch_create methods | |
119 | ||
120 | /** | |
121 | * Test civicrm_batch_create with wrong params type. | |
122 | */ | |
123 | function testCreateWrongParamsType() { | |
124 | $params = 'a string'; | |
125 | $result = civicrm_api('batch', 'create', $params); | |
126 | $this->assertEquals(1, $result['is_error'], "In line " . __LINE__); | |
127 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Test civicrm_batch_create with empty params. | |
132 | */ | |
133 | function testCreateEmptyParams() { | |
134 | $result = civicrm_api('batch', 'create', $this->_params); | |
135 | $this->assertEquals(1, $result['is_error'], "In line " . __LINE__); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Test civicrm_batch_create - success expected. | |
140 | */ | |
141 | function testCreate() { | |
142 | $params = array( | |
143 | 'name' => 'New_Batch_03', | |
144 | 'title' => 'New Batch 03', | |
145 | 'description' => 'This is description for New Batch 03', | |
146 | 'total' => '300.33', | |
147 | 'item_count' => 3, | |
148 | 'status_id' => 1, | |
149 | 'version' => $this->_apiversion, | |
150 | ); | |
151 | ||
152 | $result = civicrm_api('batch', 'create', $params); | |
153 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
154 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
155 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); | |
156 | ||
157 | // Fetch batch and compare values | |
158 | $saved = civicrm_api('batch', 'getSingle', $result); | |
159 | unset($params['version']); | |
160 | foreach ($params as $key => $value) { | |
161 | $this->assertEquals($value, $saved[$key], 'In line ' . __LINE__); | |
162 | } | |
163 | } | |
164 | ||
165 | /** | |
166 | * Test civicrm_batch_create with id. | |
167 | */ | |
168 | function testUpdate() { | |
169 | $params = array( | |
170 | 'name' => 'New_Batch_04', | |
171 | 'title' => 'New Batch 04', | |
172 | 'description' => 'This is description for New Batch 04', | |
173 | 'total' => '400.44', | |
174 | 'item_count' => 4, | |
175 | 'version' => $this->_apiversion, | |
176 | 'id' => $this->batchCreate(), | |
177 | ); | |
178 | ||
179 | $result = civicrm_api('batch', 'create', $params); | |
180 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
181 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
182 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); | |
183 | ||
184 | // Fetch batch and compare values | |
185 | $saved = civicrm_api('batch', 'getSingle', $result); | |
186 | unset($params['version']); | |
187 | foreach ($params as $key => $value) { | |
188 | $this->assertEquals($value, $saved[$key], 'In line ' . __LINE__); | |
189 | } | |
190 | } | |
191 | ||
192 | ///////////////// civicrm_batch_delete methods | |
193 | ||
194 | /** | |
195 | * Test civicrm_batch_delete without batch id. | |
196 | */ | |
197 | function testDeleteWithoutBatchId() { | |
198 | $result = civicrm_api('batch', 'delete', array('version' => $this->_apiversion)); | |
199 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
200 | $this->assertEquals('Mandatory key(s) missing from params array: id', $result['error_message'], 'In line ' . __LINE__); | |
201 | } | |
202 | ||
203 | /** | |
204 | * Test civicrm_batch_delete with wrong batch id type. | |
205 | */ | |
206 | function testDeleteWrongParams() { | |
207 | $result = civicrm_api('batch', 'delete', 'tyttyd'); | |
208 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
209 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); | |
210 | } | |
211 | ||
212 | /** | |
213 | * Test civicrm_batch_delete using the old $params['batch_id'] syntax. | |
214 | */ | |
215 | function testBatchDeleteOldSyntax() { | |
216 | $batchID = $this->batchCreate(); | |
217 | $params = array( | |
218 | 'batch_id' => $batchID, | |
219 | 'version' => $this->_apiversion, | |
220 | ); | |
221 | $result = civicrm_api('batch', 'delete', $params); | |
222 | $this->assertAPISuccess($result, 'In line ' . __LINE__); | |
223 | } | |
224 | ||
225 | /** | |
226 | * Test civicrm_batch_delete using the new $params['id'] syntax | |
227 | */ | |
228 | function testBatchDeleteCorrectSyntax() { | |
229 | $batchID = $this->batchCreate(); | |
230 | $params = array( | |
231 | 'id' => $batchID, | |
232 | 'version' => $this->_apiversion, | |
233 | ); | |
234 | $result = civicrm_api('batch', 'delete', $params); | |
235 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
236 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
237 | } | |
238 | ||
239 | } |