Mass update tests to use callAPIFailure
[civicrm-core.git] / tests / phpunit / api / v3 / BatchTest.php
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 = $this->callAPIFailure('batch', 'get', $params);
89 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
90 }
91
92 /**
93 * Test civicrm_batch_get - success expected.
94 */
95 public function testGet() {
96 $params = array(
97 'id' => $this->batchCreate(),
98 'version' => $this->_apiversion,
99 );
100 $result = civicrm_api('batch', 'get', $params);
101 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
102 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
103 $this->assertEquals($params['id'], $result['id'], 'In line ' . __LINE__);
104 }
105
106
107 ///////////////// civicrm_batch_create methods
108
109 /**
110 * Test civicrm_batch_create with empty params.
111 */
112 function testCreateEmptyParams() {
113 $this->callAPIFailure('batch', 'create', $this->_params);
114 }
115
116 /**
117 * Test civicrm_batch_create - success expected.
118 */
119 function testCreate() {
120 $params = array(
121 'name' => 'New_Batch_03',
122 'title' => 'New Batch 03',
123 'description' => 'This is description for New Batch 03',
124 'total' => '300.33',
125 'item_count' => 3,
126 'status_id' => 1,
127 'version' => $this->_apiversion,
128 );
129
130 $result = civicrm_api('batch', 'create', $params);
131 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
132 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
133 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
134
135 // Fetch batch and compare values
136 $saved = civicrm_api('batch', 'getSingle', $result);
137 unset($params['version']);
138 foreach ($params as $key => $value) {
139 $this->assertEquals($value, $saved[$key], 'In line ' . __LINE__);
140 }
141 }
142
143 /**
144 * Test civicrm_batch_create with id.
145 */
146 function testUpdate() {
147 $params = array(
148 'name' => 'New_Batch_04',
149 'title' => 'New Batch 04',
150 'description' => 'This is description for New Batch 04',
151 'total' => '400.44',
152 'item_count' => 4,
153 'version' => $this->_apiversion,
154 'id' => $this->batchCreate(),
155 );
156
157 $result = civicrm_api('batch', 'create', $params);
158 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
159 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
160 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
161
162 // Fetch batch and compare values
163 $saved = civicrm_api('batch', 'getSingle', $result);
164 unset($params['version']);
165 foreach ($params as $key => $value) {
166 $this->assertEquals($value, $saved[$key], 'In line ' . __LINE__);
167 }
168 }
169
170 ///////////////// civicrm_batch_delete methods
171
172 /**
173 * Test civicrm_batch_delete without batch id.
174 */
175 function testDeleteWithoutBatchId() {
176 $result = civicrm_api('batch', 'delete', array('version' => $this->_apiversion));
177 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
178 $this->assertEquals('Mandatory key(s) missing from params array: id', $result['error_message'], 'In line ' . __LINE__);
179 }
180
181 /**
182 * Test civicrm_batch_delete with wrong batch id type.
183 */
184 function testDeleteWrongParams() {
185 $result = $this->callAPIFailure('batch', 'delete', 'tyttyd');
186 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
187 }
188
189 /**
190 * Test civicrm_batch_delete using the old $params['batch_id'] syntax.
191 */
192 function testBatchDeleteOldSyntax() {
193 $batchID = $this->batchCreate();
194 $params = array(
195 'batch_id' => $batchID,
196 'version' => $this->_apiversion,
197 );
198 $result = civicrm_api('batch', 'delete', $params);
199 $this->assertAPISuccess($result, 'In line ' . __LINE__);
200 }
201
202 /**
203 * Test civicrm_batch_delete using the new $params['id'] syntax
204 */
205 function testBatchDeleteCorrectSyntax() {
206 $batchID = $this->batchCreate();
207 $params = array(
208 'id' => $batchID,
209 'version' => $this->_apiversion,
210 );
211 $result = civicrm_api('batch', 'delete', $params);
212 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
213 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
214 }
215
216 }