3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test class for Batch API - civicrm_batch_*
33 * @package CiviCRM_APIv3
35 class api_v3_BatchTest
extends CiviUnitTestCase
{
36 public $_eNoticeCompliant = TRUE;
37 protected $_apiversion;
42 * Initialize configuration
44 function __construct() {
45 parent
::__construct();
49 * Sets up the fixture, for example, opens a network connection.
50 * This method is called before a test is executed.
54 protected function setUp() {
55 $this->_apiversion
= 3;
56 $this->_params
= array(
57 'version' => $this->_apiversion
,
63 * Tears down the fixture, for example, closes a network connection.
64 * This method is called after a test is executed.
68 protected function tearDown() {}
71 * Create a sample batch
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);
81 ///////////////// civicrm_batch_get methods
84 * Test civicrm_batch_get with wrong params type.
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__
);
94 * Test civicrm_batch_get with empty params.
96 public function testGetEmptyParams() {
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__
);
104 * Test civicrm_batch_get - success expected.
106 public function testGet() {
108 'id' => $this->batchCreate(),
109 'version' => $this->_apiversion
,
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__
);
118 ///////////////// civicrm_batch_create methods
121 * Test civicrm_batch_create with wrong params type.
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__
);
131 * Test civicrm_batch_create with empty params.
133 function testCreateEmptyParams() {
134 $result = civicrm_api('batch', 'create', $this->_params
);
135 $this->assertEquals(1, $result['is_error'], "In line " . __LINE__
);
139 * Test civicrm_batch_create - success expected.
141 function testCreate() {
143 'name' => 'New_Batch_03',
144 'title' => 'New Batch 03',
145 'description' => 'This is description for New Batch 03',
149 'version' => $this->_apiversion
,
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__
);
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__
);
166 * Test civicrm_batch_create with id.
168 function testUpdate() {
170 'name' => 'New_Batch_04',
171 'title' => 'New Batch 04',
172 'description' => 'This is description for New Batch 04',
175 'version' => $this->_apiversion
,
176 'id' => $this->batchCreate(),
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__
);
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__
);
192 ///////////////// civicrm_batch_delete methods
195 * Test civicrm_batch_delete without batch id.
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__
);
204 * Test civicrm_batch_delete with wrong batch id type.
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__
);
213 * Test civicrm_batch_delete using the old $params['batch_id'] syntax.
215 function testBatchDeleteOldSyntax() {
216 $batchID = $this->batchCreate();
218 'batch_id' => $batchID,
219 'version' => $this->_apiversion
,
221 $result = civicrm_api('batch', 'delete', $params);
222 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
226 * Test civicrm_batch_delete using the new $params['id'] syntax
228 function testBatchDeleteCorrectSyntax() {
229 $batchID = $this->batchCreate();
232 'version' => $this->_apiversion
,
234 $result = civicrm_api('batch', 'delete', $params);
235 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
236 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);