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 | /** | |
32 | * Test APIv3 civicrm_tag_* functions | |
33 | * | |
34 | * @package CiviCRM_APIv3 | |
35 | * @subpackage API_Core | |
36 | */ | |
37 | ||
38 | class api_v3_TagTest extends CiviUnitTestCase { | |
39 | protected $_apiversion; | |
40 | public $_eNoticeCompliant = TRUE; | |
41 | function setUp() { | |
42 | $this->_apiversion = 3; | |
43 | parent::setUp(); | |
44 | } | |
45 | ||
46 | function tearDown() {} | |
47 | ||
48 | ///////////////// civicrm_tag_get methods | |
49 | ||
50 | /** | |
51 | * Test civicrm_tag_get with wrong params type. | |
52 | */ | |
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__); | |
58 | } | |
59 | ||
60 | /** | |
61 | * Test civicrm_tag_get with empty params. | |
62 | */ | |
63 | public function testGetEmptyParams() { | |
64 | $params = array(); | |
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__); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Test civicrm_tag_get with wrong params. | |
72 | */ | |
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__); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Test civicrm_tag_get - success expected. | |
82 | */ | |
83 | public function testGet() { | |
84 | $tag = $this->tagCreate(NULL); | |
85 | $this->assertEquals(0, $tag['is_error'], 'In line ' . __LINE__); | |
86 | ||
87 | $params = array( | |
88 | 'id' => $tag['id'], | |
89 | 'name' => $tag['values'][$tag['id']]['name'], | |
90 | 'version' => $this->_apiversion, | |
91 | ); | |
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__); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Test civicrm_tag_get - success expected. | |
101 | */ | |
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__); | |
107 | ||
108 | $params = array( | |
109 | 'id' => $tag['id'], | |
110 | 'name' => $tag['values'][$tag['id']]['name'], | |
111 | 'version' => $this->_apiversion, | |
112 | 'return' => array('name'), | |
113 | ); | |
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__); | |
119 | } | |
120 | ||
121 | ///////////////// civicrm_tag_create methods | |
122 | ||
123 | /** | |
124 | * Test civicrm_tag_create with wrong params type. | |
125 | */ | |
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__); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Test civicrm_tag_create with empty params. | |
135 | */ | |
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__); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Test civicrm_tag_create | |
145 | */ | |
146 | function testCreatePasstagInParams() { | |
147 | $params = array( | |
148 | 'tag' => 10, | |
149 | 'name' => 'New Tag23', | |
150 | 'description' => 'This is description for New Tag 02', | |
151 | 'version' => $this->_apiversion, | |
152 | ); | |
153 | $result = civicrm_api('tag', 'create', $params); | |
154 | $this->assertEquals(10, $result['id'], 'In line ' . __LINE__); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Test civicrm_tag_create - success expected. | |
159 | */ | |
160 | function testCreate() { | |
161 | $params = array( | |
162 | 'name' => 'New Tag3', | |
163 | 'description' => 'This is description for New Tag 02', | |
164 | 'version' => $this->_apiversion, | |
165 | ); | |
166 | ||
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'); | |
173 | } | |
174 | ||
175 | /** | |
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 | |
178 | */ | |
179 | function testCreateContributionTag() { | |
180 | $params = array( | |
181 | 'name' => 'New Tag4', | |
182 | 'description' => 'This is description for New Cont tag', | |
183 | 'version' => $this->_apiversion, | |
184 | 'used_for' => 'civicrm_contribution', | |
185 | ); | |
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__); | |
197 | } | |
198 | ///////////////// civicrm_tag_delete methods | |
199 | ||
200 | /** | |
201 | * Test civicrm_tag_delete with wrong parameters type. | |
202 | */ | |
203 | function testDeleteWrongParamsType() { | |
204 | $tag = 'is string'; | |
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__); | |
208 | } | |
209 | ||
210 | /** | |
211 | * Test civicrm_tag_delete with empty parameters. | |
212 | */ | |
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__); | |
218 | } | |
219 | ||
220 | /** | |
221 | * Test civicrm_tag_delete without tag id. | |
222 | */ | |
223 | function testDeleteWithoutTagId() { | |
224 | $tag = array('version' => 3); | |
225 | ||
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__); | |
229 | } | |
230 | ||
231 | /** | |
232 | * Test civicrm_tag_delete with wrong tag id type. | |
233 | */ | |
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__); | |
238 | } | |
239 | ||
240 | /** | |
241 | * Test civicrm_tag_delete . | |
242 | */ | |
243 | function testTagDeleteOldSyntax() { | |
244 | $tagID = $this->tagCreate(NULL); | |
245 | $params = array( | |
246 | 'tag_id' => $tagID['id'], | |
247 | 'version' => $this->_apiversion, | |
248 | ); | |
249 | $result = civicrm_api('tag', 'delete', $params); | |
250 | $this->assertAPISuccess($result, 'In line ' . __LINE__); | |
251 | } | |
252 | ||
253 | /** | |
254 | * Test civicrm_tag_delete = $params['id'] is correct | |
255 | */ | |
256 | function testTagDeleteCorrectSyntax() { | |
257 | $tagID = $this->tagCreate(NULL); | |
258 | $params = array( | |
259 | 'id' => $tagID['id'], | |
260 | 'version' => $this->_apiversion, | |
261 | ); | |
262 | $result = civicrm_api('tag', 'delete', $params); | |
263 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
264 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
265 | } | |
266 | ||
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']); | |
272 | } | |
273 | } | |
274 |