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';
32 * Test APIv3 civicrm_tag_* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Core
38 class api_v3_TagTest
extends CiviUnitTestCase
{
39 protected $_apiversion;
40 public $_eNoticeCompliant = TRUE;
42 $this->_apiversion
= 3;
46 function tearDown() {}
48 ///////////////// civicrm_tag_get methods
51 * Test civicrm_tag_get with wrong params type.
53 public function testGetWrongParamsType() {
54 $params = 'is_string';
55 $result = civicrm_api('tag', 'get', $params);
56 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
57 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
61 * Test civicrm_tag_get with empty params.
63 public function testGetEmptyParams() {
65 $result = civicrm_api('tag', 'get', $params);
66 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
67 $this->assertEquals('Mandatory key(s) missing from params array: version', $result['error_message'], 'In line ' . __LINE__
);
71 * Test civicrm_tag_get with wrong params.
73 public function testGetWrongParams() {
74 $params = array('name' => 'Wrong Tag Name', 'version' => $this->_apiversion
);
75 $result = civicrm_api('tag', 'get', $params);
76 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
77 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
81 * Test civicrm_tag_get - success expected.
83 public function testGet() {
84 $tag = $this->tagCreate(NULL);
85 $this->assertEquals(0, $tag['is_error'], 'In line ' . __LINE__
);
89 'name' => $tag['values'][$tag['id']]['name'],
90 'version' => $this->_apiversion
,
92 $result = civicrm_api('tag', 'get', $params);
93 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
94 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
95 $this->assertEquals($tag['values'][$tag['id']]['description'], $result['values'][$tag['id']]['description'], 'In line ' . __LINE__
);
96 $this->assertEquals($tag['values'][$tag['id']]['name'], $result['values'][$tag['id']]['name'], 'In line ' . __LINE__
);
100 * Test civicrm_tag_get - success expected.
102 public function testGetReturnArray() {
103 $description = "demonstrates use of Return as an array";
104 $subfile = "getReturnArray";
105 $tag = $this->tagCreate(NULL);
106 $this->assertEquals(0, $tag['is_error'], 'In line ' . __LINE__
);
110 'name' => $tag['values'][$tag['id']]['name'],
111 'version' => $this->_apiversion
,
112 'return' => array('name'),
114 $result = civicrm_api('tag', 'get', $params);
115 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
116 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
117 $this->assertTrue(empty($result['values'][$tag['id']]['description']), 'In line ' . __LINE__
);
118 $this->assertEquals($tag['values'][$tag['id']]['name'], $result['values'][$tag['id']]['name'], 'In line ' . __LINE__
);
121 ///////////////// civicrm_tag_create methods
124 * Test civicrm_tag_create with wrong params type.
126 function testCreateWrongParamsType() {
127 $params = 'a string';
128 $result = civicrm_api('tag', 'create', $params);
129 $this->assertEquals(1, $result['is_error'], "In line " . __LINE__
);
130 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
134 * Test civicrm_tag_create with empty params.
136 function testCreateEmptyParams() {
137 $params = array('version' => $this->_apiversion
);
138 $result = civicrm_api('tag', 'create', $params);
139 $this->assertEquals(1, $result['is_error'], "In line " . __LINE__
);
140 $this->assertEquals('Mandatory key(s) missing from params array: name', $result['error_message'], 'In line ' . __LINE__
);
144 * Test civicrm_tag_create
146 function testCreatePasstagInParams() {
149 'name' => 'New Tag23',
150 'description' => 'This is description for New Tag 02',
151 'version' => $this->_apiversion
,
153 $result = civicrm_api('tag', 'create', $params);
154 $this->assertEquals(10, $result['id'], 'In line ' . __LINE__
);
158 * Test civicrm_tag_create - success expected.
160 function testCreate() {
162 'name' => 'New Tag3',
163 'description' => 'This is description for New Tag 02',
164 'version' => $this->_apiversion
,
167 $result = civicrm_api('tag', 'create', $params);
168 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
169 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
170 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
171 $params['used_for'] = 'civicrm_contact';
172 $this->getAndCheck($params, $result['id'], 'tag');
176 * Test civicrm_tag_create contribution tag- success expected. Test checks that used_for is set
177 * and not over-written by default on update
179 function testCreateContributionTag() {
181 'name' => 'New Tag4',
182 'description' => 'This is description for New Cont tag',
183 'version' => $this->_apiversion
,
184 'used_for' => 'civicrm_contribution',
186 $result = civicrm_api('tag', 'create', $params);
187 $this->assertAPISuccess($result, "contribution tag created");
188 $check = civicrm_api('tag', 'get', array('version' => 3));
189 $this->getAndCheck($params, $result['id'], 'tag', 0, __FUNCTION__
. ' tag first created');
190 unset($params['used_for']);
191 $this->assertAPISuccess($result, 'tag created');
192 $params['id'] = $result['id'];
193 $result = civicrm_api('tag', 'create', $params);
194 $this->assertAPISuccess($result);
195 $params['used_for'] = 'civicrm_contribution';
196 $this->getAndCheck($params, $result['id'], 'tag', 1, __FUNCTION__
. ' tag updated in line ' . __LINE__
);
198 ///////////////// civicrm_tag_delete methods
201 * Test civicrm_tag_delete with wrong parameters type.
203 function testDeleteWrongParamsType() {
205 $result = civicrm_api('tag', 'delete', $tag);
206 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
207 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
211 * Test civicrm_tag_delete with empty parameters.
213 function testDeleteEmptyParams() {
214 $tag = array('version' => $this->_apiversion
);
215 $result = civicrm_api('tag', 'delete', $tag);
216 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
217 $this->assertEquals('Mandatory key(s) missing from params array: id', $result['error_message'], 'In line ' . __LINE__
);
221 * Test civicrm_tag_delete without tag id.
223 function testDeleteWithoutTagId() {
224 $tag = array('version' => 3);
226 $result = civicrm_api('tag', 'delete', $tag);
227 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
228 $this->assertEquals('Mandatory key(s) missing from params array: id', $result['error_message'], 'In line ' . __LINE__
);
232 * Test civicrm_tag_delete with wrong tag id type.
234 function testDeleteWrongParams() {
235 $result = civicrm_api('tag', 'delete', 'tyttyd');
236 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
237 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
241 * Test civicrm_tag_delete .
243 function testTagDeleteOldSyntax() {
244 $tagID = $this->tagCreate(NULL);
246 'tag_id' => $tagID['id'],
247 'version' => $this->_apiversion
,
249 $result = civicrm_api('tag', 'delete', $params);
250 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
254 * Test civicrm_tag_delete = $params['id'] is correct
256 function testTagDeleteCorrectSyntax() {
257 $tagID = $this->tagCreate(NULL);
259 'id' => $tagID['id'],
260 'version' => $this->_apiversion
,
262 $result = civicrm_api('tag', 'delete', $params);
263 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
264 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
267 function testTaggetfields() {
268 $description = "demonstrate use of getfields to interogate api";
269 $params = array('version' => 3, 'action' => 'create');
270 $result = civicrm_api('tag', 'getfields', $params);
271 $this->assertEquals('civicrm_contact', $result['values']['used_for']['api.default']);